Skip to content

Commit

Permalink
Remove callee (#3)
Browse files Browse the repository at this point in the history
* Make it only declare globals once.

* globalThis for browser too

* Fix formatting
Remove arguments.callee
  • Loading branch information
useafterfree authored May 11, 2020
1 parent 4e1b146 commit 07ba0e6
Show file tree
Hide file tree
Showing 6 changed files with 1,402 additions and 84 deletions.
19 changes: 19 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
env: {
browser: true,
commonjs: true,
es6: true,
},
extends: [
'airbnb-base',
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parserOptions: {
ecmaVersion: 11,
},
rules: {
},
};
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A wrapper to node-debug with line numbers and other magic
## Install

```
npm install bo01ean/boolean-debug
yarn add bo01ean/ndebug
```

## usage
Expand All @@ -15,16 +15,16 @@ Require the library with it's name-space passed to it's constructor.

```
var debug = require('boolean-debug')('info');
const debug = require('ndebug')('info');
debug('hai!');
```


## Run node with DEBUG set to name-space;

```
DEBUG=info files.js
DEBUG=info node index.js # OR
DEBUG=info node -e "const debug = require('ndebug')('info'); debug('hai\!');"
```

:)
136 changes: 59 additions & 77 deletions debug.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,71 @@
module.exports = function (space, depth = 2) {

if (Object.getOwnPropertyDescriptor(global, '__stack') === undefined) {
Object.defineProperty(global, '__stack', {
get: function () {
var orig = Error.prepareStackTrace;
Error.prepareStackTrace = function (_, stack) {
return stack;
};
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
var stack = err.stack;
Error.prepareStackTrace = orig;
return stack;
}
});
}

if (Object.getOwnPropertyDescriptor(global, '__line') === undefined) {
Object.defineProperty(global, '__line', {
get: function () {
return __stack[depth].getLineNumber();
}
});
}
/* globals globalThis __stack __file __function __line */
const path = require('path');
const debug = require('debug');

if (Object.getOwnPropertyDescriptor(global, '__function') === undefined) {
Object.defineProperty(global, '__function', {
get: function () {
return __stack[depth].getFunctionName();
}
});
}
// Types to utils.format
const fmtMap = {
[typeof 1]: '%d',
[typeof []]: '%o',
[typeof {}]: '%O',
[typeof '']: '%s',
[typeof undefined]: '%s',
};

if (Object.getOwnPropertyDescriptor(global, '__file') === undefined) {
Object.defineProperty(global, '__file', {
get: function () {
return __stack[depth].getFileName();
}
});
}
module.exports = (space = 'default', depth = 3) => {
const getGlobalThis = () => {
if (typeof globalThis !== 'undefined') return globalThis;
if (typeof self !== 'undefined') return self; /* eslint-disable-line */
if (typeof window !== 'undefined') return window;
if (typeof global !== 'undefined') return global;
if (typeof this !== 'undefined') return this;
throw new Error('Unable to locate global `this`');
};

function censor(censor) {
var i = 0;
return function (key, value) {
if (i !== 0 && typeof censor === 'object' && typeof value == 'object' && censor == value) { return '[Circular]'; }
if (i >= 29) { return '[Unknown]'; }
++i;
return value;
};
}
const g = getGlobalThis();

const regex = /["']?([\.a-z_0-9]+)["']?\s?:(!?.+)(!?\s+)/ig;
const groupIndexForKeys = 1;
const get = () => {
const orig = Error.prepareStackTrace;
Error.prepareStackTrace = (_, stack) => stack;
const { stack } = new Error();
Error.prepareStackTrace = orig;
return stack;
};

function colorify(str, color) {
color = (color === undefined) ? 6 : color;
var colors = [6, 2, 3, 4, 5, 1];
var colorCode = colors[color];
return '\u001b[3' + color + ';1m' + str + ' ' + '\u001b[0m';
if (Object.getOwnPropertyDescriptor(g, '__stack') === undefined) {
Object.defineProperty(g, '__stack', {
get,
});
}

function colorifyObjectKeys(str) {
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
if (Object.getOwnPropertyDescriptor(g, '__line') === undefined) {
Object.defineProperty(g, '__line', {
get: () => __stack[depth].getLineNumber(),
});
}

function parseIt(obj) {
var textRepresentation = JSON.stringify(obj, censor(obj));
return textRepresentation;
if (Object.getOwnPropertyDescriptor(g, '__function') === undefined) {
Object.defineProperty(g, '__function', {
get: () => __stack[depth].getFunctionName(),
});
}


space = (space === undefined) ? 'default' : space;
const path = require('path');
const bug = require('debug')(space);
return function () {
for (var arg in arguments) {
arguments[arg] = (typeof arguments[arg] === typeof {}) ? parseIt(arguments[arg]) : arguments[arg];
}
return bug([].concat.apply([path.basename(__file) + ':' + __line], arguments).join(' '));
if (Object.getOwnPropertyDescriptor(g, '__file') === undefined) {
Object.defineProperty(g, '__file', {
get: () => __stack[depth].getFileName(),
});
}
}

const bug = debug(space);
return (...statement) => {
const fmtChunks = [];
const reduced = statement.reduce((previous, current) => {
fmtChunks.push(fmtMap[typeof current]);
return [...previous, current];
}, []);
const func = __function ? ` ${__function}() ` : '';
const line = __line ? `${__line}` : '';
const filePath = __file ? `${path.basename(__file)}` : '';
const fileInfo = filePath ? `${filePath}:${line}${func}` : '';
bug(`${fileInfo}${fmtChunks.join(' ')}`, ...reduced);
};
};
21 changes: 21 additions & 0 deletions hai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const debug = require('./debug.js')('info');
const original = require('debug')('info');


const msg = ['Hai!', 'There', { a: 5, b: { c: 6 } }, 2, 2.1, undefined, '', {}, null, {}, { a: 'a' }, console];

const b = () => {
original('Hai!');
original(...msg);

const a = () => {
debug('Hai!');
debug(...msg);
};

a();
};

b();

console.log(...msg);
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/bo01ean/boolean-debug.git"
"url": "git+https://github.com/bo01ean/ndebug.git"
},
"keywords": [
"debug",
Expand All @@ -22,7 +22,11 @@
},
"homepage": "https://github.com/bo01ean/boolean-debug#readme",
"dependencies": {
"debug": "^3.1.0",
"path": "^0.12.7"
"debug": "^4.1.1"
},
"devDependencies": {
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-plugin-import": "^2.20.2"
}
}
Loading

0 comments on commit 07ba0e6

Please sign in to comment.