bupkis 0.1.2 → 0.3.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/CHANGELOG.md +28 -0
- package/README.md +16 -16
- package/dist/commonjs/assertion/assertion-async.d.ts +2 -1
- package/dist/commonjs/assertion/assertion-async.d.ts.map +1 -1
- package/dist/commonjs/assertion/assertion-async.js +84 -2
- package/dist/commonjs/assertion/assertion-async.js.map +1 -1
- package/dist/commonjs/assertion/assertion-sync.d.ts +1 -1
- package/dist/commonjs/assertion/assertion-sync.d.ts.map +1 -1
- package/dist/commonjs/assertion/assertion-sync.js +5 -1
- package/dist/commonjs/assertion/assertion-sync.js.map +1 -1
- package/dist/commonjs/assertion/assertion-types.d.ts +39 -84
- package/dist/commonjs/assertion/assertion-types.d.ts.map +1 -1
- package/dist/commonjs/assertion/assertion.d.ts +1 -1
- package/dist/commonjs/assertion/assertion.d.ts.map +1 -1
- package/dist/commonjs/assertion/assertion.js +1 -14
- package/dist/commonjs/assertion/assertion.js.map +1 -1
- package/dist/commonjs/assertion/create.d.ts +5 -33
- package/dist/commonjs/assertion/create.d.ts.map +1 -1
- package/dist/commonjs/assertion/create.js +17 -6
- package/dist/commonjs/assertion/create.js.map +1 -1
- package/dist/commonjs/assertion/impl/async.d.ts +122 -21
- package/dist/commonjs/assertion/impl/async.d.ts.map +1 -1
- package/dist/commonjs/assertion/impl/async.js +114 -90
- package/dist/commonjs/assertion/impl/async.js.map +1 -1
- package/dist/commonjs/assertion/impl/callback.d.ts +104 -0
- package/dist/commonjs/assertion/impl/callback.d.ts.map +1 -0
- package/dist/commonjs/assertion/impl/callback.js +694 -0
- package/dist/commonjs/assertion/impl/callback.js.map +1 -0
- package/dist/commonjs/assertion/impl/index.d.ts +1 -1
- package/dist/commonjs/assertion/impl/index.d.ts.map +1 -1
- package/dist/commonjs/assertion/impl/index.js.map +1 -1
- package/dist/commonjs/assertion/impl/sync-esoteric.js +1 -1
- package/dist/commonjs/assertion/impl/sync-esoteric.js.map +1 -1
- package/dist/commonjs/assertion/impl/sync-parametric.d.ts +37 -34
- package/dist/commonjs/assertion/impl/sync-parametric.d.ts.map +1 -1
- package/dist/commonjs/assertion/impl/sync-parametric.js +32 -47
- package/dist/commonjs/assertion/impl/sync-parametric.js.map +1 -1
- package/dist/commonjs/assertion/impl/sync.d.ts +105 -58
- package/dist/commonjs/assertion/impl/sync.d.ts.map +1 -1
- package/dist/commonjs/assertion/impl/sync.js +4 -1
- package/dist/commonjs/assertion/impl/sync.js.map +1 -1
- package/dist/commonjs/bootstrap.d.ts +199 -85
- package/dist/commonjs/bootstrap.d.ts.map +1 -1
- package/dist/commonjs/bootstrap.js +19 -10
- package/dist/commonjs/bootstrap.js.map +1 -1
- package/dist/commonjs/constant.js +7 -1
- package/dist/commonjs/constant.js.map +1 -1
- package/dist/commonjs/error.d.ts +32 -5
- package/dist/commonjs/error.d.ts.map +1 -1
- package/dist/commonjs/error.js +60 -5
- package/dist/commonjs/error.js.map +1 -1
- package/dist/commonjs/expect.d.ts +130 -3
- package/dist/commonjs/expect.d.ts.map +1 -1
- package/dist/commonjs/expect.js +116 -1
- package/dist/commonjs/expect.js.map +1 -1
- package/dist/commonjs/guards.d.ts +45 -20
- package/dist/commonjs/guards.d.ts.map +1 -1
- package/dist/commonjs/guards.js +56 -40
- package/dist/commonjs/guards.js.map +1 -1
- package/dist/commonjs/index.d.ts +241 -86
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +44 -42
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/metadata.d.ts +1 -27
- package/dist/commonjs/metadata.d.ts.map +1 -1
- package/dist/commonjs/metadata.js +16 -15
- package/dist/commonjs/metadata.js.map +1 -1
- package/dist/commonjs/schema.d.ts +76 -33
- package/dist/commonjs/schema.d.ts.map +1 -1
- package/dist/commonjs/schema.js +77 -34
- package/dist/commonjs/schema.js.map +1 -1
- package/dist/commonjs/types.d.ts +480 -39
- package/dist/commonjs/types.d.ts.map +1 -1
- package/dist/commonjs/types.js +12 -2
- package/dist/commonjs/types.js.map +1 -1
- package/dist/commonjs/util.d.ts +72 -49
- package/dist/commonjs/util.d.ts.map +1 -1
- package/dist/commonjs/util.js +175 -155
- package/dist/commonjs/util.js.map +1 -1
- package/dist/commonjs/value-to-schema.d.ts +122 -0
- package/dist/commonjs/value-to-schema.d.ts.map +1 -0
- package/dist/commonjs/value-to-schema.js +309 -0
- package/dist/commonjs/value-to-schema.js.map +1 -0
- package/dist/esm/assertion/assertion-async.d.ts +2 -1
- package/dist/esm/assertion/assertion-async.d.ts.map +1 -1
- package/dist/esm/assertion/assertion-async.js +85 -3
- package/dist/esm/assertion/assertion-async.js.map +1 -1
- package/dist/esm/assertion/assertion-sync.d.ts +1 -1
- package/dist/esm/assertion/assertion-sync.d.ts.map +1 -1
- package/dist/esm/assertion/assertion-sync.js +6 -2
- package/dist/esm/assertion/assertion-sync.js.map +1 -1
- package/dist/esm/assertion/assertion-types.d.ts +39 -84
- package/dist/esm/assertion/assertion-types.d.ts.map +1 -1
- package/dist/esm/assertion/assertion.d.ts +1 -1
- package/dist/esm/assertion/assertion.d.ts.map +1 -1
- package/dist/esm/assertion/assertion.js +1 -14
- package/dist/esm/assertion/assertion.js.map +1 -1
- package/dist/esm/assertion/create.d.ts +5 -33
- package/dist/esm/assertion/create.d.ts.map +1 -1
- package/dist/esm/assertion/create.js +14 -4
- package/dist/esm/assertion/create.js.map +1 -1
- package/dist/esm/assertion/impl/async.d.ts +122 -21
- package/dist/esm/assertion/impl/async.d.ts.map +1 -1
- package/dist/esm/assertion/impl/async.js +113 -89
- package/dist/esm/assertion/impl/async.js.map +1 -1
- package/dist/esm/assertion/impl/callback.d.ts +104 -0
- package/dist/esm/assertion/impl/callback.d.ts.map +1 -0
- package/dist/esm/assertion/impl/callback.js +691 -0
- package/dist/esm/assertion/impl/callback.js.map +1 -0
- package/dist/esm/assertion/impl/index.d.ts +1 -1
- package/dist/esm/assertion/impl/index.d.ts.map +1 -1
- package/dist/esm/assertion/impl/index.js +1 -1
- package/dist/esm/assertion/impl/index.js.map +1 -1
- package/dist/esm/assertion/impl/sync-esoteric.js +2 -2
- package/dist/esm/assertion/impl/sync-esoteric.js.map +1 -1
- package/dist/esm/assertion/impl/sync-parametric.d.ts +37 -34
- package/dist/esm/assertion/impl/sync-parametric.d.ts.map +1 -1
- package/dist/esm/assertion/impl/sync-parametric.js +32 -47
- package/dist/esm/assertion/impl/sync-parametric.js.map +1 -1
- package/dist/esm/assertion/impl/sync.d.ts +105 -58
- package/dist/esm/assertion/impl/sync.d.ts.map +1 -1
- package/dist/esm/assertion/impl/sync.js +3 -1
- package/dist/esm/assertion/impl/sync.js.map +1 -1
- package/dist/esm/bootstrap.d.ts +199 -85
- package/dist/esm/bootstrap.d.ts.map +1 -1
- package/dist/esm/bootstrap.js +19 -10
- package/dist/esm/bootstrap.js.map +1 -1
- package/dist/esm/constant.js +6 -0
- package/dist/esm/constant.js.map +1 -1
- package/dist/esm/error.d.ts +32 -5
- package/dist/esm/error.d.ts.map +1 -1
- package/dist/esm/error.js +59 -5
- package/dist/esm/error.js.map +1 -1
- package/dist/esm/expect.d.ts +130 -3
- package/dist/esm/expect.d.ts.map +1 -1
- package/dist/esm/expect.js +117 -2
- package/dist/esm/expect.js.map +1 -1
- package/dist/esm/guards.d.ts +45 -20
- package/dist/esm/guards.d.ts.map +1 -1
- package/dist/esm/guards.js +48 -31
- package/dist/esm/guards.js.map +1 -1
- package/dist/esm/index.d.ts +241 -86
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +46 -7
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/metadata.d.ts +1 -27
- package/dist/esm/metadata.d.ts.map +1 -1
- package/dist/esm/metadata.js +2 -1
- package/dist/esm/metadata.js.map +1 -1
- package/dist/esm/schema.d.ts +76 -33
- package/dist/esm/schema.d.ts.map +1 -1
- package/dist/esm/schema.js +77 -34
- package/dist/esm/schema.js.map +1 -1
- package/dist/esm/types.d.ts +480 -39
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/types.js +12 -2
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/util.d.ts +72 -49
- package/dist/esm/util.d.ts.map +1 -1
- package/dist/esm/util.js +159 -153
- package/dist/esm/util.js.map +1 -1
- package/dist/esm/value-to-schema.d.ts +122 -0
- package/dist/esm/value-to-schema.d.ts.map +1 -0
- package/dist/esm/value-to-schema.js +305 -0
- package/dist/esm/value-to-schema.js.map +1 -0
- package/package.json +94 -17
- package/src/assertion/assertion-async.ts +113 -3
- package/src/assertion/assertion-sync.ts +5 -2
- package/src/assertion/assertion-types.ts +52 -45
- package/src/assertion/assertion.ts +2 -17
- package/src/assertion/create.ts +16 -65
- package/src/assertion/impl/async.ts +132 -92
- package/src/assertion/impl/callback.ts +882 -0
- package/src/assertion/impl/index.ts +1 -1
- package/src/assertion/impl/sync-esoteric.ts +2 -2
- package/src/assertion/impl/sync-parametric.ts +41 -49
- package/src/assertion/impl/sync.ts +3 -0
- package/src/bootstrap.ts +21 -11
- package/src/constant.ts +8 -0
- package/src/error.ts +75 -4
- package/src/expect.ts +275 -20
- package/src/guards.ts +74 -69
- package/src/index.ts +72 -11
- package/src/metadata.ts +3 -4
- package/src/schema.ts +80 -36
- package/src/types.ts +625 -72
- package/src/util.ts +174 -222
- package/src/value-to-schema.ts +464 -0
- package/dist/commonjs/api.d.ts +0 -93
- package/dist/commonjs/api.d.ts.map +0 -1
- package/dist/commonjs/api.js +0 -8
- package/dist/commonjs/api.js.map +0 -1
- package/dist/esm/api.d.ts +0 -93
- package/dist/esm/api.d.ts.map +0 -1
- package/dist/esm/api.js +0 -7
- package/dist/esm/api.js.map +0 -1
- package/src/api.ts +0 -149
- package/src/schema.md +0 -15
|
@@ -2,30 +2,54 @@
|
|
|
2
2
|
* Type guard functions and runtime type checking utilities.
|
|
3
3
|
*
|
|
4
4
|
* This module provides various type guard functions for runtime type checking,
|
|
5
|
-
* including guards for Zod schemas, constructors,
|
|
6
|
-
* assertion parts. These are used throughout the library for safe type
|
|
5
|
+
* including guards for Zod schemas, constructors, {@link PromiseLike} objects,
|
|
6
|
+
* and assertion parts. These are used throughout the library for safe type
|
|
7
7
|
* narrowing and validation.
|
|
8
8
|
*
|
|
9
|
+
* @category API
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import * as guards from 'bupkis/guards';
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
9
16
|
* @packageDocumentation
|
|
10
17
|
*/
|
|
11
18
|
import { type Primitive } from 'type-fest';
|
|
12
|
-
import { z } from 'zod';
|
|
13
|
-
import type {
|
|
14
|
-
|
|
19
|
+
import { z } from 'zod/v4';
|
|
20
|
+
import type { Constructor, ZodTypeMap } from './types.js';
|
|
21
|
+
/**
|
|
22
|
+
* Returns `true` if the given value looks like a Zod v4 schema, determined by
|
|
23
|
+
* the presence of an internal {@link z.core.$ZodTypeDef} field.
|
|
24
|
+
*
|
|
25
|
+
* Note: This relies on Zod's internal shape and is intended for runtime
|
|
26
|
+
* discrimination within this library.
|
|
27
|
+
*
|
|
28
|
+
* @template T - The specific ZodType to check for (based on def.type)
|
|
29
|
+
* @param value - Value to test
|
|
30
|
+
* @returns Whether the value is `ZodType`-like
|
|
31
|
+
*/
|
|
32
|
+
export declare function isZodType<T extends keyof ZodTypeMap>(value: unknown, type: T): value is ZodTypeMap[T];
|
|
15
33
|
/**
|
|
16
|
-
* Returns true if the given value looks like a Zod schema
|
|
17
|
-
* the presence of an internal
|
|
34
|
+
* Returns `true` if the given value looks like a Zod v4 schema, determined by
|
|
35
|
+
* the presence of an internal {@link z.core.$ZodTypeDef} field.
|
|
18
36
|
*
|
|
19
37
|
* Note: This relies on Zod's internal shape and is intended for runtime
|
|
20
38
|
* discrimination within this library.
|
|
21
39
|
*
|
|
22
|
-
* @template T
|
|
23
40
|
* @param value - Value to test
|
|
24
|
-
* @returns Whether the value is
|
|
41
|
+
* @returns Whether the value is `ZodType`-like
|
|
25
42
|
*/
|
|
26
|
-
export declare
|
|
43
|
+
export declare function isZodType(value: unknown): value is z.ZodType;
|
|
27
44
|
/**
|
|
28
|
-
*
|
|
45
|
+
* Type guard for a plain object.
|
|
46
|
+
*
|
|
47
|
+
* @param value Value to test
|
|
48
|
+
* @returns `true` if the value is a plain object, `false` otherwise
|
|
49
|
+
*/
|
|
50
|
+
export declare const isObject: (value: unknown) => value is NonNullable<object>;
|
|
51
|
+
/**
|
|
52
|
+
* Returns `true` if the given value is a {@link z.ZodPromise} schema.
|
|
29
53
|
*
|
|
30
54
|
* @param value - Value to test
|
|
31
55
|
* @returns `true` if the value is a `ZodPromise` schema; `false` otherwise
|
|
@@ -39,15 +63,21 @@ export declare const isZodPromise: (value: unknown) => value is z.ZodPromise;
|
|
|
39
63
|
*/
|
|
40
64
|
export declare const isPromiseLike: (value: unknown) => value is PromiseLike<unknown>;
|
|
41
65
|
/**
|
|
42
|
-
* Returns true if the given value is a constructable function (i.e., a
|
|
66
|
+
* Returns `true` if the given value is a constructable function (i.e., a
|
|
67
|
+
* class).
|
|
43
68
|
*
|
|
44
|
-
* This
|
|
45
|
-
*
|
|
69
|
+
* This works by wrapping `fn` in a {@link Proxy}, attaching a no-op
|
|
70
|
+
* {@link ProxyHandler.construct} trap to it, then attempting to construct the
|
|
71
|
+
* proxy via `new`.
|
|
46
72
|
*
|
|
73
|
+
* @privateRemarks
|
|
74
|
+
* This may be the only way we can determine, at runtime, if a function is a
|
|
75
|
+
* constructor without actually calling it. I am unsure if this only works for
|
|
76
|
+
* classes.
|
|
47
77
|
* @param fn - Function to test
|
|
48
78
|
* @returns Whether the function is constructable
|
|
49
79
|
*/
|
|
50
|
-
export declare const
|
|
80
|
+
export declare const isConstructible: (fn: unknown) => fn is Constructor;
|
|
51
81
|
/**
|
|
52
82
|
* Type guard for a boolean value
|
|
53
83
|
*
|
|
@@ -62,8 +92,6 @@ export declare const isBoolean: (value: unknown) => value is boolean;
|
|
|
62
92
|
* @returns `true` if the value is a function, `false` otherwise
|
|
63
93
|
*/
|
|
64
94
|
export declare const isFunction: (value: unknown) => value is (...args: any[]) => any;
|
|
65
|
-
export declare const isAssertionFailure: (value: unknown) => value is AssertionFailure;
|
|
66
|
-
export declare const isAsyncFunction: (value: unknown) => value is (...args: any[]) => Promise<any>;
|
|
67
95
|
/**
|
|
68
96
|
* Type guard for a string value
|
|
69
97
|
*
|
|
@@ -85,9 +113,6 @@ export declare const isNonNullObject: (value: unknown) => value is object;
|
|
|
85
113
|
* @returns `true` if the value is null or not an object, `false` otherwise
|
|
86
114
|
*/
|
|
87
115
|
export declare const isNullOrNonObject: (value: unknown) => value is null | Primitive;
|
|
88
|
-
export type PrimitiveTypeName = 'bigint' | 'boolean' | 'function' | 'null' | 'number' | 'object' | 'string' | 'symbol' | 'undefined';
|
|
89
|
-
export type PrimitiveTypeNameToType<T extends PrimitiveTypeName> = T extends 'undefined' ? undefined : T extends 'object' ? null | object : T extends 'function' ? (...args: any[]) => any : T extends 'string' ? string : T extends 'number' ? number : T extends 'boolean' ? boolean : T extends 'bigint' ? bigint : T extends 'symbol' ? symbol : never;
|
|
90
|
-
export declare const isType: <T extends PrimitiveTypeName>(a: unknown, b: T) => a is PrimitiveTypeNameToType<T>;
|
|
91
116
|
export declare const isA: <T extends Constructor>(value: unknown, ctor: T) => value is InstanceType<T>;
|
|
92
117
|
export declare const isError: (value: unknown) => value is Error;
|
|
93
118
|
//# sourceMappingURL=guards.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../../src/guards.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../../src/guards.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,CAAC,EAAE,MAAM,Q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,KAAG,KAAK,IAAI,IAAI,GAAG,SACtB,CAAC;AAyB9C,eAAO,MAAM,GAAG,GAAI,CAAC,SAAS,WAAW,EACvC,OAAO,OAAO,EACd,MAAM,CAAC,KACN,KAAK,IAAI,YAAY,CAAC,CAAC,CAEzB,CAAC;AAEF,eAAO,MAAM,OAAO,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,KAA0B,CAAC"}
|
package/dist/commonjs/guards.js
CHANGED
|
@@ -3,40 +3,52 @@
|
|
|
3
3
|
* Type guard functions and runtime type checking utilities.
|
|
4
4
|
*
|
|
5
5
|
* This module provides various type guard functions for runtime type checking,
|
|
6
|
-
* including guards for Zod schemas, constructors,
|
|
7
|
-
* assertion parts. These are used throughout the library for safe type
|
|
6
|
+
* including guards for Zod schemas, constructors, {@link PromiseLike} objects,
|
|
7
|
+
* and assertion parts. These are used throughout the library for safe type
|
|
8
8
|
* narrowing and validation.
|
|
9
9
|
*
|
|
10
|
+
* @category API
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import * as guards from 'bupkis/guards';
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
10
17
|
* @packageDocumentation
|
|
11
18
|
*/
|
|
12
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.isError = exports.isA = exports.
|
|
14
|
-
|
|
20
|
+
exports.isError = exports.isA = exports.isPhraseLiteral = exports.isPhraseLiteralChoice = exports.isNullOrNonObject = exports.isNonNullObject = exports.isString = exports.isAssertionFailure = exports.isFunction = exports.isBoolean = exports.isConstructible = exports.isPromiseLike = exports.isZodPromise = exports.isObject = void 0;
|
|
21
|
+
exports.isZodType = isZodType;
|
|
22
|
+
const v4_1 = require("zod/v4");
|
|
23
|
+
function isZodType(value, type) {
|
|
24
|
+
const isValid = (0, exports.isObject)(value) &&
|
|
25
|
+
'def' in value &&
|
|
26
|
+
!!value.def &&
|
|
27
|
+
typeof value.def === 'object' &&
|
|
28
|
+
'type' in value.def;
|
|
29
|
+
if (!isValid)
|
|
30
|
+
return false;
|
|
31
|
+
if (type === undefined)
|
|
32
|
+
return true;
|
|
33
|
+
return value.def.type === type;
|
|
34
|
+
}
|
|
15
35
|
/**
|
|
16
|
-
*
|
|
17
|
-
* the presence of an internal `def.type` field.
|
|
36
|
+
* Type guard for a plain object.
|
|
18
37
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* @template T
|
|
23
|
-
* @param value - Value to test
|
|
24
|
-
* @returns Whether the value is Zod-like
|
|
38
|
+
* @param value Value to test
|
|
39
|
+
* @returns `true` if the value is a plain object, `false` otherwise
|
|
25
40
|
*/
|
|
26
|
-
const
|
|
27
|
-
typeof value === 'object' &&
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
typeof value.def === 'object' &&
|
|
31
|
-
'type' in value.def);
|
|
32
|
-
exports.isZodType = isZodType;
|
|
41
|
+
const isObject = (value) => {
|
|
42
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
43
|
+
};
|
|
44
|
+
exports.isObject = isObject;
|
|
33
45
|
/**
|
|
34
|
-
* Returns true if the given value is a {@link z.ZodPromise} schema.
|
|
46
|
+
* Returns `true` if the given value is a {@link z.ZodPromise} schema.
|
|
35
47
|
*
|
|
36
48
|
* @param value - Value to test
|
|
37
49
|
* @returns `true` if the value is a `ZodPromise` schema; `false` otherwise
|
|
38
50
|
*/
|
|
39
|
-
const isZodPromise = (value) =>
|
|
51
|
+
const isZodPromise = (value) => isZodType(value, 'promise');
|
|
40
52
|
exports.isZodPromise = isZodPromise;
|
|
41
53
|
/**
|
|
42
54
|
* Checks if a value is "promise-like", meaning it is a "thenable" object.
|
|
@@ -44,21 +56,24 @@ exports.isZodPromise = isZodPromise;
|
|
|
44
56
|
* @param value - Value to test
|
|
45
57
|
* @returns `true` if the value is promise-like, `false` otherwise
|
|
46
58
|
*/
|
|
47
|
-
const isPromiseLike = (value) =>
|
|
48
|
-
typeof value === 'object' &&
|
|
49
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
50
|
-
typeof value.then === 'function');
|
|
59
|
+
const isPromiseLike = (value) => (0, exports.isObject)(value) && 'then' in value && (0, exports.isFunction)(value.then);
|
|
51
60
|
exports.isPromiseLike = isPromiseLike;
|
|
52
61
|
/**
|
|
53
|
-
* Returns true if the given value is a constructable function (i.e., a
|
|
62
|
+
* Returns `true` if the given value is a constructable function (i.e., a
|
|
63
|
+
* class).
|
|
54
64
|
*
|
|
55
|
-
* This
|
|
56
|
-
*
|
|
65
|
+
* This works by wrapping `fn` in a {@link Proxy}, attaching a no-op
|
|
66
|
+
* {@link ProxyHandler.construct} trap to it, then attempting to construct the
|
|
67
|
+
* proxy via `new`.
|
|
57
68
|
*
|
|
69
|
+
* @privateRemarks
|
|
70
|
+
* This may be the only way we can determine, at runtime, if a function is a
|
|
71
|
+
* constructor without actually calling it. I am unsure if this only works for
|
|
72
|
+
* classes.
|
|
58
73
|
* @param fn - Function to test
|
|
59
74
|
* @returns Whether the function is constructable
|
|
60
75
|
*/
|
|
61
|
-
const
|
|
76
|
+
const isConstructible = (fn) => {
|
|
62
77
|
if (fn === Symbol || fn === BigInt) {
|
|
63
78
|
return false;
|
|
64
79
|
}
|
|
@@ -72,7 +87,7 @@ const isConstructable = (fn) => {
|
|
|
72
87
|
return false;
|
|
73
88
|
}
|
|
74
89
|
};
|
|
75
|
-
exports.
|
|
90
|
+
exports.isConstructible = isConstructible;
|
|
76
91
|
/**
|
|
77
92
|
* Type guard for a boolean value
|
|
78
93
|
*
|
|
@@ -89,24 +104,29 @@ exports.isBoolean = isBoolean;
|
|
|
89
104
|
*/
|
|
90
105
|
const isFunction = (value) => typeof value === 'function';
|
|
91
106
|
exports.isFunction = isFunction;
|
|
92
|
-
const AssertionFailureSchema =
|
|
93
|
-
actual:
|
|
107
|
+
const AssertionFailureSchema = v4_1.z.object({
|
|
108
|
+
actual: v4_1.z
|
|
94
109
|
.unknown()
|
|
95
110
|
.optional()
|
|
96
111
|
.describe('The actual value or description of what actually occurred'),
|
|
97
|
-
expected:
|
|
112
|
+
expected: v4_1.z
|
|
98
113
|
.unknown()
|
|
99
114
|
.optional()
|
|
100
115
|
.describe('The expected value or description of what was expected to occur'),
|
|
101
|
-
message:
|
|
116
|
+
message: v4_1.z
|
|
102
117
|
.string()
|
|
103
118
|
.optional()
|
|
104
119
|
.describe('A human-readable message describing the failure'),
|
|
105
120
|
});
|
|
121
|
+
/**
|
|
122
|
+
* Type guard for a {@link AssertionFailure} object
|
|
123
|
+
*
|
|
124
|
+
* @param value Value to check
|
|
125
|
+
* @returns `true` if the value is an `AssertionFailure`, `false` otherwise
|
|
126
|
+
* @internal
|
|
127
|
+
*/
|
|
106
128
|
const isAssertionFailure = (value) => AssertionFailureSchema.safeParse(value).success;
|
|
107
129
|
exports.isAssertionFailure = isAssertionFailure;
|
|
108
|
-
const isAsyncFunction = (value) => (0, exports.isFunction)(value) && value.constructor.name === 'AsyncFunction';
|
|
109
|
-
exports.isAsyncFunction = isAsyncFunction;
|
|
110
130
|
/**
|
|
111
131
|
* Type guard for a string value
|
|
112
132
|
*
|
|
@@ -150,10 +170,6 @@ exports.isPhraseLiteralChoice = isPhraseLiteralChoice;
|
|
|
150
170
|
*/
|
|
151
171
|
const isPhraseLiteral = (value) => (0, exports.isString)(value) && !value.startsWith('not ');
|
|
152
172
|
exports.isPhraseLiteral = isPhraseLiteral;
|
|
153
|
-
const isType = (a, b) => {
|
|
154
|
-
return typeof a === b;
|
|
155
|
-
};
|
|
156
|
-
exports.isType = isType;
|
|
157
173
|
const isA = (value, ctor) => {
|
|
158
174
|
return (0, exports.isNonNullObject)(value) && value instanceof ctor;
|
|
159
175
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guards.js","sourceRoot":"","sources":["../../src/guards.ts"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"guards.js","sourceRoot":"","sources":["../../src/guards.ts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