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.
Files changed (198) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +16 -16
  3. package/dist/commonjs/assertion/assertion-async.d.ts +2 -1
  4. package/dist/commonjs/assertion/assertion-async.d.ts.map +1 -1
  5. package/dist/commonjs/assertion/assertion-async.js +84 -2
  6. package/dist/commonjs/assertion/assertion-async.js.map +1 -1
  7. package/dist/commonjs/assertion/assertion-sync.d.ts +1 -1
  8. package/dist/commonjs/assertion/assertion-sync.d.ts.map +1 -1
  9. package/dist/commonjs/assertion/assertion-sync.js +5 -1
  10. package/dist/commonjs/assertion/assertion-sync.js.map +1 -1
  11. package/dist/commonjs/assertion/assertion-types.d.ts +39 -84
  12. package/dist/commonjs/assertion/assertion-types.d.ts.map +1 -1
  13. package/dist/commonjs/assertion/assertion.d.ts +1 -1
  14. package/dist/commonjs/assertion/assertion.d.ts.map +1 -1
  15. package/dist/commonjs/assertion/assertion.js +1 -14
  16. package/dist/commonjs/assertion/assertion.js.map +1 -1
  17. package/dist/commonjs/assertion/create.d.ts +5 -33
  18. package/dist/commonjs/assertion/create.d.ts.map +1 -1
  19. package/dist/commonjs/assertion/create.js +17 -6
  20. package/dist/commonjs/assertion/create.js.map +1 -1
  21. package/dist/commonjs/assertion/impl/async.d.ts +122 -21
  22. package/dist/commonjs/assertion/impl/async.d.ts.map +1 -1
  23. package/dist/commonjs/assertion/impl/async.js +114 -90
  24. package/dist/commonjs/assertion/impl/async.js.map +1 -1
  25. package/dist/commonjs/assertion/impl/callback.d.ts +104 -0
  26. package/dist/commonjs/assertion/impl/callback.d.ts.map +1 -0
  27. package/dist/commonjs/assertion/impl/callback.js +694 -0
  28. package/dist/commonjs/assertion/impl/callback.js.map +1 -0
  29. package/dist/commonjs/assertion/impl/index.d.ts +1 -1
  30. package/dist/commonjs/assertion/impl/index.d.ts.map +1 -1
  31. package/dist/commonjs/assertion/impl/index.js.map +1 -1
  32. package/dist/commonjs/assertion/impl/sync-esoteric.js +1 -1
  33. package/dist/commonjs/assertion/impl/sync-esoteric.js.map +1 -1
  34. package/dist/commonjs/assertion/impl/sync-parametric.d.ts +37 -34
  35. package/dist/commonjs/assertion/impl/sync-parametric.d.ts.map +1 -1
  36. package/dist/commonjs/assertion/impl/sync-parametric.js +32 -47
  37. package/dist/commonjs/assertion/impl/sync-parametric.js.map +1 -1
  38. package/dist/commonjs/assertion/impl/sync.d.ts +105 -58
  39. package/dist/commonjs/assertion/impl/sync.d.ts.map +1 -1
  40. package/dist/commonjs/assertion/impl/sync.js +4 -1
  41. package/dist/commonjs/assertion/impl/sync.js.map +1 -1
  42. package/dist/commonjs/bootstrap.d.ts +199 -85
  43. package/dist/commonjs/bootstrap.d.ts.map +1 -1
  44. package/dist/commonjs/bootstrap.js +19 -10
  45. package/dist/commonjs/bootstrap.js.map +1 -1
  46. package/dist/commonjs/constant.js +7 -1
  47. package/dist/commonjs/constant.js.map +1 -1
  48. package/dist/commonjs/error.d.ts +32 -5
  49. package/dist/commonjs/error.d.ts.map +1 -1
  50. package/dist/commonjs/error.js +60 -5
  51. package/dist/commonjs/error.js.map +1 -1
  52. package/dist/commonjs/expect.d.ts +130 -3
  53. package/dist/commonjs/expect.d.ts.map +1 -1
  54. package/dist/commonjs/expect.js +116 -1
  55. package/dist/commonjs/expect.js.map +1 -1
  56. package/dist/commonjs/guards.d.ts +45 -20
  57. package/dist/commonjs/guards.d.ts.map +1 -1
  58. package/dist/commonjs/guards.js +56 -40
  59. package/dist/commonjs/guards.js.map +1 -1
  60. package/dist/commonjs/index.d.ts +241 -86
  61. package/dist/commonjs/index.d.ts.map +1 -1
  62. package/dist/commonjs/index.js +44 -42
  63. package/dist/commonjs/index.js.map +1 -1
  64. package/dist/commonjs/metadata.d.ts +1 -27
  65. package/dist/commonjs/metadata.d.ts.map +1 -1
  66. package/dist/commonjs/metadata.js +16 -15
  67. package/dist/commonjs/metadata.js.map +1 -1
  68. package/dist/commonjs/schema.d.ts +76 -33
  69. package/dist/commonjs/schema.d.ts.map +1 -1
  70. package/dist/commonjs/schema.js +77 -34
  71. package/dist/commonjs/schema.js.map +1 -1
  72. package/dist/commonjs/types.d.ts +480 -39
  73. package/dist/commonjs/types.d.ts.map +1 -1
  74. package/dist/commonjs/types.js +12 -2
  75. package/dist/commonjs/types.js.map +1 -1
  76. package/dist/commonjs/util.d.ts +72 -49
  77. package/dist/commonjs/util.d.ts.map +1 -1
  78. package/dist/commonjs/util.js +175 -155
  79. package/dist/commonjs/util.js.map +1 -1
  80. package/dist/commonjs/value-to-schema.d.ts +122 -0
  81. package/dist/commonjs/value-to-schema.d.ts.map +1 -0
  82. package/dist/commonjs/value-to-schema.js +309 -0
  83. package/dist/commonjs/value-to-schema.js.map +1 -0
  84. package/dist/esm/assertion/assertion-async.d.ts +2 -1
  85. package/dist/esm/assertion/assertion-async.d.ts.map +1 -1
  86. package/dist/esm/assertion/assertion-async.js +85 -3
  87. package/dist/esm/assertion/assertion-async.js.map +1 -1
  88. package/dist/esm/assertion/assertion-sync.d.ts +1 -1
  89. package/dist/esm/assertion/assertion-sync.d.ts.map +1 -1
  90. package/dist/esm/assertion/assertion-sync.js +6 -2
  91. package/dist/esm/assertion/assertion-sync.js.map +1 -1
  92. package/dist/esm/assertion/assertion-types.d.ts +39 -84
  93. package/dist/esm/assertion/assertion-types.d.ts.map +1 -1
  94. package/dist/esm/assertion/assertion.d.ts +1 -1
  95. package/dist/esm/assertion/assertion.d.ts.map +1 -1
  96. package/dist/esm/assertion/assertion.js +1 -14
  97. package/dist/esm/assertion/assertion.js.map +1 -1
  98. package/dist/esm/assertion/create.d.ts +5 -33
  99. package/dist/esm/assertion/create.d.ts.map +1 -1
  100. package/dist/esm/assertion/create.js +14 -4
  101. package/dist/esm/assertion/create.js.map +1 -1
  102. package/dist/esm/assertion/impl/async.d.ts +122 -21
  103. package/dist/esm/assertion/impl/async.d.ts.map +1 -1
  104. package/dist/esm/assertion/impl/async.js +113 -89
  105. package/dist/esm/assertion/impl/async.js.map +1 -1
  106. package/dist/esm/assertion/impl/callback.d.ts +104 -0
  107. package/dist/esm/assertion/impl/callback.d.ts.map +1 -0
  108. package/dist/esm/assertion/impl/callback.js +691 -0
  109. package/dist/esm/assertion/impl/callback.js.map +1 -0
  110. package/dist/esm/assertion/impl/index.d.ts +1 -1
  111. package/dist/esm/assertion/impl/index.d.ts.map +1 -1
  112. package/dist/esm/assertion/impl/index.js +1 -1
  113. package/dist/esm/assertion/impl/index.js.map +1 -1
  114. package/dist/esm/assertion/impl/sync-esoteric.js +2 -2
  115. package/dist/esm/assertion/impl/sync-esoteric.js.map +1 -1
  116. package/dist/esm/assertion/impl/sync-parametric.d.ts +37 -34
  117. package/dist/esm/assertion/impl/sync-parametric.d.ts.map +1 -1
  118. package/dist/esm/assertion/impl/sync-parametric.js +32 -47
  119. package/dist/esm/assertion/impl/sync-parametric.js.map +1 -1
  120. package/dist/esm/assertion/impl/sync.d.ts +105 -58
  121. package/dist/esm/assertion/impl/sync.d.ts.map +1 -1
  122. package/dist/esm/assertion/impl/sync.js +3 -1
  123. package/dist/esm/assertion/impl/sync.js.map +1 -1
  124. package/dist/esm/bootstrap.d.ts +199 -85
  125. package/dist/esm/bootstrap.d.ts.map +1 -1
  126. package/dist/esm/bootstrap.js +19 -10
  127. package/dist/esm/bootstrap.js.map +1 -1
  128. package/dist/esm/constant.js +6 -0
  129. package/dist/esm/constant.js.map +1 -1
  130. package/dist/esm/error.d.ts +32 -5
  131. package/dist/esm/error.d.ts.map +1 -1
  132. package/dist/esm/error.js +59 -5
  133. package/dist/esm/error.js.map +1 -1
  134. package/dist/esm/expect.d.ts +130 -3
  135. package/dist/esm/expect.d.ts.map +1 -1
  136. package/dist/esm/expect.js +117 -2
  137. package/dist/esm/expect.js.map +1 -1
  138. package/dist/esm/guards.d.ts +45 -20
  139. package/dist/esm/guards.d.ts.map +1 -1
  140. package/dist/esm/guards.js +48 -31
  141. package/dist/esm/guards.js.map +1 -1
  142. package/dist/esm/index.d.ts +241 -86
  143. package/dist/esm/index.d.ts.map +1 -1
  144. package/dist/esm/index.js +46 -7
  145. package/dist/esm/index.js.map +1 -1
  146. package/dist/esm/metadata.d.ts +1 -27
  147. package/dist/esm/metadata.d.ts.map +1 -1
  148. package/dist/esm/metadata.js +2 -1
  149. package/dist/esm/metadata.js.map +1 -1
  150. package/dist/esm/schema.d.ts +76 -33
  151. package/dist/esm/schema.d.ts.map +1 -1
  152. package/dist/esm/schema.js +77 -34
  153. package/dist/esm/schema.js.map +1 -1
  154. package/dist/esm/types.d.ts +480 -39
  155. package/dist/esm/types.d.ts.map +1 -1
  156. package/dist/esm/types.js +12 -2
  157. package/dist/esm/types.js.map +1 -1
  158. package/dist/esm/util.d.ts +72 -49
  159. package/dist/esm/util.d.ts.map +1 -1
  160. package/dist/esm/util.js +159 -153
  161. package/dist/esm/util.js.map +1 -1
  162. package/dist/esm/value-to-schema.d.ts +122 -0
  163. package/dist/esm/value-to-schema.d.ts.map +1 -0
  164. package/dist/esm/value-to-schema.js +305 -0
  165. package/dist/esm/value-to-schema.js.map +1 -0
  166. package/package.json +94 -17
  167. package/src/assertion/assertion-async.ts +113 -3
  168. package/src/assertion/assertion-sync.ts +5 -2
  169. package/src/assertion/assertion-types.ts +52 -45
  170. package/src/assertion/assertion.ts +2 -17
  171. package/src/assertion/create.ts +16 -65
  172. package/src/assertion/impl/async.ts +132 -92
  173. package/src/assertion/impl/callback.ts +882 -0
  174. package/src/assertion/impl/index.ts +1 -1
  175. package/src/assertion/impl/sync-esoteric.ts +2 -2
  176. package/src/assertion/impl/sync-parametric.ts +41 -49
  177. package/src/assertion/impl/sync.ts +3 -0
  178. package/src/bootstrap.ts +21 -11
  179. package/src/constant.ts +8 -0
  180. package/src/error.ts +75 -4
  181. package/src/expect.ts +275 -20
  182. package/src/guards.ts +74 -69
  183. package/src/index.ts +72 -11
  184. package/src/metadata.ts +3 -4
  185. package/src/schema.ts +80 -36
  186. package/src/types.ts +625 -72
  187. package/src/util.ts +174 -222
  188. package/src/value-to-schema.ts +464 -0
  189. package/dist/commonjs/api.d.ts +0 -93
  190. package/dist/commonjs/api.d.ts.map +0 -1
  191. package/dist/commonjs/api.js +0 -8
  192. package/dist/commonjs/api.js.map +0 -1
  193. package/dist/esm/api.d.ts +0 -93
  194. package/dist/esm/api.d.ts.map +0 -1
  195. package/dist/esm/api.js +0 -7
  196. package/dist/esm/api.js.map +0 -1
  197. package/src/api.ts +0 -149
  198. 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, Promise-like objects, and
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 { AssertionFailure } from './assertion/assertion-types.js';
14
- import type { Constructor } from './types.js';
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 (v4), determined by
17
- * the presence of an internal `def.type` field.
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 Zod-like
41
+ * @returns Whether the value is `ZodType`-like
25
42
  */
