Skip to content

Commit d5ca829

Browse files
committed
[Fix] use a robust check for a boxed bigint
1 parent 15e6c3e commit d5ca829

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,17 @@ function isNumber(obj) { return toStr(obj) === '[object Number]' && (!hasToStrin
226226
function isBoolean(obj) { return toStr(obj) === '[object Boolean]' && (!hasToStringTag || !(Symbol.toStringTag in obj)); }
227227
// Symbol and BigInt do have Symbol.toStringTag by spec, so that can't be used to eliminate false positives
228228
function isSymbol(obj) { return toStr(obj) === '[object Symbol]'; }
229-
function isBigInt(obj) { return toStr(obj) === '[object BigInt]'; }
229+
230+
function isBigInt(obj) {
231+
if (!obj || typeof obj !== 'object' || !bigIntValueOf) {
232+
return false;
233+
}
234+
try {
235+
bigIntValueOf.call(obj);
236+
return true;
237+
} catch (e) {}
238+
return false;
239+
}
230240

231241
var hasOwn = Object.prototype.hasOwnProperty || function (key) { return key in this; };
232242
function has(obj, key) {

test/bigint.js

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
'use strict';
2+
13
var inspect = require('../');
24
var test = require('tape');
5+
var hasSymbols = require('has-symbols')();
36

47
test('bigint', { skip: typeof BigInt === 'undefined' }, function (t) {
58
t.test('primitives', function (st) {
@@ -27,5 +30,17 @@ test('bigint', { skip: typeof BigInt === 'undefined' }, function (t) {
2730
st.equal(inspect(Function('return 256n')()), '256n');
2831
});
2932

33+
t.test('toStringTag', { skip: !hasSymbols || typeof Symbol.toStringTag !== 'symbol' }, function (st) {
34+
st.plan(1);
35+
36+
var faker = {};
37+
faker[Symbol.toStringTag] = 'BigInt';
38+
st.equal(
39+
inspect(faker),
40+
'{ [Symbol(Symbol.toStringTag)]: \'BigInt\' }',
41+
'object lying about being a BigInt inspects as an object'
42+
);
43+
});
44+
3045
t.end();
3146
});

0 commit comments

Comments
 (0)