diff --git a/package.json b/package.json index e34361a..1ab22a3 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,11 @@ "extensions": [ "ts" ], + "globals": [ + "BigInt", + "BigInt64Array", + "BigUint64Array" + ], "rules": { "@typescript-eslint/explicit-function-return-type": "off" } diff --git a/source/index.ts b/source/index.ts index a819a5d..945b2e4 100644 --- a/source/index.ts +++ b/source/index.ts @@ -1,5 +1,4 @@ -/// -/// +/// /// // TODO: Use the `URL` global when targeting Node.js 10 @@ -14,6 +13,7 @@ export const enum TypeName { undefined = 'undefined', string = 'string', number = 'number', + bigint = 'bigint', symbol = 'symbol', Function = 'Function', Generator = 'Generator', @@ -39,6 +39,8 @@ export const enum TypeName { Uint32Array = 'Uint32Array', Float32Array = 'Float32Array', Float64Array = 'Float64Array', + BigInt64Array = 'BigInt64Array', + BigUint64Array = 'BigUint64Array', ArrayBuffer = 'ArrayBuffer', SharedArrayBuffer = 'SharedArrayBuffer', DataView = 'DataView', @@ -77,6 +79,8 @@ function is(value: unknown): TypeName { return TypeName.string; case 'number': return TypeName.number; + case 'bigint': + return TypeName.bigint; case 'symbol': return TypeName.symbol; default: @@ -115,6 +119,7 @@ const isObject = (value: unknown): value is object => typeof value === 'object'; is.undefined = isOfType('undefined'); is.string = isOfType('string'); is.number = isOfType('number'); +is.bigint = isOfType('bigint'); // eslint-disable-next-line @typescript-eslint/ban-types is.function_ = isOfType('function'); @@ -175,6 +180,8 @@ is.int32Array = isObjectOfType(TypeName.Int32Array); is.uint32Array = isObjectOfType(TypeName.Uint32Array); is.float32Array = isObjectOfType(TypeName.Float32Array); is.float64Array = isObjectOfType(TypeName.Float64Array); +is.bigint64Array = isObjectOfType(TypeName.BigInt64Array); +is.biguint64Array = isObjectOfType(TypeName.BigUint64Array); is.arrayBuffer = isObjectOfType(TypeName.ArrayBuffer); is.sharedArrayBuffer = isObjectOfType(TypeName.SharedArrayBuffer); @@ -208,12 +215,13 @@ const primitiveTypeOfTypes = new Set([ 'undefined', 'string', 'number', + 'bigint', 'boolean', 'symbol' ]); // TODO: This should be able to be `not object` when the `not` operator is out -export type Primitive = null | undefined | string | number | boolean | symbol; +export type Primitive = null | undefined | string | number | bigint | boolean | symbol; is.primitive = (value: unknown): value is Primitive => is.null_(value) || primitiveTypeOfTypes.has(typeof value); @@ -240,10 +248,12 @@ const typedArrayTypes = new Set([ TypeName.Int32Array, TypeName.Uint32Array, TypeName.Float32Array, - TypeName.Float64Array + TypeName.Float64Array, + TypeName.BigInt64Array, + TypeName.BigUint64Array ]); -export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array; +export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array; is.typedArray = (value: unknown): value is TypedArray => { const objectType = getObjectType(value); diff --git a/test/test.ts b/test/test.ts index 363d647..75aac80 100644 --- a/test/test.ts +++ b/test/test.ts @@ -2,7 +2,6 @@ import fs from 'fs'; import net from 'net'; import Stream from 'stream'; import util from 'util'; -import {URL} from 'url'; import tempy from 'tempy'; import test, {ExecutionContext} from 'ava'; import {JSDOM} from 'jsdom'; @@ -10,6 +9,9 @@ import {Subject, Observable} from 'rxjs'; import ZenObservable from 'zen-observable'; import is, {TypeName} from '../source'; +// eslint-disable-next-line @typescript-eslint/no-require-imports +const URLGlobal = typeof URL === 'undefined' ? require('url').URL : URL; + const isNode10orHigher = Number(process.versions.node.split('.')[0]) >= 10; class PromiseSubclassFixture extends Promise {} @@ -69,6 +71,18 @@ const types = new Map([ ], typename: TypeName.number }], + // TODO: Nodejs 10 only + // ['bigint', { + // is: is.bigint, + // fixtures: [ + // 1n, + // 0n, + // -0n, + // // eslint-disable-next-line new-cap + // BigInt('1234') + // ], + // typename: TypeName.bigint + // }], ['boolean', { is: is.boolean, fixtures: [ @@ -311,6 +325,21 @@ const types = new Map([ ], typename: TypeName.Float64Array }], + // TODO: Nodejs 10 only + // ['bigint64Array', { + // is: is.bigint64Array, + // fixtures: [ + // new BigInt64Array() + // ], + // typename: TypeName.BigInt64Array + // }], + // ['biguint64Array', { + // is: is.biguint64Array, + // fixtures: [ + // new BigUint64Array() + // ], + // typename: TypeName.BigUint64Array + // }], ['arrayBuffer', { is: is.arrayBuffer, fixtures: [ @@ -468,6 +497,11 @@ test('is.number', t => { testType(t, 'number', ['nan', 'integer', 'safeInteger', 'infinite']); }); +// TODO: Nodejs 10 only +// test('is.bigint', t => { +// testType(t, 'bigint'); +// }); + test('is.boolean', t => { testType(t, 'boolean'); }); @@ -596,6 +630,15 @@ test('is.float64Array', t => { testType(t, 'float64Array'); }); +// TODO: Nodejs 10 only +// test('is.bigint64Array', t => { +// testType(t, 'bigint64Array'); +// }); + +// test('is.biguint64Array', t => { +// testType(t, 'biguint64Array'); +// }); + test('is.arrayBuffer', t => { testType(t, 'arrayBuffer'); }); @@ -616,7 +659,7 @@ test('is.directInstanceOf', t => { }); test('is.urlInstance', t => { - const url = new URL('https://example.com'); + const url = new URLGlobal('https://example.com'); t.true(is.urlInstance(url)); t.false(is.urlInstance({})); t.false(is.urlInstance(undefined)); @@ -626,7 +669,7 @@ test('is.urlInstance', t => { test('is.urlString', t => { const url = 'https://example.com'; t.true(is.urlString(url)); - t.false(is.urlString(new URL(url))); + t.false(is.urlString(new URLGlobal(url))); t.false(is.urlString({})); t.false(is.urlString(undefined)); t.false(is.urlString(null)); @@ -638,6 +681,14 @@ test('is.truthy', t => { t.true(is.truthy(new Set())); t.true(is.truthy(Symbol('🦄'))); t.true(is.truthy(true)); + t.true(is.truthy(1)); + + // TODO: Nodejs 10 only + // if (isNode10orHigher) { + // t.true(is.truthy(1n)); + // // eslint-disable-next-line new-cap + // t.true(is.truthy(BigInt(1))); + // } }); test('is.falsy', t => { @@ -647,6 +698,13 @@ test('is.falsy', t => { t.true(is.falsy(null)); t.true(is.falsy(undefined)); t.true(is.falsy(NaN)); + + // TODO: Nodejs 10 only + // if (isNode10orHigher) { + // t.true(is.falsy(0n)); + // // eslint-disable-next-line new-cap + // t.true(is.falsy(BigInt(0))); + // } }); test('is.nan', t => { @@ -670,6 +728,11 @@ test('is.primitive', t => { Symbol('🦄') ]; + // TODO: Nodejs 10 only + // if (isNode10orHigher) { + // primitives.push(6n); + // } + for (const el of primitives) { t.true(is.primitive(el)); } @@ -742,6 +805,14 @@ test('is.typedArray', t => { new Float64Array() ]; + // TODO: Nodejs 10 only + // if (isNode10orHigher) { + // typedArrays.push( + // new BigInt64Array(), + // new BigUint64Array() + // ); + // } + for (const item of typedArrays) { t.true(is.typedArray(item)); } diff --git a/tsconfig.json b/tsconfig.json index c76ee17..2acbd3e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,8 +4,7 @@ "outDir": "dist", "target": "es2017", "lib": [ - "es2017", - "esnext.asynciterable", + "esnext", "dom" ] },