From dee66b0c5930d9c40786a11f346ed61d9c1ac8e0 Mon Sep 17 00:00:00 2001 From: Luca Ban Date: Tue, 23 May 2023 11:20:53 +0900 Subject: [PATCH] fix: exports --- dist/cjs/index.cjs | 384 ++++++++------------------------------- dist/cjs/index.d.cts | 89 ++++----- dist/index.d.ts | 89 ++++----- dist/index.js | 384 ++++++++------------------------------- package-lock.json | 278 ++++++++++++---------------- package.json | 9 +- rollup.config.js | 45 +++++ scripts/rollup.config.js | 34 ---- 8 files changed, 410 insertions(+), 902 deletions(-) create mode 100644 rollup.config.js delete mode 100644 scripts/rollup.config.js diff --git a/dist/cjs/index.cjs b/dist/cjs/index.cjs index d660d22..73b80e3 100644 --- a/dist/cjs/index.cjs +++ b/dist/cjs/index.cjs @@ -1,346 +1,120 @@ 'use strict'; -/** - * Returns the object type of the given payload - * - * @param {*} payload - * @returns {string} - */ function getType(payload) { - return Object.prototype.toString.call(payload).slice(8, -1); -} -/** - * Returns whether the payload is undefined - * - * @param {*} payload - * @returns {payload is undefined} - */ + return Object.prototype.toString.call(payload).slice(8, -1); +} function isUndefined(payload) { - return getType(payload) === 'Undefined'; -} -/** - * Returns whether the payload is null - * - * @param {*} payload - * @returns {payload is null} - */ + return getType(payload) === "Undefined"; +} 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} - */ + return getType(payload) === "Null"; +} function isPlainObject(payload) { - if (getType(payload) !== 'Object') - return false; - const prototype = Object.getPrototypeOf(payload); - return 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} - */ + if (getType(payload) !== "Object") + return false; + const prototype = Object.getPrototypeOf(payload); + return prototype.constructor === Object && prototype === Object.prototype; +} 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 }} - */ + return isPlainObject(payload); +} 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} - */ + return isPlainObject(payload) && Object.keys(payload).length === 0; +} 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} - */ + return isPlainObject(payload) && Object.keys(payload).length > 0; +} 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} - */ + return getType(payload) === "Object"; +} function isObjectLike(payload) { - return isAnyObject(payload); -} -/** - * Returns whether the payload is a function (regular or async) - * - * @param {*} payload - * @returns {payload is AnyFunction} - */ + return isAnyObject(payload); +} function isFunction(payload) { - return typeof payload === 'function'; -} -/** - * Returns whether the payload is an array - * - * @param {any} payload - * @returns {payload is any[]} - */ + return typeof payload === "function"; +} 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[]} - */ + return getType(payload) === "Array"; +} function isFullArray(payload) { - return isArray(payload) && payload.length > 0; -} -/** - * Returns whether the payload is a an empty array - * - * @param {*} payload - * @returns {payload is []} - */ + return isArray(payload) && payload.length > 0; +} function isEmptyArray(payload) { - return isArray(payload) && payload.length === 0; -} -/** - * Returns whether the payload is a string - * - * @param {*} payload - * @returns {payload is string} - */ + return isArray(payload) && payload.length === 0; +} function isString(payload) { - return getType(payload) === 'String'; -} -/** - * Returns whether the payload is a string, BUT returns false for '' - * - * @param {*} payload - * @returns {payload is string} - */ + return getType(payload) === "String"; +} function isFullString(payload) { - return isString(payload) && payload !== ''; -} -/** - * Returns whether the payload is '' - * - * @param {*} payload - * @returns {payload is string} - */ + return isString(payload) && payload !== ""; +} 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} - */ + return payload === ""; +} 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} - */ + return getType(payload) === "Number" && !isNaN(payload); +} 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} - */ + return isNumber(payload) && payload > 0; +} function isNegativeNumber(payload) { - return isNumber(payload) && payload < 0; -} -/** - * Returns whether the payload is a boolean - * - * @param {*} payload - * @returns {payload is boolean} - */ + return isNumber(payload) && payload < 0; +} function isBoolean(payload) { - return getType(payload) === 'Boolean'; -} -/** - * Returns whether the payload is a regular expression (RegExp) - * - * @param {*} payload - * @returns {payload is RegExp} - */ + return getType(payload) === "Boolean"; +} function isRegExp(payload) { - return getType(payload) === 'RegExp'; -} -/** - * Returns whether the payload is a Map - * - * @param {*} payload - * @returns {payload is Map} - */ + return getType(payload) === "RegExp"; +} function isMap(payload) { - return getType(payload) === 'Map'; -} -/** - * Returns whether the payload is a WeakMap - * - * @param {*} payload - * @returns {payload is WeakMap} - */ + return getType(payload) === "Map"; +} function isWeakMap(payload) { - return getType(payload) === 'WeakMap'; -} -/** - * Returns whether the payload is a Set - * - * @param {*} payload - * @returns {payload is Set} - */ + return getType(payload) === "WeakMap"; +} function isSet(payload) { - return getType(payload) === 'Set'; -} -/** - * Returns whether the payload is a WeakSet - * - * @param {*} payload - * @returns {payload is WeakSet} - */ + return getType(payload) === "Set"; +} function isWeakSet(payload) { - return getType(payload) === 'WeakSet'; -} -/** - * Returns whether the payload is a Symbol - * - * @param {*} payload - * @returns {payload is symbol} - */ + return getType(payload) === "WeakSet"; +} 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} - */ + return getType(payload) === "Symbol"; +} function isDate(payload) { - return getType(payload) === 'Date' && !isNaN(payload); -} -/** - * Returns whether the payload is a Blob - * - * @param {*} payload - * @returns {payload is Blob} - */ + return getType(payload) === "Date" && !isNaN(payload); +} function isBlob(payload) { - return getType(payload) === 'Blob'; -} -/** - * Returns whether the payload is a File - * - * @param {*} payload - * @returns {payload is File} - */ + return getType(payload) === "Blob"; +} function isFile(payload) { - return getType(payload) === 'File'; -} -/** - * Returns whether the payload is a Promise - * - * @param {*} payload - * @returns {payload is Promise} - */ + return getType(payload) === "File"; +} function isPromise(payload) { - return getType(payload) === 'Promise'; -} -/** - * Returns whether the payload is an Error - * - * @param {*} payload - * @returns {payload is Error} - */ + return getType(payload) === "Promise"; +} 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} - */ + return getType(payload) === "Error"; +} 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)} - */ + return getType(payload) === "Number" && isNaN(payload); +} 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)} - */ + 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)); -} -/** - * 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} - */ + 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'); - } - // Classes usually have names (as functions usually have names) - const name = type.name; - return getType(payload) === name || Boolean(payload && payload.constructor === 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); } exports.getType = getType; diff --git a/dist/cjs/index.d.cts b/dist/cjs/index.d.cts index 8ae1153..92cb699 100644 --- a/dist/cjs/index.d.cts +++ b/dist/cjs/index.d.cts @@ -1,7 +1,7 @@ -export type AnyFunction = (...args: any[]) => any; -export type AnyAsyncFunction = (...args: any[]) => Promise; -export type AnyClass = new (...args: any[]) => any; -export type PlainObject = Record; +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 @@ -9,42 +9,42 @@ type TypeGuard = (payload: A) => payload is B; * @param {*} payload * @returns {string} */ -export declare function getType(payload: any): string; +declare function getType(payload: any): string; /** * Returns whether the payload is undefined * * @param {*} payload * @returns {payload is undefined} */ -export declare function isUndefined(payload: any): payload is undefined; +declare function isUndefined(payload: any): payload is undefined; /** * Returns whether the payload is null * * @param {*} payload * @returns {payload is null} */ -export declare function isNull(payload: any): payload is null; +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} */ -export declare function isPlainObject(payload: any): payload is PlainObject; +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} */ -export declare function isObject(payload: any): payload is PlainObject; +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 }} */ -export declare function isEmptyObject(payload: any): payload is { +declare function isEmptyObject(payload: any): payload is { [K in any]: never; }; /** @@ -53,14 +53,14 @@ export declare function isEmptyObject(payload: any): payload is { * @param {*} payload * @returns {payload is PlainObject} */ -export declare function isFullObject(payload: any): payload is PlainObject; +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} */ -export declare function isAnyObject(payload: any): payload is PlainObject; +declare function isAnyObject(payload: any): payload is PlainObject; /** * Returns whether the payload is an object like a type passed in < > * @@ -70,56 +70,56 @@ export declare function isAnyObject(payload: any): payload is PlainObject; * @param {*} payload * @returns {payload is T} */ -export declare function isObjectLike(payload: any): payload is T; +declare function isObjectLike(payload: any): payload is T; /** * Returns whether the payload is a function (regular or async) * * @param {*} payload * @returns {payload is AnyFunction} */ -export declare function isFunction(payload: any): payload is AnyFunction; +declare function isFunction(payload: any): payload is AnyFunction; /** * Returns whether the payload is an array * * @param {any} payload * @returns {payload is any[]} */ -export declare function isArray(payload: any): payload is any[]; +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[]} */ -export declare function isFullArray(payload: any): payload is any[]; +declare function isFullArray(payload: any): payload is any[]; /** * Returns whether the payload is a an empty array * * @param {*} payload * @returns {payload is []} */ -export declare function isEmptyArray(payload: any): payload is []; +declare function isEmptyArray(payload: any): payload is []; /** * Returns whether the payload is a string * * @param {*} payload * @returns {payload is string} */ -export declare function isString(payload: any): payload is string; +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} */ -export declare function isFullString(payload: any): payload is string; +declare function isFullString(payload: any): payload is string; /** * Returns whether the payload is '' * * @param {*} payload * @returns {payload is string} */ -export declare function isEmptyString(payload: any): payload is string; +declare function isEmptyString(payload: any): payload is string; /** * Returns whether the payload is a number (but not NaN) * @@ -128,130 +128,130 @@ export declare function isEmptyString(payload: any): payload is string; * @param {*} payload * @returns {payload is number} */ -export declare function isNumber(payload: any): payload is number; +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} */ -export declare function isPositiveNumber(payload: any): payload is number; +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} */ -export declare function isNegativeNumber(payload: any): payload is number; +declare function isNegativeNumber(payload: any): payload is number; /** * Returns whether the payload is a boolean * * @param {*} payload * @returns {payload is boolean} */ -export declare function isBoolean(payload: any): payload is boolean; +declare function isBoolean(payload: any): payload is boolean; /** * Returns whether the payload is a regular expression (RegExp) * * @param {*} payload * @returns {payload is RegExp} */ -export declare function isRegExp(payload: any): payload is RegExp; +declare function isRegExp(payload: any): payload is RegExp; /** * Returns whether the payload is a Map * * @param {*} payload * @returns {payload is Map} */ -export declare function isMap(payload: any): payload is Map; +declare function isMap(payload: any): payload is Map; /** * Returns whether the payload is a WeakMap * * @param {*} payload * @returns {payload is WeakMap} */ -export declare function isWeakMap(payload: any): payload is WeakMap; +declare function isWeakMap(payload: any): payload is WeakMap; /** * Returns whether the payload is a Set * * @param {*} payload * @returns {payload is Set} */ -export declare function isSet(payload: any): payload is Set; +declare function isSet(payload: any): payload is Set; /** * Returns whether the payload is a WeakSet * * @param {*} payload * @returns {payload is WeakSet} */ -export declare function isWeakSet(payload: any): payload is WeakSet; +declare function isWeakSet(payload: any): payload is WeakSet; /** * Returns whether the payload is a Symbol * * @param {*} payload * @returns {payload is symbol} */ -export declare function isSymbol(payload: any): payload is symbol; +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} */ -export declare function isDate(payload: any): payload is Date; +declare function isDate(payload: any): payload is Date; /** * Returns whether the payload is a Blob * * @param {*} payload * @returns {payload is Blob} */ -export declare function isBlob(payload: any): payload is Blob; +declare function isBlob(payload: any): payload is Blob; /** * Returns whether the payload is a File * * @param {*} payload * @returns {payload is File} */ -export declare function isFile(payload: any): payload is File; +declare function isFile(payload: any): payload is File; /** * Returns whether the payload is a Promise * * @param {*} payload * @returns {payload is Promise} */ -export declare function isPromise(payload: any): payload is Promise; +declare function isPromise(payload: any): payload is Promise; /** * Returns whether the payload is an Error * * @param {*} payload * @returns {payload is Error} */ -export declare function isError(payload: any): payload is Error; +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} */ -export declare function isNaNValue(payload: any): payload is typeof NaN; +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)} */ -export declare function isPrimitive(payload: any): 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 * * @param {*} payload * @returns {(payload is null | undefined)} */ -export declare const isNullOrUndefined: TypeGuard; -export declare function isOneOf(a: TypeGuard, b: TypeGuard): TypeGuard; -export declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard): TypeGuard; -export declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard): TypeGuard; -export declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard, e: TypeGuard): TypeGuard; +declare const isNullOrUndefined: TypeGuard; +declare function isOneOf(a: TypeGuard, b: TypeGuard): TypeGuard; +declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard): TypeGuard; +declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard): TypeGuard; +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!); @@ -263,5 +263,6 @@ export declare function isOneOf(payload: any, type: T): payload is T; -export {}; +declare function isType(payload: any, type: T): payload is T; + +export { AnyAsyncFunction, AnyClass, AnyFunction, PlainObject, getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullObject, isFullString, isFunction, 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 8ae1153..92cb699 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,7 +1,7 @@ -export type AnyFunction = (...args: any[]) => any; -export type AnyAsyncFunction = (...args: any[]) => Promise; -export type AnyClass = new (...args: any[]) => any; -export type PlainObject = Record; +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 @@ -9,42 +9,42 @@ type TypeGuard = (payload: A) => payload is B; * @param {*} payload * @returns {string} */ -export declare function getType(payload: any): string; +declare function getType(payload: any): string; /** * Returns whether the payload is undefined * * @param {*} payload * @returns {payload is undefined} */ -export declare function isUndefined(payload: any): payload is undefined; +declare function isUndefined(payload: any): payload is undefined; /** * Returns whether the payload is null * * @param {*} payload * @returns {payload is null} */ -export declare function isNull(payload: any): payload is null; +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} */ -export declare function isPlainObject(payload: any): payload is PlainObject; +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} */ -export declare function isObject(payload: any): payload is PlainObject; +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 }} */ -export declare function isEmptyObject(payload: any): payload is { +declare function isEmptyObject(payload: any): payload is { [K in any]: never; }; /** @@ -53,14 +53,14 @@ export declare function isEmptyObject(payload: any): payload is { * @param {*} payload * @returns {payload is PlainObject} */ -export declare function isFullObject(payload: any): payload is PlainObject; +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} */ -export declare function isAnyObject(payload: any): payload is PlainObject; +declare function isAnyObject(payload: any): payload is PlainObject; /** * Returns whether the payload is an object like a type passed in < > * @@ -70,56 +70,56 @@ export declare function isAnyObject(payload: any): payload is PlainObject; * @param {*} payload * @returns {payload is T} */ -export declare function isObjectLike(payload: any): payload is T; +declare function isObjectLike(payload: any): payload is T; /** * Returns whether the payload is a function (regular or async) * * @param {*} payload * @returns {payload is AnyFunction} */ -export declare function isFunction(payload: any): payload is AnyFunction; +declare function isFunction(payload: any): payload is AnyFunction; /** * Returns whether the payload is an array * * @param {any} payload * @returns {payload is any[]} */ -export declare function isArray(payload: any): payload is any[]; +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[]} */ -export declare function isFullArray(payload: any): payload is any[]; +declare function isFullArray(payload: any): payload is any[]; /** * Returns whether the payload is a an empty array * * @param {*} payload * @returns {payload is []} */ -export declare function isEmptyArray(payload: any): payload is []; +declare function isEmptyArray(payload: any): payload is []; /** * Returns whether the payload is a string * * @param {*} payload * @returns {payload is string} */ -export declare function isString(payload: any): payload is string; +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} */ -export declare function isFullString(payload: any): payload is string; +declare function isFullString(payload: any): payload is string; /** * Returns whether the payload is '' * * @param {*} payload * @returns {payload is string} */ -export declare function isEmptyString(payload: any): payload is string; +declare function isEmptyString(payload: any): payload is string; /** * Returns whether the payload is a number (but not NaN) * @@ -128,130 +128,130 @@ export declare function isEmptyString(payload: any): payload is string; * @param {*} payload * @returns {payload is number} */ -export declare function isNumber(payload: any): payload is number; +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} */ -export declare function isPositiveNumber(payload: any): payload is number; +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} */ -export declare function isNegativeNumber(payload: any): payload is number; +declare function isNegativeNumber(payload: any): payload is number; /** * Returns whether the payload is a boolean * * @param {*} payload * @returns {payload is boolean} */ -export declare function isBoolean(payload: any): payload is boolean; +declare function isBoolean(payload: any): payload is boolean; /** * Returns whether the payload is a regular expression (RegExp) * * @param {*} payload * @returns {payload is RegExp} */ -export declare function isRegExp(payload: any): payload is RegExp; +declare function isRegExp(payload: any): payload is RegExp; /** * Returns whether the payload is a Map * * @param {*} payload * @returns {payload is Map} */ -export declare function isMap(payload: any): payload is Map; +declare function isMap(payload: any): payload is Map; /** * Returns whether the payload is a WeakMap * * @param {*} payload * @returns {payload is WeakMap} */ -export declare function isWeakMap(payload: any): payload is WeakMap; +declare function isWeakMap(payload: any): payload is WeakMap; /** * Returns whether the payload is a Set * * @param {*} payload * @returns {payload is Set} */ -export declare function isSet(payload: any): payload is Set; +declare function isSet(payload: any): payload is Set; /** * Returns whether the payload is a WeakSet * * @param {*} payload * @returns {payload is WeakSet} */ -export declare function isWeakSet(payload: any): payload is WeakSet; +declare function isWeakSet(payload: any): payload is WeakSet; /** * Returns whether the payload is a Symbol * * @param {*} payload * @returns {payload is symbol} */ -export declare function isSymbol(payload: any): payload is symbol; +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} */ -export declare function isDate(payload: any): payload is Date; +declare function isDate(payload: any): payload is Date; /** * Returns whether the payload is a Blob * * @param {*} payload * @returns {payload is Blob} */ -export declare function isBlob(payload: any): payload is Blob; +declare function isBlob(payload: any): payload is Blob; /** * Returns whether the payload is a File * * @param {*} payload * @returns {payload is File} */ -export declare function isFile(payload: any): payload is File; +declare function isFile(payload: any): payload is File; /** * Returns whether the payload is a Promise * * @param {*} payload * @returns {payload is Promise} */ -export declare function isPromise(payload: any): payload is Promise; +declare function isPromise(payload: any): payload is Promise; /** * Returns whether the payload is an Error * * @param {*} payload * @returns {payload is Error} */ -export declare function isError(payload: any): payload is Error; +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} */ -export declare function isNaNValue(payload: any): payload is typeof NaN; +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)} */ -export declare function isPrimitive(payload: any): 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 * * @param {*} payload * @returns {(payload is null | undefined)} */ -export declare const isNullOrUndefined: TypeGuard; -export declare function isOneOf(a: TypeGuard, b: TypeGuard): TypeGuard; -export declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard): TypeGuard; -export declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard): TypeGuard; -export declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard, e: TypeGuard): TypeGuard; +declare const isNullOrUndefined: TypeGuard; +declare function isOneOf(a: TypeGuard, b: TypeGuard): TypeGuard; +declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard): TypeGuard; +declare function isOneOf(a: TypeGuard, b: TypeGuard, c: TypeGuard, d: TypeGuard): TypeGuard; +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!); @@ -263,5 +263,6 @@ export declare function isOneOf(payload: any, type: T): payload is T; -export {}; +declare function isType(payload: any, type: T): payload is T; + +export { AnyAsyncFunction, AnyClass, AnyFunction, PlainObject, getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullObject, isFullString, isFunction, 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 3dbe65e..4f3379b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,344 +1,118 @@ -/** - * Returns the object type of the given payload - * - * @param {*} payload - * @returns {string} - */ function getType(payload) { - return Object.prototype.toString.call(payload).slice(8, -1); -} -/** - * Returns whether the payload is undefined - * - * @param {*} payload - * @returns {payload is undefined} - */ + return Object.prototype.toString.call(payload).slice(8, -1); +} function isUndefined(payload) { - return getType(payload) === 'Undefined'; -} -/** - * Returns whether the payload is null - * - * @param {*} payload - * @returns {payload is null} - */ + return getType(payload) === "Undefined"; +} 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} - */ + return getType(payload) === "Null"; +} function isPlainObject(payload) { - if (getType(payload) !== 'Object') - return false; - const prototype = Object.getPrototypeOf(payload); - return 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} - */ + if (getType(payload) !== "Object") + return false; + const prototype = Object.getPrototypeOf(payload); + return prototype.constructor === Object && prototype === Object.prototype; +} 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 }} - */ + return isPlainObject(payload); +} 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} - */ + return isPlainObject(payload) && Object.keys(payload).length === 0; +} 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} - */ + return isPlainObject(payload) && Object.keys(payload).length > 0; +} 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} - */ + return getType(payload) === "Object"; +} function isObjectLike(payload) { - return isAnyObject(payload); -} -/** - * Returns whether the payload is a function (regular or async) - * - * @param {*} payload - * @returns {payload is AnyFunction} - */ + return isAnyObject(payload); +} function isFunction(payload) { - return typeof payload === 'function'; -} -/** - * Returns whether the payload is an array - * - * @param {any} payload - * @returns {payload is any[]} - */ + return typeof payload === "function"; +} 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[]} - */ + return getType(payload) === "Array"; +} function isFullArray(payload) { - return isArray(payload) && payload.length > 0; -} -/** - * Returns whether the payload is a an empty array - * - * @param {*} payload - * @returns {payload is []} - */ + return isArray(payload) && payload.length > 0; +} function isEmptyArray(payload) { - return isArray(payload) && payload.length === 0; -} -/** - * Returns whether the payload is a string - * - * @param {*} payload - * @returns {payload is string} - */ + return isArray(payload) && payload.length === 0; +} function isString(payload) { - return getType(payload) === 'String'; -} -/** - * Returns whether the payload is a string, BUT returns false for '' - * - * @param {*} payload - * @returns {payload is string} - */ + return getType(payload) === "String"; +} function isFullString(payload) { - return isString(payload) && payload !== ''; -} -/** - * Returns whether the payload is '' - * - * @param {*} payload - * @returns {payload is string} - */ + return isString(payload) && payload !== ""; +} 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} - */ + return payload === ""; +} 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} - */ + return getType(payload) === "Number" && !isNaN(payload); +} 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} - */ + return isNumber(payload) && payload > 0; +} function isNegativeNumber(payload) { - return isNumber(payload) && payload < 0; -} -/** - * Returns whether the payload is a boolean - * - * @param {*} payload - * @returns {payload is boolean} - */ + return isNumber(payload) && payload < 0; +} function isBoolean(payload) { - return getType(payload) === 'Boolean'; -} -/** - * Returns whether the payload is a regular expression (RegExp) - * - * @param {*} payload - * @returns {payload is RegExp} - */ + return getType(payload) === "Boolean"; +} function isRegExp(payload) { - return getType(payload) === 'RegExp'; -} -/** - * Returns whether the payload is a Map - * - * @param {*} payload - * @returns {payload is Map} - */ + return getType(payload) === "RegExp"; +} function isMap(payload) { - return getType(payload) === 'Map'; -} -/** - * Returns whether the payload is a WeakMap - * - * @param {*} payload - * @returns {payload is WeakMap} - */ + return getType(payload) === "Map"; +} function isWeakMap(payload) { - return getType(payload) === 'WeakMap'; -} -/** - * Returns whether the payload is a Set - * - * @param {*} payload - * @returns {payload is Set} - */ + return getType(payload) === "WeakMap"; +} function isSet(payload) { - return getType(payload) === 'Set'; -} -/** - * Returns whether the payload is a WeakSet - * - * @param {*} payload - * @returns {payload is WeakSet} - */ + return getType(payload) === "Set"; +} function isWeakSet(payload) { - return getType(payload) === 'WeakSet'; -} -/** - * Returns whether the payload is a Symbol - * - * @param {*} payload - * @returns {payload is symbol} - */ + return getType(payload) === "WeakSet"; +} 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} - */ + return getType(payload) === "Symbol"; +} function isDate(payload) { - return getType(payload) === 'Date' && !isNaN(payload); -} -/** - * Returns whether the payload is a Blob - * - * @param {*} payload - * @returns {payload is Blob} - */ + return getType(payload) === "Date" && !isNaN(payload); +} function isBlob(payload) { - return getType(payload) === 'Blob'; -} -/** - * Returns whether the payload is a File - * - * @param {*} payload - * @returns {payload is File} - */ + return getType(payload) === "Blob"; +} function isFile(payload) { - return getType(payload) === 'File'; -} -/** - * Returns whether the payload is a Promise - * - * @param {*} payload - * @returns {payload is Promise} - */ + return getType(payload) === "File"; +} function isPromise(payload) { - return getType(payload) === 'Promise'; -} -/** - * Returns whether the payload is an Error - * - * @param {*} payload - * @returns {payload is Error} - */ + return getType(payload) === "Promise"; +} 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} - */ + return getType(payload) === "Error"; +} 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)} - */ + return getType(payload) === "Number" && isNaN(payload); +} 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)} - */ + 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)); -} -/** - * 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} - */ + 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'); - } - // Classes usually have names (as functions usually have names) - const name = type.name; - return getType(payload) === name || Boolean(payload && payload.constructor === 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); } export { getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullObject, isFullString, isFunction, isMap, isNaNValue, isNegativeNumber, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isOneOf, isPlainObject, isPositiveNumber, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet }; diff --git a/package-lock.json b/package-lock.json index f4acfb5..91748ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,8 @@ "np": "^7.7.0", "prettier": "^2.8.8", "rollup": "^3.22.0", - "rollup-plugin-typescript2": "^0.34.1", + "rollup-plugin-dts": "^5.3.0", + "rollup-plugin-esbuild": "^5.0.0", "typescript": "^5.0.4", "vitest": "^0.31.1" }, @@ -547,16 +548,25 @@ } }, "node_modules/@rollup/pluginutils": { - "version": "4.2.1", - "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-4.2.1.tgz", - "integrity": "sha512-iKnFXr7NkdZAIHiIWE+BX5ULi/ucVFYWD6TbAV+rZctiRTY2PL6tsIKhoIOaoskiWAkgu+VsbXgUVDNLHf+InQ==", + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.0.2.tgz", + "integrity": "sha512-pTd9rIsP92h+B6wWwFbW8RkZv4hiR/xKsqre4SIuAOaOEQRxi0lqLke9k2/7WegC85GgUs9pjmOjCUi3In4vwA==", "dev": true, "dependencies": { - "estree-walker": "^2.0.1", - "picomatch": "^2.2.2" + "@types/estree": "^1.0.0", + "estree-walker": "^2.0.2", + "picomatch": "^2.3.1" }, "engines": { - "node": ">= 8.0.0" + "node": ">=14.0.0" + }, + "peerDependencies": { + "rollup": "^1.20.0||^2.0.0||^3.0.0" + }, + "peerDependenciesMeta": { + "rollup": { + "optional": true + } } }, "node_modules/@samverschueren/stream-to-observable": { @@ -636,6 +646,12 @@ "@types/chai": "*" } }, + "node_modules/@types/estree": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.1.tgz", + "integrity": "sha512-LG4opVs2ANWZ1TJoKc937iMmNstM/d0ae1vNbnBvBhqCSezgVUOzcLCqbI5elV8Vy6WKwKjaqR+zO9VKirBBCA==", + "dev": true + }, "node_modules/@types/http-cache-semantics": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.0.tgz", @@ -1624,12 +1640,6 @@ "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", "dev": true }, - "node_modules/commondir": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", - "integrity": "sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==", - "dev": true - }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -2371,6 +2381,12 @@ "is-arrayish": "^0.2.1" } }, + "node_modules/es-module-lexer": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-1.2.1.tgz", + "integrity": "sha512-9978wrXM50Y4rTMmW5kXIC09ZdXQZqkE4mxhwkd8VbzsGkXGPgV4zWuqQJgCEzYngdo2dYDa0l8xhX4fkSwJSg==", + "dev": true + }, "node_modules/esbuild": { "version": "0.17.19", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.17.19.tgz", @@ -2914,23 +2930,6 @@ "node": ">=8" } }, - "node_modules/find-cache-dir": { - "version": "3.3.2", - "resolved": "https://registry.npmjs.org/find-cache-dir/-/find-cache-dir-3.3.2.tgz", - "integrity": "sha512-wXZV5emFEjrridIgED11OoUKLxiYjAcqot/NJdAkOhlJ+vGzwhOAfcG5OX1jP+S0PcjEn8bdMJv+g2jwQ3Onig==", - "dev": true, - "dependencies": { - "commondir": "^1.0.1", - "make-dir": "^3.0.2", - "pkg-dir": "^4.1.0" - }, - "engines": { - "node": ">=8" - }, - "funding": { - "url": "https://github.com/avajs/find-cache-dir?sponsor=1" - } - }, "node_modules/find-up": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", @@ -2963,20 +2962,6 @@ "integrity": "sha512-zAoAQiudy+r5SvnSw3KJy5os/oRJYHzrzja/tBDqrZtNhUw8bt6y8OBzMWcjWr+8liV8Eb6yOhw8WZ7VFZ5ZzA==", "dev": true }, - "node_modules/fs-extra": { - "version": "10.1.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz", - "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==", - "dev": true, - "dependencies": { - "graceful-fs": "^4.2.0", - "jsonfile": "^6.0.1", - "universalify": "^2.0.0" - }, - "engines": { - "node": ">=12" - } - }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -3875,6 +3860,15 @@ "node": ">=10" } }, + "node_modules/joycon": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/joycon/-/joycon-3.1.1.tgz", + "integrity": "sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw==", + "dev": true, + "engines": { + "node": ">=10" + } + }, "node_modules/js-string-escape": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/js-string-escape/-/js-string-escape-1.0.1.tgz", @@ -3932,18 +3926,6 @@ "integrity": "sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==", "dev": true }, - "node_modules/jsonfile": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", - "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", - "dev": true, - "dependencies": { - "universalify": "^2.0.0" - }, - "optionalDependencies": { - "graceful-fs": "^4.1.6" - } - }, "node_modules/keyv": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/keyv/-/keyv-3.1.0.tgz", @@ -6266,44 +6248,49 @@ "fsevents": "~2.3.2" } }, - "node_modules/rollup-plugin-typescript2": { - "version": "0.34.1", - "resolved": "https://registry.npmjs.org/rollup-plugin-typescript2/-/rollup-plugin-typescript2-0.34.1.tgz", - "integrity": "sha512-P4cHLtGikESmqi1CA+tdMDUv8WbQV48mzPYt77TSTOPJpERyZ9TXdDgjSDix8Fkqce6soYz3+fa4lrC93IEkcw==", + "node_modules/rollup-plugin-dts": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/rollup-plugin-dts/-/rollup-plugin-dts-5.3.0.tgz", + "integrity": "sha512-8FXp0ZkyZj1iU5klkIJYLjIq/YZSwBoERu33QBDxm/1yw5UU4txrEtcmMkrq+ZiKu3Q4qvPCNqc3ovX6rjqzbQ==", "dev": true, "dependencies": { - "@rollup/pluginutils": "^4.1.2", - "find-cache-dir": "^3.3.2", - "fs-extra": "^10.0.0", - "semver": "^7.3.7", - "tslib": "^2.4.0" + "magic-string": "^0.30.0" + }, + "engines": { + "node": ">=v14" + }, + "funding": { + "url": "https://github.com/sponsors/Swatinem" + }, + "optionalDependencies": { + "@babel/code-frame": "^7.18.6" }, "peerDependencies": { - "rollup": ">=1.26.3", - "typescript": ">=2.4.0" + "rollup": "^3.0.0", + "typescript": "^4.1 || ^5.0" } }, - "node_modules/rollup-plugin-typescript2/node_modules/semver": { - "version": "7.3.8", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.8.tgz", - "integrity": "sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==", + "node_modules/rollup-plugin-esbuild": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/rollup-plugin-esbuild/-/rollup-plugin-esbuild-5.0.0.tgz", + "integrity": "sha512-1cRIOHAPh8WQgdQQyyvFdeOdxuiyk+zB5zJ5+YOwrZP4cJ0MT3Fs48pQxrZeyZHcn+klFherytILVfE4aYrneg==", "dev": true, "dependencies": { - "lru-cache": "^6.0.0" - }, - "bin": { - "semver": "bin/semver.js" + "@rollup/pluginutils": "^5.0.1", + "debug": "^4.3.4", + "es-module-lexer": "^1.0.5", + "joycon": "^3.1.1", + "jsonc-parser": "^3.2.0" }, "engines": { - "node": ">=10" + "node": ">=14.18.0", + "npm": ">=8.0.0" + }, + "peerDependencies": { + "esbuild": ">=0.10.1", + "rollup": "^1.20.0 || ^2.0.0 || ^3.0.0" } }, - "node_modules/rollup-plugin-typescript2/node_modules/tslib": { - "version": "2.4.1", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.1.tgz", - "integrity": "sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==", - "dev": true - }, "node_modules/run-async": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/run-async/-/run-async-2.4.1.tgz", @@ -6853,15 +6840,6 @@ "node": ">=8" } }, - "node_modules/universalify": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", - "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", - "dev": true, - "engines": { - "node": ">= 10.0.0" - } - }, "node_modules/update-notifier": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/update-notifier/-/update-notifier-5.1.0.tgz", @@ -7644,13 +7622,14 @@ } }, "@rollup/pluginutils": { - "version": "4.2.1", - "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-4.2.1.tgz", - "integrity": "sha512-iKnFXr7NkdZAIHiIWE+BX5ULi/ucVFYWD6TbAV+rZctiRTY2PL6tsIKhoIOaoskiWAkgu+VsbXgUVDNLHf+InQ==", + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.0.2.tgz", + "integrity": "sha512-pTd9rIsP92h+B6wWwFbW8RkZv4hiR/xKsqre4SIuAOaOEQRxi0lqLke9k2/7WegC85GgUs9pjmOjCUi3In4vwA==", "dev": true, "requires": { - "estree-walker": "^2.0.1", - "picomatch": "^2.2.2" + "@types/estree": "^1.0.0", + "estree-walker": "^2.0.2", + "picomatch": "^2.3.1" } }, "@samverschueren/stream-to-observable": { @@ -7712,6 +7691,12 @@ "@types/chai": "*" } }, + "@types/estree": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.1.tgz", + "integrity": "sha512-LG4opVs2ANWZ1TJoKc937iMmNstM/d0ae1vNbnBvBhqCSezgVUOzcLCqbI5elV8Vy6WKwKjaqR+zO9VKirBBCA==", + "dev": true + }, "@types/http-cache-semantics": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.0.tgz", @@ -8426,12 +8411,6 @@ "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", "dev": true }, - "commondir": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", - "integrity": "sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==", - "dev": true - }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -8942,6 +8921,12 @@ "is-arrayish": "^0.2.1" } }, + "es-module-lexer": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-1.2.1.tgz", + "integrity": "sha512-9978wrXM50Y4rTMmW5kXIC09ZdXQZqkE4mxhwkd8VbzsGkXGPgV4zWuqQJgCEzYngdo2dYDa0l8xhX4fkSwJSg==", + "dev": true + }, "esbuild": { "version": "0.17.19", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.17.19.tgz", @@ -9347,17 +9332,6 @@ "to-regex-range": "^5.0.1" } }, - "find-cache-dir": { - "version": "3.3.2", - "resolved": "https://registry.npmjs.org/find-cache-dir/-/find-cache-dir-3.3.2.tgz", - "integrity": "sha512-wXZV5emFEjrridIgED11OoUKLxiYjAcqot/NJdAkOhlJ+vGzwhOAfcG5OX1jP+S0PcjEn8bdMJv+g2jwQ3Onig==", - "dev": true, - "requires": { - "commondir": "^1.0.1", - "make-dir": "^3.0.2", - "pkg-dir": "^4.1.0" - } - }, "find-up": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", @@ -9384,17 +9358,6 @@ "integrity": "sha512-zAoAQiudy+r5SvnSw3KJy5os/oRJYHzrzja/tBDqrZtNhUw8bt6y8OBzMWcjWr+8liV8Eb6yOhw8WZ7VFZ5ZzA==", "dev": true }, - "fs-extra": { - "version": "10.1.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz", - "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==", - "dev": true, - "requires": { - "graceful-fs": "^4.2.0", - "jsonfile": "^6.0.1", - "universalify": "^2.0.0" - } - }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -10071,6 +10034,12 @@ "integrity": "sha512-0RHjbtw9QXeSYnIEY5Yrp2QZrdtz21xBDV9C/GIlY2POmgoS6a7qjkYS5siRKXScnuAj5/SPv1C3YForNCHTJA==", "dev": true }, + "joycon": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/joycon/-/joycon-3.1.1.tgz", + "integrity": "sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw==", + "dev": true + }, "js-string-escape": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/js-string-escape/-/js-string-escape-1.0.1.tgz", @@ -10122,16 +10091,6 @@ "integrity": "sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==", "dev": true }, - "jsonfile": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", - "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", - "dev": true, - "requires": { - "graceful-fs": "^4.1.6", - "universalify": "^2.0.0" - } - }, "keyv": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/keyv/-/keyv-3.1.0.tgz", @@ -11833,34 +11792,27 @@ "fsevents": "~2.3.2" } }, - "rollup-plugin-typescript2": { - "version": "0.34.1", - "resolved": "https://registry.npmjs.org/rollup-plugin-typescript2/-/rollup-plugin-typescript2-0.34.1.tgz", - "integrity": "sha512-P4cHLtGikESmqi1CA+tdMDUv8WbQV48mzPYt77TSTOPJpERyZ9TXdDgjSDix8Fkqce6soYz3+fa4lrC93IEkcw==", + "rollup-plugin-dts": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/rollup-plugin-dts/-/rollup-plugin-dts-5.3.0.tgz", + "integrity": "sha512-8FXp0ZkyZj1iU5klkIJYLjIq/YZSwBoERu33QBDxm/1yw5UU4txrEtcmMkrq+ZiKu3Q4qvPCNqc3ovX6rjqzbQ==", "dev": true, "requires": { - "@rollup/pluginutils": "^4.1.2", - "find-cache-dir": "^3.3.2", - "fs-extra": "^10.0.0", - "semver": "^7.3.7", - "tslib": "^2.4.0" - }, - "dependencies": { - "semver": { - "version": "7.3.8", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.8.tgz", - "integrity": "sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==", - "dev": true, - "requires": { - "lru-cache": "^6.0.0" - } - }, - "tslib": { - "version": "2.4.1", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.1.tgz", - "integrity": "sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==", - "dev": true - } + "@babel/code-frame": "^7.18.6", + "magic-string": "^0.30.0" + } + }, + "rollup-plugin-esbuild": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/rollup-plugin-esbuild/-/rollup-plugin-esbuild-5.0.0.tgz", + "integrity": "sha512-1cRIOHAPh8WQgdQQyyvFdeOdxuiyk+zB5zJ5+YOwrZP4cJ0MT3Fs48pQxrZeyZHcn+klFherytILVfE4aYrneg==", + "dev": true, + "requires": { + "@rollup/pluginutils": "^5.0.1", + "debug": "^4.3.4", + "es-module-lexer": "^1.0.5", + "joycon": "^3.1.1", + "jsonc-parser": "^3.2.0" } }, "run-async": { @@ -12271,12 +12223,6 @@ "crypto-random-string": "^2.0.0" } }, - "universalify": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", - "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", - "dev": true - }, "update-notifier": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/update-notifier/-/update-notifier-5.1.0.tgz", diff --git a/package.json b/package.json index 5863aa6..5652420 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name": "is-what", - "sideEffects": false, - "type": "module", "version": "4.1.10", "description": "JS type check (TypeScript supported) functions like `isPlainObject() isArray()` etc. A simple & small integration.", + "type": "module", + "sideEffects": false, "types": "./dist/index.d.ts", "module": "./dist/index.js", "main": "./dist/index.js", @@ -28,7 +28,7 @@ "scripts": { "test": "vitest run", "lint": "tsc --noEmit && eslint ./src --ext .ts", - "build": "rollup -c ./scripts/rollup.config.js", + "build": "rollup -c ./rollup.config.js", "release": "npm run lint && del dist && npm run build && np" }, "repository": { @@ -73,7 +73,8 @@ "np": "^7.7.0", "prettier": "^2.8.8", "rollup": "^3.22.0", - "rollup-plugin-typescript2": "^0.34.1", + "rollup-plugin-dts": "^5.3.0", + "rollup-plugin-esbuild": "^5.0.0", "typescript": "^5.0.4", "vitest": "^0.31.1" }, diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..64e8d75 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,45 @@ +/* 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()], + }, +] diff --git a/scripts/rollup.config.js b/scripts/rollup.config.js deleted file mode 100644 index 4f1aa02..0000000 --- a/scripts/rollup.config.js +++ /dev/null @@ -1,34 +0,0 @@ -/* eslint-disable */ - -// npm i -D rollup rollup-plugin-typescript2 typescript -import typescript from 'rollup-plugin-typescript2' -import pkg from '../package.json' assert { type: 'json' } -import fs from 'fs' - -function renameCjsTypeDeclaration() { - return { - name: 'renameCjsTypeDeclaration', - writeBundle: { - sequential: true, - order: 'post', - handler({ dir, format }) { - if (format === 'cjs') { - fs.rename('dist/cjs/index.d.ts', 'dist/cjs/index.d.cts', console.error) - } - }, - }, - } -} - -export default { - input: 'src/index.ts', - output: [ - { file: pkg.exports['.'].import.default, format: 'esm' }, - { file: pkg.exports['.'].require.default, format: 'cjs' }, - ], - external: Object.keys(pkg.dependencies || []), - plugins: [ - typescript({ tsconfigOverride: { exclude: ['test/**/*'] } }), - renameCjsTypeDeclaration(), - ], -}