diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c1870cf..d50ada6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,12 +10,12 @@ jobs: fail-fast: false matrix: node-version: + - 18 + - 16 - 14 - - 12 - - 10 steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/package.json b/package.json index 8052a87..34f31f6 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,11 @@ "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, - "main": "dist/index.js", + "type": "module", + "exports": "./index.js", + "types": "./index.d.ts", "engines": { - "node": ">=10" + "node": ">=14.16" }, "scripts": { "build": "del dist && tsc", @@ -47,50 +49,34 @@ "types" ], "devDependencies": { - "@sindresorhus/tsconfig": "^0.7.0", - "@types/jsdom": "^16.1.0", - "@types/node": "^14.0.13", - "@types/zen-observable": "^0.8.0", - "@typescript-eslint/eslint-plugin": "^2.20.0", - "@typescript-eslint/parser": "^2.20.0", - "ava": "^3.3.0", - "del-cli": "^2.0.0", - "eslint-config-xo-typescript": "^0.26.0", - "jsdom": "^16.0.1", - "rxjs": "^6.4.0", - "tempy": "^0.4.0", - "ts-node": "^8.3.0", - "typescript": "~3.8.2", - "xo": "^0.26.1", - "zen-observable": "^0.8.8" + "@sindresorhus/tsconfig": "^3.0.1", + "@types/jsdom": "^16.2.14", + "@types/node": "^17.0.42", + "@types/zen-observable": "^0.8.3", + "ava": "^4.3.0", + "del-cli": "^4.0.1", + "jsdom": "^19.0.0", + "rxjs": "^7.5.5", + "tempy": "^3.0.0", + "ts-node": "^10.8.1", + "typescript": "~4.7.3", + "xo": "^0.50.0", + "zen-observable": "^0.8.15" }, - "types": "dist/index.d.ts", "sideEffects": false, "ava": { - "extensions": [ - "ts" - ], - "require": [ - "ts-node/register" + "extensions": { + "ts": "module" + }, + "nodeArguments": [ + "--loader=ts-node/esm" ] }, "xo": { - "extends": "xo-typescript", - "extensions": [ - "ts" - ], - "parserOptions": { - "project": "./tsconfig.xo.json" - }, - "globals": [ - "BigInt", - "BigInt64Array", - "BigUint64Array" - ], "rules": { - "@typescript-eslint/promise-function-async": "off", "@typescript-eslint/no-empty-function": "off", - "@typescript-eslint/explicit-function-return-type": "off" + "@typescript-eslint/explicit-function-return-type": "off", + "@typescript-eslint/triple-slash-reference": "off" } } } diff --git a/readme.md b/readme.md index 05a291a..90ac932 100644 --- a/readme.md +++ b/readme.md @@ -24,7 +24,7 @@ npm install @sindresorhus/is ## Usage ```js -const is = require('@sindresorhus/is'); +import is from '@sindresorhus/is'; is('🦄'); //=> 'string' @@ -39,7 +39,7 @@ is.number(6); [Assertions](#type-assertions) perform the same type checks, but throw an error if the type does not match. ```js -const {assert} = require('@sindresorhus/is'); +import {assert} from '@sindresorhus/is'; assert.string(2); //=> Error: Expected value which is `string`, received value of type `number`. @@ -436,7 +436,7 @@ Returns `true` if `value` is a DOM Element. Returns `true` if `value` is a Node.js [stream](https://nodejs.org/api/stream.html). ```js -const fs = require('fs'); +import fs from 'node:fs'; is.nodeStream(fs.createReadStream('unicorn.png')); //=> true @@ -447,7 +447,7 @@ is.nodeStream(fs.createReadStream('unicorn.png')); Returns `true` if `value` is an `Observable`. ```js -const {Observable} = require('rxjs'); +import {Observable} from 'rxjs'; is.observable(new Observable()); //=> true diff --git a/source/index.ts b/source/index.ts index 39e5883..6457799 100644 --- a/source/index.ts +++ b/source/index.ts @@ -1,8 +1,5 @@ -/// -/// -/// - -import {Class, Falsy, TypedArray, ObservableLike, Primitive} from './types'; +import type {Buffer} from 'node:buffer'; +import type {Class, Falsy, TypedArray, ObservableLike, Primitive} from './types.js'; const typedArrayTypeNames = [ 'Int8Array', @@ -15,7 +12,7 @@ const typedArrayTypeNames = [ 'Float32Array', 'Float64Array', 'BigInt64Array', - 'BigUint64Array' + 'BigUint64Array', ] as const; type TypedArrayTypeName = typeof typedArrayTypeNames[number]; @@ -52,7 +49,7 @@ const objectTypeNames = [ 'URLSearchParams', 'HTMLElement', 'NaN', - ...typedArrayTypeNames + ...typedArrayTypeNames, ] as const; type ObjectTypeName = typeof objectTypeNames[number]; @@ -68,7 +65,7 @@ const primitiveTypeNames = [ 'number', 'bigint', 'boolean', - 'symbol' + 'symbol', ] as const; type PrimitiveTypeName = typeof primitiveTypeNames[number]; @@ -149,6 +146,7 @@ function is(value: unknown): TypeName { } is.undefined = isOfType('undefined'); + is.string = isOfType('string'); const isNumberType = isOfType('number'); @@ -159,9 +157,13 @@ is.bigint = isOfType('bigint'); // eslint-disable-next-line @typescript-eslint/ban-types is.function_ = isOfType('function'); +// eslint-disable-next-line @typescript-eslint/ban-types is.null_ = (value: unknown): value is null => value === null; + is.class_ = (value: unknown): value is Class => is.function_(value) && value.toString().startsWith('class '); + is.boolean = (value: unknown): value is boolean => value === true || value === false; + is.symbol = isOfType('symbol'); is.numericString = (value: unknown): value is string => @@ -176,14 +178,18 @@ is.array = (value: unknown, assertion?: (value: T) => value is T): return true; } - return value.every(assertion); + return value.every(element => assertion(element)); }; +// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call is.buffer = (value: unknown): value is Buffer => (value as any)?.constructor?.isBuffer?.(value) ?? false; + is.blob = (value: unknown): value is Blob => isObjectOfType('Blob')(value); -is.nullOrUndefined = (value: unknown): value is null | undefined => is.null_(value) || is.undefined(value); -is.object = (value: unknown): value is object => !is.null_(value) && (typeof value === 'object' || is.function_(value)); +is.nullOrUndefined = (value: unknown): value is null | undefined => is.null_(value) || is.undefined(value); // eslint-disable-line @typescript-eslint/ban-types + +is.object = (value: unknown): value is object => !is.null_(value) && (typeof value === 'object' || is.function_(value)); // eslint-disable-line @typescript-eslint/ban-types + is.iterable = (value: unknown): value is Iterable => is.function_((value as Iterable)?.[Symbol.iterator]); is.asyncIterable = (value: unknown): value is AsyncIterable => is.function_((value as AsyncIterable)?.[Symbol.asyncIterator]); @@ -195,11 +201,11 @@ is.asyncGenerator = (value: unknown): value is AsyncGenerator => is.asyncIterabl is.nativePromise = (value: unknown): value is Promise => isObjectOfType>('Promise')(value); -const hasPromiseAPI = (value: unknown): value is Promise => - is.function_((value as Promise)?.then) && - is.function_((value as Promise)?.catch); +const hasPromiseApi = (value: unknown): value is Promise => + is.function_((value as Promise)?.then) + && is.function_((value as Promise)?.catch); -is.promise = (value: unknown): value is Promise => is.nativePromise(value) || hasPromiseAPI(value); +is.promise = (value: unknown): value is Promise => is.nativePromise(value) || hasPromiseApi(value); is.generatorFunction = isObjectOfType('GeneratorFunction'); @@ -211,12 +217,18 @@ is.asyncFunction = (value: unknown): value is ((...args: any[]) => is.boundFunction = (value: unknown): value is Function => is.function_(value) && !value.hasOwnProperty('prototype'); is.regExp = isObjectOfType('RegExp'); + is.date = isObjectOfType('Date'); + is.error = isObjectOfType('Error'); + is.map = (value: unknown): value is Map => isObjectOfType>('Map')(value); + is.set = (value: unknown): value is Set => isObjectOfType>('Set')(value); -is.weakMap = (value: unknown): value is WeakMap => isObjectOfType>('WeakMap')(value); -is.weakSet = (value: unknown): value is WeakSet => isObjectOfType>('WeakSet')(value); + +is.weakMap = (value: unknown): value is WeakMap => isObjectOfType>('WeakMap')(value); // eslint-disable-line @typescript-eslint/ban-types + +is.weakSet = (value: unknown): value is WeakSet => isObjectOfType>('WeakSet')(value); // eslint-disable-line @typescript-eslint/ban-types is.int8Array = isObjectOfType('Int8Array'); is.uint8Array = isObjectOfType('Uint8Array'); @@ -231,11 +243,15 @@ is.bigInt64Array = isObjectOfType('BigInt64Array'); is.bigUint64Array = isObjectOfType('BigUint64Array'); is.arrayBuffer = isObjectOfType('ArrayBuffer'); + is.sharedArrayBuffer = isObjectOfType('SharedArrayBuffer'); + is.dataView = isObjectOfType('DataView'); + is.enumCase = (value: unknown, targetEnum: T) => Object.values(targetEnum).includes(value as string); is.directInstanceOf = (instance: unknown, class_: Class): instance is T => Object.getPrototypeOf(instance) === class_.prototype; + is.urlInstance = (value: unknown): value is URL => isObjectOfType('URL')(value); is.urlString = (value: unknown): value is string => { @@ -252,7 +268,8 @@ is.urlString = (value: unknown): value is string => { }; // Example: `is.truthy = (value: unknown): value is (not false | not 0 | not '' | not undefined | not null) => Boolean(value);` -is.truthy = (value: T | Falsy): value is T => Boolean(value); +is.truthy = (value: T | Falsy): value is T => Boolean(value); // eslint-disable-line unicorn/prefer-native-coercion-functions + // Example: `is.falsy = (value: unknown): value is (not true | 0 | '' | undefined | null) => Boolean(value);` is.falsy = (value: T | Falsy): value is Falsy => !value; @@ -261,6 +278,7 @@ is.nan = (value: unknown) => Number.isNaN(value as number); is.primitive = (value: unknown): value is Primitive => is.null_(value) || isPrimitiveTypeName(typeof value); is.integer = (value: unknown): value is number => Number.isInteger(value as number); + is.safeInteger = (value: unknown): value is number => Number.isSafeInteger(value as number); is.plainObject = (value: unknown): value is Record => { @@ -269,6 +287,7 @@ is.plainObject = (value: unknown): value is Record { throw new TypeError(`Invalid range: ${JSON.stringify(range)}`); }; +// eslint-disable-next-line @typescript-eslint/naming-convention const NODE_TYPE_ELEMENT = 1; + +// eslint-disable-next-line @typescript-eslint/naming-convention const DOM_PROPERTIES_TO_CHECK: Array<(keyof HTMLElement)> = [ 'innerHTML', 'ownerDocument', 'style', 'attributes', - 'nodeValue' + 'nodeValue', ]; -is.domElement = (value: unknown): value is HTMLElement => { - return is.object(value) && - (value as HTMLElement).nodeType === NODE_TYPE_ELEMENT && - is.string((value as HTMLElement).nodeName) && - !is.plainObject(value) && - DOM_PROPERTIES_TO_CHECK.every(property => property in value); -}; +is.domElement = (value: unknown): value is HTMLElement => is.object(value) + && (value as HTMLElement).nodeType === NODE_TYPE_ELEMENT + && is.string((value as HTMLElement).nodeName) + && !is.plainObject(value) + && DOM_PROPERTIES_TO_CHECK.every(property => property in value); is.observable = (value: unknown): value is ObservableLike => { if (!value) { return false; } - // eslint-disable-next-line no-use-extend-native/no-use-extend-native + // eslint-disable-next-line no-use-extend-native/no-use-extend-native, @typescript-eslint/no-unsafe-call if (value === (value as any)[Symbol.observable]?.()) { return true; } + // eslint-disable-next-line @typescript-eslint/no-unsafe-call if (value === (value as any)['@@observable']?.()) { return true; } @@ -336,13 +357,14 @@ export interface NodeStream extends NodeJS.EventEmitter { is.nodeStream = (value: unknown): value is NodeStream => is.object(value) && is.function_((value as NodeStream).pipe) && !is.observable(value); -is.infinite = (value: unknown): value is number => value === Infinity || value === -Infinity; +is.infinite = (value: unknown): value is number => value === Number.POSITIVE_INFINITY || value === Number.NEGATIVE_INFINITY; const isAbsoluteMod2 = (remainder: number) => (value: number): value is number => is.integer(value) && Math.abs(value % 2) === remainder; is.evenInteger = isAbsoluteMod2(0); is.oddInteger = isAbsoluteMod2(1); is.emptyArray = (value: unknown): value is never[] => is.array(value) && value.length === 0; + is.nonEmptyArray = (value: unknown): value is unknown[] => is.array(value) && value.length > 0; is.emptyString = (value: unknown): value is '' => is.string(value) && value.length === 0; @@ -356,20 +378,27 @@ is.nonEmptyString = (value: unknown): value is string => is.string(value) && val // TODO: Use `not ''` when the `not` operator is available. is.nonEmptyStringAndNotWhitespace = (value: unknown): value is string => is.string(value) && !is.emptyStringOrWhitespace(value); +// eslint-disable-next-line unicorn/no-array-callback-reference is.emptyObject = (value: unknown): value is Record => is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length === 0; // TODO: Use `not` operator here to remove `Map` and `Set` from type guard: // - https://github.com/Microsoft/TypeScript/pull/29317 +// eslint-disable-next-line unicorn/no-array-callback-reference is.nonEmptyObject = (value: unknown): value is Record => is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length > 0; is.emptySet = (value: unknown): value is Set => is.set(value) && value.size === 0; + is.nonEmptySet = (value: unknown): value is Set => is.set(value) && value.size > 0; +// eslint-disable-next-line unicorn/no-array-callback-reference is.emptyMap = (value: unknown): value is Map => is.map(value) && value.size === 0; + +// eslint-disable-next-line unicorn/no-array-callback-reference is.nonEmptyMap = (value: unknown): value is Map => is.map(value) && value.size > 0; // `PropertyKey` is any value that can be used as an object key (string, number, or symbol) is.propertyKey = (value: unknown): value is PropertyKey => is.any([is.string, is.number, is.symbol], value); + is.formData = (value: unknown): value is FormData => isObjectOfType('FormData')(value); is.urlSearchParams = (value: unknown): value is URLSearchParams => isObjectOfType('URLSearchParams')(value); @@ -393,7 +422,7 @@ const predicateOnArray = (method: ArrayMethod, predicate: Predicate, values: unk is.any = (predicate: Predicate | Predicate[], ...values: unknown[]): boolean => { const predicates = is.array(predicate) ? predicate : [predicate]; return predicates.some(singlePredicate => - predicateOnArray(Array.prototype.some, singlePredicate, values) + predicateOnArray(Array.prototype.some, singlePredicate, values), ); }; @@ -402,13 +431,13 @@ is.all = (predicate: Predicate, ...values: unknown[]): boolean => predicateOnArr const assertType = (condition: boolean, description: string, value: unknown, options: {multipleValues?: boolean} = {}): asserts condition => { if (!condition) { const {multipleValues} = options; - const valuesMessage = multipleValues ? - `received values of types ${[ + const valuesMessage = multipleValues + ? `received values of types ${[ ...new Set( - (value as any[]).map(singleValue => `\`${is(singleValue)}\``) - ) - ].join(', ')}` : - `received value of type \`${is(value)}\``; + (value as any[]).map(singleValue => `\`${is(singleValue)}\``), + ), + ].join(', ')}` + : `received value of type \`${is(value)}\``; throw new TypeError(`Expected value which is \`${description}\`, ${valuesMessage}.`); } @@ -427,7 +456,7 @@ export const enum AssertionTypeDescription { nan = 'NaN', primitive = 'primitive', integer = 'integer', - safeInteger = 'integer', + safeInteger = 'integer', // eslint-disable-line @typescript-eslint/no-duplicate-enum-values plainObject = 'plain object', arrayLike = 'array-like', typedArray = 'TypedArray', @@ -466,7 +495,7 @@ interface Assert { bigint: (value: unknown) => asserts value is bigint; // eslint-disable-next-line @typescript-eslint/ban-types function_: (value: unknown) => asserts value is Function; - null_: (value: unknown) => asserts value is null; + null_: (value: unknown) => asserts value is null; // eslint-disable-line @typescript-eslint/ban-types class_: (value: unknown) => asserts value is Class; boolean: (value: unknown) => asserts value is boolean; symbol: (value: unknown) => asserts value is symbol; @@ -474,7 +503,7 @@ interface Assert { array: (value: unknown, assertion?: (element: unknown) => asserts element is T) => asserts value is T[]; buffer: (value: unknown) => asserts value is Buffer; blob: (value: unknown) => asserts value is Blob; - nullOrUndefined: (value: unknown) => asserts value is null | undefined; + nullOrUndefined: (value: unknown) => asserts value is null | undefined; // eslint-disable-line @typescript-eslint/ban-types object: (value: unknown) => asserts value is Record; iterable: (value: unknown) => asserts value is Iterable; asyncIterable: (value: unknown) => asserts value is AsyncIterable; @@ -493,8 +522,8 @@ interface Assert { error: (value: unknown) => asserts value is Error; map: (value: unknown) => asserts value is Map; set: (value: unknown) => asserts value is Set; - weakMap: (value: unknown) => asserts value is WeakMap; - weakSet: (value: unknown) => asserts value is WeakSet; + weakMap: (value: unknown) => asserts value is WeakMap; // eslint-disable-line @typescript-eslint/ban-types + weakSet: (value: unknown) => asserts value is WeakSet; // eslint-disable-line @typescript-eslint/ban-types int8Array: (value: unknown) => asserts value is Int8Array; uint8Array: (value: unknown) => asserts value is Uint8Array; uint8ClampedArray: (value: unknown) => asserts value is Uint8ClampedArray; @@ -554,6 +583,7 @@ interface Assert { all: (predicate: Predicate, ...values: unknown[]) => void | never; } +/* eslint-disable @typescript-eslint/no-confusing-void-expression */ export const assert: Assert = { // Unknowns. undefined: (value: unknown): asserts value is undefined => assertType(is.undefined(value), 'undefined', value), @@ -562,23 +592,24 @@ export const assert: Assert = { bigint: (value: unknown): asserts value is bigint => assertType(is.bigint(value), 'bigint', value), // eslint-disable-next-line @typescript-eslint/ban-types function_: (value: unknown): asserts value is Function => assertType(is.function_(value), 'Function', value), - null_: (value: unknown): asserts value is null => assertType(is.null_(value), 'null', value), + null_: (value: unknown): asserts value is null => assertType(is.null_(value), 'null', value), // eslint-disable-line @typescript-eslint/ban-types class_: (value: unknown): asserts value is Class => assertType(is.class_(value), AssertionTypeDescription.class_, value), boolean: (value: unknown): asserts value is boolean => assertType(is.boolean(value), 'boolean', value), symbol: (value: unknown): asserts value is symbol => assertType(is.symbol(value), 'symbol', value), numericString: (value: unknown): asserts value is string => assertType(is.numericString(value), AssertionTypeDescription.numericString, value), - array: (value: unknown, assertion?: (element: unknown) => asserts element is T): asserts value is T[] => { + array: (value: unknown, assertion?: (element: unknown) => asserts element is T): asserts value is T[] => { // eslint-disable-line object-shorthand const assert: (condition: boolean, description: string, value: unknown) => asserts condition = assertType; assert(is.array(value), 'Array', value); if (assertion) { + // eslint-disable-next-line unicorn/no-array-for-each, unicorn/no-array-callback-reference value.forEach(assertion); } }, buffer: (value: unknown): asserts value is Buffer => assertType(is.buffer(value), 'Buffer', value), blob: (value: unknown): asserts value is Blob => assertType(is.blob(value), 'Blob', value), - nullOrUndefined: (value: unknown): asserts value is null | undefined => assertType(is.nullOrUndefined(value), AssertionTypeDescription.nullOrUndefined, value), - object: (value: unknown): asserts value is object => assertType(is.object(value), 'Object', value), + nullOrUndefined: (value: unknown): asserts value is null | undefined => assertType(is.nullOrUndefined(value), AssertionTypeDescription.nullOrUndefined, value), // eslint-disable-line @typescript-eslint/ban-types + object: (value: unknown): asserts value is object => assertType(is.object(value), 'Object', value), // eslint-disable-line @typescript-eslint/ban-types iterable: (value: unknown): asserts value is Iterable => assertType(is.iterable(value), AssertionTypeDescription.iterable, value), asyncIterable: (value: unknown): asserts value is AsyncIterable => assertType(is.asyncIterable(value), AssertionTypeDescription.asyncIterable, value), generator: (value: unknown): asserts value is Generator => assertType(is.generator(value), 'Generator', value), @@ -594,10 +625,10 @@ export const assert: Assert = { regExp: (value: unknown): asserts value is RegExp => assertType(is.regExp(value), 'RegExp', value), date: (value: unknown): asserts value is Date => assertType(is.date(value), 'Date', value), error: (value: unknown): asserts value is Error => assertType(is.error(value), 'Error', value), - map: (value: unknown): asserts value is Map => assertType(is.map(value), 'Map', value), + map: (value: unknown): asserts value is Map => assertType(is.map(value), 'Map', value), // eslint-disable-line unicorn/no-array-callback-reference set: (value: unknown): asserts value is Set => assertType(is.set(value), 'Set', value), - weakMap: (value: unknown): asserts value is WeakMap => assertType(is.weakMap(value), 'WeakMap', value), - weakSet: (value: unknown): asserts value is WeakSet => assertType(is.weakSet(value), 'WeakSet', value), + weakMap: (value: unknown): asserts value is WeakMap => assertType(is.weakMap(value), 'WeakMap', value), // eslint-disable-line @typescript-eslint/ban-types + weakSet: (value: unknown): asserts value is WeakSet => assertType(is.weakSet(value), 'WeakSet', value), // eslint-disable-line @typescript-eslint/ban-types int8Array: (value: unknown): asserts value is Int8Array => assertType(is.int8Array(value), 'Int8Array', value), uint8Array: (value: unknown): asserts value is Uint8Array => assertType(is.uint8Array(value), 'Uint8Array', value), uint8ClampedArray: (value: unknown): asserts value is Uint8ClampedArray => assertType(is.uint8ClampedArray(value), 'Uint8ClampedArray', value), @@ -653,42 +684,36 @@ export const assert: Assert = { inRange: (value: number, range: number | number[]): asserts value is number => assertType(is.inRange(value, range), AssertionTypeDescription.inRange, value), // Variadic functions. - any: (predicate: Predicate | Predicate[], ...values: unknown[]): void | never => { - return assertType(is.any(predicate, ...values), AssertionTypeDescription.any, values, {multipleValues: true}); - }, - all: (predicate: Predicate, ...values: unknown[]): void | never => assertType(is.all(predicate, ...values), AssertionTypeDescription.all, values, {multipleValues: true}) + any: (predicate: Predicate | Predicate[], ...values: unknown[]): void | never => assertType(is.any(predicate, ...values), AssertionTypeDescription.any, values, {multipleValues: true}), + all: (predicate: Predicate, ...values: unknown[]): void | never => assertType(is.all(predicate, ...values), AssertionTypeDescription.all, values, {multipleValues: true}), }; +/* eslint-enable @typescript-eslint/no-confusing-void-expression */ // Some few keywords are reserved, but we'll populate them for Node.js users // See https://github.com/Microsoft/TypeScript/issues/2536 Object.defineProperties(is, { class: { - value: is.class_ + value: is.class_, }, function: { - value: is.function_ + value: is.function_, }, null: { - value: is.null_ - } + value: is.null_, + }, }); Object.defineProperties(assert, { class: { - value: assert.class_ + value: assert.class_, }, function: { - value: assert.function_ + value: assert.function_, }, null: { - value: assert.null_ - } + value: assert.null_, + }, }); export default is; -export {Class, TypedArray, ObservableLike, Primitive} from './types'; - -// For CommonJS default export support -module.exports = is; -module.exports.default = is; -module.exports.assert = assert; +export type {Class, TypedArray, ObservableLike, Primitive} from './types.js'; diff --git a/source/types.ts b/source/types.ts index f6da50c..3940da2 100644 --- a/source/types.ts +++ b/source/types.ts @@ -4,7 +4,7 @@ Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive). */ export type Primitive = - | null + | null // eslint-disable-line @typescript-eslint/ban-types | undefined | string | number @@ -12,7 +12,6 @@ export type Primitive = | symbol | bigint; -// TODO: Remove the `= unknown` sometime in the future when most users are on TS 3.5 as it's now the default /** Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes). */ @@ -48,4 +47,5 @@ export interface ObservableLike { [Symbol.observable](): ObservableLike; } +// eslint-disable-next-line @typescript-eslint/ban-types export type Falsy = false | 0 | 0n | '' | null | undefined; diff --git a/test/test.ts b/test/test.ts index f4fe213..c74f76a 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1,13 +1,14 @@ -import fs = require('fs'); -import net = require('net'); -import Stream = require('stream'); -import {inspect} from 'util'; +import {Buffer} from 'node:buffer'; +import fs from 'node:fs'; +import net from 'node:net'; +import Stream from 'node:stream'; +import {inspect} from 'node:util'; import test, {ExecutionContext} from 'ava'; import {JSDOM} from 'jsdom'; import {Subject, Observable} from 'rxjs'; -import tempy = require('tempy'); -import ZenObservable = require('zen-observable'); -import is, {assert, AssertionTypeDescription, Primitive, TypedArray, TypeName} from '../source'; +import {temporaryFile} from 'tempy'; +import ZenObservable from 'zen-observable'; +import is, {assert, AssertionTypeDescription, Primitive, TypedArray, TypeName} from '../source/index.js'; class PromiseSubclassFixture extends Promise {} class ErrorSubclassFixture extends Error {} @@ -29,7 +30,7 @@ const invertAssertThrow = (description: string, fn: () => void | never, value: u try { fn(); - } catch (error) { + } catch (error: unknown) { if (error instanceof TypeError && error.message.includes(expectedAssertErrorMessage)) { return; } @@ -45,17 +46,17 @@ const types = new Map([ is: is.undefined, assert: assert.undefined, fixtures: [ - undefined + undefined, ], - typename: 'undefined' + typename: 'undefined', }], ['null', { is: is.null_, assert: assert.null_, fixtures: [ - null + null, ], - typename: 'null' + typename: 'null', }], ['string', { is: is.string, @@ -63,19 +64,19 @@ const types = new Map([ fixtures: [ '🦄', 'hello world', - '' + '', ], - typename: 'string' + typename: 'string', }], ['emptyString', { is: is.emptyString, assert: assert.emptyString, fixtures: [ '', - String() + String(), ], typename: 'string', - typeDescription: AssertionTypeDescription.emptyString + typeDescription: AssertionTypeDescription.emptyString, }], ['number', { is: is.number, @@ -85,10 +86,10 @@ const types = new Map([ 1.4, 0, -0, - Infinity, - -Infinity + Number.POSITIVE_INFINITY, + Number.NEGATIVE_INFINITY, ], - typename: 'number' + typename: 'number', }], ['bigint', { is: is.bigint, @@ -98,25 +99,25 @@ const types = new Map([ // 1n, // 0n, // -0n, - BigInt('1234') + BigInt('1234'), ], - typename: 'bigint' + typename: 'bigint', }], ['boolean', { is: is.boolean, assert: assert.boolean, fixtures: [ - true, false + true, false, ], - typename: 'boolean' + typename: 'boolean', }], ['symbol', { is: is.symbol, assert: assert.symbol, fixtures: [ - Symbol('🦄') + Symbol('🦄'), ], - typename: 'symbol' + typename: 'symbol', }], ['numericString', { is: is.numericString, @@ -125,29 +126,29 @@ const types = new Map([ '5', '-3.2', 'Infinity', - '0x56' + '0x56', ], typename: 'string', - typeDescription: AssertionTypeDescription.numericString + typeDescription: AssertionTypeDescription.numericString, }], ['array', { is: is.array, assert: assert.array, fixtures: [ [1, 2], - new Array(2) + Array.from({length: 2}), ], - typename: 'Array' + typename: 'Array', }], ['emptyArray', { is: is.emptyArray, assert: assert.emptyArray, fixtures: [ [], - new Array() // eslint-disable-line @typescript-eslint/no-array-constructor + new Array(), // eslint-disable-line @typescript-eslint/no-array-constructor ], typename: 'Array', - typeDescription: AssertionTypeDescription.emptyArray + typeDescription: AssertionTypeDescription.emptyArray, }], ['function', { is: is.function_, @@ -158,79 +159,79 @@ const types = new Map([ () => {}, async function () {}, function * (): unknown {}, - async function * (): unknown {} + async function * (): unknown {}, ], - typename: 'Function' + typename: 'Function', }], ['buffer', { is: is.buffer, assert: assert.buffer, fixtures: [ - Buffer.from('🦄') + Buffer.from('🦄'), ], - typename: 'Buffer' + typename: 'Buffer', }], ['blob', { is: is.blob, assert: assert.blob, fixtures: [ - new window.Blob() + new window.Blob(), ], - typename: 'Blob' + typename: 'Blob', }], ['object', { is: is.object, assert: assert.object, fixtures: [ {x: 1}, - Object.create({x: 1}) + Object.create({x: 1}), ], - typename: 'Object' + typename: 'Object', }], ['regExp', { is: is.regExp, assert: assert.regExp, fixtures: [ /\w/, - new RegExp('\\w') // eslint-disable-line prefer-regex-literals + new RegExp('\\w'), // eslint-disable-line prefer-regex-literals ], - typename: 'RegExp' + typename: 'RegExp', }], ['date', { is: is.date, assert: assert.date, fixtures: [ - new Date() + new Date(), ], - typename: 'Date' + typename: 'Date', }], ['error', { is: is.error, assert: assert.error, fixtures: [ new Error('🦄'), - new ErrorSubclassFixture() + new ErrorSubclassFixture(), ], - typename: 'Error' + typename: 'Error', }], ['nativePromise', { is: is.nativePromise, assert: assert.nativePromise, fixtures: [ Promise.resolve(), - PromiseSubclassFixture.resolve() + PromiseSubclassFixture.resolve(), ], typename: 'Promise', - typeDescription: AssertionTypeDescription.nativePromise + typeDescription: AssertionTypeDescription.nativePromise, }], ['promise', { is: is.promise, assert: assert.promise, fixtures: [ - {then() {}, catch() {}} + {then() {}, catch() {}}, // eslint-disable-line unicorn/no-thenable ], typename: 'Object', - typeDescription: 'Promise' + typeDescription: 'Promise', }], ['generator', { is: is.generator, @@ -238,9 +239,9 @@ const types = new Map([ fixtures: [ (function * () { yield 4; - })() + })(), ], - typename: 'Generator' + typename: 'Generator', }], ['asyncGenerator', { is: is.asyncGenerator, @@ -248,9 +249,9 @@ const types = new Map([ fixtures: [ (async function * () { yield 4; - })() + })(), ], - typename: 'AsyncGenerator' + typename: 'AsyncGenerator', }], ['generatorFunction', { is: is.generatorFunction, @@ -258,10 +259,10 @@ const types = new Map([ fixtures: [ function * () { yield 4; - } + }, ], typename: 'Function', - typeDescription: 'GeneratorFunction' + typeDescription: 'GeneratorFunction', }], ['asyncGeneratorFunction', { is: is.asyncGeneratorFunction, @@ -269,202 +270,202 @@ const types = new Map([ fixtures: [ async function * () { yield 4; - } + }, ], typename: 'Function', - typeDescription: 'AsyncGeneratorFunction' + typeDescription: 'AsyncGeneratorFunction', }], ['asyncFunction', { is: is.asyncFunction, assert: assert.asyncFunction, fixtures: [ async function () {}, - async () => {} + async () => {}, ], typename: 'Function', - typeDescription: 'AsyncFunction' + typeDescription: 'AsyncFunction', }], ['boundFunction', { is: is.boundFunction, assert: assert.boundFunction, fixtures: [ () => {}, - function () {}.bind(null) // eslint-disable-line no-extra-bind + function () {}.bind(null), // eslint-disable-line no-extra-bind ], - typename: 'Function' + typename: 'Function', }], ['map', { is: is.map, assert: assert.map, fixtures: [ - new Map([['one', '1']]) + new Map([['one', '1']]), ], - typename: 'Map' + typename: 'Map', }], ['emptyMap', { is: is.emptyMap, assert: assert.emptyMap, fixtures: [ - new Map() + new Map(), ], typename: 'Map', - typeDescription: AssertionTypeDescription.emptyMap + typeDescription: AssertionTypeDescription.emptyMap, }], ['set', { is: is.set, assert: assert.set, fixtures: [ - new Set(['one']) + new Set(['one']), ], - typename: 'Set' + typename: 'Set', }], ['emptySet', { is: is.emptySet, assert: assert.emptySet, fixtures: [ - new Set() + new Set(), ], typename: 'Set', - typeDescription: AssertionTypeDescription.emptySet + typeDescription: AssertionTypeDescription.emptySet, }], ['weakSet', { is: is.weakSet, assert: assert.weakSet, fixtures: [ - new WeakSet() + new WeakSet(), ], - typename: 'WeakSet' + typename: 'WeakSet', }], ['weakMap', { is: is.weakMap, assert: assert.weakMap, fixtures: [ - new WeakMap() + new WeakMap(), ], - typename: 'WeakMap' + typename: 'WeakMap', }], ['int8Array', { is: is.int8Array, assert: assert.int8Array, fixtures: [ - new Int8Array() + new Int8Array(), ], - typename: 'Int8Array' + typename: 'Int8Array', }], ['uint8Array', { is: is.uint8Array, assert: assert.uint8Array, fixtures: [ - new Uint8Array() + new Uint8Array(), ], - typename: 'Uint8Array' + typename: 'Uint8Array', }], ['uint8ClampedArray', { is: is.uint8ClampedArray, assert: assert.uint8ClampedArray, fixtures: [ - new Uint8ClampedArray() + new Uint8ClampedArray(), ], - typename: 'Uint8ClampedArray' + typename: 'Uint8ClampedArray', }], ['int16Array', { is: is.int16Array, assert: assert.int16Array, fixtures: [ - new Int16Array() + new Int16Array(), ], - typename: 'Int16Array' + typename: 'Int16Array', }], ['uint16Array', { is: is.uint16Array, assert: assert.uint16Array, fixtures: [ - new Uint16Array() + new Uint16Array(), ], - typename: 'Uint16Array' + typename: 'Uint16Array', }], ['int32Array', { is: is.int32Array, assert: assert.int32Array, fixtures: [ - new Int32Array() + new Int32Array(), ], - typename: 'Int32Array' + typename: 'Int32Array', }], ['uint32Array', { is: is.uint32Array, assert: assert.uint32Array, fixtures: [ - new Uint32Array() + new Uint32Array(), ], - typename: 'Uint32Array' + typename: 'Uint32Array', }], ['float32Array', { is: is.float32Array, assert: assert.float32Array, fixtures: [ - new Float32Array() + new Float32Array(), ], - typename: 'Float32Array' + typename: 'Float32Array', }], ['float64Array', { is: is.float64Array, assert: assert.float64Array, fixtures: [ - new Float64Array() + new Float64Array(), ], - typename: 'Float64Array' + typename: 'Float64Array', }], ['bigInt64Array', { is: is.bigInt64Array, assert: assert.bigInt64Array, fixtures: [ - new BigInt64Array() + new BigInt64Array(), ], - typename: 'BigInt64Array' + typename: 'BigInt64Array', }], ['bigUint64Array', { is: is.bigUint64Array, assert: assert.bigUint64Array, fixtures: [ - new BigUint64Array() + new BigUint64Array(), ], - typename: 'BigUint64Array' + typename: 'BigUint64Array', }], ['arrayBuffer', { is: is.arrayBuffer, assert: assert.arrayBuffer, fixtures: [ - new ArrayBuffer(10) + new ArrayBuffer(10), ], - typename: 'ArrayBuffer' + typename: 'ArrayBuffer', }], ['dataView', { is: is.dataView, assert: assert.dataView, fixtures: [ - new DataView(new ArrayBuffer(10)) + new DataView(new ArrayBuffer(10)), ], - typename: 'DataView' + typename: 'DataView', }], ['nan', { is: is.nan, assert: assert.nan, fixtures: [ - NaN, - Number.NaN + NaN, // eslint-disable-line unicorn/prefer-number-properties + Number.NaN, ], typename: 'NaN', - typeDescription: AssertionTypeDescription.nan + typeDescription: AssertionTypeDescription.nan, }], ['nullOrUndefined', { is: is.nullOrUndefined, assert: assert.nullOrUndefined, fixtures: [ null, - undefined + undefined, ], - typeDescription: AssertionTypeDescription.nullOrUndefined + typeDescription: AssertionTypeDescription.nullOrUndefined, }], ['plainObject', { is: is.plainObject, @@ -472,29 +473,29 @@ const types = new Map([ fixtures: [ {x: 1}, Object.create(null), - new Object() // eslint-disable-line no-new-object + new Object(), // eslint-disable-line no-new-object ], typename: 'Object', - typeDescription: AssertionTypeDescription.plainObject + typeDescription: AssertionTypeDescription.plainObject, }], ['integer', { is: is.integer, assert: assert.integer, fixtures: [ - 6 + 6, ], typename: 'number', - typeDescription: AssertionTypeDescription.integer + typeDescription: AssertionTypeDescription.integer, }], ['safeInteger', { is: is.safeInteger, assert: assert.safeInteger, fixtures: [ (2 ** 53) - 1, - -(2 ** 53) + 1 + -(2 ** 53) + 1, ], typename: 'number', - typeDescription: AssertionTypeDescription.safeInteger + typeDescription: AssertionTypeDescription.safeInteger, }], ['domElement', { is: is.domElement, @@ -505,21 +506,26 @@ const types = new Map([ 'span', 'img', 'canvas', - 'script' - ].map(createDomElement), - typeDescription: AssertionTypeDescription.domElement + 'script', + ] + .map(fixture => createDomElement(fixture)), + typeDescription: AssertionTypeDescription.domElement, }], ['non-domElements', { is: value => !is.domElement(value), - assert: (value: unknown) => invertAssertThrow(AssertionTypeDescription.domElement, () => assert.domElement(value), value), + assert(value: unknown) { + invertAssertThrow(AssertionTypeDescription.domElement, () => { + assert.domElement(value); + }, value); + }, fixtures: [ document.createTextNode('data'), document.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"'), document.createComment('This is a comment'), document, document.implementation.createDocumentType('svg:svg', '-//W3C//DTD SVG 1.1//EN', 'https://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'), - document.createDocumentFragment() - ] + document.createDocumentFragment(), + ], }], ['observable', { is: is.observable, @@ -527,37 +533,37 @@ const types = new Map([ fixtures: [ new Observable(), new Subject(), - new ZenObservable(() => {}) + new ZenObservable(() => {}), ], - typename: 'Observable' + typename: 'Observable', }], ['nodeStream', { is: is.nodeStream, assert: assert.nodeStream, fixtures: [ fs.createReadStream('readme.md'), - fs.createWriteStream(tempy.file()), + fs.createWriteStream(temporaryFile()), new net.Socket(), new Stream.Duplex(), new Stream.PassThrough(), new Stream.Readable(), new Stream.Transform(), new Stream.Stream(), - new Stream.Writable() + new Stream.Writable(), ], typename: 'Object', - typeDescription: AssertionTypeDescription.nodeStream + typeDescription: AssertionTypeDescription.nodeStream, }], ['infinite', { is: is.infinite, assert: assert.infinite, fixtures: [ - Infinity, - -Infinity + Number.POSITIVE_INFINITY, + Number.NEGATIVE_INFINITY, ], typename: 'number', - typeDescription: AssertionTypeDescription.infinite - }] + typeDescription: AssertionTypeDescription.infinite, + }], ]); // This ensures a certain method matches only the types it's supposed to and none of the other methods' types @@ -594,7 +600,7 @@ const testType = (t: ExecutionContext, type: string, exclude?: string[]) => { t.throws(() => { testAssert(fixture); }, { - message: `Expected value which is \`${valueType!}\`, received value of type \`${is(fixture)}\`.` + message: `Expected value which is \`${valueType as string}\`, received value of type \`${is(fixture)}\`.`, }); } @@ -704,9 +710,11 @@ test('is.object', t => { return; } - for (const el of testData.fixtures) { - t.true(is.object(el)); - t.notThrows(() => assert.object(el)); + for (const element of testData.fixtures) { + t.true(is.object(element)); + t.notThrows(() => { + assert.object(element); + }); } }); @@ -735,11 +743,9 @@ test('is.asyncFunction', t => { const fixture = async () => {}; if (is.asyncFunction(fixture)) { - // eslint-disable-next-line promise/prefer-await-to-then t.true(is.function_(fixture().then)); t.notThrows(() => { - // eslint-disable-next-line promise/prefer-await-to-then assert.function_(fixture().then); }); } @@ -862,7 +868,7 @@ test('is.enumCase', t => { }); test('is.directInstanceOf', t => { - const error = new Error(); + const error = new Error('fixture'); const errorSubclass = new ErrorSubclassFixture(); t.true(is.directInstanceOf(error, Error)); @@ -944,24 +950,31 @@ test('is.truthy', t => { t.notThrows(() => { assert.truthy('unicorn'); }); + t.notThrows(() => { assert.truthy('🦄'); }); + t.notThrows(() => { assert.truthy(new Set()); }); + t.notThrows(() => { assert.truthy(Symbol('🦄')); }); + t.notThrows(() => { assert.truthy(true); }); + t.notThrows(() => { assert.truthy(1); }); - // TODO: Disabled until TS supports it for an ESnnnn target. - // t.notThrows(() => assert.truthy(1n)); + t.notThrows(() => { + assert.truthy(1n); + }); + t.notThrows(() => { assert.truthy(BigInt(1)); }); @@ -973,31 +986,38 @@ test('is.falsy', t => { t.true(is.falsy('')); t.true(is.falsy(null)); t.true(is.falsy(undefined)); - t.true(is.falsy(NaN)); - // TODO: Disabled until TS supports it for an ESnnnn target. - // t.true(is.falsy(0n)); + t.true(is.falsy(Number.NaN)); + t.true(is.falsy(0n)); t.true(is.falsy(BigInt(0))); t.notThrows(() => { assert.falsy(false); }); + t.notThrows(() => { assert.falsy(0); }); + t.notThrows(() => { assert.falsy(''); }); + t.notThrows(() => { assert.falsy(null); }); + t.notThrows(() => { assert.falsy(undefined); }); + t.notThrows(() => { - assert.falsy(NaN); + assert.falsy(Number.NaN); }); - // TODO: Disabled until TS supports it for an ESnnnn target. - // t.notThrows(() => assert.falsy(0n)); + + t.notThrows(() => { + assert.falsy(0n); + }); + t.notThrows(() => { assert.falsy(BigInt(0)); }); @@ -1017,11 +1037,11 @@ test('is.primitive', t => { null, '🦄', 6, - Infinity, - -Infinity, + Number.POSITIVE_INFINITY, + Number.NEGATIVE_INFINITY, true, false, - Symbol('🦄') + Symbol('🦄'), // Disabled until TS supports it for an ESnnnn target. // 6n ]; @@ -1065,8 +1085,8 @@ test('is.iterable', t => { t.false(is.iterable(null)); t.false(is.iterable(undefined)); t.false(is.iterable(0)); - t.false(is.iterable(NaN)); - t.false(is.iterable(Infinity)); + t.false(is.iterable(Number.NaN)); + t.false(is.iterable(Number.POSITIVE_INFINITY)); t.false(is.iterable({})); t.notThrows(() => { @@ -1088,10 +1108,10 @@ test('is.iterable', t => { assert.iterable(0); }); t.throws(() => { - assert.iterable(NaN); + assert.iterable(Number.NaN); }); t.throws(() => { - assert.iterable(Infinity); + assert.iterable(Number.POSITIVE_INFINITY); }); t.throws(() => { assert.iterable({}); @@ -1100,19 +1120,19 @@ test('is.iterable', t => { test('is.asyncIterable', t => { t.true(is.asyncIterable({ - [Symbol.asyncIterator]: () => {} + [Symbol.asyncIterator]() {}, })); t.false(is.asyncIterable(null)); t.false(is.asyncIterable(undefined)); t.false(is.asyncIterable(0)); - t.false(is.asyncIterable(NaN)); - t.false(is.asyncIterable(Infinity)); + t.false(is.asyncIterable(Number.NaN)); + t.false(is.asyncIterable(Number.POSITIVE_INFINITY)); t.false(is.asyncIterable({})); t.notThrows(() => { assert.asyncIterable({ - [Symbol.asyncIterator]: () => { } + [Symbol.asyncIterator]() {}, }); }); @@ -1126,10 +1146,10 @@ test('is.asyncIterable', t => { assert.asyncIterable(0); }); t.throws(() => { - assert.asyncIterable(NaN); + assert.asyncIterable(Number.NaN); }); t.throws(() => { - assert.asyncIterable(Infinity); + assert.asyncIterable(Number.POSITIVE_INFINITY); }); t.throws(() => { assert.asyncIterable({}); @@ -1141,7 +1161,7 @@ test('is.class', t => { const classDeclarations = [ Foo, - class Bar extends Foo {} + class Bar extends Foo {}, ]; for (const classDeclaration of classDeclarations) { @@ -1164,7 +1184,7 @@ test('is.typedArray', t => { new Float32Array(), new Float64Array(), new BigInt64Array(), - new BigUint64Array() + new BigUint64Array(), ]; for (const item of typedArrays) { @@ -1334,7 +1354,7 @@ test('is.domElement', t => { 'span', 'img', 'canvas', - 'script' + 'script', ]; for (const tagName of tagNames) { @@ -1356,33 +1376,33 @@ test('is.infinite', t => { }); test('is.evenInteger', t => { - for (const el of [-6, 2, 4]) { - t.true(is.evenInteger(el)); + for (const element of [-6, 2, 4]) { + t.true(is.evenInteger(element)); t.notThrows(() => { - assert.evenInteger(el); + assert.evenInteger(element); }); } - for (const el of [-3, 1, 5]) { - t.false(is.evenInteger(el)); + for (const element of [-3, 1, 5]) { + t.false(is.evenInteger(element)); t.throws(() => { - assert.evenInteger(el); + assert.evenInteger(element); }); } }); test('is.oddInteger', t => { - for (const el of [-5, 7, 13]) { - t.true(is.oddInteger(el)); + for (const element of [-5, 7, 13]) { + t.true(is.oddInteger(element)); t.notThrows(() => { - assert.oddInteger(el); + assert.oddInteger(element); }); } - for (const el of [-8, 8, 10]) { - t.false(is.oddInteger(el)); + for (const element of [-8, 8, 10]) { + t.false(is.oddInteger(element)); t.throws(() => { - assert.oddInteger(el); + assert.oddInteger(element); }); } }); @@ -1452,7 +1472,7 @@ test('is.nonEmptyStringAndNotWhitespace', t => { t.false(is.nonEmptyStringAndNotWhitespace(' ')); t.true(is.nonEmptyStringAndNotWhitespace('🦄')); - for (const value of [null, undefined, 5, NaN, {}, []]) { + for (const value of [null, undefined, 5, Number.NaN, {}, []]) { t.false(is.nonEmptyStringAndNotWhitespace(value)); t.throws(() => { @@ -1509,16 +1529,16 @@ test('is.emptySet', t => { }); test('is.nonEmptySet', t => { - const tempSet = new Set(); - t.false(is.nonEmptySet(tempSet)); + const temporarySet = new Set(); + t.false(is.nonEmptySet(temporarySet)); t.throws(() => { - assert.nonEmptySet(tempSet); + assert.nonEmptySet(temporarySet); }); - tempSet.add(1); - t.true(is.nonEmptySet(tempSet)); + temporarySet.add(1); + t.true(is.nonEmptySet(temporarySet)); t.notThrows(() => { - assert.nonEmptySet(tempSet); + assert.nonEmptySet(temporarySet); }); }); @@ -1527,16 +1547,16 @@ test('is.emptyMap', t => { }); test('is.nonEmptyMap', t => { - const tempMap = new Map(); - t.false(is.nonEmptyMap(tempMap)); + const temporaryMap = new Map(); + t.false(is.nonEmptyMap(temporaryMap)); t.throws(() => { - assert.nonEmptyMap(tempMap); + assert.nonEmptyMap(temporaryMap); }); - tempMap.set('unicorn', '🦄'); - t.true(is.nonEmptyMap(tempMap)); + temporaryMap.set('unicorn', '🦄'); + t.true(is.nonEmptyMap(temporaryMap)); t.notThrows(() => { - assert.nonEmptyMap(tempMap); + assert.nonEmptyMap(temporaryMap); }); }); @@ -1598,21 +1618,21 @@ test('is.any', t => { assert.any(is.string, 1, 2, 3); }, { // Removes duplicates: - message: /received values of types `number`./ + message: /received values of types `number`./, }); t.throws(() => { assert.any(is.string, 1, [4]); }, { // Lists all types: - message: /received values of types `number`, `Array`./ + message: /received values of types `number`, `Array`./, }); t.throws(() => { assert.any([is.string, is.nullOrUndefined], 1); }, { // Handles array as first argument: - message: /received values of types `number`./ + message: /received values of types `number`./, }); }); @@ -1622,7 +1642,7 @@ test('is.all', t => { t.false(is.all(is.string, '🦄', [])); t.false(is.all(is.set, new Map(), {})); - t.true(is.all(is.array, ...[['1'], ['2']])); + t.true(is.all(is.array, ['1'], ['2'])); t.throws(() => { is.all(null as any, true); @@ -1660,14 +1680,14 @@ test('is.all', t => { assert.all(is.string, 1, 2, 3); }, { // Removes duplicates: - message: /received values of types `number`./ + message: /received values of types `number`./, }); t.throws(() => { assert.all(is.string, 1, [4]); }, { // Lists all types: - message: /received values of types `number`, `Array`./ + message: /received values of types `number`, `Array`./, }); }); @@ -1693,14 +1713,14 @@ test('is.formData', t => { }); test('is.urlSearchParams', t => { - const searchParams = new URLSearchParams(); - t.true(is.urlSearchParams(searchParams)); + const searchParameters = new URLSearchParams(); + t.true(is.urlSearchParams(searchParameters)); t.false(is.urlSearchParams({})); t.false(is.urlSearchParams(undefined)); t.false(is.urlSearchParams(null)); t.notThrows(() => { - assert.urlSearchParams(searchParams); + assert.urlSearchParams(searchParameters); }); t.throws(() => { assert.urlSearchParams({}); diff --git a/tsconfig.json b/tsconfig.json index d1f6382..e1c05ee 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,14 @@ { "extends": "@sindresorhus/tsconfig", "compilerOptions": { - "outDir": "dist", - "target": "es2018", - "lib": [ - "es2018", - "dom" - ] + "outDir": "dist" }, "include": [ "source" - ] + ], + "ts-node": { + "transpileOnly": true, + "files": true, + "experimentalResolver": true + } } diff --git a/tsconfig.xo.json b/tsconfig.xo.json deleted file mode 100644 index b01049f..0000000 --- a/tsconfig.xo.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "extends": "./tsconfig.json", - "include": [ - "test" - ] -}