object-inspect
Advanced tools
Comparing version 1.10.2 to 1.10.3
25
index.js
@@ -22,2 +22,3 @@ var hasMap = typeof Map === 'function' && Map.prototype; | ||
var symToString = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ? Symbol.prototype.toString : null; | ||
var hasShammedSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'object'; | ||
var isEnumerable = Object.prototype.propertyIsEnumerable; | ||
@@ -35,3 +36,3 @@ | ||
var inspectSymbol = inspectCustom && isSymbol(inspectCustom) ? inspectCustom : null; | ||
var toStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol' ? Symbol.toStringTag : null; | ||
var toStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag !== 'undefined' ? Symbol.toStringTag : null; | ||
@@ -126,4 +127,4 @@ module.exports = function inspect_(obj, options, depth, seen) { | ||
if (isSymbol(obj)) { | ||
var symString = symToString.call(obj); | ||
return typeof obj === 'object' ? markBoxed(symString) : symString; | ||
var symString = hasShammedSymbols ? String(obj).replace(/^(Symbol\(.*\))_[^)]*$/, '$1') : symToString.call(obj); | ||
return typeof obj === 'object' && !hasShammedSymbols ? markBoxed(symString) : symString; | ||
} | ||
@@ -231,2 +232,5 @@ if (isElement(obj)) { | ||
function isSymbol(obj) { | ||
if (hasShammedSymbols) { | ||
return obj && typeof obj === 'object' && obj instanceof Symbol; | ||
} | ||
if (typeof obj === 'symbol') { | ||
@@ -439,6 +443,18 @@ return true; | ||
} | ||
var syms = typeof gOPS === 'function' ? gOPS(obj) : []; | ||
var symMap; | ||
if (hasShammedSymbols) { | ||
symMap = {}; | ||
for (var k = 0; k < syms.length; k++) { | ||
symMap['$' + syms[k]] = syms[k]; | ||
} | ||
} | ||
for (var key in obj) { // eslint-disable-line no-restricted-syntax | ||
if (!has(obj, key)) { continue; } // eslint-disable-line no-restricted-syntax, no-continue | ||
if (isArr && String(Number(key)) === key && key < obj.length) { continue; } // eslint-disable-line no-restricted-syntax, no-continue | ||
if ((/[^\w$]/).test(key)) { | ||
if (hasShammedSymbols && symMap['$' + key] instanceof Symbol) { | ||
// this is to prevent shammed Symbols, which are stored as strings, from being included in the string key section | ||
continue; // eslint-disable-line no-restricted-syntax, no-continue | ||
} else if ((/[^\w$]/).test(key)) { | ||
xs.push(inspect(key, obj) + ': ' + inspect(obj[key], obj)); | ||
@@ -450,3 +466,2 @@ } else { | ||
if (typeof gOPS === 'function') { | ||
var syms = gOPS(obj); | ||
for (var j = 0; j < syms.length; j++) { | ||
@@ -453,0 +468,0 @@ if (isEnumerable.call(obj, syms[j])) { |
{ | ||
"name": "object-inspect", | ||
"version": "1.10.2", | ||
"version": "1.10.3", | ||
"description": "string representations of objects in node and the browser", | ||
"main": "index.js", | ||
"devDependencies": { | ||
"@ljharb/eslint-config": "^17.5.1", | ||
"aud": "^1.1.4", | ||
"@ljharb/eslint-config": "^17.6.0", | ||
"aud": "^1.1.5", | ||
"core-js": "^2.6.12", | ||
"eslint": "^7.24.0", | ||
"eslint": "^7.26.0", | ||
"for-each": "^0.3.3", | ||
@@ -24,6 +24,5 @@ "functions-have-names": "^1.2.2", | ||
"lint": "eslint .", | ||
"test": "npm run tests-only", | ||
"tests-only": "nyc npm run tests-only:tape", | ||
"pretests-only:tape": "node test-core-js", | ||
"tests-only:tape": "tape 'test/*.js'", | ||
"test": "npm run tests-only && npm run test:corejs", | ||
"tests-only": "nyc tape 'test/*.js'", | ||
"test:corejs": "nyc tape test-core-js.js 'test/*.js'", | ||
"posttest": "npx aud --production" | ||
@@ -30,0 +29,0 @@ }, |
@@ -5,3 +5,3 @@ 'use strict'; | ||
var test = require('tape'); | ||
var hasSymbols = require('has-symbols')(); | ||
var hasSymbols = require('has-symbols/shams')(); | ||
@@ -34,3 +34,3 @@ test('bigint', { skip: typeof BigInt === 'undefined' }, function (t) { | ||
t.test('toStringTag', { skip: !hasSymbols || typeof Symbol.toStringTag !== 'symbol' }, function (st) { | ||
t.test('toStringTag', { skip: !hasSymbols || typeof Symbol.toStringTag === 'undefined' }, function (st) { | ||
st.plan(1); | ||
@@ -37,0 +37,0 @@ |
@@ -5,6 +5,6 @@ 'use strict'; | ||
var test = require('tape'); | ||
var hasSymbols = require('has-symbols')(); | ||
var hasSymbols = require('has-symbols/shams')(); | ||
var forEach = require('for-each'); | ||
test('fakes', { skip: !hasSymbols || typeof Symbol.toStringTag !== 'symbol' }, function (t) { | ||
test('fakes', { skip: !hasSymbols || typeof Symbol.toStringTag === 'undefined' }, function (t) { | ||
forEach([ | ||
@@ -11,0 +11,0 @@ 'Array', |
var test = require('tape'); | ||
var hasSymbols = require('has-symbols')(); | ||
var hasSymbols = require('has-symbols/shams')(); | ||
var utilInspect = require('../util.inspect'); | ||
@@ -46,4 +46,18 @@ var repeat = require('string.prototype.repeat'); | ||
t.equal(inspect(obj), '{ a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }', 'object with symbols'); | ||
t.equal(inspect([obj, []]), '[ { a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }, [] ]', 'object with symbols'); | ||
if (typeof Symbol.iterator === 'symbol') { | ||
t.equal(inspect(obj), '{ a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }', 'object with symbols'); | ||
t.equal(inspect([obj, []]), '[ { a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }, [] ]', 'object with symbols in array'); | ||
} else { | ||
// symbol sham key ordering is unreliable | ||
t.match( | ||
inspect(obj), | ||
/^(?:{ a: 1, \[Symbol\(test\)\]: 2, \[Symbol\(Symbol.iterator\)\]: 3 }|{ a: 1, \[Symbol\(Symbol.iterator\)\]: 3, \[Symbol\(test\)\]: 2 })$/, | ||
'object with symbols (nondeterministic symbol sham key ordering)' | ||
); | ||
t.match( | ||
inspect([obj, []]), | ||
/^\[ (?:{ a: 1, \[Symbol\(test\)\]: 2, \[Symbol\(Symbol.iterator\)\]: 3 }|{ a: 1, \[Symbol\(Symbol.iterator\)\]: 3, \[Symbol\(test\)\]: 2 }), \[\] \]$/, | ||
'object with symbols in array (nondeterministic symbol sham key ordering)' | ||
); | ||
} | ||
}); | ||
@@ -50,0 +64,0 @@ |
'use strict'; | ||
var test = require('tape'); | ||
var hasSymbols = require('has-symbols')(); | ||
var hasSymbols = require('has-symbols/shams')(); | ||
var inspect = require('..'); | ||
var inspect = require('../'); | ||
test('Symbol.toStringTag', { skip: !hasSymbols || typeof Symbol.toStringTag !== 'symbol' }, function (t) { | ||
test('Symbol.toStringTag', { skip: !hasSymbols || typeof Symbol.toStringTag === 'undefined' }, function (t) { | ||
t.plan(4); | ||
@@ -10,0 +10,0 @@ |
@@ -5,3 +5,3 @@ 'use strict'; | ||
var test = require('tape'); | ||
var hasSymbols = require('has-symbols')(); | ||
var hasSymbols = require('has-symbols/shams')(); | ||
@@ -72,8 +72,11 @@ test('values', function (t) { | ||
test('symbols', { skip: typeof Symbol !== 'function' }, function (t) { | ||
test('symbols', { skip: !hasSymbols }, function (t) { | ||
var sym = Symbol('foo'); | ||
t.equal(inspect(sym), 'Symbol(foo)', 'Symbol("foo") should be "Symbol(foo)"'); | ||
t.equal(inspect(Object(sym)), 'Object(Symbol(foo))', 'Object(Symbol("foo")) should be "Object(Symbol(foo))"'); | ||
if (typeof sym === 'symbol') { | ||
// Symbol shams are incapable of differentiating boxed from unboxed symbols | ||
t.equal(inspect(Object(sym)), 'Object(Symbol(foo))', 'Object(Symbol("foo")) should be "Object(Symbol(foo))"'); | ||
} | ||
t.test('toStringTag', { skip: !hasSymbols || typeof Symbol.toStringTag !== 'symbol' }, function (st) { | ||
t.test('toStringTag', { skip: !hasSymbols || typeof Symbol.toStringTag === 'undefined' }, function (st) { | ||
st.plan(1); | ||
@@ -80,0 +83,0 @@ |
Sorry, the diff of this file is not supported yet
55416
1322
86