From bbf0e2eaac785eee7c952f7b69b1b69ffb6197bc Mon Sep 17 00:00:00 2001 From: mesqueeb Date: Wed, 31 May 2023 17:00:15 +0900 Subject: [PATCH] chore: build --- dist/cjs/index.cjs | 20 ++++++++++++- dist/cjs/index.d.cts | 68 +++++++++++++++++++++++++++++++++++++++++++- dist/index.d.ts | 68 +++++++++++++++++++++++++++++++++++++++++++- dist/index.js | 21 ++++++++++++-- 4 files changed, 172 insertions(+), 5 deletions(-) diff --git a/dist/cjs/index.cjs b/dist/cjs/index.cjs index 73b80e3..cb52cef 100644 --- a/dist/cjs/index.cjs +++ b/dist/cjs/index.cjs @@ -13,7 +13,7 @@ function isPlainObject(payload) { if (getType(payload) !== "Object") return false; const prototype = Object.getPrototypeOf(payload); - return prototype.constructor === Object && prototype === Object.prototype; + return !!prototype && prototype.constructor === Object && prototype === Object.prototype; } function isObject(payload) { return isPlainObject(payload); @@ -116,6 +116,23 @@ function isType(payload, type) { const name = type.name; return getType(payload) === name || Boolean(payload && payload.constructor === type); } +function isInstanceOf(value, classOrClassName) { + if (typeof classOrClassName === "function") { + for (let p = value; p; p = Object.getPrototypeOf(p)) { + 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; @@ -132,6 +149,7 @@ exports.isFullArray = isFullArray; exports.isFullObject = isFullObject; exports.isFullString = isFullString; exports.isFunction = isFunction; +exports.isInstanceOf = isInstanceOf; exports.isMap = isMap; exports.isNaNValue = isNaNValue; exports.isNegativeNumber = isNegativeNumber; diff --git a/dist/cjs/index.d.cts b/dist/cjs/index.d.cts index 92cb699..f63e49d 100644 --- a/dist/cjs/index.d.cts +++ b/dist/cjs/index.d.cts @@ -248,9 +248,53 @@ declare function isPrimitive(payload: any): payload is boolean | null | undefine * @returns {(payload is null | undefined)} */ declare const isNullOrUndefined: TypeGuard; +/** + * 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 + */ 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 + * import { isOneOf, isNull, isUndefined } from 'is-what' + * + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false + */ 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 + * import { isOneOf, isNull, isUndefined } from 'is-what' + * + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false + */ 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 + * import { isOneOf, isNull, isUndefined } from 'is-what' + * + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false + */ 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. @@ -264,5 +308,27 @@ declare function isOneOf(payload: any, type: T): payload is T; +type GlobalClassName = { + [K in keyof typeof globalThis]: (typeof globalThis)[K] extends AnyClass ? K : never; +}[keyof typeof globalThis]; +/** + * Checks if a value is an instance of a class or a class name. Useful when you + * want to check if a value is an instance of a class that may not be defined in + * the current scope. For example, if you want to check if a value is an + * `OffscreenCanvas` instance, you might not want to do the song and dance of + * using `typeof OffscreenCanvas !== 'undefined'` and then shimming + * `OffscreenCanvas` if the types aren't around. + * + * @example + * if (isInstanceOf(value, 'OffscreenCanvas')) { + * // value is an OffscreenCanvas + * } + * + * @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, isMap, isNaNValue, isNegativeNumber, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isOneOf, isPlainObject, isPositiveNumber, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet }; +export { AnyAsyncFunction, AnyClass, AnyFunction, PlainObject, getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullObject, isFullString, isFunction, isInstanceOf, isMap, isNaNValue, isNegativeNumber, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isOneOf, isPlainObject, isPositiveNumber, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet }; diff --git a/dist/index.d.ts b/dist/index.d.ts index 92cb699..f63e49d 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -248,9 +248,53 @@ declare function isPrimitive(payload: any): payload is boolean | null | undefine * @returns {(payload is null | undefined)} */ declare const isNullOrUndefined: TypeGuard; +/** + * 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 + */ 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 + * import { isOneOf, isNull, isUndefined } from 'is-what' + * + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false + */ 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 + * import { isOneOf, isNull, isUndefined } from 'is-what' + * + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false + */ 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 + * import { isOneOf, isNull, isUndefined } from 'is-what' + * + * const isNullOrUndefined = isOneOf(isNull, isUndefined) + * + * isNullOrUndefined(null) // true + * isNullOrUndefined(undefined) // true + * isNullOrUndefined(123) // false + */ 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. @@ -264,5 +308,27 @@ declare function isOneOf(payload: any, type: T): payload is T; +type GlobalClassName = { + [K in keyof typeof globalThis]: (typeof globalThis)[K] extends AnyClass ? K : never; +}[keyof typeof globalThis]; +/** + * Checks if a value is an instance of a class or a class name. Useful when you + * want to check if a value is an instance of a class that may not be defined in + * the current scope. For example, if you want to check if a value is an + * `OffscreenCanvas` instance, you might not want to do the song and dance of + * using `typeof OffscreenCanvas !== 'undefined'` and then shimming + * `OffscreenCanvas` if the types aren't around. + * + * @example + * if (isInstanceOf(value, 'OffscreenCanvas')) { + * // value is an OffscreenCanvas + * } + * + * @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, isMap, isNaNValue, isNegativeNumber, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isOneOf, isPlainObject, isPositiveNumber, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet }; +export { AnyAsyncFunction, AnyClass, AnyFunction, PlainObject, getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullObject, isFullString, isFunction, isInstanceOf, isMap, isNaNValue, isNegativeNumber, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isOneOf, isPlainObject, isPositiveNumber, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet }; diff --git a/dist/index.js b/dist/index.js index 4f3379b..fff9275 100644 --- a/dist/index.js +++ b/dist/index.js @@ -11,7 +11,7 @@ function isPlainObject(payload) { if (getType(payload) !== "Object") return false; const prototype = Object.getPrototypeOf(payload); - return prototype.constructor === Object && prototype === Object.prototype; + return !!prototype && prototype.constructor === Object && prototype === Object.prototype; } function isObject(payload) { return isPlainObject(payload); @@ -114,5 +114,22 @@ function isType(payload, type) { const name = type.name; return getType(payload) === name || Boolean(payload && payload.constructor === type); } +function isInstanceOf(value, classOrClassName) { + if (typeof classOrClassName === "function") { + for (let p = value; p; p = Object.getPrototypeOf(p)) { + 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; + } +} -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 }; +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 };