object-inspect
Advanced tools
Comparing version 1.9.0 to 1.10.0
53
index.js
@@ -13,2 +13,4 @@ var hasMap = typeof Map === 'function' && Map.prototype; | ||
var weakSetHas = hasWeakSet ? WeakSet.prototype.has : null; | ||
var hasWeakRef = typeof WeakRef === 'function' && WeakRef.prototype; | ||
var weakRefDeref = hasWeakRef ? WeakRef.prototype.deref : null; | ||
var booleanValueOf = Boolean.prototype.valueOf; | ||
@@ -23,4 +25,13 @@ var objectToString = Object.prototype.toString; | ||
var gPO = (typeof Reflect === 'function' ? Reflect.getPrototypeOf : Object.getPrototypeOf) || ( | ||
[].__proto__ === Array.prototype // eslint-disable-line no-proto | ||
? function (O) { | ||
return O.__proto__; // eslint-disable-line no-proto | ||
} | ||
: null | ||
); | ||
var inspectCustom = require('./util.inspect').custom; | ||
var inspectSymbol = inspectCustom && isSymbol(inspectCustom) ? inspectCustom : null; | ||
var toStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol' ? Symbol.toStringTag : null; | ||
@@ -169,2 +180,5 @@ module.exports = function inspect_(obj, options, depth, seen) { | ||
} | ||
if (isWeakRef(obj)) { | ||
return weakCollectionOf('WeakRef'); | ||
} | ||
if (isNumber(obj)) { | ||
@@ -184,7 +198,12 @@ return markBoxed(inspect(Number(obj))); | ||
var ys = arrObjKeys(obj, inspect); | ||
if (ys.length === 0) { return '{}'; } | ||
var isPlainObject = gPO ? gPO(obj) === Object.prototype : obj instanceof Object || obj.constructor === Object; | ||
var protoTag = obj instanceof Object ? '' : 'null prototype'; | ||
var stringTag = !isPlainObject && toStringTag && toStringTag in obj ? toStr(obj).slice(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(': ') + '] ' : ''); | ||
if (ys.length === 0) { return tag + '{}'; } | ||
if (indent) { | ||
return '{' + indentedJoin(ys, indent) + '}'; | ||
return tag + '{' + indentedJoin(ys, indent) + '}'; | ||
} | ||
return '{ ' + ys.join(', ') + ' }'; | ||
return tag + '{ ' + ys.join(', ') + ' }'; | ||
} | ||
@@ -203,11 +222,14 @@ return String(obj); | ||
function isArray(obj) { return toStr(obj) === '[object Array]'; } | ||
function isDate(obj) { return toStr(obj) === '[object Date]'; } | ||
function isRegExp(obj) { return toStr(obj) === '[object RegExp]'; } | ||
function isError(obj) { return toStr(obj) === '[object Error]'; } | ||
var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol'; | ||
function isArray(obj) { return toStr(obj) === '[object Array]' && (!hasToStringTag || !(Symbol.toStringTag in obj)); } | ||
function isDate(obj) { return toStr(obj) === '[object Date]' && (!hasToStringTag || !(Symbol.toStringTag in obj)); } | ||
function isRegExp(obj) { return toStr(obj) === '[object RegExp]' && (!hasToStringTag || !(Symbol.toStringTag in obj)); } | ||
function isError(obj) { return toStr(obj) === '[object Error]' && (!hasToStringTag || !(Symbol.toStringTag in obj)); } | ||
function isString(obj) { return toStr(obj) === '[object String]' && (!hasToStringTag || !(Symbol.toStringTag in obj)); } | ||
function isNumber(obj) { return toStr(obj) === '[object Number]' && (!hasToStringTag || !(Symbol.toStringTag in obj)); } | ||
function isBoolean(obj) { return toStr(obj) === '[object Boolean]' && (!hasToStringTag || !(Symbol.toStringTag in obj)); } | ||
// Symbol and BigInt do have Symbol.toStringTag by spec, so that can't be used to eliminate false positives | ||
function isSymbol(obj) { return toStr(obj) === '[object Symbol]'; } | ||
function isString(obj) { return toStr(obj) === '[object String]'; } | ||
function isNumber(obj) { return toStr(obj) === '[object Number]'; } | ||
function isBigInt(obj) { return toStr(obj) === '[object BigInt]'; } | ||
function isBoolean(obj) { return toStr(obj) === '[object Boolean]'; } | ||
@@ -270,2 +292,13 @@ var hasOwn = Object.prototype.hasOwnProperty || function (key) { return key in this; }; | ||
function isWeakRef(x) { | ||
if (!weakRefDeref || !x || typeof x !== 'object') { | ||
return false; | ||
} | ||
try { | ||
weakRefDeref.call(x); | ||
return true; | ||
} catch (e) {} | ||
return false; | ||
} | ||
function isSet(x) { | ||
@@ -272,0 +305,0 @@ if (!setSize || !x || typeof x !== 'object') { |
{ | ||
"name": "object-inspect", | ||
"version": "1.9.0", | ||
"version": "1.10.0", | ||
"description": "string representations of objects in node and the browser", | ||
"main": "index.js", | ||
"devDependencies": { | ||
"@ljharb/eslint-config": "^17.3.0", | ||
"aud": "^1.1.3", | ||
"@ljharb/eslint-config": "^17.5.1", | ||
"aud": "^1.1.4", | ||
"core-js": "^2.6.12", | ||
"eslint": "^7.14.0", | ||
"eslint": "^7.24.0", | ||
"for-each": "^0.3.3", | ||
"functions-have-names": "^1.2.2", | ||
"make-arrow-function": "^1.2.0", | ||
"nyc": "^10.3.2", | ||
"safe-publish-latest": "^1.1.4", | ||
"string.prototype.repeat": "^1.0.0", | ||
"tape": "^5.0.1" | ||
"tape": "^5.2.2" | ||
}, | ||
"scripts": { | ||
"prepublish": "safe-publish-latest", | ||
"prepublish": "not-in-publish || npm run prepublishOnly", | ||
"prepublishOnly": "safe-publish-latest", | ||
"pretest": "npm run lint", | ||
@@ -20,0 +23,0 @@ "lint": "eslint .", |
@@ -5,3 +5,3 @@ var inspect = require('../'); | ||
test('deep', function (t) { | ||
t.plan(3); | ||
t.plan(4); | ||
var obj = [[[[[[500]]]]]]; | ||
@@ -11,2 +11,4 @@ t.equal(inspect(obj), '[ [ [ [ [ [Array] ] ] ] ] ]'); | ||
t.equal(inspect(obj, { depth: 2 }), '[ [ [Array] ] ]'); | ||
t.equal(inspect([[[{ a: 1 }]]], { depth: 3 }), '[ [ [ [Object] ] ] ]'); | ||
}); |
var inspect = require('../'); | ||
var test = require('tape'); | ||
var arrow = require('make-arrow-function')(); | ||
var functionsHaveConfigurableNames = require('functions-have-names').functionsHaveConfigurableNames(); | ||
@@ -29,1 +31,47 @@ test('function', function (t) { | ||
}); | ||
test('arrow function', { skip: !arrow }, function (t) { | ||
t.equal(inspect(arrow), '[Function (anonymous)]'); | ||
t.end(); | ||
}); | ||
test('truly nameless function', { skip: !arrow || !functionsHaveConfigurableNames }, function (t) { | ||
function f() {} | ||
Object.defineProperty(f, 'name', { value: false }); | ||
t.equal(f.name, false); | ||
t.equal( | ||
inspect(f), | ||
'[Function: f]', | ||
'named function with falsy `.name` does not hide its original name' | ||
); | ||
function g() {} | ||
Object.defineProperty(g, 'name', { value: true }); | ||
t.equal(g.name, true); | ||
t.equal( | ||
inspect(g), | ||
'[Function: true]', | ||
'named function with truthy `.name` hides its original name' | ||
); | ||
var anon = function () {}; // eslint-disable-line func-style | ||
Object.defineProperty(anon, 'name', { value: null }); | ||
t.equal(anon.name, null); | ||
t.equal( | ||
inspect(anon), | ||
'[Function (anonymous)]', | ||
'anon function with falsy `.name` does not hide its anonymity' | ||
); | ||
var anon2 = function () {}; // eslint-disable-line func-style | ||
Object.defineProperty(anon2, 'name', { value: 1 }); | ||
t.equal(anon2.name, 1); | ||
t.equal( | ||
inspect(anon2), | ||
'[Function: 1]', | ||
'anon function with truthy `.name` hides its anonymity' | ||
); | ||
t.end(); | ||
}); |
@@ -9,3 +9,3 @@ var test = require('tape'); | ||
test('inspect', function (t) { | ||
t.plan(3); | ||
t.plan(4); | ||
var obj = [{ inspect: function xyzInspect() { return '!XYZ¡'; } }, []]; | ||
@@ -15,2 +15,7 @@ t.equal(inspect(obj), '[ !XYZ¡, [] ]'); | ||
t.equal(inspect(obj, { customInspect: false }), '[ { inspect: [Function: xyzInspect] }, [] ]'); | ||
t['throws']( | ||
function () { inspect(obj, { customInspect: 'not a boolean' }); }, | ||
TypeError, | ||
'`customInspect` must be a boolean' | ||
); | ||
}); | ||
@@ -48,4 +53,12 @@ | ||
test('maxStringLength', function (t) { | ||
t['throws']( | ||
function () { inspect('', { maxStringLength: -1 }); }, | ||
TypeError, | ||
'maxStringLength must be >= 0, or Infinity, not negative' | ||
); | ||
var str = repeat('a', 1e8); | ||
t.equal( | ||
inspect([repeat('a', 1e8)], { maxStringLength: 10 }), | ||
inspect([str], { maxStringLength: 10 }), | ||
'[ \'aaaaaaaaaa\'... 99999990 more characters ]', | ||
@@ -55,3 +68,15 @@ 'maxStringLength option limits output' | ||
t.equal( | ||
inspect(['f'], { maxStringLength: null }), | ||
'[ \'\'... 1 more character ]', | ||
'maxStringLength option accepts `null`' | ||
); | ||
t.equal( | ||
inspect([str], { maxStringLength: Infinity }), | ||
'[ \'' + str + '\' ]', | ||
'maxStringLength option accepts ∞' | ||
); | ||
t.end(); | ||
}); |
@@ -126,2 +126,18 @@ var inspect = require('../'); | ||
test('WeakRef', { skip: typeof WeakRef !== 'function' }, function (t) { | ||
var ref = new WeakRef({ a: 1 }); | ||
var expectedString = 'WeakRef { ? }'; | ||
t.equal(inspect(ref), expectedString, 'new WeakRef({ a: 1 }) should not show contents'); | ||
t.end(); | ||
}); | ||
test('FinalizationRegistry', { skip: typeof FinalizationRegistry !== 'function' }, function (t) { | ||
var registry = new FinalizationRegistry(function () {}); | ||
var expectedString = 'FinalizationRegistry [FinalizationRegistry] {}'; | ||
t.equal(inspect(registry), expectedString, 'new FinalizationRegistry(function () {}) should work normallys'); | ||
t.end(); | ||
}); | ||
test('Strings', function (t) { | ||
@@ -128,0 +144,0 @@ var str = 'abc'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51220
1247
11
32