object-inspect
Advanced tools
Comparing version 1.11.1 to 1.12.0
@@ -8,2 +8,13 @@ # Changelog | ||
## [v1.12.0](https://github.com/inspect-js/object-inspect/compare/v1.11.1...v1.12.0) - 2021-12-18 | ||
### Commits | ||
- [New] add `numericSeparator` boolean option [`2d2d537`](https://github.com/inspect-js/object-inspect/commit/2d2d537f5359a4300ce1c10241369f8024f89e11) | ||
- [Robustness] cache more prototype methods [`191533d`](https://github.com/inspect-js/object-inspect/commit/191533da8aec98a05eadd73a5a6e979c9c8653e8) | ||
- [New] ensure an Error’s `cause` is displayed [`53bc2ce`](https://github.com/inspect-js/object-inspect/commit/53bc2cee4e5a9cc4986f3cafa22c0685f340715e) | ||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config` [`bc164b6`](https://github.com/inspect-js/object-inspect/commit/bc164b6e2e7d36b263970f16f54de63048b84a36) | ||
- [Robustness] cache `RegExp.prototype.test` [`a314ab8`](https://github.com/inspect-js/object-inspect/commit/a314ab8271b905cbabc594c82914d2485a8daf12) | ||
- [meta] fix auto-changelog settings [`5ed0983`](https://github.com/inspect-js/object-inspect/commit/5ed0983be72f73e32e2559997517a95525c7e20d) | ||
## [v1.11.1](https://github.com/inspect-js/object-inspect/compare/v1.11.0...v1.11.1) - 2021-12-05 | ||
@@ -10,0 +21,0 @@ |
88
index.js
@@ -18,3 +18,12 @@ var hasMap = typeof Map === 'function' && Map.prototype; | ||
var functionToString = Function.prototype.toString; | ||
var match = String.prototype.match; | ||
var $match = String.prototype.match; | ||
var $slice = String.prototype.slice; | ||
var $replace = String.prototype.replace; | ||
var $toUpperCase = String.prototype.toUpperCase; | ||
var $toLowerCase = String.prototype.toLowerCase; | ||
var $test = RegExp.prototype.test; | ||
var $concat = Array.prototype.concat; | ||
var $join = Array.prototype.join; | ||
var $arrSlice = Array.prototype.slice; | ||
var $floor = Math.floor; | ||
var bigIntValueOf = typeof BigInt === 'function' ? BigInt.prototype.valueOf : null; | ||
@@ -38,2 +47,24 @@ var gOPS = Object.getOwnPropertySymbols; | ||
function addNumericSeparator(num, str) { | ||
if ( | ||
num === Infinity | ||
|| num === -Infinity | ||
|| num !== num | ||
|| (num && num > -1000 && num < 1000) | ||
|| $test.call(/e/, str) | ||
) { | ||
return str; | ||
} | ||
var sepRegex = /[0-9](?=(?:[0-9]{3})+(?![0-9]))/g; | ||
if (typeof num === 'number') { | ||
var int = num < 0 ? -$floor(-num) : $floor(num); // trunc(num) | ||
if (int !== num) { | ||
var intStr = String(int); | ||
var dec = $slice.call(str, intStr.length + 1); | ||
return $replace.call(intStr, sepRegex, '$&_') + '.' + $replace.call($replace.call(dec, /([0-9]{3})/g, '$&_'), /_$/, ''); | ||
} | ||
} | ||
return $replace.call(str, sepRegex, '$&_'); | ||
} | ||
var inspectCustom = require('./util.inspect').custom; | ||
@@ -67,4 +98,8 @@ var inspectSymbol = inspectCustom && isSymbol(inspectCustom) ? inspectCustom : null; | ||
) { | ||
throw new TypeError('options "indent" must be "\\t", an integer > 0, or `null`'); | ||
throw new TypeError('option "indent" must be "\\t", an integer > 0, or `null`'); | ||
} | ||
if (has(opts, 'numericSeparator') && typeof opts.numericSeparator !== 'boolean') { | ||
throw new TypeError('option "numericSeparator", if provided, must be `true` or `false`'); | ||
} | ||
var numericSeparator = opts.numericSeparator; | ||
@@ -88,6 +123,8 @@ if (typeof obj === 'undefined') { | ||
} | ||
return String(obj); | ||
var str = String(obj); | ||
return numericSeparator ? addNumericSeparator(obj, str) : str; | ||
} | ||
if (typeof obj === 'bigint') { | ||
return String(obj) + 'n'; | ||
var bigIntStr = String(obj) + 'n'; | ||
return numericSeparator ? addNumericSeparator(obj, bigIntStr) : bigIntStr; | ||
} | ||
@@ -111,3 +148,3 @@ | ||
if (from) { | ||
seen = seen.slice(); | ||
seen = $arrSlice.call(seen); | ||
seen.push(from); | ||
@@ -130,10 +167,10 @@ } | ||
var keys = arrObjKeys(obj, inspect); | ||
return '[Function' + (name ? ': ' + name : ' (anonymous)') + ']' + (keys.length > 0 ? ' { ' + keys.join(', ') + ' }' : ''); | ||
return '[Function' + (name ? ': ' + name : ' (anonymous)') + ']' + (keys.length > 0 ? ' { ' + $join.call(keys, ', ') + ' }' : ''); | ||
} | ||
if (isSymbol(obj)) { | ||
var symString = hasShammedSymbols ? String(obj).replace(/^(Symbol\(.*\))_[^)]*$/, '$1') : symToString.call(obj); | ||
var symString = hasShammedSymbols ? $replace.call(String(obj), /^(Symbol\(.*\))_[^)]*$/, '$1') : symToString.call(obj); | ||
return typeof obj === 'object' && !hasShammedSymbols ? markBoxed(symString) : symString; | ||
} | ||
if (isElement(obj)) { | ||
var s = '<' + String(obj.nodeName).toLowerCase(); | ||
var s = '<' + $toLowerCase.call(String(obj.nodeName)); | ||
var attrs = obj.attributes || []; | ||
@@ -145,3 +182,3 @@ for (var i = 0; i < attrs.length; i++) { | ||
if (obj.childNodes && obj.childNodes.length) { s += '...'; } | ||
s += '</' + String(obj.nodeName).toLowerCase() + '>'; | ||
s += '</' + $toLowerCase.call(String(obj.nodeName)) + '>'; | ||
return s; | ||
@@ -155,8 +192,11 @@ } | ||
} | ||
return '[ ' + xs.join(', ') + ' ]'; | ||
return '[ ' + $join.call(xs, ', ') + ' ]'; | ||
} | ||
if (isError(obj)) { | ||
var parts = arrObjKeys(obj, inspect); | ||
if ('cause' in obj && !isEnumerable.call(obj, 'cause')) { | ||
return '{ [' + String(obj) + '] ' + $join.call($concat.call('[cause]: ' + inspect(obj.cause), parts), ', ') + ' }'; | ||
} | ||
if (parts.length === 0) { return '[' + String(obj) + ']'; } | ||
return '{ [' + String(obj) + '] ' + parts.join(', ') + ' }'; | ||
return '{ [' + String(obj) + '] ' + $join.call(parts, ', ') + ' }'; | ||
} | ||
@@ -209,5 +249,5 @@ if (typeof obj === 'object' && customInspect) { | ||
var protoTag = obj instanceof Object ? '' : 'null prototype'; | ||
var stringTag = !isPlainObject && toStringTag && Object(obj) === obj && toStringTag in obj ? toStr(obj).slice(8, -1) : protoTag ? 'Object' : ''; | ||
var stringTag = !isPlainObject && toStringTag && Object(obj) === obj && toStringTag in obj ? $slice.call(toStr(obj), 8, -1) : protoTag ? 'Object' : ''; | ||
var constructorTag = isPlainObject || typeof obj.constructor !== 'function' ? '' : obj.constructor.name ? obj.constructor.name + ' ' : ''; | ||
var tag = constructorTag + (stringTag || protoTag ? '[' + [].concat(stringTag || [], protoTag || []).join(': ') + '] ' : ''); | ||
var tag = constructorTag + (stringTag || protoTag ? '[' + $join.call($concat.call([], stringTag || [], protoTag || []), ': ') + '] ' : ''); | ||
if (ys.length === 0) { return tag + '{}'; } | ||
@@ -217,3 +257,3 @@ if (indent) { | ||
} | ||
return tag + '{ ' + ys.join(', ') + ' }'; | ||
return tag + '{ ' + $join.call(ys, ', ') + ' }'; | ||
} | ||
@@ -229,3 +269,3 @@ return String(obj); | ||
function quote(s) { | ||
return String(s).replace(/"/g, '"'); | ||
return $replace.call(String(s), /"/g, '"'); | ||
} | ||
@@ -281,3 +321,3 @@ | ||
if (f.name) { return f.name; } | ||
var m = match.call(functionToString.call(f), /^function\s*([\w$]+)/); | ||
var m = $match.call(functionToString.call(f), /^function\s*([\w$]+)/); | ||
if (m) { return m[1]; } | ||
@@ -382,6 +422,6 @@ return null; | ||
var trailer = '... ' + remaining + ' more character' + (remaining > 1 ? 's' : ''); | ||
return inspectString(str.slice(0, opts.maxStringLength), opts) + trailer; | ||
return inspectString($slice.call(str, 0, opts.maxStringLength), opts) + trailer; | ||
} | ||
// eslint-disable-next-line no-control-regex | ||
var s = str.replace(/(['\\])/g, '\\$1').replace(/[\x00-\x1f]/g, lowbyte); | ||
var s = $replace.call($replace.call(str, /(['\\])/g, '\\$1'), /[\x00-\x1f]/g, lowbyte); | ||
return wrapQuotes(s, 'single', opts); | ||
@@ -400,3 +440,3 @@ } | ||
if (x) { return '\\' + x; } | ||
return '\\x' + (n < 0x10 ? '0' : '') + n.toString(16).toUpperCase(); | ||
return '\\x' + (n < 0x10 ? '0' : '') + $toUpperCase.call(n.toString(16)); | ||
} | ||
@@ -413,3 +453,3 @@ | ||
function collectionOf(type, size, entries, indent) { | ||
var joinedEntries = indent ? indentedJoin(entries, indent) : entries.join(', '); | ||
var joinedEntries = indent ? indentedJoin(entries, indent) : $join.call(entries, ', '); | ||
return type + ' (' + size + ') {' + joinedEntries + '}'; | ||
@@ -432,3 +472,3 @@ } | ||
} else if (typeof opts.indent === 'number' && opts.indent > 0) { | ||
baseIndent = Array(opts.indent + 1).join(' '); | ||
baseIndent = $join.call(Array(opts.indent + 1), ' '); | ||
} else { | ||
@@ -439,3 +479,3 @@ return null; | ||
base: baseIndent, | ||
prev: Array(depth + 1).join(baseIndent) | ||
prev: $join.call(Array(depth + 1), baseIndent) | ||
}; | ||
@@ -447,3 +487,3 @@ } | ||
var lineJoiner = '\n' + indent.prev + indent.base; | ||
return lineJoiner + xs.join(',' + lineJoiner) + '\n' + indent.prev; | ||
return lineJoiner + $join.call(xs, ',' + lineJoiner) + '\n' + indent.prev; | ||
} | ||
@@ -475,3 +515,3 @@ | ||
continue; // eslint-disable-line no-restricted-syntax, no-continue | ||
} else if ((/[^\w$]/).test(key)) { | ||
} else if ($test.call(/[^\w$]/, key)) { | ||
xs.push(inspect(key, obj) + ': ' + inspect(obj[key], obj)); | ||
@@ -478,0 +518,0 @@ } else { |
{ | ||
"name": "object-inspect", | ||
"version": "1.11.1", | ||
"version": "1.12.0", | ||
"description": "string representations of objects in node and the browser", | ||
@@ -8,7 +8,9 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@ljharb/eslint-config": "^20.0.0", | ||
"@ljharb/eslint-config": "^20.1.0", | ||
"aud": "^1.1.5", | ||
"auto-changelog": "^2.3.0", | ||
"core-js": "^2.6.12", | ||
"eslint": "^8.4.0", | ||
"error-cause": "^1.0.3", | ||
"es-value-fixtures": "^1.2.1", | ||
"eslint": "^8.5.0", | ||
"for-each": "^0.3.3", | ||
@@ -15,0 +17,0 @@ "functions-have-names": "^1.2.2", |
@@ -45,3 +45,15 @@ 'use strict'; | ||
t.test('numericSeparator', function (st) { | ||
st.equal(inspect(BigInt(0), { numericSeparator: false }), '0n', '0n, numericSeparator false'); | ||
st.equal(inspect(BigInt(0), { numericSeparator: true }), '0n', '0n, numericSeparator true'); | ||
st.equal(inspect(BigInt(1234), { numericSeparator: false }), '1234n', '1234n, numericSeparator false'); | ||
st.equal(inspect(BigInt(1234), { numericSeparator: true }), '1_234n', '1234n, numericSeparator true'); | ||
st.equal(inspect(BigInt(-1234), { numericSeparator: false }), '-1234n', '1234n, numericSeparator false'); | ||
st.equal(inspect(BigInt(-1234), { numericSeparator: true }), '-1_234n', '1234n, numericSeparator true'); | ||
st.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -1,4 +0,6 @@ | ||
var inspect = require('../'); | ||
var test = require('tape'); | ||
var ErrorWithCause = require('error-cause/Error'); | ||
var inspect = require('../'); | ||
test('type error', function (t) { | ||
@@ -17,2 +19,9 @@ t.plan(1); | ||
var withCause = new ErrorWithCause('foo', { cause: 'bar' }); | ||
var withCausePlus = new ErrorWithCause('foo', { cause: 'bar' }); | ||
withCausePlus.foo = 'bar'; | ||
var withUndefinedCause = new ErrorWithCause('foo', { cause: undefined }); | ||
var withEnumerableCause = new Error('foo'); | ||
withEnumerableCause.cause = 'bar'; | ||
var obj = [ | ||
@@ -23,3 +32,7 @@ new TypeError(), | ||
berr, | ||
cerr | ||
cerr, | ||
withCause, | ||
withCausePlus, | ||
withUndefinedCause, | ||
withEnumerableCause | ||
]; | ||
@@ -31,4 +44,8 @@ t.equal(inspect(obj), '[ ' + [ | ||
'{ [TypeError: tuv] baz: 555 }', | ||
'{ [SyntaxError: whoa] message: \'whoa\', \'a-b\': 5 }' | ||
'{ [SyntaxError: whoa] message: \'whoa\', \'a-b\': 5 }', | ||
'{ [Error: foo] [cause]: \'bar\' }', | ||
'{ [Error: foo] [cause]: \'bar\', foo: \'bar\' }', | ||
'{ [Error: foo] [cause]: undefined }', | ||
'{ [Error: foo] cause: \'bar\' }' | ||
].join(', ') + ' ]'); | ||
}); |
@@ -1,4 +0,7 @@ | ||
var inspect = require('../'); | ||
var test = require('tape'); | ||
var v = require('es-value-fixtures'); | ||
var forEach = require('for-each'); | ||
var inspect = require('../'); | ||
test('negative zero', function (t) { | ||
@@ -13,1 +16,44 @@ t.equal(inspect(0), '0', 'inspect(0) === "0"'); | ||
}); | ||
test('numericSeparator', function (t) { | ||
forEach(v.nonBooleans, function (nonBoolean) { | ||
t['throws']( | ||
function () { inspect(true, { numericSeparator: nonBoolean }); }, | ||
TypeError, | ||
inspect(nonBoolean) + ' is not a boolean' | ||
); | ||
}); | ||
t.test('3 digit numbers', function (st) { | ||
var failed = false; | ||
for (var i = -999; i < 1000; i += 1) { | ||
var actual = inspect(i); | ||
var actualSepNo = inspect(i, { numericSeparator: false }); | ||
var actualSepYes = inspect(i, { numericSeparator: true }); | ||
var expected = String(i); | ||
if (actual !== expected || actualSepNo !== expected || actualSepYes !== expected) { | ||
failed = true; | ||
t.equal(actual, expected); | ||
t.equal(actualSepNo, expected); | ||
t.equal(actualSepYes, expected); | ||
} | ||
} | ||
st.notOk(failed, 'all 3 digit numbers passed'); | ||
st.end(); | ||
}); | ||
t.equal(inspect(1e3), '1000', '1000'); | ||
t.equal(inspect(1e3, { numericSeparator: false }), '1000', '1000, numericSeparator false'); | ||
t.equal(inspect(1e3, { numericSeparator: true }), '1_000', '1000, numericSeparator true'); | ||
t.equal(inspect(-1e3), '-1000', '-1000'); | ||
t.equal(inspect(-1e3, { numericSeparator: false }), '-1000', '-1000, numericSeparator false'); | ||
t.equal(inspect(-1e3, { numericSeparator: true }), '-1_000', '-1000, numericSeparator true'); | ||
t.equal(inspect(1234.5678, { numericSeparator: true }), '1_234.567_8', 'fractional numbers get separators'); | ||
t.equal(inspect(1234.56789, { numericSeparator: true }), '1_234.567_89', 'fractional numbers get separators'); | ||
t.equal(inspect(1234.567891, { numericSeparator: true }), '1_234.567_891', 'fractional numbers get separators'); | ||
t.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
90592
1433
87
15