diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) 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: diff --git a/package.json b/package.json index cf2b36d..be6df98 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, "main": "dist", "engines": { @@ -48,27 +48,25 @@ ], "devDependencies": { "@sindresorhus/tsconfig": "^0.7.0", - "@types/jsdom": "^12.2.4", - "@types/node": "^12.12.6", + "@types/jsdom": "^16.1.0", + "@types/node": "^13.7.4", "@types/zen-observable": "^0.8.0", - "@typescript-eslint/eslint-plugin": "^2.18.0", - "@typescript-eslint/parser": "^2.18.0", - "ava": "^2.1.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.24.1", + "eslint-config-xo-typescript": "^0.26.0", "jsdom": "^16.0.1", "rxjs": "^6.4.0", - "tempy": "^0.3.0", + "tempy": "^0.4.0", "ts-node": "^8.3.0", - "typescript": "^3.7.5", - "xo": "^0.25.3", + "typescript": "~3.8.2", + "xo": "^0.26.1", "zen-observable": "^0.8.8" }, "types": "dist", "sideEffects": false, "ava": { - "babel": false, - "compileEnhancements": false, "extensions": [ "ts" ], diff --git a/source/index.ts b/source/index.ts index faabc65..cd8af29 100644 --- a/source/index.ts +++ b/source/index.ts @@ -133,13 +133,13 @@ is.numericString = (value: unknown): value is string => is.string(value) && value.length > 0 && !Number.isNaN(Number(value)); is.array = Array.isArray; -is.buffer = (value: unknown): value is Buffer => !is.nullOrUndefined(value) && !is.nullOrUndefined((value as Buffer).constructor) && is.function_((value as Buffer).constructor.isBuffer) && (value as Buffer).constructor.isBuffer(value); +is.buffer = (value: unknown): value is Buffer => (value as any)?.constructor?.isBuffer?.(value) ?? false; 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.iterable = (value: unknown): value is IterableIterator => !is.nullOrUndefined(value) && is.function_((value as IterableIterator)[Symbol.iterator]); +is.iterable = (value: unknown): value is IterableIterator => is.function_((value as IterableIterator)?.[Symbol.iterator]); -is.asyncIterable = (value: unknown): value is AsyncIterableIterator => !is.nullOrUndefined(value) && is.function_((value as AsyncIterableIterator)[Symbol.asyncIterator]); +is.asyncIterable = (value: unknown): value is AsyncIterableIterator => is.function_((value as AsyncIterableIterator)?.[Symbol.asyncIterator]); is.generator = (value: unknown): value is Generator => is.iterable(value) && is.function_(value.next) && is.function_(value.throw); @@ -149,9 +149,8 @@ is.nativePromise = (value: unknown): value is Promise => isObjectOfType>(TypeName.Promise)(value); const hasPromiseAPI = (value: unknown): value is Promise => - is.object(value) && - is.function_((value as Promise).then) && // eslint-disable-line promise/prefer-await-to-then - is.function_((value as Promise).catch); + is.function_((value as Promise)?.then) && + is.function_((value as Promise)?.catch); is.promise = (value: unknown): value is Promise => is.nativePromise(value) || hasPromiseAPI(value); @@ -326,11 +325,11 @@ is.observable = (value: unknown): value is ObservableLike => { } // eslint-disable-next-line no-use-extend-native/no-use-extend-native - if ((value as any)[Symbol.observable] && value === (value as any)[Symbol.observable]()) { + if (value === (value as any)[Symbol.observable]?.()) { return true; } - if ((value as any)['@@observable'] && value === (value as any)['@@observable']()) { + if (value === (value as any)['@@observable']?.()) { return true; } @@ -357,7 +356,7 @@ is.emptyString = (value: unknown): value is '' => is.string(value) && value.leng // TODO: Use `not ''` when the `not` operator is available. is.nonEmptyString = (value: unknown): value is string => is.string(value) && value.length > 0; -const isWhiteSpaceString = (value: unknown): value is string => is.string(value) && /\S/.test(value) === false; +const isWhiteSpaceString = (value: unknown): value is string => is.string(value) && !/\S/.test(value); is.emptyStringOrWhitespace = (value: unknown): value is string => is.emptyString(value) || isWhiteSpaceString(value); is.emptyObject = (value: unknown): value is Record => is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length === 0; @@ -377,7 +376,7 @@ export type Predicate = (value: unknown) => boolean; type ArrayMethod = (fn: (value: unknown, index: number, array: unknown[]) => boolean, thisArg?: unknown) => boolean; const predicateOnArray = (method: ArrayMethod, predicate: Predicate, values: unknown[]) => { - if (is.function_(predicate) === false) { + if (!is.function_(predicate)) { throw new TypeError(`Invalid predicate: ${JSON.stringify(predicate)}`); } diff --git a/test/test.ts b/test/test.ts index 2909247..6d6a4ce 100644 --- a/test/test.ts +++ b/test/test.ts @@ -571,13 +571,23 @@ const testType = (t: ExecutionContext, type: string, exclude?: string[]) => { } const isTypeUnderTest = key === type; - const assertIs = isTypeUnderTest ? t.true.bind(t) : t.false.bind(t); - const assertAsserts = isTypeUnderTest ? t.notThrows.bind(t) : t.throws.bind(t); + const assertIs = isTypeUnderTest ? t.true : t.false; for (const fixture of fixtures) { assertIs(testIs(fixture), `Value: ${inspect(fixture)}`); const valueType = typeDescription ? typeDescription : typename; - assertAsserts(() => testAssert(fixture), `Expected value which is \`${valueType!}\`, received value of type \`${is(fixture)}\`.`); + + if (isTypeUnderTest) { + t.notThrows(() => { + testAssert(fixture); + }); + } else { + t.throws(() => { + testAssert(fixture); + }, { + message: `Expected value which is \`${valueType!}\`, received value of type \`${is(fixture)}\`.` + }); + } if (isTypeUnderTest && typename) { t.is(is(fixture), typename);