From bf3146197599bd6248cf05fd2825d8aa736ba3f4 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 21 Sep 2018 01:05:50 -0700 Subject: [PATCH] [New] add support for BigInts --- .eslintrc | 5 +++++ package.json | 1 + test/native.js | 30 ++++++++++++++++++++++++++++++ test/why.js | 38 ++++++++++++++++++++++++++++++++++++++ why.js | 13 +++++++++++++ 5 files changed, 87 insertions(+) diff --git a/.eslintrc b/.eslintrc index 48f8535..8a666d9 100644 --- a/.eslintrc +++ b/.eslintrc @@ -3,6 +3,10 @@ "extends": "@ljharb", + "globals": { + "BigInt": false, + }, + "rules": { "complexity": [1, 10], "eqeqeq": [2, "allow-null"], @@ -10,6 +14,7 @@ "max-depth": [2, 5], "max-statements": [1, 10], "max-statements-per-line": [2, { "max": 2 }], + "new-cap": [2, { "capIsNewExceptions": ["BigInt"] }], "no-extra-parens": [1], "no-implicit-coercion": [2, { "boolean": false, diff --git a/package.json b/package.json index 0e0085b..617bbe9 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "dependencies": { "has": "^1.0.3", "is-arrow-function": "^2.0.3", + "is-bigint": "^1.0.0", "is-boolean-object": "^1.0.0", "is-callable": "^1.1.3", "is-date-object": "^1.0.1", diff --git a/test/native.js b/test/native.js index 12cba16..8b55283 100644 --- a/test/native.js +++ b/test/native.js @@ -276,6 +276,36 @@ test('symbols', { skip: !hasSymbols() }, function (t) { t.end(); }); +var hasBigInts = typeof BigInt === 'function'; +test('bigints', { skip: !hasBigInts }, function (t) { + var bigInt = BigInt(42); + var objectBigInt = Object(bigInt); + t.ok(isEqual(bigInt, bigInt), '42n is equal to itself'); + t.ok(isEqual(bigInt, objectBigInt), '42n is equal to the object form of itself'); + t.notOk(isEqual(bigInt, BigInt(40)), '42n !== 40n'); + + t.test('arrays containing bigints', function (st) { + st.ok( + isEqual([bigInt], [bigInt]), + 'Arrays each containing 42n are equal' + ); + + st.ok( + isEqual([objectBigInt], [Object(bigInt)]), + 'Arrays each containing different instances of Object(42n) are equal' + ); + + st.ok( + isEqual([bigInt], [objectBigInt]), + 'An array containing 42n is equal to an array containing Object(42n)' + ); + + st.end(); + }); + + t.end(); +}); + var genericIterator = function (obj) { var entries = objectEntries(obj); return function iterator() { diff --git a/test/why.js b/test/why.js index aa765d0..083b6b1 100644 --- a/test/why.js +++ b/test/why.js @@ -427,6 +427,44 @@ test('symbols', { skip: !hasSymbols() }, function (t) { t.end(); }); +var hasBigInts = typeof BigInt === 'function'; +test('bigints', { skip: !hasBigInts }, function (t) { + var bigInt = BigInt(42); + var objectBigInt = Object(bigInt); + t.equal('', isEqualWhy(bigInt, bigInt), '42n is equal to itself'); + t.equal('', isEqualWhy(bigInt, objectBigInt), '42n is equal to the object form of itself'); + + t.equal( + 'first BigInt value !== second BigInt value', + isEqualWhy(bigInt, BigInt(40)), + '42n and 40n are not equal' + ); + + t.test('arrays containing bigints', function (st) { + st.equal( + '', + isEqualWhy([bigInt], [bigInt]), + 'Arrays each containing 42n are equal' + ); + + st.equal( + '', + isEqualWhy([objectBigInt], [Object(bigInt)]), + 'Arrays each containing different instances of Object(42n) are equal' + ); + + st.equal( + '', + isEqualWhy([bigInt], [objectBigInt]), + 'An array containing 42n is equal to an array containing Object(42n)' + ); + + st.end(); + }); + + t.end(); +}); + var genericIterator = function (obj) { var entries = objectEntries(obj); return function iterator() { diff --git a/why.js b/why.js index e98daad..f1674a6 100644 --- a/why.js +++ b/why.js @@ -13,6 +13,7 @@ var isRegex = require('is-regex'); var isString = require('is-string'); var isSymbol = require('is-symbol'); var isCallable = require('is-callable'); +var isBigInt = require('is-bigint'); var isProto = Object.prototype.isPrototypeOf; @@ -22,6 +23,8 @@ var functionsHaveNames = namedFoo.name === 'foo'; var symbolValue = typeof Symbol === 'function' ? Symbol.prototype.valueOf : null; var symbolIterator = require('./getSymbolIterator')(); +var bigIntValue = typeof BigInt === 'function' ? BigInt.prototype.valueOf : null; + var collectionsForEach = require('./getCollectionsForEach')(); var getPrototypeOf = Object.getPrototypeOf; @@ -185,6 +188,16 @@ module.exports = function whyNotEqual(value, other) { return symbolValue.call(value) === symbolValue.call(other) ? '' : 'first Symbol value !== second Symbol value'; } + var valueIsBigInt = isBigInt(value); + var otherIsBigInt = isBigInt(other); + if (valueIsBigInt !== otherIsBigInt) { + if (valueIsBigInt) { return 'first argument is BigInt; second is not'; } + return 'second argument is BigInt; first is not'; + } + if (valueIsBigInt && otherIsBigInt) { + return bigIntValue.call(value) === bigIntValue.call(other) ? '' : 'first BigInt value !== second BigInt value'; + } + var valueIsGen = isGenerator(value); var otherIsGen = isGenerator(other); if (valueIsGen !== otherIsGen) {