Skip to content

Commit

Permalink
[Refactor] use has-tostringtag to behave correctly in the presence …
Browse files Browse the repository at this point in the history
…of symbol shams
  • Loading branch information
ljharb committed Aug 6, 2021
1 parent dcee6ed commit b904a54
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 8 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var bigIntValueOf = typeof BigInt === 'function' ? BigInt.prototype.valueOf : nu
var gOPS = Object.getOwnPropertySymbols;
var symToString = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ? Symbol.prototype.toString : null;
var hasShammedSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'object';
// ie, `has-tostringtag/shams
var toStringTag = typeof Symbol === 'function' && Symbol.toStringTag && (typeof Symbol.toStringTag === hasShammedSymbols ? 'object' : 'symbol')
? Symbol.toStringTag
: null;
var isEnumerable = Object.prototype.propertyIsEnumerable;

var gPO = (typeof Reflect === 'function' ? Reflect.getPrototypeOf : Object.getPrototypeOf) || (
Expand All @@ -32,7 +36,6 @@ var gPO = (typeof Reflect === 'function' ? Reflect.getPrototypeOf : Object.getPr

var inspectCustom = require('./util.inspect').custom;
var inspectSymbol = inspectCustom && isSymbol(inspectCustom) ? inspectCustom : null;
var toStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag !== 'undefined' ? Symbol.toStringTag : null;

module.exports = function inspect_(obj, options, depth, seen) {
var opts = options || {};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"eslint": "^7.30.0",
"for-each": "^0.3.3",
"functions-have-names": "^1.2.2",
"has-tostringtag": "^1.0.0",
"make-arrow-function": "^1.2.0",
"nyc": "^10.3.2",
"safe-publish-latest": "^1.1.4",
Expand Down
4 changes: 2 additions & 2 deletions test/bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var inspect = require('../');
var test = require('tape');
var hasSymbols = require('has-symbols/shams')();
var hasToStringTag = require('has-tostringtag/shams')();

test('bigint', { skip: typeof BigInt === 'undefined' }, function (t) {
t.test('primitives', function (st) {
Expand Down Expand Up @@ -30,7 +30,7 @@ test('bigint', { skip: typeof BigInt === 'undefined' }, function (t) {
st.equal(inspect(Function('return 256n')()), '256n');
});

t.test('toStringTag', { skip: !hasSymbols || typeof Symbol.toStringTag === 'undefined' }, function (st) {
t.test('toStringTag', { skip: !hasToStringTag }, function (st) {
st.plan(1);

var faker = {};
Expand Down
4 changes: 2 additions & 2 deletions test/fakes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

var inspect = require('../');
var test = require('tape');
var hasSymbols = require('has-symbols/shams')();
var hasToStringTag = require('has-tostringtag/shams')();
var forEach = require('for-each');

test('fakes', { skip: !hasSymbols || typeof Symbol.toStringTag === 'undefined' }, function (t) {
test('fakes', { skip: !hasToStringTag }, function (t) {
forEach([
'Array',
'Boolean',
Expand Down
4 changes: 2 additions & 2 deletions test/toStringTag.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

var test = require('tape');
var hasSymbols = require('has-symbols/shams')();
var hasToStringTag = require('has-tostringtag/shams')();

var inspect = require('../');

test('Symbol.toStringTag', { skip: !hasSymbols || typeof Symbol.toStringTag === 'undefined' }, function (t) {
test('Symbol.toStringTag', { skip: !hasToStringTag }, function (t) {
t.plan(4);

var obj = { a: 1 };
Expand Down
3 changes: 2 additions & 1 deletion test/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var inspect = require('../');
var test = require('tape');
var hasSymbols = require('has-symbols/shams')();
var hasToStringTag = require('has-tostringtag/shams')();

test('values', function (t) {
t.plan(1);
Expand Down Expand Up @@ -76,7 +77,7 @@ test('symbols', { skip: !hasSymbols }, function (t) {
t.equal(inspect(Object(sym)), 'Object(Symbol(foo))', 'Object(Symbol("foo")) should be "Object(Symbol(foo))"');
}

t.test('toStringTag', { skip: !hasSymbols || typeof Symbol.toStringTag === 'undefined' }, function (st) {
t.test('toStringTag', { skip: !hasToStringTag === 'undefined' }, function (st) {
st.plan(1);

var faker = {};
Expand Down

0 comments on commit b904a54

Please sign in to comment.