Skip to content

Commit

Permalink
[New] add support for boxed BigInt primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed May 2, 2018
1 parent d31b738 commit 356c66a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
6 changes: 3 additions & 3 deletions .nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"instrumentation": false,
"sourceMap": false,
"reporter": "html",
"lines": 95.45,
"statements": 94.71,
"lines": 94.94,
"statements": 94.25,
"functions": 96,
"branches": 92,
"branches": 91.02,
"exclude": [
"coverage",
"example",
Expand Down
21 changes: 13 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var setSize = hasSet && setSizeDescriptor && typeof setSizeDescriptor.get === 'f
var setForEach = hasSet && Set.prototype.forEach;
var booleanValueOf = Boolean.prototype.valueOf;
var objectToString = Object.prototype.toString;
var bigIntValueOf = typeof BigInt === 'function' ? BigInt.prototype.valueOf : null;

var inspectCustom = require('./util.inspect').custom;
var inspectSymbol = (inspectCustom && isSymbol(inspectCustom)) ? inspectCustom : null;
Expand Down Expand Up @@ -113,6 +114,9 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
if (isNumber(obj)) {
return markBoxed(inspect(Number(obj)));
}
if (isBigInt(obj)) {
return markBoxed(inspect(bigIntValueOf.call(obj)));
}
if (isBoolean(obj)) {
return markBoxed(booleanValueOf.call(obj));
}
Expand All @@ -136,14 +140,15 @@ function quote (s) {
return String(s).replace(/"/g, '"');
}

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]' }
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 isBoolean (obj) { return toStr(obj) === '[object Boolean]' }
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]'; }
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]'; }

var hasOwn = Object.prototype.hasOwnProperty || function (key) { return key in this; };
function has (obj, key) {
Expand Down
28 changes: 24 additions & 4 deletions test/bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,29 @@ var inspect = require('../');
var test = require('tape');

test('bigint', { skip: typeof BigInt === 'undefined' }, function (t) {
t.plan(3);
t.test('primitives', function (st) {
st.plan(3);

t.equal(inspect(BigInt(-256)), '-256n');
t.equal(inspect(BigInt(0)), '0n');
t.equal(inspect(BigInt(256)), '256n');
st.equal(inspect(BigInt(-256)), '-256n');
st.equal(inspect(BigInt(0)), '0n');
st.equal(inspect(BigInt(256)), '256n');
});

t.test('objects', function (st) {
st.plan(3);

st.equal(inspect(Object(BigInt(-256))), 'Object(-256n)');
st.equal(inspect(Object(BigInt(0))), 'Object(0n)');
st.equal(inspect(Object(BigInt(256))), 'Object(256n)');
});

t.test('syntactic primitives', function (st) {
st.plan(3);

st.equal(inspect(Function('return -256n')()), '-256n');
st.equal(inspect(Function('return 0n')()), '0n');
st.equal(inspect(Function('return 256n')()), '256n');
});

t.end();
});

0 comments on commit 356c66a

Please sign in to comment.