Skip to content

Commit

Permalink
[New] add support for BigInts
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 21, 2018
1 parent da43e4d commit bf31461
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .eslintrc
Expand Up @@ -3,13 +3,18 @@

"extends": "@ljharb",

"globals": {
"BigInt": false,
},

"rules": {
"complexity": [1, 10],
"eqeqeq": [2, "allow-null"],
"id-length": [2, { "min": 1, "max": 23 }],
"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,
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
30 changes: 30 additions & 0 deletions test/native.js
Expand Up @@ -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() {
Expand Down
38 changes: 38 additions & 0 deletions test/why.js
Expand Up @@ -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() {
Expand Down
13 changes: 13 additions & 0 deletions why.js
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit bf31461

Please sign in to comment.