-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make it only declare globals once. * globalThis for browser too * Fix formatting Remove arguments.callee
- Loading branch information
1 parent
4e1b146
commit 07ba0e6
Showing
6 changed files
with
1,402 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: { | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.