26
- export declare const isZodType: (value: unknown) => value is z.ZodType;
43
+ export declare function isZodType(value: unknown): value is z.ZodType;
27
44
  /**
28
- * Returns true if the given value is a {@link z.ZodPromise} schema.
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 class).
66
+ * Returns `true` if the given value is a constructable function (i.e., a
67
+ * class).
43
68
  *
44
- * This may be the only way we can determine, at runtime, if a function is a
45
- * constructor without actually calling it.
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 isConstructable: (fn: unknown) => fn is Constructor;
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;;;;;;;;;GASG;AAEH,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EACV,gBAAgB,EAGjB,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,GAAI,OAAO,OAAO,KAAG,KAA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ions for runtime type checking,
6
- * including guards for Zod schemas, constructors, Promise-like objects, and
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.isType = exports.isPhraseLiteral = exports.isPhraseLiteralChoice = exports.isNullOrNonObject = exports.isNonNullObject = exports.isString = exports.isAsyncFunction = exports.isAssertionFailure = exports.isFunction = exports.isBoolean = exports.isConstructable = exports.isPromiseLike = exports.isZodPromise = exports.isZodType = void 0;
14
- const zod_1 = require("zod");
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
- * Returns true if the given value looks like a Zod schema (v4), determined by
17
- * the presence of an internal `def.type` field.
36
+ * Type guard for a plain object.
18
37
  *
19
- * Note: This relies on Zod's internal shape and is intended for runtime
20
- * discrimination within this library.
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 isZodType = (value) => !!(value &&
27
- typeof value === 'object' &&
28
- 'def' in value &&
29
- value.def &&
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) => (0, exports.isZodType)(value) && value.def.type === 'promise';
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) => !!(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 class).
62
+ * Returns `true` if the given value is a constructable function (i.e., a
63
+ * class).
54
64
  *
55
- * This may be the only way we can determine, at runtime, if a function is a
56
- * constructor without actually calling it.
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 isConstructable = (fn) => {
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.isConstructable = isConstructable;
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 = zod_1.z.object({
93
- actual: zod_1.z
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: zod_1.z
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: zod_1.z
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
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