Skip to content

Commit 356c66a

Browse files
committed
[New] add support for boxed BigInt primitives
1 parent d31b738 commit 356c66a

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

.nycrc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"instrumentation": false,
55
"sourceMap": false,
66
"reporter": "html",
7-
"lines": 95.45,
8-
"statements": 94.71,
7+
"lines": 94.94,
8+
"statements": 94.25,
99
"functions": 96,
10-
"branches": 92,
10+
"branches": 91.02,
1111
"exclude": [
1212
"coverage",
1313
"example",

index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var setSize = hasSet && setSizeDescriptor && typeof setSizeDescriptor.get === 'f
88
var setForEach = hasSet && Set.prototype.forEach;
99
var booleanValueOf = Boolean.prototype.valueOf;
1010
var objectToString = Object.prototype.toString;
11+
var bigIntValueOf = typeof BigInt === 'function' ? BigInt.prototype.valueOf : null;
1112

1213
var inspectCustom = require('./util.inspect').custom;
1314
var inspectSymbol = (inspectCustom && isSymbol(inspectCustom)) ? inspectCustom : null;
@@ -113,6 +114,9 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
113114
if (isNumber(obj)) {
114115
return markBoxed(inspect(Number(obj)));
115116
}
117+
if (isBigInt(obj)) {
118+
return markBoxed(inspect(bigIntValueOf.call(obj)));
119+
}
116120
if (isBoolean(obj)) {
117121
return markBoxed(booleanValueOf.call(obj));
118122
}
@@ -136,14 +140,15 @@ function quote (s) {
136140
return String(s).replace(/"/g, '"');
137141
}
138142

139-
function isArray (obj) { return toStr(obj) === '[object Array]' }
140-
function isDate (obj) { return toStr(obj) === '[object Date]' }
141-
function isRegExp (obj) { return toStr(obj) === '[object RegExp]' }
142-
function isError (obj) { return toStr(obj) === '[object Error]' }
143-
function isSymbol (obj) { return toStr(obj) === '[object Symbol]' }
144-
function isString (obj) { return toStr(obj) === '[object String]' }
145-
function isNumber (obj) { return toStr(obj) === '[object Number]' }
146-
function isBoolean (obj) { return toStr(obj) === '[object Boolean]' }
143+
function isArray (obj) { return toStr(obj) === '[object Array]'; }
144+
function isDate (obj) { return toStr(obj) === '[object Date]'; }
145+
function isRegExp (obj) { return toStr(obj) === '[object RegExp]'; }
146+
function isError (obj) { return toStr(obj) === '[object Error]'; }
147+
function isSymbol (obj) { return toStr(obj) === '[object Symbol]'; }
148+
function isString (obj) { return toStr(obj) === '[object String]'; }
149+
function isNumber (obj) { return toStr(obj) === '[object Number]'; }
150+
function isBigInt (obj) { return toStr(obj) === '[object BigInt]'; }
151+
function isBoolean (obj) { return toStr(obj) === '[object Boolean]'; }
147152

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

test/bigint.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,29 @@ var inspect = require('../');
22
var test = require('tape');
33

44
test('bigint', { skip: typeof BigInt === 'undefined' }, function (t) {
5-
t.plan(3);
5+
t.test('primitives', function (st) {
6+
st.plan(3);
67

7-
t.equal(inspect(BigInt(-256)), '-256n');
8-
t.equal(inspect(BigInt(0)), '0n');
9-
t.equal(inspect(BigInt(256)), '256n');
8+
st.equal(inspect(BigInt(-256)), '-256n');
9+
st.equal(inspect(BigInt(0)), '0n');
10+
st.equal(inspect(BigInt(256)), '256n');
11+
});
12+
13+
t.test('objects', function (st) {
14+
st.plan(3);
15+
16+
st.equal(inspect(Object(BigInt(-256))), 'Object(-256n)');
17+
st.equal(inspect(Object(BigInt(0))), 'Object(0n)');
18+
st.equal(inspect(Object(BigInt(256))), 'Object(256n)');
19+
});
20+
21+
t.test('syntactic primitives', function (st) {
22+
st.plan(3);
23+
24+
st.equal(inspect(Function('return -256n')()), '-256n');
25+
st.equal(inspect(Function('return 0n')()), '0n');
26+
st.equal(inspect(Function('return 256n')()), '256n');
27+
});
28+
29+
t.end();
1030
});

0 commit comments

Comments
 (0)