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,
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
- import { z } from 'zod';
18
+ import { z } from 'zod/v4';
19
+ export function isZodType(value, type) {
20
+ const isValid = isObject(value) &&
21
+ 'def' in value &&
22
+ !!value.def &&
23
+ typeof value.def === 'object' &&
24
+ 'type' in value.def;
25
+ if (!isValid)
26
+ return false;
27
+ if (type === undefined)
28
+ return true;
29
+ return value.def.type === type;
30
+ }
12
31
  /**
13
- * Returns true if the given value looks like a Zod schema (v4), determined by
14
- * the presence of an internal `def.type` field.
32
+ * Type guard for a plain object.
15
33
  *
16
- * Note: This relies on Zod's internal shape and is intended for runtime
17
- * discrimination within this library.
18
- *
19
- * @template T
20
- * @param value - Value to test
21
- * @returns Whether the value is Zod-like
34
+ * @param value Value to test
35
+ * @returns `true` if the value is a plain object, `false` otherwise
22
36
  */
23
- export const isZodType = (value) => !!(value &&
24
- typeof value === 'object' &&
25
- 'def' in value &&
26
- value.def &&
27
- typeof value.def === 'object' &&
28
- 'type' in value.def);
37
+ export const isObject = (value) => {
38
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
39
+ };
29
40
  /**
30
- * Returns true if the given value is a {@link z.ZodPromise} schema.
41
+ * Returns `true` if the given value is a {@link z.ZodPromise} schema.
31
42
  *
32
43
  * @param value - Value to test
33
44
  * @returns `true` if the value is a `ZodPromise` schema; `false` otherwise
34
45
  */
35
- export const isZodPromise = (value) => isZodType(value) && value.def.type === 'promise';
46
+ export const isZodPromise = (value) => isZodType(value, 'promise');
36
47
  /**
37
48
  * Checks if a value is "promise-like", meaning it is a "thenable" object.
38
49
  *
39
50
  * @param value - Value to test
40
51
  * @returns `true` if the value is promise-like, `false` otherwise
41
52
  */
42
- export const isPromiseLike = (value) => !!(value &&
43
- typeof value === 'object' &&
44
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
45
- typeof value.then === 'function');
53
+ export const isPromiseLike = (value) => isObject(value) && 'then' in value && isFunction(value.then);
46
54
  /**
47
- * Returns true if the given value is a constructable function (i.e., a class).
55
+ * Returns `true` if the given value is a constructable function (i.e., a
56
+ * class).
48
57
  *
49
- * This may be the only way we can determine, at runtime, if a function is a
50
- * constructor without actually calling it.
58
+ * This works by wrapping `fn` in a {@link Proxy}, attaching a no-op
59
+ * {@link ProxyHandler.construct} trap to it, then attempting to construct the
60
+ * proxy via `new`.
51
61
  *
62
+ * @privateRemarks
63
+ * This may be the only way we can determine, at runtime, if a function is a
64
+ * constructor without actually calling it. I am unsure if this only works for
65
+ * classes.
52
66
  * @param fn - Function to test
53
67
  * @returns Whether the function is constructable
54
68
  */
55
- export const isConstructable = (fn) => {
69
+ export const isConstructible = (fn) => {
56
70
  if (fn === Symbol || fn === BigInt) {
57
71
  return false;
58
72
  }
@@ -94,8 +108,14 @@ const AssertionFailureSchema = z.object({
94
108
  .optional()
95
109
  .describe('A human-readable message describing the failure'),
96
110
  });
111
+ /**
112
+ * Type guard for a {@link AssertionFailure} object
113
+ *
114
+ * @param value Value to check
115
+ * @returns `true` if the value is an `AssertionFailure`, `false` otherwise
116
+ * @internal
117
+ */
97
118
  export const isAssertionFailure = (value) => AssertionFailureSchema.safeParse(value).success;
98
- export const isAsyncFunction = (value) => isFunction(value) && value.constructor.name === 'AsyncFunction';
99
119
  /**
100
120
  * Type guard for a string value
101
121
  *
@@ -134,9 +154,6 @@ export const isPhraseLiteralChoice = (value) => Array.isArray(value) && value.ev
134
154
  * @internal
135
155
  */
136
156
  export const isPhraseLiteral = (value) => isString(value) && !value.startsWith('not ');
137
- export const isType = (a, b) => {
138
- return typeof a === b;
139
- };
140
157
  export const isA = (value, ctor) => {
141
158
  return isNonNullObject(value) && value instanceof ctor;
142
159
  };
@@ -1 +1 @@
1
- {"version":3,"file":"guards.js","sourceRoot":"","sources":["../../src/guards.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AASxB;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAc,EAAsB,EAAE,CAC9D,CAAC,CAAC,CACA,KAAK;IACL,OAAO,KAAK,KAAK,QAAQ;IACzB,KAAK,IAAI,KAAK;IACd,KAAK,CAAC,GAAG;IACT,OAAO,KAAK,CAAC,GAAG,KAAK,QAAQ;IAC7B,MAAM,IAAI,KAAK,CAAC,GAAG,CACpB,CAAC;AAEJ;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAc,EAAyB,EAAE,CACpE,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,IAAI,KAAK,SAAS,CAAC;AAEnD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAc,EAAiC,EAAE,CAC7E,CAAC,CAAC,CACA,KAAK;IACL,OAAO,KAAK,KAAK,QAAQ;IACzB,sEAAsE;IACtE,OAAQ,KAAa,CAAC,IAAI,KAAK,UAAU,CAC1C,CAAC;AAEJ;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,EAAW,EAAqB,EAAE;IAChE,IAAI,EAAE,KAAK,MAAM,IAAI,EAAE,KAAK,MAAM,EAAE,CAAC;QACnC,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,CAAC;QACH,0EAA0E;QAC1E,6DAA6D;QAC7D,IAAI,IAAI,KAAK,CAAC,EAAS,EAAE,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QACtD,OAAO,IAAI,CAAC;IACd,CAAC;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