is-what 4.1.16 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/dist/getType.d.ts +2 -0
- package/dist/getType.js +4 -0
- package/dist/index.d.ts +41 -380
- package/dist/index.js +37 -171
- package/dist/isAnyObject.d.ts +6 -0
- package/dist/isAnyObject.js +8 -0
- package/dist/isArray.d.ts +2 -0
- package/dist/isArray.js +5 -0
- package/dist/isBlob.d.ts +2 -0
- package/dist/isBlob.js +5 -0
- package/dist/isBoolean.d.ts +2 -0
- package/dist/isBoolean.js +5 -0
- package/dist/isDate.d.ts +2 -0
- package/dist/isDate.js +5 -0
- package/dist/isEmptyArray.d.ts +2 -0
- package/dist/isEmptyArray.js +5 -0
- package/dist/isEmptyObject.d.ts +7 -0
- package/dist/isEmptyObject.js +8 -0
- package/dist/isEmptyString.d.ts +2 -0
- package/dist/isEmptyString.js +4 -0
- package/dist/isError.d.ts +2 -0
- package/dist/isError.js +5 -0
- package/dist/isFile.d.ts +2 -0
- package/dist/isFile.js +5 -0
- package/dist/isFullArray.d.ts +2 -0
- package/dist/isFullArray.js +5 -0
- package/dist/isFullObject.d.ts +6 -0
- package/dist/isFullObject.js +8 -0
- package/dist/isFullString.d.ts +2 -0
- package/dist/isFullString.js +5 -0
- package/dist/isFunction.d.ts +3 -0
- package/dist/isFunction.js +4 -0
- package/dist/isInstanceOf.d.ts +23 -0
- package/dist/isInstanceOf.js +20 -0
- package/dist/isMap.d.ts +2 -0
- package/dist/isMap.js +5 -0
- package/dist/isNaNValue.d.ts +2 -0
- package/dist/isNaNValue.js +5 -0
- package/dist/isNegativeNumber.d.ts +2 -0
- package/dist/isNegativeNumber.js +5 -0
- package/dist/isNull.d.ts +2 -0
- package/dist/isNull.js +5 -0
- package/dist/isNullOrUndefined.d.ts +2 -0
- package/dist/isNullOrUndefined.js +5 -0
- package/dist/isNumber.d.ts +6 -0
- package/dist/isNumber.js +9 -0
- package/dist/isObject.d.ts +6 -0
- package/dist/isObject.js +8 -0
- package/dist/isObjectLike.d.ts +9 -0
- package/dist/isObjectLike.js +11 -0
- package/dist/isOneOf.d.ts +54 -0
- package/dist/isOneOf.js +15 -0
- package/dist/isPlainObject.d.ts +6 -0
- package/dist/isPlainObject.js +11 -0
- package/dist/isPositiveNumber.d.ts +2 -0
- package/dist/isPositiveNumber.js +5 -0
- package/dist/isPrimitive.d.ts +6 -0
- package/dist/isPrimitive.js +19 -0
- package/dist/isPromise.d.ts +2 -0
- package/dist/isPromise.js +5 -0
- package/dist/isRegExp.d.ts +2 -0
- package/dist/isRegExp.js +5 -0
- package/dist/isSet.d.ts +2 -0
- package/dist/isSet.js +5 -0
- package/dist/isString.d.ts +2 -0
- package/dist/isString.js +5 -0
- package/dist/isSymbol.d.ts +2 -0
- package/dist/isSymbol.js +5 -0
- package/dist/isType.d.ts +10 -0
- package/dist/isType.js +19 -0
- package/dist/isUndefined.d.ts +2 -0
- package/dist/isUndefined.js +5 -0
- package/dist/isWeakMap.d.ts +2 -0
- package/dist/isWeakMap.js +5 -0
- package/dist/isWeakSet.d.ts +2 -0
- package/dist/isWeakSet.js +5 -0
- package/package.json +12 -74
- package/dist/cjs/index.cjs +0 -209
- package/dist/cjs/index.d.cts +0 -380
package/dist/isArray.js
ADDED
package/dist/isBlob.d.ts
ADDED
package/dist/isBlob.js
ADDED
package/dist/isDate.d.ts
ADDED
package/dist/isDate.js
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
import { isPlainObject } from './isPlainObject.js';
|
2
|
+
/**
|
3
|
+
* Returns whether the payload is a an empty object (excluding special classes or objects with other
|
4
|
+
* prototypes)
|
5
|
+
*/
|
6
|
+
export function isEmptyObject(payload) {
|
7
|
+
return isPlainObject(payload) && Object.keys(payload).length === 0;
|
8
|
+
}
|
package/dist/isError.js
ADDED
package/dist/isFile.d.ts
ADDED
package/dist/isFile.js
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
import { isPlainObject } from './isPlainObject.js';
|
2
|
+
/**
|
3
|
+
* Returns whether the payload is a an empty object (excluding special classes or objects with other
|
4
|
+
* prototypes)
|
5
|
+
*/
|
6
|
+
export function isFullObject(payload) {
|
7
|
+
return isPlainObject(payload) && Object.keys(payload).length > 0;
|
8
|
+
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { AnyClass } from './isType.js';
|
2
|
+
type GlobalClassName = {
|
3
|
+
[K in keyof typeof globalThis]: (typeof globalThis)[K] extends AnyClass ? K : never;
|
4
|
+
}[keyof typeof globalThis];
|
5
|
+
/**
|
6
|
+
* Checks if a value is an instance of a class or a class name. Useful when you want to check if a
|
7
|
+
* value is an instance of a class that may not be defined in the current scope. For example, if you
|
8
|
+
* want to check if a value is an `OffscreenCanvas` instance, you might not want to do the song and
|
9
|
+
* dance of using `typeof OffscreenCanvas !== 'undefined'` and then shimming `OffscreenCanvas` if
|
10
|
+
* the types aren't around.
|
11
|
+
*
|
12
|
+
* @example
|
13
|
+
* if (isInstanceOf(value, 'OffscreenCanvas')) {
|
14
|
+
* // value is an OffscreenCanvas
|
15
|
+
* }
|
16
|
+
*
|
17
|
+
* @param value The value to recursively check
|
18
|
+
* @param class_ A string or class that the value should be an instance of
|
19
|
+
*/
|
20
|
+
export declare function isInstanceOf<T extends AnyClass>(value: unknown, class_: T): value is T;
|
21
|
+
export declare function isInstanceOf<K extends GlobalClassName>(value: unknown, className: K): value is (typeof globalThis)[K];
|
22
|
+
export declare function isInstanceOf(value: unknown, className: string): value is object;
|
23
|
+
export {};
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { getType } from './getType.js';
|
2
|
+
import { isType } from './isType.js';
|
3
|
+
export function isInstanceOf(value, classOrClassName) {
|
4
|
+
if (typeof classOrClassName === 'function') {
|
5
|
+
for (let p = value; p; p = Object.getPrototypeOf(p)) {
|
6
|
+
if (isType(p, classOrClassName)) {
|
7
|
+
return true;
|
8
|
+
}
|
9
|
+
}
|
10
|
+
return false;
|
11
|
+
}
|
12
|
+
else {
|
13
|
+
for (let p = value; p; p = Object.getPrototypeOf(p)) {
|
14
|
+
if (getType(p) === classOrClassName) {
|
15
|
+
return true;
|
16
|
+
}
|
17
|
+
}
|
18
|
+
return false;
|
19
|
+
}
|
20
|
+
}
|
package/dist/isMap.d.ts
ADDED
package/dist/isMap.js
ADDED
package/dist/isNull.d.ts
ADDED
package/dist/isNull.js
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
import { isNull } from './isNull.js';
|
2
|
+
import { /* tree-shaking no-side-effects-when-called */ isOneOf } from './isOneOf.js';
|
3
|
+
import { isUndefined } from './isUndefined.js';
|
4
|
+
/** Returns true whether the payload is null or undefined */
|
5
|
+
export const isNullOrUndefined = isOneOf(isNull, isUndefined);
|
package/dist/isNumber.js
ADDED
package/dist/isObject.js
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
import { PlainObject } from './isPlainObject.js';
|
2
|
+
/**
|
3
|
+
* Returns whether the payload is an object like a type passed in < >
|
4
|
+
*
|
5
|
+
* Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop.
|
6
|
+
*
|
7
|
+
* @template T This must be passed in < >
|
8
|
+
*/
|
9
|
+
export declare function isObjectLike<T extends PlainObject>(payload: unknown): payload is T;
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { isAnyObject } from './isAnyObject.js';
|
2
|
+
/**
|
3
|
+
* Returns whether the payload is an object like a type passed in < >
|
4
|
+
*
|
5
|
+
* Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop.
|
6
|
+
*
|
7
|
+
* @template T This must be passed in < >
|
8
|
+
*/
|
9
|
+
export function isObjectLike(payload) {
|
10
|
+
return isAnyObject(payload);
|
11
|
+
}
|
@@ -0,0 +1,54 @@
|
|
1
|
+
type TypeGuard<A, B extends A> = (payload: A) => payload is B;
|
2
|
+
/**
|
3
|
+
* A factory function that creates a function to check if the payload is one of the given types.
|
4
|
+
*
|
5
|
+
* @example
|
6
|
+
* import { isOneOf, isNull, isUndefined } from 'is-what'
|
7
|
+
*
|
8
|
+
* const isNullOrUndefined = isOneOf(isNull, isUndefined)
|
9
|
+
*
|
10
|
+
* isNullOrUndefined(null) // true
|
11
|
+
* isNullOrUndefined(undefined) // true
|
12
|
+
* isNullOrUndefined(123) // false
|
13
|
+
*/
|
14
|
+
export declare function isOneOf<A, B extends A, C extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>): TypeGuard<A, B | C>;
|
15
|
+
/**
|
16
|
+
* A factory function that creates a function to check if the payload is one of the given types.
|
17
|
+
*
|
18
|
+
* @example
|
19
|
+
* import { isOneOf, isNull, isUndefined } from 'is-what'
|
20
|
+
*
|
21
|
+
* const isNullOrUndefined = isOneOf(isNull, isUndefined)
|
22
|
+
*
|
23
|
+
* isNullOrUndefined(null) // true
|
24
|
+
* isNullOrUndefined(undefined) // true
|
25
|
+
* isNullOrUndefined(123) // false
|
26
|
+
*/
|
27
|
+
export declare function isOneOf<A, B extends A, C extends A, D extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>, c: TypeGuard<A, D>): TypeGuard<A, B | C | D>;
|
28
|
+
/**
|
29
|
+
* A factory function that creates a function to check if the payload is one of the given types.
|
30
|
+
*
|
31
|
+
* @example
|
32
|
+
* import { isOneOf, isNull, isUndefined } from 'is-what'
|
33
|
+
*
|
34
|
+
* const isNullOrUndefined = isOneOf(isNull, isUndefined)
|
35
|
+
*
|
36
|
+
* isNullOrUndefined(null) // true
|
37
|
+
* isNullOrUndefined(undefined) // true
|
38
|
+
* isNullOrUndefined(123) // false
|
39
|
+
*/
|
40
|
+
export declare function isOneOf<A, B extends A, C extends A, D extends A, E extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>, c: TypeGuard<A, D>, d: TypeGuard<A, E>): TypeGuard<A, B | C | D | E>;
|
41
|
+
/**
|
42
|
+
* A factory function that creates a function to check if the payload is one of the given types.
|
43
|
+
*
|
44
|
+
* @example
|
45
|
+
* import { isOneOf, isNull, isUndefined } from 'is-what'
|
46
|
+
*
|
47
|
+
* const isNullOrUndefined = isOneOf(isNull, isUndefined)
|
48
|
+
*
|
49
|
+
* isNullOrUndefined(null) // true
|
50
|
+
* isNullOrUndefined(undefined) // true
|
51
|
+
* isNullOrUndefined(123) // false
|
52
|
+
*/
|
53
|
+
export declare function isOneOf<A, B extends A, C extends A, D extends A, E extends A, F extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>, c: TypeGuard<A, D>, d: TypeGuard<A, E>, e: TypeGuard<A, F>): TypeGuard<A, B | C | D | E | F>;
|
54
|
+
export {};
|
package/dist/isOneOf.js
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
/**
|
2
|
+
* A factory function that creates a function to check if the payload is one of the given types.
|
3
|
+
*
|
4
|
+
* @example
|
5
|
+
* import { isOneOf, isNull, isUndefined } from 'is-what'
|
6
|
+
*
|
7
|
+
* const isNullOrUndefined = isOneOf(isNull, isUndefined)
|
8
|
+
*
|
9
|
+
* isNullOrUndefined(null) // true
|
10
|
+
* isNullOrUndefined(undefined) // true
|
11
|
+
* isNullOrUndefined(123) // false
|
12
|
+
*/
|
13
|
+
export function isOneOf(a, b, c, d, e) {
|
14
|
+
return (value) => a(value) || b(value) || (!!c && c(value)) || (!!d && d(value)) || (!!e && e(value));
|
15
|
+
}
|
@@ -0,0 +1,6 @@
|
|
1
|
+
export type PlainObject = Record<string | number | symbol, unknown>;
|
2
|
+
/**
|
3
|
+
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects
|
4
|
+
* with other prototypes)
|
5
|
+
*/
|
6
|
+
export declare function isPlainObject(payload: unknown): payload is PlainObject;
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { getType } from './getType.js';
|
2
|
+
/**
|
3
|
+
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects
|
4
|
+
* with other prototypes)
|
5
|
+
*/
|
6
|
+
export function isPlainObject(payload) {
|
7
|
+
if (getType(payload) !== 'Object')
|
8
|
+
return false;
|
9
|
+
const prototype = Object.getPrototypeOf(payload);
|
10
|
+
return !!prototype && prototype.constructor === Object && prototype === Object.prototype;
|
11
|
+
}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { isBoolean } from './isBoolean.js';
|
2
|
+
import { isNull } from './isNull.js';
|
3
|
+
import { isNumber } from './isNumber.js';
|
4
|
+
import { isString } from './isString.js';
|
5
|
+
import { isSymbol } from './isSymbol.js';
|
6
|
+
import { isUndefined } from './isUndefined.js';
|
7
|
+
/**
|
8
|
+
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String
|
9
|
+
*
|
10
|
+
* | Symbol)
|
11
|
+
*/
|
12
|
+
export function isPrimitive(payload) {
|
13
|
+
return (isBoolean(payload) ||
|
14
|
+
isNull(payload) ||
|
15
|
+
isUndefined(payload) ||
|
16
|
+
isNumber(payload) ||
|
17
|
+
isString(payload) ||
|
18
|
+
isSymbol(payload));
|
19
|
+
}
|
package/dist/isRegExp.js
ADDED
package/dist/isSet.d.ts
ADDED
package/dist/isSet.js
ADDED
package/dist/isString.js
ADDED
package/dist/isSymbol.js
ADDED
package/dist/isType.d.ts
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
import { AnyFunction } from './isFunction.js';
|
2
|
+
export type AnyClass = new (...args: unknown[]) => unknown;
|
3
|
+
/**
|
4
|
+
* Does a generic check to check that the given payload is of a given type. In cases like Number, it
|
5
|
+
* will return true for NaN as NaN is a Number (thanks javascript!); It will, however, differentiate
|
6
|
+
* between object and null
|
7
|
+
*
|
8
|
+
* @throws {TypeError} Will throw type error if type is an invalid type
|
9
|
+
*/
|
10
|
+
export declare function isType<T extends AnyFunction | AnyClass>(payload: unknown, type: T): payload is T;
|
package/dist/isType.js
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
import { getType } from './getType.js';
|
2
|
+
/**
|
3
|
+
* Does a generic check to check that the given payload is of a given type. In cases like Number, it
|
4
|
+
* will return true for NaN as NaN is a Number (thanks javascript!); It will, however, differentiate
|
5
|
+
* between object and null
|
6
|
+
*
|
7
|
+
* @throws {TypeError} Will throw type error if type is an invalid type
|
8
|
+
*/
|
9
|
+
export function isType(payload, type) {
|
10
|
+
if (!(type instanceof Function)) {
|
11
|
+
throw new TypeError('Type must be a function');
|
12
|
+
}
|
13
|
+
if (!Object.prototype.hasOwnProperty.call(type, 'prototype')) {
|
14
|
+
throw new TypeError('Type is not a class');
|
15
|
+
}
|
16
|
+
// Classes usually have names (as functions usually have names)
|
17
|
+
const name = type.name;
|
18
|
+
return getType(payload) === name || Boolean(payload && payload.constructor === type);
|
19
|
+
}
|