diff --git a/dist/cjs/index.cjs b/dist/cjs/index.cjs index cb52cef..cb77eb4 100644 --- a/dist/cjs/index.cjs +++ b/dist/cjs/index.cjs @@ -3,109 +3,74 @@ function getType(payload) { return Object.prototype.toString.call(payload).slice(8, -1); } -function isUndefined(payload) { - return getType(payload) === "Undefined"; + +function isAnyObject(payload) { + return getType(payload) === "Object"; } -function isNull(payload) { - return getType(payload) === "Null"; + +function isArray(payload) { + return getType(payload) === "Array"; +} + +function isBlob(payload) { + return getType(payload) === "Blob"; +} + +function isBoolean(payload) { + return getType(payload) === "Boolean"; +} + +function isDate(payload) { + return getType(payload) === "Date" && !isNaN(payload); +} + +function isEmptyArray(payload) { + return isArray(payload) && payload.length === 0; } + function isPlainObject(payload) { if (getType(payload) !== "Object") return false; const prototype = Object.getPrototypeOf(payload); return !!prototype && prototype.constructor === Object && prototype === Object.prototype; } -function isObject(payload) { - return isPlainObject(payload); -} + function isEmptyObject(payload) { return isPlainObject(payload) && Object.keys(payload).length === 0; } -function isFullObject(payload) { - return isPlainObject(payload) && Object.keys(payload).length > 0; -} -function isAnyObject(payload) { - return getType(payload) === "Object"; -} -function isObjectLike(payload) { - return isAnyObject(payload); + +function isEmptyString(payload) { + return payload === ""; } -function isFunction(payload) { - return typeof payload === "function"; + +function isError(payload) { + return getType(payload) === "Error" || payload instanceof Error; } -function isArray(payload) { - return getType(payload) === "Array"; + +function isFile(payload) { + return getType(payload) === "File"; } + function isFullArray(payload) { return isArray(payload) && payload.length > 0; } -function isEmptyArray(payload) { - return isArray(payload) && payload.length === 0; + +function isFullObject(payload) { + return isPlainObject(payload) && Object.keys(payload).length > 0; } + function isString(payload) { return getType(payload) === "String"; } + function isFullString(payload) { return isString(payload) && payload !== ""; } -function isEmptyString(payload) { - return payload === ""; -} -function isNumber(payload) { - return getType(payload) === "Number" && !isNaN(payload); -} -function isPositiveNumber(payload) { - return isNumber(payload) && payload > 0; -} -function isNegativeNumber(payload) { - return isNumber(payload) && payload < 0; -} -function isBoolean(payload) { - return getType(payload) === "Boolean"; -} -function isRegExp(payload) { - return getType(payload) === "RegExp"; -} -function isMap(payload) { - return getType(payload) === "Map"; -} -function isWeakMap(payload) { - return getType(payload) === "WeakMap"; -} -function isSet(payload) { - return getType(payload) === "Set"; -} -function isWeakSet(payload) { - return getType(payload) === "WeakSet"; -} -function isSymbol(payload) { - return getType(payload) === "Symbol"; -} -function isDate(payload) { - return getType(payload) === "Date" && !isNaN(payload); -} -function isBlob(payload) { - return getType(payload) === "Blob"; -} -function isFile(payload) { - return getType(payload) === "File"; -} -function isPromise(payload) { - return getType(payload) === "Promise"; -} -function isError(payload) { - return getType(payload) === "Error"; -} -function isNaNValue(payload) { - return getType(payload) === "Number" && isNaN(payload); -} -function isPrimitive(payload) { - return isBoolean(payload) || isNull(payload) || isUndefined(payload) || isNumber(payload) || isString(payload) || isSymbol(payload); -} -const isNullOrUndefined = isOneOf(isNull, isUndefined); -function isOneOf(a, b, c, d, e) { - return (value) => a(value) || b(value) || !!c && c(value) || !!d && d(value) || !!e && e(value); + +function isFunction(payload) { + return typeof payload === "function"; } + function isType(payload, type) { if (!(type instanceof Function)) { throw new TypeError("Type must be a function"); @@ -116,6 +81,7 @@ function isType(payload, type) { const name = type.name; return getType(payload) === name || Boolean(payload && payload.constructor === type); } + function isInstanceOf(value, classOrClassName) { if (typeof classOrClassName === "function") { for (let p = value; p; p = Object.getPrototypeOf(p)) { @@ -134,6 +100,76 @@ function isInstanceOf(value, classOrClassName) { } } +function isMap(payload) { + return getType(payload) === "Map"; +} + +function isNaNValue(payload) { + return getType(payload) === "Number" && isNaN(payload); +} + +function isNumber(payload) { + return getType(payload) === "Number" && !isNaN(payload); +} + +function isNegativeNumber(payload) { + return isNumber(payload) && payload < 0; +} + +function isNull(payload) { + return getType(payload) === "Null"; +} + +function isOneOf(a, b, c, d, e) { + return (value) => a(value) || b(value) || !!c && c(value) || !!d && d(value) || !!e && e(value); +} + +function isUndefined(payload) { + return getType(payload) === "Undefined"; +} + +const isNullOrUndefined = isOneOf(isNull, isUndefined); + +function isObject(payload) { + return isPlainObject(payload); +} + +function isObjectLike(payload) { + return isAnyObject(payload); +} + +function isPositiveNumber(payload) { + return isNumber(payload) && payload > 0; +} + +function isSymbol(payload) { + return getType(payload) === "Symbol"; +} + +function isPrimitive(payload) { + return isBoolean(payload) || isNull(payload) || isUndefined(payload) || isNumber(payload) || isString(payload) || isSymbol(payload); +} + +function isPromise(payload) { + return getType(payload) === "Promise"; +} + +function isRegExp(payload) { + return getType(payload) === "RegExp"; +} + +function isSet(payload) { + return getType(payload) === "Set"; +} + +function isWeakMap(payload) { + return getType(payload) === "WeakMap"; +} + +function isWeakSet(payload) { + return getType(payload) === "WeakSet"; +} + exports.getType = getType; exports.isAnyObject = isAnyObject; exports.isArray = isArray; diff --git a/dist/cjs/index.d.cts b/dist/cjs/index.d.cts index f63e49d..f76ddd9 100644 --- a/dist/cjs/index.d.cts +++ b/dist/cjs/index.d.cts @@ -1,334 +1,380 @@ -type AnyFunction = (...args: any[]) => any; -type AnyAsyncFunction = (...args: any[]) => Promise; -type AnyClass = new (...args: any[]) => any; -type PlainObject = Record; -type TypeGuard = (payload: A) => payload is B; /** * Returns the object type of the given payload * - * @param {*} payload + * @param {any} payload * @returns {string} */ declare function getType(payload: any): string; + +type PlainObject = Record; /** - * Returns whether the payload is undefined + * Returns whether the payload is a plain JavaScript object (excluding special classes or objects + * with other prototypes) * - * @param {*} payload - * @returns {payload is undefined} + * @param {any} payload + * @returns {payload is PlainObject} */ -declare function isUndefined(payload: any): payload is undefined; +declare function isPlainObject(payload: any): payload is PlainObject; + /** - * Returns whether the payload is null + * Returns whether the payload is an any kind of object (including special classes or objects with + * different prototypes) * - * @param {*} payload - * @returns {payload is null} + * @param {any} payload + * @returns {payload is PlainObject} */ -declare function isNull(payload: any): payload is null; +declare function isAnyObject(payload: any): payload is PlainObject; + /** - * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes) + * Returns whether the payload is an array * - * @param {*} payload - * @returns {payload is PlainObject} + * @param {any} payload + * @returns {payload is any[]} */ -declare function isPlainObject(payload: any): payload is PlainObject; +declare function isArray(payload: any): payload is any[]; + /** - * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes) + * Returns whether the payload is a Blob * - * @param {*} payload - * @returns {payload is PlainObject} + * @param {any} payload + * @returns {payload is Blob} */ -declare function isObject(payload: any): payload is PlainObject; +declare function isBlob(payload: any): payload is Blob; + /** - * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes) + * Returns whether the payload is a boolean * - * @param {*} payload - * @returns {payload is { [K in any]: never }} + * @param {any} payload + * @returns {payload is boolean} */ -declare function isEmptyObject(payload: any): payload is { - [K in any]: never; -}; +declare function isBoolean(payload: any): payload is boolean; + /** - * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes) + * Returns whether the payload is a Date, and that the date is valid * - * @param {*} payload - * @returns {payload is PlainObject} + * @param {any} payload + * @returns {payload is Date} */ -declare function isFullObject(payload: any): payload is PlainObject; +declare function isDate(payload: any): payload is Date; + /** - * Returns whether the payload is an any kind of object (including special classes or objects with different prototypes) + * Returns whether the payload is a an empty array * - * @param {*} payload - * @returns {payload is PlainObject} + * @param {any} payload + * @returns {payload is []} */ -declare function isAnyObject(payload: any): payload is PlainObject; +declare function isEmptyArray(payload: any): payload is []; + /** - * Returns whether the payload is an object like a type passed in < > + * Returns whether the payload is a an empty object (excluding special classes or objects with other + * prototypes) * - * Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop. + * @param {any} payload + * @returns {payload is { [K in any]: never }} + */ +declare function isEmptyObject(payload: any): payload is { + [K in any]: never; +}; + +/** + * Returns whether the payload is '' * - * @template T this must be passed in < > - * @param {*} payload - * @returns {payload is T} + * @param {any} payload + * @returns {payload is string} */ -declare function isObjectLike(payload: any): payload is T; +declare function isEmptyString(payload: any): payload is string; + /** - * Returns whether the payload is a function (regular or async) + * Returns whether the payload is an Error * - * @param {*} payload - * @returns {payload is AnyFunction} + * @param {any} payload + * @returns {payload is Error} */ -declare function isFunction(payload: any): payload is AnyFunction; +declare function isError(payload: any): payload is Error; + /** - * Returns whether the payload is an array + * Returns whether the payload is a File * * @param {any} payload - * @returns {payload is any[]} + * @returns {payload is File} */ -declare function isArray(payload: any): payload is any[]; +declare function isFile(payload: any): payload is File; + /** * Returns whether the payload is a an array with at least 1 item * - * @param {*} payload + * @param {any} payload * @returns {payload is any[]} */ declare function isFullArray(payload: any): payload is any[]; + /** - * Returns whether the payload is a an empty array + * Returns whether the payload is a an empty object (excluding special classes or objects with other + * prototypes) * - * @param {*} payload - * @returns {payload is []} + * @param {any} payload + * @returns {payload is PlainObject} */ -declare function isEmptyArray(payload: any): payload is []; +declare function isFullObject(payload: any): payload is PlainObject; + /** - * Returns whether the payload is a string + * Returns whether the payload is a string, BUT returns false for '' * - * @param {*} payload + * @param {any} payload * @returns {payload is string} */ -declare function isString(payload: any): payload is string; +declare function isFullString(payload: any): payload is string; + +type AnyFunction = (...args: any[]) => any; /** - * Returns whether the payload is a string, BUT returns false for '' + * Returns whether the payload is a function (regular or async) * - * @param {*} payload - * @returns {payload is string} + * @param {any} payload + * @returns {payload is AnyFunction} */ -declare function isFullString(payload: any): payload is string; +declare function isFunction(payload: any): payload is AnyFunction; + +type AnyClass = new (...args: any[]) => any; /** - * Returns whether the payload is '' + * Does a generic check to check that the given payload is of a given type. In cases like Number, it + * will return true for NaN as NaN is a Number (thanks javascript!); It will, however, differentiate + * between object and null * - * @param {*} payload - * @returns {payload is string} + * @template T + * @param {any} payload + * @param {T} type + * @returns {payload is T} + * @throws {TypeError} Will throw type error if type is an invalid type */ -declare function isEmptyString(payload: any): payload is string; +declare function isType(payload: any, type: T): payload is T; + +type GlobalClassName = { + [K in keyof typeof globalThis]: (typeof globalThis)[K] extends AnyClass ? K : never; +}[keyof typeof globalThis]; /** - * Returns whether the payload is a number (but not NaN) + * Checks if a value is an instance of a class or a class name. Useful when you want to check if a + * value is an instance of a class that may not be defined in the current scope. For example, if you + * want to check if a value is an `OffscreenCanvas` instance, you might not want to do the song and + * dance of using `typeof OffscreenCanvas !== 'undefined'` and then shimming `OffscreenCanvas` if + * the types aren't around. * - * This will return `false` for `NaN`!! + * @example + * if (isInstanceOf(value, 'OffscreenCanvas')) { + * // value is an OffscreenCanvas + * } * - * @param {*} payload - * @returns {payload is number} + * @param value The value to recursively check + * @param class_ A string or class that the value should be an instance of */ -declare function isNumber(payload: any): payload is number; +declare function isInstanceOf(value: unknown, class_: T): value is T; +declare function isInstanceOf(value: unknown, className: K): value is (typeof globalThis)[K]; +declare function isInstanceOf(value: unknown, className: string): value is object; + /** - * Returns whether the payload is a positive number (but not 0) + * Returns whether the payload is a Map * - * @param {*} payload - * @returns {payload is number} + * @param {any} payload + * @returns {payload is Map} */ -declare function isPositiveNumber(payload: any): payload is number; +declare function isMap(payload: any): payload is Map; + +/** + * Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`) + * + * @param {any} payload + * @returns {payload is typeof NaN} + */ +declare function isNaNValue(payload: any): payload is typeof NaN; + /** * Returns whether the payload is a negative number (but not 0) * - * @param {*} payload + * @param {any} payload * @returns {payload is number} */ declare function isNegativeNumber(payload: any): payload is number; + /** - * Returns whether the payload is a boolean + * Returns whether the payload is null * - * @param {*} payload - * @returns {payload is boolean} + * @param {any} payload + * @returns {payload is null} */ -declare function isBoolean(payload: any): payload is boolean; +declare function isNull(payload: any): payload is null; + /** - * Returns whether the payload is a regular expression (RegExp) + * Returns true whether the payload is null or undefined * - * @param {*} payload - * @returns {payload is RegExp} + * @param {any} payload + * @returns {(payload is null | undefined)} */ -declare function isRegExp(payload: any): payload is RegExp; +declare const isNullOrUndefined: (payload: any) => payload is null | undefined; + /** - * Returns whether the payload is a Map + * Returns whether the payload is a number (but not NaN) * - * @param {*} payload - * @returns {payload is Map} - */ -declare function isMap(payload: any): payload is Map; -/** - * Returns whether the payload is a WeakMap + * This will return `false` for `NaN`!! * - * @param {*} payload - * @returns {payload is WeakMap} + * @param {any} payload + * @returns {payload is number} */ -declare function isWeakMap(payload: any): payload is WeakMap; +declare function isNumber(payload: any): payload is number; + /** - * Returns whether the payload is a Set + * Returns whether the payload is a plain JavaScript object (excluding special classes or objects + * with other prototypes) * - * @param {*} payload - * @returns {payload is Set} + * @param {any} payload + * @returns {payload is PlainObject} */ -declare function isSet(payload: any): payload is Set; +declare function isObject(payload: any): payload is PlainObject; + /** - * Returns whether the payload is a WeakSet + * Returns whether the payload is an object like a type passed in < > * - * @param {*} payload - * @returns {payload is WeakSet} - */ -declare function isWeakSet(payload: any): payload is WeakSet; -/** - * Returns whether the payload is a Symbol + * Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop. * - * @param {*} payload - * @returns {payload is symbol} + * @template T This must be passed in < > + * @param {any} payload + * @returns {payload is T} */ -declare function isSymbol(payload: any): payload is symbol; +declare function isObjectLike(payload: any): payload is T; + +type TypeGuard = (payload: A) => payload is B; /** - * Returns whether the payload is a Date, and that the date is valid + * A factory function that creates a function to check if the payload is one of the given types. * - * @param {*} payload - * @returns {payload is Date} - */ -declare function isDate(payload: any): payload is Date; -/** - * Returns whether the payload is a Blob + * @example + * import { isOneOf, isNull, isUndefined } from 'is-what' * - * @param {*} payload - * @returns {payload is Blob} + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false */ -declare function isBlob(payload: any): payload is Blob; +declare function isOneOf(a: TypeGuard, b: TypeGuard): TypeGuard; /** - * Returns whether the payload is a File + * A factory function that creates a function to check if the payload is one of the given types. * - * @param {*} payload - * @returns {payload is File} + * @example + * import { isOneOf, isNull, isUndefined } from 'is-what' + * + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false */ -declare function isFile(payload: any): payload is File; +declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard): TypeGuard; /** - * Returns whether the payload is a Promise + * A factory function that creates a function to check if the payload is one of the given types. * - * @param {*} payload - * @returns {payload is Promise} + * @example + * import { isOneOf, isNull, isUndefined } from 'is-what' + * + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false */ -declare function isPromise(payload: any): payload is Promise; +declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard): TypeGuard; /** - * Returns whether the payload is an Error + * A factory function that creates a function to check if the payload is one of the given types. * - * @param {*} payload - * @returns {payload is Error} + * @example + * import { isOneOf, isNull, isUndefined } from 'is-what' + * + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false */ -declare function isError(payload: any): payload is Error; +declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard, e: TypeGuard): TypeGuard; + /** - * Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`) + * Returns whether the payload is a positive number (but not 0) * - * @param {*} payload - * @returns {payload is typeof NaN} + * @param {any} payload + * @returns {payload is number} */ -declare function isNaNValue(payload: any): payload is typeof NaN; +declare function isPositiveNumber(payload: any): payload is number; + /** - * Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol) + * Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String + * | Symbol) * - * @param {*} payload + * @param {any} payload * @returns {(payload is boolean | null | undefined | number | string | symbol)} */ declare function isPrimitive(payload: any): payload is boolean | null | undefined | number | string | symbol; + /** - * Returns true whether the payload is null or undefined + * Returns whether the payload is a Promise * - * @param {*} payload - * @returns {(payload is null | undefined)} + * @param {any} payload + * @returns {payload is Promise} */ -declare const isNullOrUndefined: TypeGuard; +declare function isPromise(payload: any): payload is Promise; + /** - * A factory function that creates a function to check if the payload is one of the given types. - * @example - * import { isOneOf, isNull, isUndefined } from 'is-what' - * - * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * Returns whether the payload is a regular expression (RegExp) * - * isNullOrUndefined(null) // true - * isNullOrUndefined(undefined) // true - * isNullOrUndefined(123) // false + * @param {any} payload + * @returns {payload is RegExp} */ -declare function isOneOf(a: TypeGuard, b: TypeGuard): TypeGuard; +declare function isRegExp(payload: any): payload is RegExp; + /** - * A factory function that creates a function to check if the payload is one of the given types. - * @example - * import { isOneOf, isNull, isUndefined } from 'is-what' - * - * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * Returns whether the payload is a Set * - * isNullOrUndefined(null) // true - * isNullOrUndefined(undefined) // true - * isNullOrUndefined(123) // false + * @param {any} payload + * @returns {payload is Set} */ -declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard): TypeGuard; +declare function isSet(payload: any): payload is Set; + /** - * A factory function that creates a function to check if the payload is one of the given types. - * @example - * import { isOneOf, isNull, isUndefined } from 'is-what' - * - * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * Returns whether the payload is a string * - * isNullOrUndefined(null) // true - * isNullOrUndefined(undefined) // true - * isNullOrUndefined(123) // false + * @param {any} payload + * @returns {payload is string} */ -declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard): TypeGuard; +declare function isString(payload: any): payload is string; + /** - * A factory function that creates a function to check if the payload is one of the given types. - * @example - * import { isOneOf, isNull, isUndefined } from 'is-what' - * - * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * Returns whether the payload is a Symbol * - * isNullOrUndefined(null) // true - * isNullOrUndefined(undefined) // true - * isNullOrUndefined(123) // false + * @param {any} payload + * @returns {payload is symbol} */ -declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard, e: TypeGuard): TypeGuard; +declare function isSymbol(payload: any): payload is symbol; + /** - * Does a generic check to check that the given payload is of a given type. - * In cases like Number, it will return true for NaN as NaN is a Number (thanks javascript!); - * It will, however, differentiate between object and null + * Returns whether the payload is undefined * - * @template T - * @param {*} payload - * @param {T} type - * @throws {TypeError} Will throw type error if type is an invalid type - * @returns {payload is T} + * @param {any} payload + * @returns {payload is undefined} */ -declare function isType(payload: any, type: T): payload is T; -type GlobalClassName = { - [K in keyof typeof globalThis]: (typeof globalThis)[K] extends AnyClass ? K : never; -}[keyof typeof globalThis]; +declare function isUndefined(payload: any): payload is undefined; + /** - * Checks if a value is an instance of a class or a class name. Useful when you - * want to check if a value is an instance of a class that may not be defined in - * the current scope. For example, if you want to check if a value is an - * `OffscreenCanvas` instance, you might not want to do the song and dance of - * using `typeof OffscreenCanvas !== 'undefined'` and then shimming - * `OffscreenCanvas` if the types aren't around. + * Returns whether the payload is a WeakMap * - * @example - * if (isInstanceOf(value, 'OffscreenCanvas')) { - * // value is an OffscreenCanvas - * } + * @param {any} payload + * @returns {payload is WeakMap} + */ +declare function isWeakMap(payload: any): payload is WeakMap; + +/** + * Returns whether the payload is a WeakSet * - * @param value The value to recursively check - * @param class_ A string or class that the value should be an instance of + * @param {any} payload + * @returns {payload is WeakSet} */ -declare function isInstanceOf(value: unknown, class_: T): value is T; -declare function isInstanceOf(value: unknown, className: K): value is (typeof globalThis)[K]; -declare function isInstanceOf(value: unknown, className: string): value is object; +declare function isWeakSet(payload: any): payload is WeakSet; + +type AnyAsyncFunction = (...args: any[]) => Promise; export { AnyAsyncFunction, AnyClass, AnyFunction, PlainObject, getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullObject, isFullString, isFunction, isInstanceOf, isMap, isNaNValue, isNegativeNumber, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isOneOf, isPlainObject, isPositiveNumber, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet }; diff --git a/dist/index.d.ts b/dist/index.d.ts index f63e49d..f76ddd9 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,334 +1,380 @@ -type AnyFunction = (...args: any[]) => any; -type AnyAsyncFunction = (...args: any[]) => Promise; -type AnyClass = new (...args: any[]) => any; -type PlainObject = Record; -type TypeGuard = (payload: A) => payload is B; /** * Returns the object type of the given payload * - * @param {*} payload + * @param {any} payload * @returns {string} */ declare function getType(payload: any): string; + +type PlainObject = Record; /** - * Returns whether the payload is undefined + * Returns whether the payload is a plain JavaScript object (excluding special classes or objects + * with other prototypes) * - * @param {*} payload - * @returns {payload is undefined} + * @param {any} payload + * @returns {payload is PlainObject} */ -declare function isUndefined(payload: any): payload is undefined; +declare function isPlainObject(payload: any): payload is PlainObject; + /** - * Returns whether the payload is null + * Returns whether the payload is an any kind of object (including special classes or objects with + * different prototypes) * - * @param {*} payload - * @returns {payload is null} + * @param {any} payload + * @returns {payload is PlainObject} */ -declare function isNull(payload: any): payload is null; +declare function isAnyObject(payload: any): payload is PlainObject; + /** - * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes) + * Returns whether the payload is an array * - * @param {*} payload - * @returns {payload is PlainObject} + * @param {any} payload + * @returns {payload is any[]} */ -declare function isPlainObject(payload: any): payload is PlainObject; +declare function isArray(payload: any): payload is any[]; + /** - * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes) + * Returns whether the payload is a Blob * - * @param {*} payload - * @returns {payload is PlainObject} + * @param {any} payload + * @returns {payload is Blob} */ -declare function isObject(payload: any): payload is PlainObject; +declare function isBlob(payload: any): payload is Blob; + /** - * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes) + * Returns whether the payload is a boolean * - * @param {*} payload - * @returns {payload is { [K in any]: never }} + * @param {any} payload + * @returns {payload is boolean} */ -declare function isEmptyObject(payload: any): payload is { - [K in any]: never; -}; +declare function isBoolean(payload: any): payload is boolean; + /** - * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes) + * Returns whether the payload is a Date, and that the date is valid * - * @param {*} payload - * @returns {payload is PlainObject} + * @param {any} payload + * @returns {payload is Date} */ -declare function isFullObject(payload: any): payload is PlainObject; +declare function isDate(payload: any): payload is Date; + /** - * Returns whether the payload is an any kind of object (including special classes or objects with different prototypes) + * Returns whether the payload is a an empty array * - * @param {*} payload - * @returns {payload is PlainObject} + * @param {any} payload + * @returns {payload is []} */ -declare function isAnyObject(payload: any): payload is PlainObject; +declare function isEmptyArray(payload: any): payload is []; + /** - * Returns whether the payload is an object like a type passed in < > + * Returns whether the payload is a an empty object (excluding special classes or objects with other + * prototypes) * - * Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop. + * @param {any} payload + * @returns {payload is { [K in any]: never }} + */ +declare function isEmptyObject(payload: any): payload is { + [K in any]: never; +}; + +/** + * Returns whether the payload is '' * - * @template T this must be passed in < > - * @param {*} payload - * @returns {payload is T} + * @param {any} payload + * @returns {payload is string} */ -declare function isObjectLike(payload: any): payload is T; +declare function isEmptyString(payload: any): payload is string; + /** - * Returns whether the payload is a function (regular or async) + * Returns whether the payload is an Error * - * @param {*} payload - * @returns {payload is AnyFunction} + * @param {any} payload + * @returns {payload is Error} */ -declare function isFunction(payload: any): payload is AnyFunction; +declare function isError(payload: any): payload is Error; + /** - * Returns whether the payload is an array + * Returns whether the payload is a File * * @param {any} payload - * @returns {payload is any[]} + * @returns {payload is File} */ -declare function isArray(payload: any): payload is any[]; +declare function isFile(payload: any): payload is File; + /** * Returns whether the payload is a an array with at least 1 item * - * @param {*} payload + * @param {any} payload * @returns {payload is any[]} */ declare function isFullArray(payload: any): payload is any[]; + /** - * Returns whether the payload is a an empty array + * Returns whether the payload is a an empty object (excluding special classes or objects with other + * prototypes) * - * @param {*} payload - * @returns {payload is []} + * @param {any} payload + * @returns {payload is PlainObject} */ -declare function isEmptyArray(payload: any): payload is []; +declare function isFullObject(payload: any): payload is PlainObject; + /** - * Returns whether the payload is a string + * Returns whether the payload is a string, BUT returns false for '' * - * @param {*} payload + * @param {any} payload * @returns {payload is string} */ -declare function isString(payload: any): payload is string; +declare function isFullString(payload: any): payload is string; + +type AnyFunction = (...args: any[]) => any; /** - * Returns whether the payload is a string, BUT returns false for '' + * Returns whether the payload is a function (regular or async) * - * @param {*} payload - * @returns {payload is string} + * @param {any} payload + * @returns {payload is AnyFunction} */ -declare function isFullString(payload: any): payload is string; +declare function isFunction(payload: any): payload is AnyFunction; + +type AnyClass = new (...args: any[]) => any; /** - * Returns whether the payload is '' + * Does a generic check to check that the given payload is of a given type. In cases like Number, it + * will return true for NaN as NaN is a Number (thanks javascript!); It will, however, differentiate + * between object and null * - * @param {*} payload - * @returns {payload is string} + * @template T + * @param {any} payload + * @param {T} type + * @returns {payload is T} + * @throws {TypeError} Will throw type error if type is an invalid type */ -declare function isEmptyString(payload: any): payload is string; +declare function isType(payload: any, type: T): payload is T; + +type GlobalClassName = { + [K in keyof typeof globalThis]: (typeof globalThis)[K] extends AnyClass ? K : never; +}[keyof typeof globalThis]; /** - * Returns whether the payload is a number (but not NaN) + * Checks if a value is an instance of a class or a class name. Useful when you want to check if a + * value is an instance of a class that may not be defined in the current scope. For example, if you + * want to check if a value is an `OffscreenCanvas` instance, you might not want to do the song and + * dance of using `typeof OffscreenCanvas !== 'undefined'` and then shimming `OffscreenCanvas` if + * the types aren't around. * - * This will return `false` for `NaN`!! + * @example + * if (isInstanceOf(value, 'OffscreenCanvas')) { + * // value is an OffscreenCanvas + * } * - * @param {*} payload - * @returns {payload is number} + * @param value The value to recursively check + * @param class_ A string or class that the value should be an instance of */ -declare function isNumber(payload: any): payload is number; +declare function isInstanceOf(value: unknown, class_: T): value is T; +declare function isInstanceOf(value: unknown, className: K): value is (typeof globalThis)[K]; +declare function isInstanceOf(value: unknown, className: string): value is object; + /** - * Returns whether the payload is a positive number (but not 0) + * Returns whether the payload is a Map * - * @param {*} payload - * @returns {payload is number} + * @param {any} payload + * @returns {payload is Map} */ -declare function isPositiveNumber(payload: any): payload is number; +declare function isMap(payload: any): payload is Map; + +/** + * Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`) + * + * @param {any} payload + * @returns {payload is typeof NaN} + */ +declare function isNaNValue(payload: any): payload is typeof NaN; + /** * Returns whether the payload is a negative number (but not 0) * - * @param {*} payload + * @param {any} payload * @returns {payload is number} */ declare function isNegativeNumber(payload: any): payload is number; + /** - * Returns whether the payload is a boolean + * Returns whether the payload is null * - * @param {*} payload - * @returns {payload is boolean} + * @param {any} payload + * @returns {payload is null} */ -declare function isBoolean(payload: any): payload is boolean; +declare function isNull(payload: any): payload is null; + /** - * Returns whether the payload is a regular expression (RegExp) + * Returns true whether the payload is null or undefined * - * @param {*} payload - * @returns {payload is RegExp} + * @param {any} payload + * @returns {(payload is null | undefined)} */ -declare function isRegExp(payload: any): payload is RegExp; +declare const isNullOrUndefined: (payload: any) => payload is null | undefined; + /** - * Returns whether the payload is a Map + * Returns whether the payload is a number (but not NaN) * - * @param {*} payload - * @returns {payload is Map} - */ -declare function isMap(payload: any): payload is Map; -/** - * Returns whether the payload is a WeakMap + * This will return `false` for `NaN`!! * - * @param {*} payload - * @returns {payload is WeakMap} + * @param {any} payload + * @returns {payload is number} */ -declare function isWeakMap(payload: any): payload is WeakMap; +declare function isNumber(payload: any): payload is number; + /** - * Returns whether the payload is a Set + * Returns whether the payload is a plain JavaScript object (excluding special classes or objects + * with other prototypes) * - * @param {*} payload - * @returns {payload is Set} + * @param {any} payload + * @returns {payload is PlainObject} */ -declare function isSet(payload: any): payload is Set; +declare function isObject(payload: any): payload is PlainObject; + /** - * Returns whether the payload is a WeakSet + * Returns whether the payload is an object like a type passed in < > * - * @param {*} payload - * @returns {payload is WeakSet} - */ -declare function isWeakSet(payload: any): payload is WeakSet; -/** - * Returns whether the payload is a Symbol + * Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop. * - * @param {*} payload - * @returns {payload is symbol} + * @template T This must be passed in < > + * @param {any} payload + * @returns {payload is T} */ -declare function isSymbol(payload: any): payload is symbol; +declare function isObjectLike(payload: any): payload is T; + +type TypeGuard = (payload: A) => payload is B; /** - * Returns whether the payload is a Date, and that the date is valid + * A factory function that creates a function to check if the payload is one of the given types. * - * @param {*} payload - * @returns {payload is Date} - */ -declare function isDate(payload: any): payload is Date; -/** - * Returns whether the payload is a Blob + * @example + * import { isOneOf, isNull, isUndefined } from 'is-what' * - * @param {*} payload - * @returns {payload is Blob} + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false */ -declare function isBlob(payload: any): payload is Blob; +declare function isOneOf(a: TypeGuard, b: TypeGuard): TypeGuard; /** - * Returns whether the payload is a File + * A factory function that creates a function to check if the payload is one of the given types. * - * @param {*} payload - * @returns {payload is File} + * @example + * import { isOneOf, isNull, isUndefined } from 'is-what' + * + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false */ -declare function isFile(payload: any): payload is File; +declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard): TypeGuard; /** - * Returns whether the payload is a Promise + * A factory function that creates a function to check if the payload is one of the given types. * - * @param {*} payload - * @returns {payload is Promise} + * @example + * import { isOneOf, isNull, isUndefined } from 'is-what' + * + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false */ -declare function isPromise(payload: any): payload is Promise; +declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard): TypeGuard; /** - * Returns whether the payload is an Error + * A factory function that creates a function to check if the payload is one of the given types. * - * @param {*} payload - * @returns {payload is Error} + * @example + * import { isOneOf, isNull, isUndefined } from 'is-what' + * + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false */ -declare function isError(payload: any): payload is Error; +declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard, e: TypeGuard): TypeGuard; + /** - * Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`) + * Returns whether the payload is a positive number (but not 0) * - * @param {*} payload - * @returns {payload is typeof NaN} + * @param {any} payload + * @returns {payload is number} */ -declare function isNaNValue(payload: any): payload is typeof NaN; +declare function isPositiveNumber(payload: any): payload is number; + /** - * Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol) + * Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String + * | Symbol) * - * @param {*} payload + * @param {any} payload * @returns {(payload is boolean | null | undefined | number | string | symbol)} */ declare function isPrimitive(payload: any): payload is boolean | null | undefined | number | string | symbol; + /** - * Returns true whether the payload is null or undefined + * Returns whether the payload is a Promise * - * @param {*} payload - * @returns {(payload is null | undefined)} + * @param {any} payload + * @returns {payload is Promise} */ -declare const isNullOrUndefined: TypeGuard; +declare function isPromise(payload: any): payload is Promise; + /** - * A factory function that creates a function to check if the payload is one of the given types. - * @example - * import { isOneOf, isNull, isUndefined } from 'is-what' - * - * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * Returns whether the payload is a regular expression (RegExp) * - * isNullOrUndefined(null) // true - * isNullOrUndefined(undefined) // true - * isNullOrUndefined(123) // false + * @param {any} payload + * @returns {payload is RegExp} */ -declare function isOneOf(a: TypeGuard, b: TypeGuard): TypeGuard; +declare function isRegExp(payload: any): payload is RegExp; + /** - * A factory function that creates a function to check if the payload is one of the given types. - * @example - * import { isOneOf, isNull, isUndefined } from 'is-what' - * - * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * Returns whether the payload is a Set * - * isNullOrUndefined(null) // true - * isNullOrUndefined(undefined) // true - * isNullOrUndefined(123) // false + * @param {any} payload + * @returns {payload is Set} */ -declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard): TypeGuard; +declare function isSet(payload: any): payload is Set; + /** - * A factory function that creates a function to check if the payload is one of the given types. - * @example - * import { isOneOf, isNull, isUndefined } from 'is-what' - * - * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * Returns whether the payload is a string * - * isNullOrUndefined(null) // true - * isNullOrUndefined(undefined) // true - * isNullOrUndefined(123) // false + * @param {any} payload + * @returns {payload is string} */ -declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard): TypeGuard; +declare function isString(payload: any): payload is string; + /** - * A factory function that creates a function to check if the payload is one of the given types. - * @example - * import { isOneOf, isNull, isUndefined } from 'is-what' - * - * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * Returns whether the payload is a Symbol * - * isNullOrUndefined(null) // true - * isNullOrUndefined(undefined) // true - * isNullOrUndefined(123) // false + * @param {any} payload + * @returns {payload is symbol} */ -declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard, e: TypeGuard): TypeGuard; +declare function isSymbol(payload: any): payload is symbol; + /** - * Does a generic check to check that the given payload is of a given type. - * In cases like Number, it will return true for NaN as NaN is a Number (thanks javascript!); - * It will, however, differentiate between object and null + * Returns whether the payload is undefined * - * @template T - * @param {*} payload - * @param {T} type - * @throws {TypeError} Will throw type error if type is an invalid type - * @returns {payload is T} + * @param {any} payload + * @returns {payload is undefined} */ -declare function isType(payload: any, type: T): payload is T; -type GlobalClassName = { - [K in keyof typeof globalThis]: (typeof globalThis)[K] extends AnyClass ? K : never; -}[keyof typeof globalThis]; +declare function isUndefined(payload: any): payload is undefined; + /** - * Checks if a value is an instance of a class or a class name. Useful when you - * want to check if a value is an instance of a class that may not be defined in - * the current scope. For example, if you want to check if a value is an - * `OffscreenCanvas` instance, you might not want to do the song and dance of - * using `typeof OffscreenCanvas !== 'undefined'` and then shimming - * `OffscreenCanvas` if the types aren't around. + * Returns whether the payload is a WeakMap * - * @example - * if (isInstanceOf(value, 'OffscreenCanvas')) { - * // value is an OffscreenCanvas - * } + * @param {any} payload + * @returns {payload is WeakMap} + */ +declare function isWeakMap(payload: any): payload is WeakMap; + +/** + * Returns whether the payload is a WeakSet * - * @param value The value to recursively check - * @param class_ A string or class that the value should be an instance of + * @param {any} payload + * @returns {payload is WeakSet} */ -declare function isInstanceOf(value: unknown, class_: T): value is T; -declare function isInstanceOf(value: unknown, className: K): value is (typeof globalThis)[K]; -declare function isInstanceOf(value: unknown, className: string): value is object; +declare function isWeakSet(payload: any): payload is WeakSet; + +type AnyAsyncFunction = (...args: any[]) => Promise; export { AnyAsyncFunction, AnyClass, AnyFunction, PlainObject, getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullObject, isFullString, isFunction, isInstanceOf, isMap, isNaNValue, isNegativeNumber, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isOneOf, isPlainObject, isPositiveNumber, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet }; diff --git a/dist/index.js b/dist/index.js index fff9275..6289097 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,109 +1,74 @@ function getType(payload) { return Object.prototype.toString.call(payload).slice(8, -1); } -function isUndefined(payload) { - return getType(payload) === "Undefined"; + +function isAnyObject(payload) { + return getType(payload) === "Object"; } -function isNull(payload) { - return getType(payload) === "Null"; + +function isArray(payload) { + return getType(payload) === "Array"; +} + +function isBlob(payload) { + return getType(payload) === "Blob"; +} + +function isBoolean(payload) { + return getType(payload) === "Boolean"; +} + +function isDate(payload) { + return getType(payload) === "Date" && !isNaN(payload); +} + +function isEmptyArray(payload) { + return isArray(payload) && payload.length === 0; } + function isPlainObject(payload) { if (getType(payload) !== "Object") return false; const prototype = Object.getPrototypeOf(payload); return !!prototype && prototype.constructor === Object && prototype === Object.prototype; } -function isObject(payload) { - return isPlainObject(payload); -} + function isEmptyObject(payload) { return isPlainObject(payload) && Object.keys(payload).length === 0; } -function isFullObject(payload) { - return isPlainObject(payload) && Object.keys(payload).length > 0; -} -function isAnyObject(payload) { - return getType(payload) === "Object"; -} -function isObjectLike(payload) { - return isAnyObject(payload); + +function isEmptyString(payload) { + return payload === ""; } -function isFunction(payload) { - return typeof payload === "function"; + +function isError(payload) { + return getType(payload) === "Error" || payload instanceof Error; } -function isArray(payload) { - return getType(payload) === "Array"; + +function isFile(payload) { + return getType(payload) === "File"; } + function isFullArray(payload) { return isArray(payload) && payload.length > 0; } -function isEmptyArray(payload) { - return isArray(payload) && payload.length === 0; + +function isFullObject(payload) { + return isPlainObject(payload) && Object.keys(payload).length > 0; } + function isString(payload) { return getType(payload) === "String"; } + function isFullString(payload) { return isString(payload) && payload !== ""; } -function isEmptyString(payload) { - return payload === ""; -} -function isNumber(payload) { - return getType(payload) === "Number" && !isNaN(payload); -} -function isPositiveNumber(payload) { - return isNumber(payload) && payload > 0; -} -function isNegativeNumber(payload) { - return isNumber(payload) && payload < 0; -} -function isBoolean(payload) { - return getType(payload) === "Boolean"; -} -function isRegExp(payload) { - return getType(payload) === "RegExp"; -} -function isMap(payload) { - return getType(payload) === "Map"; -} -function isWeakMap(payload) { - return getType(payload) === "WeakMap"; -} -function isSet(payload) { - return getType(payload) === "Set"; -} -function isWeakSet(payload) { - return getType(payload) === "WeakSet"; -} -function isSymbol(payload) { - return getType(payload) === "Symbol"; -} -function isDate(payload) { - return getType(payload) === "Date" && !isNaN(payload); -} -function isBlob(payload) { - return getType(payload) === "Blob"; -} -function isFile(payload) { - return getType(payload) === "File"; -} -function isPromise(payload) { - return getType(payload) === "Promise"; -} -function isError(payload) { - return getType(payload) === "Error"; -} -function isNaNValue(payload) { - return getType(payload) === "Number" && isNaN(payload); -} -function isPrimitive(payload) { - return isBoolean(payload) || isNull(payload) || isUndefined(payload) || isNumber(payload) || isString(payload) || isSymbol(payload); -} -const isNullOrUndefined = isOneOf(isNull, isUndefined); -function isOneOf(a, b, c, d, e) { - return (value) => a(value) || b(value) || !!c && c(value) || !!d && d(value) || !!e && e(value); + +function isFunction(payload) { + return typeof payload === "function"; } + function isType(payload, type) { if (!(type instanceof Function)) { throw new TypeError("Type must be a function"); @@ -114,6 +79,7 @@ function isType(payload, type) { const name = type.name; return getType(payload) === name || Boolean(payload && payload.constructor === type); } + function isInstanceOf(value, classOrClassName) { if (typeof classOrClassName === "function") { for (let p = value; p; p = Object.getPrototypeOf(p)) { @@ -132,4 +98,74 @@ function isInstanceOf(value, classOrClassName) { } } +function isMap(payload) { + return getType(payload) === "Map"; +} + +function isNaNValue(payload) { + return getType(payload) === "Number" && isNaN(payload); +} + +function isNumber(payload) { + return getType(payload) === "Number" && !isNaN(payload); +} + +function isNegativeNumber(payload) { + return isNumber(payload) && payload < 0; +} + +function isNull(payload) { + return getType(payload) === "Null"; +} + +function isOneOf(a, b, c, d, e) { + return (value) => a(value) || b(value) || !!c && c(value) || !!d && d(value) || !!e && e(value); +} + +function isUndefined(payload) { + return getType(payload) === "Undefined"; +} + +const isNullOrUndefined = isOneOf(isNull, isUndefined); + +function isObject(payload) { + return isPlainObject(payload); +} + +function isObjectLike(payload) { + return isAnyObject(payload); +} + +function isPositiveNumber(payload) { + return isNumber(payload) && payload > 0; +} + +function isSymbol(payload) { + return getType(payload) === "Symbol"; +} + +function isPrimitive(payload) { + return isBoolean(payload) || isNull(payload) || isUndefined(payload) || isNumber(payload) || isString(payload) || isSymbol(payload); +} + +function isPromise(payload) { + return getType(payload) === "Promise"; +} + +function isRegExp(payload) { + return getType(payload) === "RegExp"; +} + +function isSet(payload) { + return getType(payload) === "Set"; +} + +function isWeakMap(payload) { + return getType(payload) === "WeakMap"; +} + +function isWeakSet(payload) { + return getType(payload) === "WeakSet"; +} + export { getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullObject, isFullString, isFunction, isInstanceOf, isMap, isNaNValue, isNegativeNumber, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isOneOf, isPlainObject, isPositiveNumber, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet };