Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa5870d

Browse files
committed
[Fix] avoid being fooled by a Symbol.toStringTag
1 parent 3edfb01 commit fa5870d

2 files changed

Lines changed: 56 additions & 7 deletions

File tree

index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,16 @@ function quote(s) {
287287
return $replace.call(String(s), /"/g, '"');
288288
}
289289

290-
function isArray(obj) { return toStr(obj) === '[object Array]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
291-
function isDate(obj) { return toStr(obj) === '[object Date]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
292-
function isRegExp(obj) { return toStr(obj) === '[object RegExp]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
293-
function isError(obj) { return toStr(obj) === '[object Error]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
294-
function isString(obj) { return toStr(obj) === '[object String]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
295-
function isNumber(obj) { return toStr(obj) === '[object Number]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
296-
function isBoolean(obj) { return toStr(obj) === '[object Boolean]' && (!toStringTag || !(typeof obj === 'object' && toStringTag in obj)); }
290+
function canTrustToString(obj) {
291+
return !toStringTag || !(typeof obj === 'object' && (toStringTag in obj || typeof obj[toStringTag] !== 'undefined'));
292+
}
293+
function isArray(obj) { return toStr(obj) === '[object Array]' && canTrustToString(obj); }
294+
function isDate(obj) { return toStr(obj) === '[object Date]' && canTrustToString(obj); }
295+
function isRegExp(obj) { return toStr(obj) === '[object RegExp]' && canTrustToString(obj); }
296+
function isError(obj) { return toStr(obj) === '[object Error]' && canTrustToString(obj); }
297+
function isString(obj) { return toStr(obj) === '[object String]' && canTrustToString(obj); }
298+
function isNumber(obj) { return toStr(obj) === '[object Number]' && canTrustToString(obj); }
299+
function isBoolean(obj) { return toStr(obj) === '[object Boolean]' && canTrustToString(obj); }
297300

298301
// Symbol and BigInt do have Symbol.toStringTag by spec, so that can't be used to eliminate false positives
299302
function isSymbol(obj) {

test/values.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var test = require('tape');
55
var mockProperty = require('mock-property');
66
var hasSymbols = require('has-symbols/shams')();
77
var hasToStringTag = require('has-tostringtag/shams')();
8+
var forEach = require('for-each');
89

910
test('values', function (t) {
1011
t.plan(1);
@@ -209,3 +210,48 @@ test('RegExps', function (t) {
209210

210211
t.end();
211212
});
213+
214+
test('Proxies', { skip: typeof Proxy !== 'function' || !hasToStringTag }, function (t) {
215+
var target = { proxy: true };
216+
var fake = new Proxy(target, { has: function () { return false; } });
217+
218+
forEach([
219+
'Boolean',
220+
'Number',
221+
'String',
222+
'Symbol',
223+
'Date'
224+
], function (tag) {
225+
target[Symbol.toStringTag] = tag;
226+
227+
t.equal(
228+
inspect(fake),
229+
'{ proxy: true, [Symbol(Symbol.toStringTag)]: \'' + tag + '\' }',
230+
'Proxy for + ' + tag + ' shows as the target, which has no slots'
231+
);
232+
});
233+
234+
t.end();
235+
});
236+
237+
test('fakers', { skip: !hasToStringTag }, function (t) {
238+
var target = { proxy: false };
239+
240+
forEach([
241+
'Boolean',
242+
'Number',
243+
'String',
244+
'Symbol',
245+
'Date'
246+
], function (tag) {
247+
target[Symbol.toStringTag] = tag;
248+
249+
t.equal(
250+
inspect(target),
251+
'{ proxy: false, [Symbol(Symbol.toStringTag)]: \'' + tag + '\' }',
252+
'Object pretending to be ' + tag + ' does not trick us'
253+
);
254+
});
255+
256+
t.end();
257+
});

0 commit comments

Comments
 (0)