Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for upcoming BigInt #19

Merged
merged 3 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.95,
"statements": 95,
"lines": 94.94,
"statements": 94.25,
"functions": 96,
"branches": 92,
"branches": 91.02,
"exclude": [
"coverage",
"example",
Expand Down
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ script:
- 'if [ -n "${POSTTEST-}" ]; then npm run posttest ; fi'
- 'if [ -n "${COVERAGE-}" ]; then npm run coverage ; fi'
- 'if [ -n "${TEST-}" ]; then npm run tests-only ; fi'
- 'if [ -n "${BIGINT-}" ]; then npm run test:bigint ; fi'
sudo: false
env:
- TEST=true
matrix:
fast_finish: true
include:
- node_js: "10.0"
env: BIGINT=true
- node_js: "node"
env: COVERAGE=true
- node_js: "9.10"
Expand Down
24 changes: 16 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 @@ -38,6 +39,9 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
}
return String(obj);
}
if (typeof obj === 'bigint') {
return String(obj) + 'n';
}

var maxDepth = typeof opts.depth === 'undefined' ? 5 : opts.depth;
if (typeof depth === 'undefined') depth = 0;
Expand Down Expand Up @@ -110,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 @@ -133,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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
},
"scripts": {
"test": "npm run tests-only",
"posttest": "npm run test:bigint",
"pretests-only": "node test-core-js",
"tests-only": "tape test/*.js",
"test:bigint": "node --harmony-bigint test/bigint",
"coverage": "nyc npm run tests-only"
},
"testling": {
Expand Down
30 changes: 30 additions & 0 deletions test/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var inspect = require('../');
var test = require('tape');

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

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();
});