From 3be1225b9c0109d2cc74eebd81df289ec8459c76 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 3 Jun 2023 18:39:09 -0500 Subject: [PATCH 1/4] Set default to ESM --- tsconfig.json | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 620dd4d..a29958d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,23 +1,19 @@ { "compilerOptions": { - "allowJs": false, - "baseUrl": ".", + "outDir": "dist", "declaration": true, - "downlevelIteration": true, + "target": "ESNext", + "module": "ESNext", + "lib": ["ESNext", "DOM", "DOM.Iterable"], + "moduleResolution": "nodenext", + "allowSyntheticDefaultImports": true, "esModuleInterop": true, "isolatedModules": true, - "lib": ["esnext", "DOM", "DOM.Iterable"], - "module": "esnext", - "moduleResolution": "node", - "noImplicitReturns": true, - "noUnusedLocals": true, - "skipLibCheck": true, - "sourceMap": true, "strict": true, - "target": "es2019", - "useDefineForClassFields": true + "skipLibCheck": true }, - "include": ["src/**/*", "test/**/*"], + "include": ["src"], + "exclude": ["node_modules", "dist"], "typedocOptions": { "entryPoints": ["src/index.ts"], "out": "docs/dist", From 46edde66c020394946d4668a866064a1dec415df Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 3 Jun 2023 18:39:23 -0500 Subject: [PATCH 2/4] Remove rollup and configure dual ESM + CJS build --- package-lock.json | 1 - package.json | 17 +++++------------ 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4577b29..ffbb439 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,6 @@ "eslint-plugin-tree-shaking": "^1.10.0", "np": "^7.7.0", "prettier": "^2.8.8", - "rollup": "^3.22.0", "rollup-plugin-dts": "^5.3.0", "rollup-plugin-esbuild": "^5.0.0", "typedoc": "^0.24.7", diff --git a/package.json b/package.json index 64fbdda..c600557 100644 --- a/package.json +++ b/package.json @@ -9,14 +9,8 @@ "main": "./dist/index.js", "exports": { ".": { - "require": { - "types": "./dist/cjs/index.d.cts", - "default": "./dist/cjs/index.cjs" - }, - "import": { - "types": "./dist/index.d.ts", - "default": "./dist/index.js" - } + "require": "./dist/cjs/index.js", + "import": "./dist/index.js" } }, "files": [ @@ -28,7 +22,7 @@ "scripts": { "test": "vitest run", "lint": "tsc --noEmit && eslint ./src --ext .ts", - "build": "rollup -c ./rollup.config.js", + "build": "tsc && tsc --module CommonJS --outDir dist/cjs && echo '{\"type\": \"commonjs\"}' >dist/cjs/package.json", "build:docs": "typedoc", "release": "npm run lint && del dist && npm run build && np" }, @@ -73,12 +67,11 @@ "eslint-plugin-tree-shaking": "^1.10.0", "np": "^7.7.0", "prettier": "^2.8.8", - "rollup": "^3.22.0", "rollup-plugin-dts": "^5.3.0", "rollup-plugin-esbuild": "^5.0.0", + "typedoc": "^0.24.7", "typescript": "^5.0.4", - "vitest": "^0.31.1", - "typedoc": "^0.24.7" + "vitest": "^0.31.1" }, "ava": { "extensions": { From f54591bc6f67c7f8b016d687340c6b63dff4e3ca Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 3 Jun 2023 18:39:29 -0500 Subject: [PATCH 3/4] Remove rollup --- rollup.config.js | 45 --------------------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 rollup.config.js diff --git a/rollup.config.js b/rollup.config.js deleted file mode 100644 index 64e8d75..0000000 --- a/rollup.config.js +++ /dev/null @@ -1,45 +0,0 @@ -/* eslint-disable */ -import dts from 'rollup-plugin-dts' -import esbuild from 'rollup-plugin-esbuild' -import pkg from './package.json' assert { type: 'json' } - -export default [ - { - input: 'src/index.ts', - output: [ - { - file: pkg.exports['.'].import.default, - format: 'esm', - generatedCode: { constBindings: true }, - }, - { - file: pkg.exports['.'].require.default, - format: 'cjs', - generatedCode: { constBindings: true }, - }, - ], - plugins: [ - esbuild({ - sourceMap: false, - target: 'esnext', - loaders: { '.json': 'json' }, - }), - ], - }, - { - input: 'src/index.ts', - output: [ - { - file: pkg.exports['.'].import.types, - format: 'esm', - generatedCode: { constBindings: true }, - }, - { - file: pkg.exports['.'].require.types, - format: 'cjs', - generatedCode: { constBindings: true }, - }, - ], - plugins: [dts()], - }, -] From bbf22e69ea7a7da1fcf4a2e3129027bb0cc3f011 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 3 Jun 2023 18:39:38 -0500 Subject: [PATCH 4/4] =?UTF-8?q?Rebuild=20dist=20in=20source=20control=20?= =?UTF-8?q?=F0=9F=A4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/cjs/index.cjs | 173 --------- dist/cjs/{index.d.cts => index.d.ts} | 95 +++-- dist/cjs/index.js | 410 ++++++++++++++++++++++ dist/cjs/package.json | 1 + dist/index.d.ts | 95 +++-- dist/index.js | 500 ++++++++++++++++++++------- 6 files changed, 873 insertions(+), 401 deletions(-) delete mode 100644 dist/cjs/index.cjs rename dist/cjs/{index.d.cts => index.d.ts} (64%) create mode 100644 dist/cjs/index.js create mode 100644 dist/cjs/package.json diff --git a/dist/cjs/index.cjs b/dist/cjs/index.cjs deleted file mode 100644 index cb52cef..0000000 --- a/dist/cjs/index.cjs +++ /dev/null @@ -1,173 +0,0 @@ -'use strict'; - -function getType(payload) { - return Object.prototype.toString.call(payload).slice(8, -1); -} -function isUndefined(payload) { - return getType(payload) === "Undefined"; -} -function isNull(payload) { - return getType(payload) === "Null"; -} -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 isFunction(payload) { - return typeof payload === "function"; -} -function isArray(payload) { - return getType(payload) === "Array"; -} -function isFullArray(payload) { - return isArray(payload) && payload.length > 0; -} -function isEmptyArray(payload) { - return isArray(payload) && 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 isType(payload, type) { - if (!(type instanceof Function)) { - throw new TypeError("Type must be a function"); - } - if (!Object.prototype.hasOwnProperty.call(type, "prototype")) { - throw new TypeError("Type is not a class"); - } - 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)) { - if (isType(p, classOrClassName)) { - return true; - } - } - return false; - } else { - for (let p = value; p; p = Object.getPrototypeOf(p)) { - if (getType(p) === classOrClassName) { - return true; - } - } - return false; - } -} - -exports.getType = getType; -exports.isAnyObject = isAnyObject; -exports.isArray = isArray; -exports.isBlob = isBlob; -exports.isBoolean = isBoolean; -exports.isDate = isDate; -exports.isEmptyArray = isEmptyArray; -exports.isEmptyObject = isEmptyObject; -exports.isEmptyString = isEmptyString; -exports.isError = isError; -exports.isFile = isFile; -exports.isFullArray = isFullArray; -exports.isFullObject = isFullObject; -exports.isFullString = isFullString; -exports.isFunction = isFunction; -exports.isInstanceOf = isInstanceOf; -exports.isMap = isMap; -exports.isNaNValue = isNaNValue; -exports.isNegativeNumber = isNegativeNumber; -exports.isNull = isNull; -exports.isNullOrUndefined = isNullOrUndefined; -exports.isNumber = isNumber; -exports.isObject = isObject; -exports.isObjectLike = isObjectLike; -exports.isOneOf = isOneOf; -exports.isPlainObject = isPlainObject; -exports.isPositiveNumber = isPositiveNumber; -exports.isPrimitive = isPrimitive; -exports.isPromise = isPromise; -exports.isRegExp = isRegExp; -exports.isSet = isSet; -exports.isString = isString; -exports.isSymbol = isSymbol; -exports.isType = isType; -exports.isUndefined = isUndefined; -exports.isWeakMap = isWeakMap; -exports.isWeakSet = isWeakSet; diff --git a/dist/cjs/index.d.cts b/dist/cjs/index.d.ts similarity index 64% rename from dist/cjs/index.d.cts rename to dist/cjs/index.d.ts index f63e49d..ef3a1d3 100644 --- a/dist/cjs/index.d.cts +++ b/dist/cjs/index.d.ts @@ -1,7 +1,7 @@ -type AnyFunction = (...args: any[]) => any; -type AnyAsyncFunction = (...args: any[]) => Promise; -type AnyClass = new (...args: any[]) => any; -type PlainObject = Record; +export type AnyFunction = (...args: any[]) => any; +export type AnyAsyncFunction = (...args: any[]) => Promise; +export type AnyClass = new (...args: any[]) => any; +export type PlainObject = Record; type TypeGuard = (payload: A) => payload is B; /** * Returns the object type of the given payload @@ -9,42 +9,42 @@ type TypeGuard = (payload: A) => payload is B; * @param {*} payload * @returns {string} */ -declare function getType(payload: any): string; +export declare function getType(payload: any): string; /** * Returns whether the payload is undefined * * @param {*} payload * @returns {payload is undefined} */ -declare function isUndefined(payload: any): payload is undefined; +export declare function isUndefined(payload: any): payload is undefined; /** * Returns whether the payload is null * * @param {*} payload * @returns {payload is null} */ -declare function isNull(payload: any): payload is null; +export declare function isNull(payload: any): payload is null; /** * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes) * * @param {*} payload * @returns {payload is PlainObject} */ -declare function isPlainObject(payload: any): payload is PlainObject; +export declare function isPlainObject(payload: any): payload is PlainObject; /** * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes) * * @param {*} payload * @returns {payload is PlainObject} */ -declare function isObject(payload: any): payload is PlainObject; +export declare function isObject(payload: any): payload is PlainObject; /** * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes) * * @param {*} payload * @returns {payload is { [K in any]: never }} */ -declare function isEmptyObject(payload: any): payload is { +export declare function isEmptyObject(payload: any): payload is { [K in any]: never; }; /** @@ -53,14 +53,14 @@ declare function isEmptyObject(payload: any): payload is { * @param {*} payload * @returns {payload is PlainObject} */ -declare function isFullObject(payload: any): payload is PlainObject; +export declare function isFullObject(payload: any): payload is PlainObject; /** * Returns whether the payload is an any kind of object (including special classes or objects with different prototypes) * * @param {*} payload * @returns {payload is PlainObject} */ -declare function isAnyObject(payload: any): payload is PlainObject; +export declare function isAnyObject(payload: any): payload is PlainObject; /** * Returns whether the payload is an object like a type passed in < > * @@ -70,56 +70,56 @@ declare function isAnyObject(payload: any): payload is PlainObject; * @param {*} payload * @returns {payload is T} */ -declare function isObjectLike(payload: any): payload is T; +export declare function isObjectLike(payload: any): payload is T; /** * Returns whether the payload is a function (regular or async) * * @param {*} payload * @returns {payload is AnyFunction} */ -declare function isFunction(payload: any): payload is AnyFunction; +export declare function isFunction(payload: any): payload is AnyFunction; /** * Returns whether the payload is an array * * @param {any} payload * @returns {payload is any[]} */ -declare function isArray(payload: any): payload is any[]; +export declare function isArray(payload: any): payload is any[]; /** * Returns whether the payload is a an array with at least 1 item * * @param {*} payload * @returns {payload is any[]} */ -declare function isFullArray(payload: any): payload is any[]; +export declare function isFullArray(payload: any): payload is any[]; /** * Returns whether the payload is a an empty array * * @param {*} payload * @returns {payload is []} */ -declare function isEmptyArray(payload: any): payload is []; +export declare function isEmptyArray(payload: any): payload is []; /** * Returns whether the payload is a string * * @param {*} payload * @returns {payload is string} */ -declare function isString(payload: any): payload is string; +export declare function isString(payload: any): payload is string; /** * Returns whether the payload is a string, BUT returns false for '' * * @param {*} payload * @returns {payload is string} */ -declare function isFullString(payload: any): payload is string; +export declare function isFullString(payload: any): payload is string; /** * Returns whether the payload is '' * * @param {*} payload * @returns {payload is string} */ -declare function isEmptyString(payload: any): payload is string; +export declare function isEmptyString(payload: any): payload is string; /** * Returns whether the payload is a number (but not NaN) * @@ -128,126 +128,126 @@ declare function isEmptyString(payload: any): payload is string; * @param {*} payload * @returns {payload is number} */ -declare function isNumber(payload: any): payload is number; +export declare function isNumber(payload: any): payload is number; /** * Returns whether the payload is a positive number (but not 0) * * @param {*} payload * @returns {payload is number} */ -declare function isPositiveNumber(payload: any): payload is number; +export declare function isPositiveNumber(payload: any): payload is number; /** * Returns whether the payload is a negative number (but not 0) * * @param {*} payload * @returns {payload is number} */ -declare function isNegativeNumber(payload: any): payload is number; +export declare function isNegativeNumber(payload: any): payload is number; /** * Returns whether the payload is a boolean * * @param {*} payload * @returns {payload is boolean} */ -declare function isBoolean(payload: any): payload is boolean; +export declare function isBoolean(payload: any): payload is boolean; /** * Returns whether the payload is a regular expression (RegExp) * * @param {*} payload * @returns {payload is RegExp} */ -declare function isRegExp(payload: any): payload is RegExp; +export declare function isRegExp(payload: any): payload is RegExp; /** * Returns whether the payload is a Map * * @param {*} payload * @returns {payload is Map} */ -declare function isMap(payload: any): payload is Map; +export declare function isMap(payload: any): payload is Map; /** * Returns whether the payload is a WeakMap * * @param {*} payload * @returns {payload is WeakMap} */ -declare function isWeakMap(payload: any): payload is WeakMap; +export declare function isWeakMap(payload: any): payload is WeakMap; /** * Returns whether the payload is a Set * * @param {*} payload * @returns {payload is Set} */ -declare function isSet(payload: any): payload is Set; +export declare function isSet(payload: any): payload is Set; /** * Returns whether the payload is a WeakSet * * @param {*} payload * @returns {payload is WeakSet} */ -declare function isWeakSet(payload: any): payload is WeakSet; +export declare function isWeakSet(payload: any): payload is WeakSet; /** * Returns whether the payload is a Symbol * * @param {*} payload * @returns {payload is symbol} */ -declare function isSymbol(payload: any): payload is symbol; +export declare function isSymbol(payload: any): payload is symbol; /** * Returns whether the payload is a Date, and that the date is valid * * @param {*} payload * @returns {payload is Date} */ -declare function isDate(payload: any): payload is Date; +export declare function isDate(payload: any): payload is Date; /** * Returns whether the payload is a Blob * * @param {*} payload * @returns {payload is Blob} */ -declare function isBlob(payload: any): payload is Blob; +export declare function isBlob(payload: any): payload is Blob; /** * Returns whether the payload is a File * * @param {*} payload * @returns {payload is File} */ -declare function isFile(payload: any): payload is File; +export declare function isFile(payload: any): payload is File; /** * Returns whether the payload is a Promise * * @param {*} payload * @returns {payload is Promise} */ -declare function isPromise(payload: any): payload is Promise; +export declare function isPromise(payload: any): payload is Promise; /** * Returns whether the payload is an Error * * @param {*} payload * @returns {payload is Error} */ -declare function isError(payload: any): payload is Error; +export declare function isError(payload: any): payload is Error; /** * Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`) * * @param {*} payload * @returns {payload is typeof NaN} */ -declare function isNaNValue(payload: any): payload is typeof NaN; +export declare function isNaNValue(payload: any): payload is typeof NaN; /** * Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol) * * @param {*} payload * @returns {(payload is boolean | null | undefined | number | string | symbol)} */ -declare function isPrimitive(payload: any): payload is boolean | null | undefined | number | string | symbol; +export declare function isPrimitive(payload: any): payload is boolean | null | undefined | number | string | symbol; /** * Returns true whether the payload is null or undefined * * @param {*} payload * @returns {(payload is null | undefined)} */ -declare const isNullOrUndefined: TypeGuard; +export declare const isNullOrUndefined: TypeGuard; /** * A factory function that creates a function to check if the payload is one of the given types. * @example @@ -259,7 +259,7 @@ declare const isNullOrUndefined: TypeGuard; * isNullOrUndefined(undefined) // true * isNullOrUndefined(123) // false */ -declare function isOneOf(a: TypeGuard, b: TypeGuard): TypeGuard; +export declare function isOneOf(a: TypeGuard, b: TypeGuard): TypeGuard; /** * A factory function that creates a function to check if the payload is one of the given types. * @example @@ -271,7 +271,7 @@ declare function isOneOf(a: TypeGuard, b: Typ * isNullOrUndefined(undefined) // true * isNullOrUndefined(123) // false */ -declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard): TypeGuard; +export declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard): TypeGuard; /** * A factory function that creates a function to check if the payload is one of the given types. * @example @@ -283,7 +283,7 @@ declare function isOneOf(a: TypeGuard< * isNullOrUndefined(undefined) // true * isNullOrUndefined(123) // false */ -declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard): TypeGuard; +export declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard): TypeGuard; /** * A factory function that creates a function to check if the payload is one of the given types. * @example @@ -295,7 +295,7 @@ declare function isOneOf( * isNullOrUndefined(undefined) // true * isNullOrUndefined(123) // false */ -declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard, e: TypeGuard): TypeGuard; +export declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard, e: TypeGuard): TypeGuard; /** * 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!); @@ -307,7 +307,7 @@ declare function isOneOf(payload: any, type: T): payload is T; +export 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]; @@ -327,8 +327,7 @@ type GlobalClassName = { * @param value The value to recursively check * @param class_ A string or class that the value should be an instance of */ -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; - -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 }; +export declare function isInstanceOf(value: unknown, class_: T): value is T; +export declare function isInstanceOf(value: unknown, className: K): value is (typeof globalThis)[K]; +export declare function isInstanceOf(value: unknown, className: string): value is object; +export {}; diff --git a/dist/cjs/index.js b/dist/cjs/index.js new file mode 100644 index 0000000..8f91aaf --- /dev/null +++ b/dist/cjs/index.js @@ -0,0 +1,410 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.isInstanceOf = exports.isType = exports.isOneOf = exports.isNullOrUndefined = exports.isPrimitive = exports.isNaNValue = exports.isError = exports.isPromise = exports.isFile = exports.isBlob = exports.isDate = exports.isSymbol = exports.isWeakSet = exports.isSet = exports.isWeakMap = exports.isMap = exports.isRegExp = exports.isBoolean = exports.isNegativeNumber = exports.isPositiveNumber = exports.isNumber = exports.isEmptyString = exports.isFullString = exports.isString = exports.isEmptyArray = exports.isFullArray = exports.isArray = exports.isFunction = exports.isObjectLike = exports.isAnyObject = exports.isFullObject = exports.isEmptyObject = exports.isObject = exports.isPlainObject = exports.isNull = exports.isUndefined = exports.getType = void 0; +/** + * Returns the object type of the given payload + * + * @param {*} payload + * @returns {string} + */ +function getType(payload) { + return Object.prototype.toString.call(payload).slice(8, -1); +} +exports.getType = getType; +/** + * Returns whether the payload is undefined + * + * @param {*} payload + * @returns {payload is undefined} + */ +function isUndefined(payload) { + return getType(payload) === 'Undefined'; +} +exports.isUndefined = isUndefined; +/** + * Returns whether the payload is null + * + * @param {*} payload + * @returns {payload is null} + */ +function isNull(payload) { + return getType(payload) === 'Null'; +} +exports.isNull = isNull; +/** + * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes) + * + * @param {*} payload + * @returns {payload is PlainObject} + */ +function isPlainObject(payload) { + if (getType(payload) !== 'Object') + return false; + const prototype = Object.getPrototypeOf(payload); + return !!prototype && prototype.constructor === Object && prototype === Object.prototype; +} +exports.isPlainObject = isPlainObject; +/** + * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes) + * + * @param {*} payload + * @returns {payload is PlainObject} + */ +function isObject(payload) { + return isPlainObject(payload); +} +exports.isObject = isObject; +/** + * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes) + * + * @param {*} payload + * @returns {payload is { [K in any]: never }} + */ +function isEmptyObject(payload) { + return isPlainObject(payload) && Object.keys(payload).length === 0; +} +exports.isEmptyObject = isEmptyObject; +/** + * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes) + * + * @param {*} payload + * @returns {payload is PlainObject} + */ +function isFullObject(payload) { + return isPlainObject(payload) && Object.keys(payload).length > 0; +} +exports.isFullObject = isFullObject; +/** + * Returns whether the payload is an any kind of object (including special classes or objects with different prototypes) + * + * @param {*} payload + * @returns {payload is PlainObject} + */ +function isAnyObject(payload) { + return getType(payload) === 'Object'; +} +exports.isAnyObject = isAnyObject; +/** + * Returns whether the payload is an object like a type passed in < > + * + * Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop. + * + * @template T this must be passed in < > + * @param {*} payload + * @returns {payload is T} + */ +function isObjectLike(payload) { + return isAnyObject(payload); +} +exports.isObjectLike = isObjectLike; +/** + * Returns whether the payload is a function (regular or async) + * + * @param {*} payload + * @returns {payload is AnyFunction} + */ +function isFunction(payload) { + return typeof payload === 'function'; +} +exports.isFunction = isFunction; +/** + * Returns whether the payload is an array + * + * @param {any} payload + * @returns {payload is any[]} + */ +function isArray(payload) { + return getType(payload) === 'Array'; +} +exports.isArray = isArray; +/** + * Returns whether the payload is a an array with at least 1 item + * + * @param {*} payload + * @returns {payload is any[]} + */ +function isFullArray(payload) { + return isArray(payload) && payload.length > 0; +} +exports.isFullArray = isFullArray; +/** + * Returns whether the payload is a an empty array + * + * @param {*} payload + * @returns {payload is []} + */ +function isEmptyArray(payload) { + return isArray(payload) && payload.length === 0; +} +exports.isEmptyArray = isEmptyArray; +/** + * Returns whether the payload is a string + * + * @param {*} payload + * @returns {payload is string} + */ +function isString(payload) { + return getType(payload) === 'String'; +} +exports.isString = isString; +/** + * Returns whether the payload is a string, BUT returns false for '' + * + * @param {*} payload + * @returns {payload is string} + */ +function isFullString(payload) { + return isString(payload) && payload !== ''; +} +exports.isFullString = isFullString; +/** + * Returns whether the payload is '' + * + * @param {*} payload + * @returns {payload is string} + */ +function isEmptyString(payload) { + return payload === ''; +} +exports.isEmptyString = isEmptyString; +/** + * Returns whether the payload is a number (but not NaN) + * + * This will return `false` for `NaN`!! + * + * @param {*} payload + * @returns {payload is number} + */ +function isNumber(payload) { + return getType(payload) === 'Number' && !isNaN(payload); +} +exports.isNumber = isNumber; +/** + * Returns whether the payload is a positive number (but not 0) + * + * @param {*} payload + * @returns {payload is number} + */ +function isPositiveNumber(payload) { + return isNumber(payload) && payload > 0; +} +exports.isPositiveNumber = isPositiveNumber; +/** + * Returns whether the payload is a negative number (but not 0) + * + * @param {*} payload + * @returns {payload is number} + */ +function isNegativeNumber(payload) { + return isNumber(payload) && payload < 0; +} +exports.isNegativeNumber = isNegativeNumber; +/** + * Returns whether the payload is a boolean + * + * @param {*} payload + * @returns {payload is boolean} + */ +function isBoolean(payload) { + return getType(payload) === 'Boolean'; +} +exports.isBoolean = isBoolean; +/** + * Returns whether the payload is a regular expression (RegExp) + * + * @param {*} payload + * @returns {payload is RegExp} + */ +function isRegExp(payload) { + return getType(payload) === 'RegExp'; +} +exports.isRegExp = isRegExp; +/** + * Returns whether the payload is a Map + * + * @param {*} payload + * @returns {payload is Map} + */ +function isMap(payload) { + return getType(payload) === 'Map'; +} +exports.isMap = isMap; +/** + * Returns whether the payload is a WeakMap + * + * @param {*} payload + * @returns {payload is WeakMap} + */ +function isWeakMap(payload) { + return getType(payload) === 'WeakMap'; +} +exports.isWeakMap = isWeakMap; +/** + * Returns whether the payload is a Set + * + * @param {*} payload + * @returns {payload is Set} + */ +function isSet(payload) { + return getType(payload) === 'Set'; +} +exports.isSet = isSet; +/** + * Returns whether the payload is a WeakSet + * + * @param {*} payload + * @returns {payload is WeakSet} + */ +function isWeakSet(payload) { + return getType(payload) === 'WeakSet'; +} +exports.isWeakSet = isWeakSet; +/** + * Returns whether the payload is a Symbol + * + * @param {*} payload + * @returns {payload is symbol} + */ +function isSymbol(payload) { + return getType(payload) === 'Symbol'; +} +exports.isSymbol = isSymbol; +/** + * Returns whether the payload is a Date, and that the date is valid + * + * @param {*} payload + * @returns {payload is Date} + */ +function isDate(payload) { + return getType(payload) === 'Date' && !isNaN(payload); +} +exports.isDate = isDate; +/** + * Returns whether the payload is a Blob + * + * @param {*} payload + * @returns {payload is Blob} + */ +function isBlob(payload) { + return getType(payload) === 'Blob'; +} +exports.isBlob = isBlob; +/** + * Returns whether the payload is a File + * + * @param {*} payload + * @returns {payload is File} + */ +function isFile(payload) { + return getType(payload) === 'File'; +} +exports.isFile = isFile; +/** + * Returns whether the payload is a Promise + * + * @param {*} payload + * @returns {payload is Promise} + */ +function isPromise(payload) { + return getType(payload) === 'Promise'; +} +exports.isPromise = isPromise; +/** + * Returns whether the payload is an Error + * + * @param {*} payload + * @returns {payload is Error} + */ +function isError(payload) { + return getType(payload) === 'Error'; +} +exports.isError = isError; +/** + * Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`) + * + * @param {*} payload + * @returns {payload is typeof NaN} + */ +function isNaNValue(payload) { + return getType(payload) === 'Number' && isNaN(payload); +} +exports.isNaNValue = isNaNValue; +/** + * Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol) + * + * @param {*} payload + * @returns {(payload is boolean | null | undefined | number | string | symbol)} + */ +function isPrimitive(payload) { + return (isBoolean(payload) || + isNull(payload) || + isUndefined(payload) || + isNumber(payload) || + isString(payload) || + isSymbol(payload)); +} +exports.isPrimitive = isPrimitive; +/** + * Returns true whether the payload is null or undefined + * + * @param {*} payload + * @returns {(payload is null | undefined)} + */ +exports.isNullOrUndefined = isOneOf(isNull, isUndefined); +/** + * 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) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false + */ +function isOneOf(a, b, c, d, e) { + return (value) => a(value) || b(value) || (!!c && c(value)) || (!!d && d(value)) || (!!e && e(value)); +} +exports.isOneOf = isOneOf; +/** + * 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 + * + * @template T + * @param {*} payload + * @param {T} type + * @throws {TypeError} Will throw type error if type is an invalid type + * @returns {payload is T} + */ +function isType(payload, type) { + if (!(type instanceof Function)) { + throw new TypeError('Type must be a function'); + } + if (!Object.prototype.hasOwnProperty.call(type, 'prototype')) { + throw new TypeError('Type is not a class'); + } + // Classes usually have names (as functions usually have names) + const name = type.name; + return getType(payload) === name || Boolean(payload && payload.constructor === type); +} +exports.isType = isType; +function isInstanceOf(value, classOrClassName) { + if (typeof classOrClassName === 'function') { + for (let p = value; p; p = Object.getPrototypeOf(p)) { + if (isType(p, classOrClassName)) { + return true; + } + } + return false; + } + else { + for (let p = value; p; p = Object.getPrototypeOf(p)) { + if (getType(p) === classOrClassName) { + return true; + } + } + return false; + } +} +exports.isInstanceOf = isInstanceOf; diff --git a/dist/cjs/package.json b/dist/cjs/package.json new file mode 100644 index 0000000..b731bd6 --- /dev/null +++ b/dist/cjs/package.json @@ -0,0 +1 @@ +{"type": "commonjs"} diff --git a/dist/index.d.ts b/dist/index.d.ts index f63e49d..ef3a1d3 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,7 +1,7 @@ -type AnyFunction = (...args: any[]) => any; -type AnyAsyncFunction = (...args: any[]) => Promise; -type AnyClass = new (...args: any[]) => any; -type PlainObject = Record; +export type AnyFunction = (...args: any[]) => any; +export type AnyAsyncFunction = (...args: any[]) => Promise; +export type AnyClass = new (...args: any[]) => any; +export type PlainObject = Record; type TypeGuard = (payload: A) => payload is B; /** * Returns the object type of the given payload @@ -9,42 +9,42 @@ type TypeGuard = (payload: A) => payload is B; * @param {*} payload * @returns {string} */ -declare function getType(payload: any): string; +export declare function getType(payload: any): string; /** * Returns whether the payload is undefined * * @param {*} payload * @returns {payload is undefined} */ -declare function isUndefined(payload: any): payload is undefined; +export declare function isUndefined(payload: any): payload is undefined; /** * Returns whether the payload is null * * @param {*} payload * @returns {payload is null} */ -declare function isNull(payload: any): payload is null; +export declare function isNull(payload: any): payload is null; /** * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes) * * @param {*} payload * @returns {payload is PlainObject} */ -declare function isPlainObject(payload: any): payload is PlainObject; +export declare function isPlainObject(payload: any): payload is PlainObject; /** * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes) * * @param {*} payload * @returns {payload is PlainObject} */ -declare function isObject(payload: any): payload is PlainObject; +export declare function isObject(payload: any): payload is PlainObject; /** * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes) * * @param {*} payload * @returns {payload is { [K in any]: never }} */ -declare function isEmptyObject(payload: any): payload is { +export declare function isEmptyObject(payload: any): payload is { [K in any]: never; }; /** @@ -53,14 +53,14 @@ declare function isEmptyObject(payload: any): payload is { * @param {*} payload * @returns {payload is PlainObject} */ -declare function isFullObject(payload: any): payload is PlainObject; +export declare function isFullObject(payload: any): payload is PlainObject; /** * Returns whether the payload is an any kind of object (including special classes or objects with different prototypes) * * @param {*} payload * @returns {payload is PlainObject} */ -declare function isAnyObject(payload: any): payload is PlainObject; +export declare function isAnyObject(payload: any): payload is PlainObject; /** * Returns whether the payload is an object like a type passed in < > * @@ -70,56 +70,56 @@ declare function isAnyObject(payload: any): payload is PlainObject; * @param {*} payload * @returns {payload is T} */ -declare function isObjectLike(payload: any): payload is T; +export declare function isObjectLike(payload: any): payload is T; /** * Returns whether the payload is a function (regular or async) * * @param {*} payload * @returns {payload is AnyFunction} */ -declare function isFunction(payload: any): payload is AnyFunction; +export declare function isFunction(payload: any): payload is AnyFunction; /** * Returns whether the payload is an array * * @param {any} payload * @returns {payload is any[]} */ -declare function isArray(payload: any): payload is any[]; +export declare function isArray(payload: any): payload is any[]; /** * Returns whether the payload is a an array with at least 1 item * * @param {*} payload * @returns {payload is any[]} */ -declare function isFullArray(payload: any): payload is any[]; +export declare function isFullArray(payload: any): payload is any[]; /** * Returns whether the payload is a an empty array * * @param {*} payload * @returns {payload is []} */ -declare function isEmptyArray(payload: any): payload is []; +export declare function isEmptyArray(payload: any): payload is []; /** * Returns whether the payload is a string * * @param {*} payload * @returns {payload is string} */ -declare function isString(payload: any): payload is string; +export declare function isString(payload: any): payload is string; /** * Returns whether the payload is a string, BUT returns false for '' * * @param {*} payload * @returns {payload is string} */ -declare function isFullString(payload: any): payload is string; +export declare function isFullString(payload: any): payload is string; /** * Returns whether the payload is '' * * @param {*} payload * @returns {payload is string} */ -declare function isEmptyString(payload: any): payload is string; +export declare function isEmptyString(payload: any): payload is string; /** * Returns whether the payload is a number (but not NaN) * @@ -128,126 +128,126 @@ declare function isEmptyString(payload: any): payload is string; * @param {*} payload * @returns {payload is number} */ -declare function isNumber(payload: any): payload is number; +export declare function isNumber(payload: any): payload is number; /** * Returns whether the payload is a positive number (but not 0) * * @param {*} payload * @returns {payload is number} */ -declare function isPositiveNumber(payload: any): payload is number; +export declare function isPositiveNumber(payload: any): payload is number; /** * Returns whether the payload is a negative number (but not 0) * * @param {*} payload * @returns {payload is number} */ -declare function isNegativeNumber(payload: any): payload is number; +export declare function isNegativeNumber(payload: any): payload is number; /** * Returns whether the payload is a boolean * * @param {*} payload * @returns {payload is boolean} */ -declare function isBoolean(payload: any): payload is boolean; +export declare function isBoolean(payload: any): payload is boolean; /** * Returns whether the payload is a regular expression (RegExp) * * @param {*} payload * @returns {payload is RegExp} */ -declare function isRegExp(payload: any): payload is RegExp; +export declare function isRegExp(payload: any): payload is RegExp; /** * Returns whether the payload is a Map * * @param {*} payload * @returns {payload is Map} */ -declare function isMap(payload: any): payload is Map; +export declare function isMap(payload: any): payload is Map; /** * Returns whether the payload is a WeakMap * * @param {*} payload * @returns {payload is WeakMap} */ -declare function isWeakMap(payload: any): payload is WeakMap; +export declare function isWeakMap(payload: any): payload is WeakMap; /** * Returns whether the payload is a Set * * @param {*} payload * @returns {payload is Set} */ -declare function isSet(payload: any): payload is Set; +export declare function isSet(payload: any): payload is Set; /** * Returns whether the payload is a WeakSet * * @param {*} payload * @returns {payload is WeakSet} */ -declare function isWeakSet(payload: any): payload is WeakSet; +export declare function isWeakSet(payload: any): payload is WeakSet; /** * Returns whether the payload is a Symbol * * @param {*} payload * @returns {payload is symbol} */ -declare function isSymbol(payload: any): payload is symbol; +export declare function isSymbol(payload: any): payload is symbol; /** * Returns whether the payload is a Date, and that the date is valid * * @param {*} payload * @returns {payload is Date} */ -declare function isDate(payload: any): payload is Date; +export declare function isDate(payload: any): payload is Date; /** * Returns whether the payload is a Blob * * @param {*} payload * @returns {payload is Blob} */ -declare function isBlob(payload: any): payload is Blob; +export declare function isBlob(payload: any): payload is Blob; /** * Returns whether the payload is a File * * @param {*} payload * @returns {payload is File} */ -declare function isFile(payload: any): payload is File; +export declare function isFile(payload: any): payload is File; /** * Returns whether the payload is a Promise * * @param {*} payload * @returns {payload is Promise} */ -declare function isPromise(payload: any): payload is Promise; +export declare function isPromise(payload: any): payload is Promise; /** * Returns whether the payload is an Error * * @param {*} payload * @returns {payload is Error} */ -declare function isError(payload: any): payload is Error; +export declare function isError(payload: any): payload is Error; /** * Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`) * * @param {*} payload * @returns {payload is typeof NaN} */ -declare function isNaNValue(payload: any): payload is typeof NaN; +export declare function isNaNValue(payload: any): payload is typeof NaN; /** * Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol) * * @param {*} payload * @returns {(payload is boolean | null | undefined | number | string | symbol)} */ -declare function isPrimitive(payload: any): payload is boolean | null | undefined | number | string | symbol; +export declare function isPrimitive(payload: any): payload is boolean | null | undefined | number | string | symbol; /** * Returns true whether the payload is null or undefined * * @param {*} payload * @returns {(payload is null | undefined)} */ -declare const isNullOrUndefined: TypeGuard; +export declare const isNullOrUndefined: TypeGuard; /** * A factory function that creates a function to check if the payload is one of the given types. * @example @@ -259,7 +259,7 @@ declare const isNullOrUndefined: TypeGuard; * isNullOrUndefined(undefined) // true * isNullOrUndefined(123) // false */ -declare function isOneOf(a: TypeGuard, b: TypeGuard): TypeGuard; +export declare function isOneOf(a: TypeGuard, b: TypeGuard): TypeGuard; /** * A factory function that creates a function to check if the payload is one of the given types. * @example @@ -271,7 +271,7 @@ declare function isOneOf(a: TypeGuard, b: Typ * isNullOrUndefined(undefined) // true * isNullOrUndefined(123) // false */ -declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard): TypeGuard; +export declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard): TypeGuard; /** * A factory function that creates a function to check if the payload is one of the given types. * @example @@ -283,7 +283,7 @@ declare function isOneOf(a: TypeGuard< * isNullOrUndefined(undefined) // true * isNullOrUndefined(123) // false */ -declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard): TypeGuard; +export declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard): TypeGuard; /** * A factory function that creates a function to check if the payload is one of the given types. * @example @@ -295,7 +295,7 @@ declare function isOneOf( * isNullOrUndefined(undefined) // true * isNullOrUndefined(123) // false */ -declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard, e: TypeGuard): TypeGuard; +export declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard, e: TypeGuard): TypeGuard; /** * 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!); @@ -307,7 +307,7 @@ declare function isOneOf(payload: any, type: T): payload is T; +export 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]; @@ -327,8 +327,7 @@ type GlobalClassName = { * @param value The value to recursively check * @param class_ A string or class that the value should be an instance of */ -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; - -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 }; +export declare function isInstanceOf(value: unknown, class_: T): value is T; +export declare function isInstanceOf(value: unknown, className: K): value is (typeof globalThis)[K]; +export declare function isInstanceOf(value: unknown, className: string): value is object; +export {}; diff --git a/dist/index.js b/dist/index.js index fff9275..d4d67f5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,135 +1,371 @@ -function getType(payload) { - return Object.prototype.toString.call(payload).slice(8, -1); -} -function isUndefined(payload) { - return getType(payload) === "Undefined"; -} -function isNull(payload) { - return getType(payload) === "Null"; -} -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 isFunction(payload) { - return typeof payload === "function"; -} -function isArray(payload) { - return getType(payload) === "Array"; -} -function isFullArray(payload) { - return isArray(payload) && payload.length > 0; -} -function isEmptyArray(payload) { - return isArray(payload) && 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 isType(payload, type) { - if (!(type instanceof Function)) { - throw new TypeError("Type must be a function"); - } - if (!Object.prototype.hasOwnProperty.call(type, "prototype")) { - throw new TypeError("Type is not a class"); - } - 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)) { - if (isType(p, classOrClassName)) { - return true; - } +/** + * Returns the object type of the given payload + * + * @param {*} payload + * @returns {string} + */ +export function getType(payload) { + return Object.prototype.toString.call(payload).slice(8, -1); +} +/** + * Returns whether the payload is undefined + * + * @param {*} payload + * @returns {payload is undefined} + */ +export function isUndefined(payload) { + return getType(payload) === 'Undefined'; +} +/** + * Returns whether the payload is null + * + * @param {*} payload + * @returns {payload is null} + */ +export function isNull(payload) { + return getType(payload) === 'Null'; +} +/** + * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes) + * + * @param {*} payload + * @returns {payload is PlainObject} + */ +export function isPlainObject(payload) { + if (getType(payload) !== 'Object') + return false; + const prototype = Object.getPrototypeOf(payload); + return !!prototype && prototype.constructor === Object && prototype === Object.prototype; +} +/** + * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes) + * + * @param {*} payload + * @returns {payload is PlainObject} + */ +export function isObject(payload) { + return isPlainObject(payload); +} +/** + * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes) + * + * @param {*} payload + * @returns {payload is { [K in any]: never }} + */ +export function isEmptyObject(payload) { + return isPlainObject(payload) && Object.keys(payload).length === 0; +} +/** + * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes) + * + * @param {*} payload + * @returns {payload is PlainObject} + */ +export function isFullObject(payload) { + return isPlainObject(payload) && Object.keys(payload).length > 0; +} +/** + * Returns whether the payload is an any kind of object (including special classes or objects with different prototypes) + * + * @param {*} payload + * @returns {payload is PlainObject} + */ +export function isAnyObject(payload) { + return getType(payload) === 'Object'; +} +/** + * Returns whether the payload is an object like a type passed in < > + * + * Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop. + * + * @template T this must be passed in < > + * @param {*} payload + * @returns {payload is T} + */ +export function isObjectLike(payload) { + return isAnyObject(payload); +} +/** + * Returns whether the payload is a function (regular or async) + * + * @param {*} payload + * @returns {payload is AnyFunction} + */ +export function isFunction(payload) { + return typeof payload === 'function'; +} +/** + * Returns whether the payload is an array + * + * @param {any} payload + * @returns {payload is any[]} + */ +export function isArray(payload) { + return getType(payload) === 'Array'; +} +/** + * Returns whether the payload is a an array with at least 1 item + * + * @param {*} payload + * @returns {payload is any[]} + */ +export function isFullArray(payload) { + return isArray(payload) && payload.length > 0; +} +/** + * Returns whether the payload is a an empty array + * + * @param {*} payload + * @returns {payload is []} + */ +export function isEmptyArray(payload) { + return isArray(payload) && payload.length === 0; +} +/** + * Returns whether the payload is a string + * + * @param {*} payload + * @returns {payload is string} + */ +export function isString(payload) { + return getType(payload) === 'String'; +} +/** + * Returns whether the payload is a string, BUT returns false for '' + * + * @param {*} payload + * @returns {payload is string} + */ +export function isFullString(payload) { + return isString(payload) && payload !== ''; +} +/** + * Returns whether the payload is '' + * + * @param {*} payload + * @returns {payload is string} + */ +export function isEmptyString(payload) { + return payload === ''; +} +/** + * Returns whether the payload is a number (but not NaN) + * + * This will return `false` for `NaN`!! + * + * @param {*} payload + * @returns {payload is number} + */ +export function isNumber(payload) { + return getType(payload) === 'Number' && !isNaN(payload); +} +/** + * Returns whether the payload is a positive number (but not 0) + * + * @param {*} payload + * @returns {payload is number} + */ +export function isPositiveNumber(payload) { + return isNumber(payload) && payload > 0; +} +/** + * Returns whether the payload is a negative number (but not 0) + * + * @param {*} payload + * @returns {payload is number} + */ +export function isNegativeNumber(payload) { + return isNumber(payload) && payload < 0; +} +/** + * Returns whether the payload is a boolean + * + * @param {*} payload + * @returns {payload is boolean} + */ +export function isBoolean(payload) { + return getType(payload) === 'Boolean'; +} +/** + * Returns whether the payload is a regular expression (RegExp) + * + * @param {*} payload + * @returns {payload is RegExp} + */ +export function isRegExp(payload) { + return getType(payload) === 'RegExp'; +} +/** + * Returns whether the payload is a Map + * + * @param {*} payload + * @returns {payload is Map} + */ +export function isMap(payload) { + return getType(payload) === 'Map'; +} +/** + * Returns whether the payload is a WeakMap + * + * @param {*} payload + * @returns {payload is WeakMap} + */ +export function isWeakMap(payload) { + return getType(payload) === 'WeakMap'; +} +/** + * Returns whether the payload is a Set + * + * @param {*} payload + * @returns {payload is Set} + */ +export function isSet(payload) { + return getType(payload) === 'Set'; +} +/** + * Returns whether the payload is a WeakSet + * + * @param {*} payload + * @returns {payload is WeakSet} + */ +export function isWeakSet(payload) { + return getType(payload) === 'WeakSet'; +} +/** + * Returns whether the payload is a Symbol + * + * @param {*} payload + * @returns {payload is symbol} + */ +export function isSymbol(payload) { + return getType(payload) === 'Symbol'; +} +/** + * Returns whether the payload is a Date, and that the date is valid + * + * @param {*} payload + * @returns {payload is Date} + */ +export function isDate(payload) { + return getType(payload) === 'Date' && !isNaN(payload); +} +/** + * Returns whether the payload is a Blob + * + * @param {*} payload + * @returns {payload is Blob} + */ +export function isBlob(payload) { + return getType(payload) === 'Blob'; +} +/** + * Returns whether the payload is a File + * + * @param {*} payload + * @returns {payload is File} + */ +export function isFile(payload) { + return getType(payload) === 'File'; +} +/** + * Returns whether the payload is a Promise + * + * @param {*} payload + * @returns {payload is Promise} + */ +export function isPromise(payload) { + return getType(payload) === 'Promise'; +} +/** + * Returns whether the payload is an Error + * + * @param {*} payload + * @returns {payload is Error} + */ +export function isError(payload) { + return getType(payload) === 'Error'; +} +/** + * Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`) + * + * @param {*} payload + * @returns {payload is typeof NaN} + */ +export function isNaNValue(payload) { + return getType(payload) === 'Number' && isNaN(payload); +} +/** + * Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol) + * + * @param {*} payload + * @returns {(payload is boolean | null | undefined | number | string | symbol)} + */ +export function isPrimitive(payload) { + return (isBoolean(payload) || + isNull(payload) || + isUndefined(payload) || + isNumber(payload) || + isString(payload) || + isSymbol(payload)); +} +/** + * Returns true whether the payload is null or undefined + * + * @param {*} payload + * @returns {(payload is null | undefined)} + */ +export const isNullOrUndefined = isOneOf(isNull, isUndefined); +/** + * 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) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false + */ +export function isOneOf(a, b, c, d, e) { + return (value) => a(value) || b(value) || (!!c && c(value)) || (!!d && d(value)) || (!!e && e(value)); +} +/** + * 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 + * + * @template T + * @param {*} payload + * @param {T} type + * @throws {TypeError} Will throw type error if type is an invalid type + * @returns {payload is T} + */ +export function isType(payload, type) { + if (!(type instanceof Function)) { + throw new TypeError('Type must be a function'); + } + if (!Object.prototype.hasOwnProperty.call(type, 'prototype')) { + throw new TypeError('Type is not a class'); + } + // Classes usually have names (as functions usually have names) + const name = type.name; + return getType(payload) === name || Boolean(payload && payload.constructor === type); +} +export function isInstanceOf(value, classOrClassName) { + if (typeof classOrClassName === 'function') { + for (let p = value; p; p = Object.getPrototypeOf(p)) { + if (isType(p, classOrClassName)) { + return true; + } + } + return false; } - return false; - } else { - for (let p = value; p; p = Object.getPrototypeOf(p)) { - if (getType(p) === classOrClassName) { - return true; - } + else { + for (let p = value; p; p = Object.getPrototypeOf(p)) { + if (getType(p) === classOrClassName) { + return true; + } + } + return false; } - return false; - } } - -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 };