is-what 4.1.11 → 4.1.12

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -180,6 +180,18 @@ getType('') // returns 'String'
180
180
  isType('', String) // returns true
181
181
  ```
182
182
 
183
+ If you just want to make sure your object _inherits_ from a particular class or
184
+ `toStringTag` value, you can use `isInstanceOf()` like this:
185
+
186
+ ```js
187
+ import { isInstanceOf } from 'is-what'
188
+
189
+ isInstanceOf(new XMLHttpRequest(), "EventTarget")
190
+ // returns true
191
+ isInstanceOf(globalThis, ReadableStream)
192
+ // returns false
193
+ ```
194
+
183
195
  ## TypeScript
184
196
 
185
197
  is-what makes TypeScript know the type during if statements. This means that a check returns the type of the payload for TypeScript users.
@@ -13,7 +13,7 @@ function isPlainObject(payload) {
13
13
  if (getType(payload) !== "Object")
14
14
  return false;
15
15
  const prototype = Object.getPrototypeOf(payload);
16
- return prototype.constructor === Object && prototype === Object.prototype;
16
+ return !!prototype && prototype.constructor === Object && prototype === Object.prototype;
17
17
  }
18
18
  function isObject(payload) {
19
19
  return isPlainObject(payload);
@@ -116,6 +116,23 @@ function isType(payload, type) {
116
116
  const name = type.name;
117
117
  return getType(payload) === name || Boolean(payload && payload.constructor === type);
118
118
  }
119
+ function isInstanceOf(value, classOrClassName) {
120
+ if (typeof classOrClassName === "function") {
121
+ for (let p = value; p; p = Object.getPrototypeOf(p)) {
122
+ if (isType(p, classOrClassName)) {
123
+ return true;
124
+ }
125
+ }
126
+ return false;
127
+ } else {
128
+ for (let p = value; p; p = Object.getPrototypeOf(p)) {
129
+ if (getType(p) === classOrClassName) {
130
+ return true;
131
+ }
132
+ }
133
+ return false;
134
+ }
135
+ }
119
136
 
120
137
  exports.getType = getType;
121
138
  exports.isAnyObject = isAnyObject;
@@ -132,6 +149,7 @@ exports.isFullArray = isFullArray;
132
149
  exports.isFullObject = isFullObject;
133
150
  exports.isFullString = isFullString;
134
151
  exports.isFunction = isFunction;
152
+ exports.isInstanceOf = isInstanceOf;
135
153
  exports.isMap = isMap;
136
154
  exports.isNaNValue = isNaNValue;
137
155
  exports.isNegativeNumber = isNegativeNumber;
@@ -248,9 +248,53 @@ declare function isPrimitive(payload: any): payload is boolean | null | undefine
248
248
  * @returns {(payload is null | undefined)}
249
249
  */
250
250
  declare const isNullOrUndefined: TypeGuard<any, null | undefined>;
251
+ /**
252
+ * A factory function that creates a function to check if the payload is one of the given types.
253
+ * @example
254
+ * import { isOneOf, isNull, isUndefined } from 'is-what'
255
+ *
256
+ * const isNullOrUndefined = isOneOf(isNull, isUndefined)
257
+ *
258
+ * isNullOrUndefined(null) // true
259
+ * isNullOrUndefined(undefined) // true
260
+ * isNullOrUndefined(123) // false
261
+ */
251
262
  declare function isOneOf<A, B extends A, C extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>): TypeGuard<A, B | C>;
263
+ /**
264
+ * A factory function that creates a function to check if the payload is one of the given types.
265
+ * @example
266
+ * import { isOneOf, isNull, isUndefined } from 'is-what'
267
+ *
268
+ * const isNullOrUndefined = isOneOf(isNull, isUndefined)
269
+ *
270
+ * isNullOrUndefined(null) // true
271
+ * isNullOrUndefined(undefined) // true
272
+ * isNullOrUndefined(123) // false
273
+ */
252
274
  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>;
275
+ /**
276
+ * A factory function that creates a function to check if the payload is one of the given types.
277
+ * @example
278
+ * import { isOneOf, isNull, isUndefined } from 'is-what'
279
+ *
280
+ * const isNullOrUndefined = isOneOf(isNull, isUndefined)
281
+ *
282
+ * isNullOrUndefined(null) // true
283
+ * isNullOrUndefined(undefined) // true
284
+ * isNullOrUndefined(123) // false
285
+ */
253
286
  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>;
287
+ /**
288
+ * A factory function that creates a function to check if the payload is one of the given types.
289
+ * @example
290
+ * import { isOneOf, isNull, isUndefined } from 'is-what'
291
+ *
292
+ * const isNullOrUndefined = isOneOf(isNull, isUndefined)
293
+ *
294
+ * isNullOrUndefined(null) // true
295
+ * isNullOrUndefined(undefined) // true
296
+ * isNullOrUndefined(123) // false
297
+ */
254
298
  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>;
255
299
  /**
256
300
  * Does a generic check to check that the given payload is of a given type.
@@ -264,5 +308,27 @@ declare function isOneOf<A, B extends A, C extends A, D extends A, E extends A,
264
308
  * @returns {payload is T}
265
309
  */
266
310
  declare function isType<T extends AnyFunction | AnyClass>(payload: any, type: T): payload is T;
311
+ type GlobalClassName = {
312
+ [K in keyof typeof globalThis]: (typeof globalThis)[K] extends AnyClass ? K : never;
313
+ }[keyof typeof globalThis];
314
+ /**
315
+ * Checks if a value is an instance of a class or a class name. Useful when you
316
+ * want to check if a value is an instance of a class that may not be defined in
317
+ * the current scope. For example, if you want to check if a value is an
318
+ * `OffscreenCanvas` instance, you might not want to do the song and dance of
319
+ * using `typeof OffscreenCanvas !== 'undefined'` and then shimming
320
+ * `OffscreenCanvas` if the types aren't around.
321
+ *
322
+ * @example
323
+ * if (isInstanceOf(value, 'OffscreenCanvas')) {
324
+ * // value is an OffscreenCanvas
325
+ * }
326
+ *
327
+ * @param value The value to recursively check
328
+ * @param class_ A string or class that the value should be an instance of
329
+ */
330
+ declare function isInstanceOf<T extends AnyClass>(value: unknown, class_: T): value is T;
331
+ declare function isInstanceOf<K extends GlobalClassName>(value: unknown, className: K): value is (typeof globalThis)[K];
332
+ declare function isInstanceOf(value: unknown, className: string): value is object;
267
333
 
268
- 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 };
334
+ 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 };
package/dist/index.d.ts CHANGED
@@ -248,9 +248,53 @@ declare function isPrimitive(payload: any): payload is boolean | null | undefine
248
248
  * @returns {(payload is null | undefined)}
249
249
  */
250
250
  declare const isNullOrUndefined: TypeGuard<any, null | undefined>;
251
+ /**
252
+ * A factory function that creates a function to check if the payload is one of the given types.
253
+ * @example
254
+ * import { isOneOf, isNull, isUndefined } from 'is-what'
255
+ *
256
+ * const isNullOrUndefined = isOneOf(isNull, isUndefined)
257
+ *
258
+ * isNullOrUndefined(null) // true
259
+ * isNullOrUndefined(undefined) // true
260
+ * isNullOrUndefined(123) // false
261
+ */
251
262
  declare function isOneOf<A, B extends A, C extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>): TypeGuard<A, B | C>;
263
+ /**
264
+ * A factory function that creates a function to check if the payload is one of the given types.
265
+ * @example
266
+ * import { isOneOf, isNull, isUndefined } from 'is-what'
267
+ *
268
+ * const isNullOrUndefined = isOneOf(isNull, isUndefined)
269
+ *
270
+ * isNullOrUndefined(null) // true
271
+ * isNullOrUndefined(undefined) // true
272
+ * isNullOrUndefined(123) // false
273
+ */
252
274
  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>;
275
+ /**
276
+ * A factory function that creates a function to check if the payload is one of the given types.
277
+ * @example
278
+ * import { isOneOf, isNull, isUndefined } from 'is-what'
279
+ *
280
+ * const isNullOrUndefined = isOneOf(isNull, isUndefined)
281
+ *
282
+ * isNullOrUndefined(null) // true
283
+ * isNullOrUndefined(undefined) // true
284
+ * isNullOrUndefined(123) // false
285
+ */
253
286
  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>;
287
+ /**
288
+ * A factory function that creates a function to check if the payload is one of the given types.
289
+ * @example
290
+ * import { isOneOf, isNull, isUndefined } from 'is-what'
291
+ *
292
+ * const isNullOrUndefined = isOneOf(isNull, isUndefined)
293
+ *
294
+ * isNullOrUndefined(null) // true
295
+ * isNullOrUndefined(undefined) // true
296
+ * isNullOrUndefined(123) // false
297
+ */
254
298
  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>;
255
299
  /**
256
300
  * Does a generic check to check that the given payload is of a given type.
@@ -264,5 +308,27 @@ declare function isOneOf<A, B extends A, C extends A, D extends A, E extends A,
264
308
  * @returns {payload is T}
265
309
  */
266
310
  declare function isType<T extends AnyFunction | AnyClass>(payload: any, type: T): payload is T;
311
+ type GlobalClassName = {
312
+ [K in keyof typeof globalThis]: (typeof globalThis)[K] extends AnyClass ? K : never;
313
+ }[keyof typeof globalThis];
314
+ /**
315
+ * Checks if a value is an instance of a class or a class name. Useful when you
316
+ * want to check if a value is an instance of a class that may not be defined in
317
+ * the current scope. For example, if you want to check if a value is an
318
+ * `OffscreenCanvas` instance, you might not want to do the song and dance of
319
+ * using `typeof OffscreenCanvas !== 'undefined'` and then shimming
320
+ * `OffscreenCanvas` if the types aren't around.
321
+ *
322
+ * @example
323
+ * if (isInstanceOf(value, 'OffscreenCanvas')) {
324
+ * // value is an OffscreenCanvas
325
+ * }
326
+ *
327
+ * @param value The value to recursively check
328
+ * @param class_ A string or class that the value should be an instance of
329
+ */
330
+ declare function isInstanceOf<T extends AnyClass>(value: unknown, class_: T): value is T;
331
+ declare function isInstanceOf<K extends GlobalClassName>(value: unknown, className: K): value is (typeof globalThis)[K];
332
+ declare function isInstanceOf(value: unknown, className: string): value is object;
267
333
 
268
- 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 };
334
+ 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 };
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ function isPlainObject(payload) {
11
11
  if (getType(payload) !== "Object")
12
12
  return false;
13
13
  const prototype = Object.getPrototypeOf(payload);
14
- return prototype.constructor === Object && prototype === Object.prototype;
14
+ return !!prototype && prototype.constructor === Object && prototype === Object.prototype;
15
15
  }
16
16
  function isObject(payload) {
17
17
  return isPlainObject(payload);
@@ -114,5 +114,22 @@ function isType(payload, type) {
114
114
  const name = type.name;
115
115
  return getType(payload) === name || Boolean(payload && payload.constructor === type);
116
116
  }
117
+ function isInstanceOf(value, classOrClassName) {
118
+ if (typeof classOrClassName === "function") {
119
+ for (let p = value; p; p = Object.getPrototypeOf(p)) {
120
+ if (isType(p, classOrClassName)) {
121
+ return true;
122
+ }
123
+ }
124
+ return false;
125
+ } else {
126
+ for (let p = value; p; p = Object.getPrototypeOf(p)) {
127
+ if (getType(p) === classOrClassName) {
128
+ return true;
129
+ }
130
+ }
131
+ return false;
132
+ }
133
+ }
117
134
 
118
- 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 };
135
+ 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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "is-what",
3
- "version": "4.1.11",
3
+ "version": "4.1.12",
4
4
  "description": "JS type check (TypeScript supported) functions like `isPlainObject() isArray()` etc. A simple & small integration.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -29,6 +29,7 @@
29
29
  "test": "vitest run",
30
30
  "lint": "tsc --noEmit && eslint ./src --ext .ts",
31
31
  "build": "rollup -c ./rollup.config.js",
32
+ "build:docs": "typedoc",
32
33
  "release": "npm run lint && del dist && npm run build && np"
33
34
  },
34
35
  "repository": {
@@ -76,7 +77,8 @@
76
77
  "rollup-plugin-dts": "^5.3.0",
77
78
  "rollup-plugin-esbuild": "^5.0.0",
78
79
  "typescript": "^5.0.4",
79
- "vitest": "^0.31.1"
80
+ "vitest": "^0.31.1",
81
+ "typedoc": "^0.24.7"
80
82
  },
81
83
  "ava": {
82
84
  "extensions": {