From 356c66a410e7aece7162c8319880a5ef647beaa9 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 2 May 2018 14:27:00 -0700 Subject: [PATCH] [New] add support for boxed BigInt primitives --- .nycrc | 6 +++--- index.js | 21 +++++++++++++-------- test/bigint.js | 28 ++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/.nycrc b/.nycrc index 23e1153..e706420 100644 --- a/.nycrc +++ b/.nycrc @@ -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", diff --git a/index.js b/index.js index 7adbe04..62069e3 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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)); } @@ -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) { diff --git a/test/bigint.js b/test/bigint.js index 2e3a144..c9d918b 100644 --- a/test/bigint.js +++ b/test/bigint.js @@ -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(); });