save code
This commit is contained in:
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"root": true,
|
||||
|
||||
"extends": "@ljharb",
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"all": true,
|
||||
"check-coverage": false,
|
||||
"reporter": ["text-summary", "text", "html", "json"],
|
||||
"exclude": [
|
||||
"coverage",
|
||||
"test"
|
||||
]
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Jordan Harband
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
'use strict';
|
||||
|
||||
var hasBigInts = require('has-bigints')();
|
||||
|
||||
if (hasBigInts) {
|
||||
var bigIntValueOf = BigInt.prototype.valueOf;
|
||||
/** @type {(value: object) => value is BigInt} */
|
||||
var tryBigInt = function tryBigIntObject(value) {
|
||||
try {
|
||||
bigIntValueOf.call(value);
|
||||
return true;
|
||||
} catch (e) {
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/** @type {import('.')} */
|
||||
module.exports = function isBigInt(value) {
|
||||
if (
|
||||
value === null
|
||||
|| typeof value === 'undefined'
|
||||
|| typeof value === 'boolean'
|
||||
|| typeof value === 'string'
|
||||
|| typeof value === 'number'
|
||||
|| typeof value === 'symbol'
|
||||
|| typeof value === 'function'
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
if (typeof value === 'bigint') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return tryBigInt(value);
|
||||
};
|
||||
} else {
|
||||
/** @type {import('.')} */
|
||||
module.exports = function isBigInt(value) {
|
||||
return false && value;
|
||||
};
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
'use strict';
|
||||
|
||||
var test = require('tape');
|
||||
var inspect = require('object-inspect');
|
||||
var hasBigInts = require('has-bigints')();
|
||||
var hasToStringTag = require('has-tostringtag/shams')();
|
||||
var forEach = require('for-each');
|
||||
var v = require('es-value-fixtures');
|
||||
|
||||
var isBigInt = require('../');
|
||||
|
||||
test('non-BigInt values', function (t) {
|
||||
/** @type {(typeof v.primitives[number] | object)[]} */
|
||||
var nonBigInts = v.nonBigInts.concat(
|
||||
Object(true),
|
||||
Object(false),
|
||||
// @ts-expect-error TS sucks with concat
|
||||
{},
|
||||
[],
|
||||
/a/g,
|
||||
new Date(),
|
||||
function () {},
|
||||
NaN,
|
||||
v.symbols
|
||||
);
|
||||
t.plan(nonBigInts.length);
|
||||
forEach(nonBigInts, function (nonBigInt) {
|
||||
t.equal(false, isBigInt(nonBigInt), inspect(nonBigInt) + ' is not a BigInt');
|
||||
});
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('faked BigInt values', function (t) {
|
||||
t.test('real BigInt valueOf', { skip: !hasBigInts }, function (st) {
|
||||
var fakeBigInt = { valueOf: function () { return BigInt(42); } };
|
||||
st.equal(false, isBigInt(fakeBigInt), 'object with valueOf returning a BigInt is not a BigInt');
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('faked @@toStringTag', { skip: !hasBigInts || !hasToStringTag }, function (st) {
|
||||
/** @type {{ valueOf(): unknown; [Symbol.toStringTag]?: unknown }} */
|
||||
var fakeBigInt = { valueOf: function () { return BigInt(42); } };
|
||||
fakeBigInt[Symbol.toStringTag] = 'BigInt';
|
||||
st.equal(false, isBigInt(fakeBigInt), 'object with fake BigInt @@toStringTag and valueOf returning a BigInt is not a BigInt');
|
||||
|
||||
/** @type {{ valueOf(): unknown; [Symbol.toStringTag]?: unknown }} */
|
||||
var notSoFakeBigInt = { valueOf: function () { return 42; } };
|
||||
notSoFakeBigInt[Symbol.toStringTag] = 'BigInt';
|
||||
st.equal(false, isBigInt(notSoFakeBigInt), 'object with fake BigInt @@toStringTag and valueOf not returning a BigInt is not a BigInt');
|
||||
st.end();
|
||||
});
|
||||
|
||||
var fakeBigIntString = { toString: function () { return '42n'; } };
|
||||
t.equal(false, isBigInt(fakeBigIntString), 'object with toString returning 42n is not a BigInt');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('BigInt support', { skip: !hasBigInts }, function (t) {
|
||||
forEach(v.bigints.concat(Object(BigInt(42))), function (bigInt) {
|
||||
t.equal(true, isBigInt(bigInt), inspect(bigInt) + ' is a BigInt');
|
||||
});
|
||||
|
||||
t.end();
|
||||
});
|
||||
Reference in New Issue
Block a user