@nice2dev/ui-graphics 1.0.4 → 1.0.5

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 (183) hide show
  1. package/dist/cjs/animation/AnimationEditor.js +1 -1
  2. package/dist/cjs/animation/Audience.js +1 -1
  3. package/dist/cjs/core/LocalUI.js +1 -1
  4. package/dist/cjs/core/yjsCollaboration.js +2 -2
  5. package/dist/cjs/core/yjsCollaboration.js.map +1 -1
  6. package/dist/cjs/game/GameAsset2dEditor.js +1 -1
  7. package/dist/cjs/node_modules/lib0/array.js +103 -0
  8. package/dist/cjs/node_modules/lib0/array.js.map +1 -0
  9. package/dist/cjs/node_modules/lib0/binary.js +42 -0
  10. package/dist/cjs/node_modules/lib0/binary.js.map +1 -0
  11. package/dist/cjs/node_modules/lib0/broadcastchannel.js +121 -0
  12. package/dist/cjs/node_modules/lib0/broadcastchannel.js.map +1 -0
  13. package/dist/cjs/node_modules/lib0/buffer.js +103 -0
  14. package/dist/cjs/node_modules/lib0/buffer.js.map +1 -0
  15. package/dist/cjs/node_modules/lib0/conditions.js +18 -0
  16. package/dist/cjs/node_modules/lib0/conditions.js.map +1 -0
  17. package/dist/cjs/node_modules/lib0/decoding.js +460 -0
  18. package/dist/cjs/node_modules/lib0/decoding.js.map +1 -0
  19. package/dist/cjs/node_modules/lib0/dom.js +55 -0
  20. package/dist/cjs/node_modules/lib0/dom.js.map +1 -0
  21. package/dist/cjs/node_modules/lib0/encoding.js +688 -0
  22. package/dist/cjs/node_modules/lib0/encoding.js.map +1 -0
  23. package/dist/cjs/node_modules/lib0/environment.js +129 -0
  24. package/dist/cjs/node_modules/lib0/environment.js.map +1 -0
  25. package/dist/cjs/node_modules/lib0/error.js +37 -0
  26. package/dist/cjs/node_modules/lib0/error.js.map +1 -0
  27. package/dist/cjs/node_modules/lib0/function.js +135 -0
  28. package/dist/cjs/node_modules/lib0/function.js.map +1 -0
  29. package/dist/cjs/node_modules/lib0/iterator.js +52 -0
  30. package/dist/cjs/node_modules/lib0/iterator.js.map +1 -0
  31. package/dist/cjs/node_modules/lib0/logging.common.js +63 -0
  32. package/dist/cjs/node_modules/lib0/logging.common.js.map +1 -0
  33. package/dist/cjs/node_modules/lib0/logging.js +128 -0
  34. package/dist/cjs/node_modules/lib0/logging.js.map +1 -0
  35. package/dist/cjs/node_modules/lib0/map.js +108 -0
  36. package/dist/cjs/node_modules/lib0/map.js.map +1 -0
  37. package/dist/cjs/node_modules/lib0/math.js +44 -0
  38. package/dist/cjs/node_modules/lib0/math.js.map +1 -0
  39. package/dist/cjs/node_modules/lib0/number.js +21 -0
  40. package/dist/cjs/node_modules/lib0/number.js.map +1 -0
  41. package/dist/cjs/node_modules/lib0/object.js +132 -0
  42. package/dist/cjs/node_modules/lib0/object.js.map +1 -0
  43. package/dist/cjs/node_modules/lib0/observable.js +168 -0
  44. package/dist/cjs/node_modules/lib0/observable.js.map +1 -0
  45. package/dist/cjs/node_modules/lib0/pair.js +33 -0
  46. package/dist/cjs/node_modules/lib0/pair.js.map +1 -0
  47. package/dist/cjs/node_modules/lib0/prng.js +95 -0
  48. package/dist/cjs/node_modules/lib0/prng.js.map +1 -0
  49. package/dist/cjs/node_modules/lib0/promise.js +33 -0
  50. package/dist/cjs/node_modules/lib0/promise.js.map +1 -0
  51. package/dist/cjs/node_modules/lib0/random.js +28 -0
  52. package/dist/cjs/node_modules/lib0/random.js.map +1 -0
  53. package/dist/cjs/node_modules/lib0/schema.js +1168 -0
  54. package/dist/cjs/node_modules/lib0/schema.js.map +1 -0
  55. package/dist/cjs/node_modules/lib0/set.js +12 -0
  56. package/dist/cjs/node_modules/lib0/set.js.map +1 -0
  57. package/dist/cjs/node_modules/lib0/storage.js +79 -0
  58. package/dist/cjs/node_modules/lib0/storage.js.map +1 -0
  59. package/dist/cjs/node_modules/lib0/string.js +94 -0
  60. package/dist/cjs/node_modules/lib0/string.js.map +1 -0
  61. package/dist/cjs/node_modules/lib0/symbol.js +15 -0
  62. package/dist/cjs/node_modules/lib0/symbol.js.map +1 -0
  63. package/dist/cjs/node_modules/lib0/time.js +18 -0
  64. package/dist/cjs/node_modules/lib0/time.js.map +1 -0
  65. package/dist/cjs/node_modules/lib0/trait/equality.js +32 -0
  66. package/dist/cjs/node_modules/lib0/trait/equality.js.map +1 -0
  67. package/dist/cjs/node_modules/lib0/url.js +20 -0
  68. package/dist/cjs/node_modules/lib0/url.js.map +1 -0
  69. package/dist/cjs/node_modules/lib0/webcrypto.js +8 -0
  70. package/dist/cjs/node_modules/lib0/webcrypto.js.map +1 -0
  71. package/dist/cjs/node_modules/y-protocols/auth.js +27 -0
  72. package/dist/cjs/node_modules/y-protocols/auth.js.map +1 -0
  73. package/dist/cjs/node_modules/y-protocols/awareness.js +277 -0
  74. package/dist/cjs/node_modules/y-protocols/awareness.js.map +1 -0
  75. package/dist/cjs/node_modules/y-protocols/sync.js +149 -0
  76. package/dist/cjs/node_modules/y-protocols/sync.js.map +1 -0
  77. package/dist/cjs/node_modules/y-websocket/src/y-websocket.js +521 -0
  78. package/dist/cjs/node_modules/y-websocket/src/y-websocket.js.map +1 -0
  79. package/dist/cjs/node_modules/yjs/dist/yjs.js +10399 -0
  80. package/dist/cjs/node_modules/yjs/dist/yjs.js.map +1 -0
  81. package/dist/cjs/packages/ui/dist/index.js +65997 -0
  82. package/dist/cjs/packages/ui/dist/index.js.map +1 -0
  83. package/dist/cjs/pixel/PixelEditor.js +1 -1
  84. package/dist/cjs/pixel/PixelEditorMenuBar.js +1 -1
  85. package/dist/cjs/pixel/PixelEditorRightPanel.js +1 -1
  86. package/dist/cjs/pixel/PixelEditorTimeline.js +1 -1
  87. package/dist/cjs/pixel/PixelEditorToolbar.js +1 -1
  88. package/dist/cjs/pixel/SpriteGeneratorPanel.js +1 -1
  89. package/dist/cjs/vector/VectorEditor.js +1 -1
  90. package/dist/cjs/vector/VectorEditorMenuBar.js +1 -1
  91. package/dist/cjs/vector/VectorEditorRightPanel.js +1 -1
  92. package/dist/esm/animation/AnimationEditor.js +5 -5
  93. package/dist/esm/animation/Audience.js +2 -2
  94. package/dist/esm/core/LocalUI.js +4 -4
  95. package/dist/esm/core/yjsCollaboration.js +2 -2
  96. package/dist/esm/core/yjsCollaboration.js.map +1 -1
  97. package/dist/esm/game/GameAsset2dEditor.js +19 -19
  98. package/dist/esm/node_modules/lib0/array.js +95 -0
  99. package/dist/esm/node_modules/lib0/array.js.map +1 -0
  100. package/dist/esm/node_modules/lib0/binary.js +30 -0
  101. package/dist/esm/node_modules/lib0/binary.js.map +1 -0
  102. package/dist/esm/node_modules/lib0/broadcastchannel.js +117 -0
  103. package/dist/esm/node_modules/lib0/broadcastchannel.js.map +1 -0
  104. package/dist/esm/node_modules/lib0/buffer.js +96 -0
  105. package/dist/esm/node_modules/lib0/buffer.js.map +1 -0
  106. package/dist/esm/node_modules/lib0/conditions.js +16 -0
  107. package/dist/esm/node_modules/lib0/conditions.js.map +1 -0
  108. package/dist/esm/node_modules/lib0/decoding.js +439 -0
  109. package/dist/esm/node_modules/lib0/decoding.js.map +1 -0
  110. package/dist/esm/node_modules/lib0/dom.js +48 -0
  111. package/dist/esm/node_modules/lib0/dom.js.map +1 -0
  112. package/dist/esm/node_modules/lib0/encoding.js +663 -0
  113. package/dist/esm/node_modules/lib0/encoding.js.map +1 -0
  114. package/dist/esm/node_modules/lib0/environment.js +121 -0
  115. package/dist/esm/node_modules/lib0/environment.js.map +1 -0
  116. package/dist/esm/node_modules/lib0/error.js +33 -0
  117. package/dist/esm/node_modules/lib0/error.js.map +1 -0
  118. package/dist/esm/node_modules/lib0/function.js +130 -0
  119. package/dist/esm/node_modules/lib0/function.js.map +1 -0
  120. package/dist/esm/node_modules/lib0/iterator.js +48 -0
  121. package/dist/esm/node_modules/lib0/iterator.js.map +1 -0
  122. package/dist/esm/node_modules/lib0/logging.common.js +52 -0
  123. package/dist/esm/node_modules/lib0/logging.common.js.map +1 -0
  124. package/dist/esm/node_modules/lib0/logging.js +115 -0
  125. package/dist/esm/node_modules/lib0/logging.js.map +1 -0
  126. package/dist/esm/node_modules/lib0/map.js +102 -0
  127. package/dist/esm/node_modules/lib0/map.js.map +1 -0
  128. package/dist/esm/node_modules/lib0/math.js +37 -0
  129. package/dist/esm/node_modules/lib0/math.js.map +1 -0
  130. package/dist/esm/node_modules/lib0/number.js +17 -0
  131. package/dist/esm/node_modules/lib0/number.js.map +1 -0
  132. package/dist/esm/node_modules/lib0/object.js +119 -0
  133. package/dist/esm/node_modules/lib0/object.js.map +1 -0
  134. package/dist/esm/node_modules/lib0/observable.js +165 -0
  135. package/dist/esm/node_modules/lib0/observable.js.map +1 -0
  136. package/dist/esm/node_modules/lib0/pair.js +30 -0
  137. package/dist/esm/node_modules/lib0/pair.js.map +1 -0
  138. package/dist/esm/node_modules/lib0/prng.js +87 -0
  139. package/dist/esm/node_modules/lib0/prng.js.map +1 -0
  140. package/dist/esm/node_modules/lib0/promise.js +31 -0
  141. package/dist/esm/node_modules/lib0/promise.js.map +1 -0
  142. package/dist/esm/node_modules/lib0/random.js +25 -0
  143. package/dist/esm/node_modules/lib0/random.js.map +1 -0
  144. package/dist/esm/node_modules/lib0/schema.js +1113 -0
  145. package/dist/esm/node_modules/lib0/schema.js.map +1 -0
  146. package/dist/esm/node_modules/lib0/set.js +10 -0
  147. package/dist/esm/node_modules/lib0/set.js.map +1 -0
  148. package/dist/esm/node_modules/lib0/storage.js +75 -0
  149. package/dist/esm/node_modules/lib0/storage.js.map +1 -0
  150. package/dist/esm/node_modules/lib0/string.js +85 -0
  151. package/dist/esm/node_modules/lib0/string.js.map +1 -0
  152. package/dist/esm/node_modules/lib0/symbol.js +13 -0
  153. package/dist/esm/node_modules/lib0/symbol.js.map +1 -0
  154. package/dist/esm/node_modules/lib0/time.js +16 -0
  155. package/dist/esm/node_modules/lib0/time.js.map +1 -0
  156. package/dist/esm/node_modules/lib0/trait/equality.js +29 -0
  157. package/dist/esm/node_modules/lib0/trait/equality.js.map +1 -0
  158. package/dist/esm/node_modules/lib0/url.js +18 -0
  159. package/dist/esm/node_modules/lib0/url.js.map +1 -0
  160. package/dist/esm/node_modules/lib0/webcrypto.js +6 -0
  161. package/dist/esm/node_modules/lib0/webcrypto.js.map +1 -0
  162. package/dist/esm/node_modules/y-protocols/auth.js +24 -0
  163. package/dist/esm/node_modules/y-protocols/auth.js.map +1 -0
  164. package/dist/esm/node_modules/y-protocols/awareness.js +271 -0
  165. package/dist/esm/node_modules/y-protocols/awareness.js.map +1 -0
  166. package/dist/esm/node_modules/y-protocols/sync.js +138 -0
  167. package/dist/esm/node_modules/y-protocols/sync.js.map +1 -0
  168. package/dist/esm/node_modules/y-websocket/src/y-websocket.js +515 -0
  169. package/dist/esm/node_modules/y-websocket/src/y-websocket.js.map +1 -0
  170. package/dist/esm/node_modules/yjs/dist/yjs.js +10295 -0
  171. package/dist/esm/node_modules/yjs/dist/yjs.js.map +1 -0
  172. package/dist/esm/packages/ui/dist/index.js +65571 -0
  173. package/dist/esm/packages/ui/dist/index.js.map +1 -0
  174. package/dist/esm/pixel/PixelEditor.js +3 -3
  175. package/dist/esm/pixel/PixelEditorMenuBar.js +2 -2
  176. package/dist/esm/pixel/PixelEditorRightPanel.js +3 -3
  177. package/dist/esm/pixel/PixelEditorTimeline.js +2 -2
  178. package/dist/esm/pixel/PixelEditorToolbar.js +2 -2
  179. package/dist/esm/pixel/SpriteGeneratorPanel.js +8 -8
  180. package/dist/esm/vector/VectorEditor.js +3 -3
  181. package/dist/esm/vector/VectorEditorMenuBar.js +3 -3
  182. package/dist/esm/vector/VectorEditorRightPanel.js +13 -13
  183. package/package.json +1 -1
@@ -0,0 +1,1113 @@
1
+ import { isObject, every as every$1, hasProperty } from './object.js';
2
+ import { isArray, every, some } from './array.js';
3
+ import { methodUnimplemented, create, unexpectedCase } from './error.js';
4
+ import { production } from './environment.js';
5
+ import { EqualityTraitSymbol, equals } from './trait/equality.js';
6
+ import { equalityDeep } from './function.js';
7
+ import { repeat } from './string.js';
8
+ import { int53, word, bool, oneOf, int32 } from './prng.js';
9
+ import { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } from './number.js';
10
+
11
+ /**
12
+ * @experimental WIP
13
+ *
14
+ * Simple & efficient schemas for your data.
15
+ */
16
+
17
+
18
+ /**
19
+ * @typedef {string|number|bigint|boolean|null|undefined|symbol} Primitive
20
+ */
21
+
22
+ /**
23
+ * @typedef {{ [k:string|number|symbol]: any }} AnyObject
24
+ */
25
+
26
+ /**
27
+ * @template T
28
+ * @typedef {T extends Schema<infer X> ? X : T} Unwrap
29
+ */
30
+
31
+ /**
32
+ * @template T
33
+ * @typedef {T extends Schema<infer X> ? X : T} TypeOf
34
+ */
35
+
36
+ /**
37
+ * @template {readonly unknown[]} T
38
+ * @typedef {T extends readonly [Schema<infer First>, ...infer Rest] ? [First, ...UnwrapArray<Rest>] : [] } UnwrapArray
39
+ */
40
+
41
+ /**
42
+ * @template T
43
+ * @typedef {T extends Schema<infer S> ? Schema<S> : never} CastToSchema
44
+ */
45
+
46
+ /**
47
+ * @template {unknown[]} Arr
48
+ * @typedef {Arr extends [...unknown[], infer L] ? L : never} TupleLast
49
+ */
50
+
51
+ /**
52
+ * @template {unknown[]} Arr
53
+ * @typedef {Arr extends [...infer Fs, unknown] ? Fs : never} TuplePop
54
+ */
55
+
56
+ /**
57
+ * @template {readonly unknown[]} T
58
+ * @typedef {T extends []
59
+ * ? {}
60
+ * : T extends [infer First]
61
+ * ? First
62
+ * : T extends [infer First, ...infer Rest]
63
+ * ? First & Intersect<Rest>
64
+ * : never
65
+ * } Intersect
66
+ */
67
+
68
+ const schemaSymbol = Symbol('0schema');
69
+
70
+ class ValidationError {
71
+ constructor () {
72
+ /**
73
+ * Reverse errors
74
+ * @type {Array<{ path: string?, expected: string, has: string, message: string? }>}
75
+ */
76
+ this._rerrs = [];
77
+ }
78
+
79
+ /**
80
+ * @param {string?} path
81
+ * @param {string} expected
82
+ * @param {string} has
83
+ * @param {string?} message
84
+ */
85
+ extend (path, expected, has, message = null) {
86
+ this._rerrs.push({ path, expected, has, message });
87
+ }
88
+
89
+ toString () {
90
+ const s = [];
91
+ for (let i = this._rerrs.length - 1; i > 0; i--) {
92
+ const r = this._rerrs[i];
93
+ /* c8 ignore next */
94
+ s.push(repeat(' ', (this._rerrs.length - i) * 2) + `${r.path != null ? `[${r.path}] ` : ''}${r.has} doesn't match ${r.expected}. ${r.message}`);
95
+ }
96
+ return s.join('\n')
97
+ }
98
+ }
99
+
100
+ /**
101
+ * @param {any} a
102
+ * @param {any} b
103
+ * @return {boolean}
104
+ */
105
+ const shapeExtends = (a, b) => {
106
+ if (a === b) return true
107
+ if (a == null || b == null || a.constructor !== b.constructor) return false
108
+ if (a[EqualityTraitSymbol]) return equals(a, b) // last resort: check equality (do this before array and obj check which don't implement the equality trait)
109
+ if (isArray(a)) {
110
+ return every(a, aitem =>
111
+ some(b, bitem => shapeExtends(aitem, bitem))
112
+ )
113
+ } else if (isObject(a)) {
114
+ return every$1(a, (aitem, akey) =>
115
+ shapeExtends(aitem, b[akey])
116
+ )
117
+ }
118
+ /* c8 ignore next */
119
+ return false
120
+ };
121
+
122
+ /**
123
+ * @template T
124
+ * @implements {equalityTraits.EqualityTrait}
125
+ */
126
+ class Schema {
127
+ // this.shape must not be defined on Schema. Otherwise typecheck on metatypes (e.g. $$object) won't work as expected anymore
128
+ /**
129
+ * If true, the more things are added to the shape the more objects this schema will accept (e.g.
130
+ * union). By default, the more objects are added, the the fewer objects this schema will accept.
131
+ * @protected
132
+ */
133
+ static _dilutes = false
134
+
135
+ /**
136
+ * @param {Schema<any>} other
137
+ */
138
+ extends (other) {
139
+ let [a, b] = [/** @type {any} */(this).shape, /** @type {any} */ (other).shape];
140
+ if (/** @type {typeof Schema<any>} */ (this.constructor)._dilutes) [b, a] = [a, b];
141
+ return shapeExtends(a, b)
142
+ }
143
+
144
+ /**
145
+ * Overwrite this when necessary. By default, we only check the `shape` property which every shape
146
+ * should have.
147
+ * @param {Schema<any>} other
148
+ */
149
+ equals (other) {
150
+ // @ts-ignore
151
+ return this.constructor === other.constructor && equalityDeep(this.shape, other.shape)
152
+ }
153
+
154
+ [schemaSymbol] () { return true }
155
+
156
+ /**
157
+ * @param {object} other
158
+ */
159
+ [EqualityTraitSymbol] (other) {
160
+ return this.equals(/** @type {any} */ (other))
161
+ }
162
+
163
+ /**
164
+ * Use `schema.validate(obj)` with a typed parameter that is already of typed to be an instance of
165
+ * Schema. Validate will check the structure of the parameter and return true iff the instance
166
+ * really is an instance of Schema.
167
+ *
168
+ * @param {T} o
169
+ * @return {boolean}
170
+ */
171
+ validate (o) {
172
+ return this.check(o)
173
+ }
174
+
175
+ /* c8 ignore start */
176
+ /**
177
+ * Similar to validate, but this method accepts untyped parameters.
178
+ *
179
+ * @param {any} _o
180
+ * @param {ValidationError} [_err]
181
+ * @return {_o is T}
182
+ */
183
+ check (_o, _err) {
184
+ methodUnimplemented();
185
+ }
186
+ /* c8 ignore stop */
187
+
188
+ /**
189
+ * @type {Schema<T?>}
190
+ */
191
+ get nullable () {
192
+ // @ts-ignore
193
+ return $union(this, $null)
194
+ }
195
+
196
+ /**
197
+ * @type {$Optional<Schema<T>>}
198
+ */
199
+ get optional () {
200
+ return new $Optional(/** @type {Schema<T>} */ (this))
201
+ }
202
+
203
+ /**
204
+ * Cast a variable to a specific type. Returns the casted value, or throws an exception otherwise.
205
+ * Use this if you know that the type is of a specific type and you just want to convince the type
206
+ * system.
207
+ *
208
+ * **Do not rely on these error messages!**
209
+ * Performs an assertion check only if not in a production environment.
210
+ *
211
+ * @template OO
212
+ * @param {OO} o
213
+ * @return {Extract<OO, T> extends never ? T : (OO extends Array<never> ? T : Extract<OO,T>)}
214
+ */
215
+ cast (o) {
216
+ assert(o, this);
217
+ return /** @type {any} */ (o)
218
+ }
219
+
220
+ /**
221
+ * EXPECTO PATRONUM!! 🪄
222
+ * This function protects against type errors. Though it may not work in the real world.
223
+ *
224
+ * "After all this time?"
225
+ * "Always." - Snape, talking about type safety
226
+ *
227
+ * Ensures that a variable is a a specific type. Returns the value, or throws an exception if the assertion check failed.
228
+ * Use this if you know that the type is of a specific type and you just want to convince the type
229
+ * system.
230
+ *
231
+ * Can be useful when defining lambdas: `s.lambda(s.$number, s.$void).expect((n) => n + 1)`
232
+ *
233
+ * **Do not rely on these error messages!**
234
+ * Performs an assertion check if not in a production environment.
235
+ *
236
+ * @param {T} o
237
+ * @return {o extends T ? T : never}
238
+ */
239
+ expect (o) {
240
+ assert(o, this);
241
+ return o
242
+ }
243
+ }
244
+
245
+ /**
246
+ * @template {(new (...args:any[]) => any) | ((...args:any[]) => any)} Constr
247
+ * @typedef {Constr extends ((...args:any[]) => infer T) ? T : (Constr extends (new (...args:any[]) => any) ? InstanceType<Constr> : never)} Instance
248
+ */
249
+
250
+ /**
251
+ * @template {(new (...args:any[]) => any) | ((...args:any[]) => any)} C
252
+ * @extends {Schema<Instance<C>>}
253
+ */
254
+ class $ConstructedBy extends Schema {
255
+ /**
256
+ * @param {C} c
257
+ * @param {((o:Instance<C>)=>boolean)|null} check
258
+ */
259
+ constructor (c, check) {
260
+ super();
261
+ this.shape = c;
262
+ this._c = check;
263
+ }
264
+
265
+ /**
266
+ * @param {any} o
267
+ * @param {ValidationError} [err]
268
+ * @return {o is C extends ((...args:any[]) => infer T) ? T : (C extends (new (...args:any[]) => any) ? InstanceType<C> : never)} o
269
+ */
270
+ check (o, err = undefined) {
271
+ const c = o?.constructor === this.shape && (this._c == null || this._c(o));
272
+ /* c8 ignore next */
273
+ !c && err?.extend(null, this.shape.name, o?.constructor.name, o?.constructor !== this.shape ? 'Constructor match failed' : 'Check failed');
274
+ return c
275
+ }
276
+ }
277
+
278
+ /**
279
+ * @template {(new (...args:any[]) => any) | ((...args:any[]) => any)} C
280
+ * @param {C} c
281
+ * @param {((o:Instance<C>) => boolean)|null} check
282
+ * @return {CastToSchema<$ConstructedBy<C>>}
283
+ */
284
+ const $constructedBy = (c, check = null) => new $ConstructedBy(c, check);
285
+ $constructedBy($ConstructedBy);
286
+
287
+ /**
288
+ * Check custom properties on any object. You may want to overwrite the generated Schema<any>.
289
+ *
290
+ * @extends {Schema<any>}
291
+ */
292
+ class $Custom extends Schema {
293
+ /**
294
+ * @param {(o:any) => boolean} check
295
+ */
296
+ constructor (check) {
297
+ super();
298
+ /**
299
+ * @type {(o:any) => boolean}
300
+ */
301
+ this.shape = check;
302
+ }
303
+
304
+ /**
305
+ * @param {any} o
306
+ * @param {ValidationError} err
307
+ * @return {o is any}
308
+ */
309
+ check (o, err) {
310
+ const c = this.shape(o);
311
+ /* c8 ignore next */
312
+ !c && err?.extend(null, 'custom prop', o?.constructor.name, 'failed to check custom prop');
313
+ return c
314
+ }
315
+ }
316
+
317
+ /**
318
+ * @param {(o:any) => boolean} check
319
+ * @return {Schema<any>}
320
+ */
321
+ const $custom = (check) => new $Custom(check);
322
+ $constructedBy($Custom);
323
+
324
+ /**
325
+ * @template {Primitive} T
326
+ * @extends {Schema<T>}
327
+ */
328
+ class $Literal extends Schema {
329
+ /**
330
+ * @param {Array<T>} literals
331
+ */
332
+ constructor (literals) {
333
+ super();
334
+ this.shape = literals;
335
+ }
336
+
337
+ /**
338
+ *
339
+ * @param {any} o
340
+ * @param {ValidationError} [err]
341
+ * @return {o is T}
342
+ */
343
+ check (o, err) {
344
+ const c = this.shape.some(a => a === o);
345
+ /* c8 ignore next */
346
+ !c && err?.extend(null, this.shape.join(' | '), o.toString());
347
+ return c
348
+ }
349
+ }
350
+
351
+ /**
352
+ * @template {Primitive[]} T
353
+ * @param {T} literals
354
+ * @return {CastToSchema<$Literal<T[number]>>}
355
+ */
356
+ const $literal = (...literals) => new $Literal(literals);
357
+ const $$literal = $constructedBy($Literal);
358
+
359
+ /**
360
+ * @template {Array<string|Schema<string|number>>} Ts
361
+ * @typedef {Ts extends [] ? `` : (Ts extends [infer T] ? (Unwrap<T> extends (string|number) ? Unwrap<T> : never) : (Ts extends [infer T1, ...infer Rest] ? `${Unwrap<T1> extends (string|number) ? Unwrap<T1> : never}${Rest extends Array<string|Schema<string|number>> ? CastStringTemplateArgsToTemplate<Rest> : never}` : never))} CastStringTemplateArgsToTemplate
362
+ */
363
+
364
+ /**
365
+ * @param {string} str
366
+ * @return {string}
367
+ */
368
+ const _regexEscape = /** @type {any} */ (RegExp).escape || /** @type {(str:string) => string} */ (str =>
369
+ str.replace(/[().|&,$^[\]]/g, s => '\\' + s)
370
+ );
371
+
372
+ /**
373
+ * @param {string|Schema<any>} s
374
+ * @return {string[]}
375
+ */
376
+ const _schemaStringTemplateToRegex = s => {
377
+ if ($string.check(s)) {
378
+ return [_regexEscape(s)]
379
+ }
380
+ if ($$literal.check(s)) {
381
+ return /** @type {Array<string|number>} */ (s.shape).map(v => v + '')
382
+ }
383
+ if ($$number.check(s)) {
384
+ return ['[+-]?\\d+.?\\d*']
385
+ }
386
+ if ($$string.check(s)) {
387
+ return ['.*']
388
+ }
389
+ if ($$union.check(s)) {
390
+ return s.shape.map(_schemaStringTemplateToRegex).flat(1)
391
+ }
392
+ /* c8 ignore next 2 */
393
+ // unexpected schema structure (only supports unions and string in literal types)
394
+ unexpectedCase();
395
+ };
396
+
397
+ /**
398
+ * @template {Array<string|Schema<string|number>>} T
399
+ * @extends {Schema<CastStringTemplateArgsToTemplate<T>>}
400
+ */
401
+ class $StringTemplate extends Schema {
402
+ /**
403
+ * @param {T} shape
404
+ */
405
+ constructor (shape) {
406
+ super();
407
+ this.shape = shape;
408
+ this._r = new RegExp('^' + shape.map(_schemaStringTemplateToRegex).map(opts => `(${opts.join('|')})`).join('') + '$');
409
+ }
410
+
411
+ /**
412
+ * @param {any} o
413
+ * @param {ValidationError} [err]
414
+ * @return {o is CastStringTemplateArgsToTemplate<T>}
415
+ */
416
+ check (o, err) {
417
+ const c = this._r.exec(o) != null;
418
+ /* c8 ignore next */
419
+ !c && err?.extend(null, this._r.toString(), o.toString(), 'String doesn\'t match string template.');
420
+ return c
421
+ }
422
+ }
423
+ $constructedBy($StringTemplate);
424
+
425
+ const isOptionalSymbol = Symbol('optional');
426
+ /**
427
+ * @template {Schema<any>} S
428
+ * @extends Schema<Unwrap<S>|undefined>
429
+ */
430
+ class $Optional extends Schema {
431
+ /**
432
+ * @param {S} shape
433
+ */
434
+ constructor (shape) {
435
+ super();
436
+ this.shape = shape;
437
+ }
438
+
439
+ /**
440
+ * @param {any} o
441
+ * @param {ValidationError} [err]
442
+ * @return {o is (Unwrap<S>|undefined)}
443
+ */
444
+ check (o, err) {
445
+ const c = o === undefined || this.shape.check(o);
446
+ /* c8 ignore next */
447
+ !c && err?.extend(null, 'undefined (optional)', '()');
448
+ return c
449
+ }
450
+
451
+ get [isOptionalSymbol] () { return true }
452
+ }
453
+ const $$optional = $constructedBy($Optional);
454
+
455
+ /**
456
+ * @extends Schema<never>
457
+ */
458
+ class $Never extends Schema {
459
+ /**
460
+ * @param {any} _o
461
+ * @param {ValidationError} [err]
462
+ * @return {_o is never}
463
+ */
464
+ check (_o, err) {
465
+ /* c8 ignore next */
466
+ err?.extend(null, 'never', typeof _o);
467
+ return false
468
+ }
469
+ }
470
+ $constructedBy($Never);
471
+
472
+ /**
473
+ * @template {{ [key: string|symbol|number]: Schema<any> }} S
474
+ * @typedef {{ [Key in keyof S as S[Key] extends $Optional<Schema<any>> ? Key : never]?: S[Key] extends $Optional<Schema<infer Type>> ? Type : never } & { [Key in keyof S as S[Key] extends $Optional<Schema<any>> ? never : Key]: S[Key] extends Schema<infer Type> ? Type : never }} $ObjectToType
475
+ */
476
+
477
+ /**
478
+ * @template {{[key:string|symbol|number]: Schema<any>}} S
479
+ * @extends {Schema<$ObjectToType<S>>}
480
+ */
481
+ class $Object extends Schema {
482
+ /**
483
+ * @param {S} shape
484
+ * @param {boolean} partial
485
+ */
486
+ constructor (shape, partial = false) {
487
+ super();
488
+ /**
489
+ * @type {S}
490
+ */
491
+ this.shape = shape;
492
+ this._isPartial = partial;
493
+ }
494
+
495
+ static _dilutes = true
496
+
497
+ /**
498
+ * @type {Schema<Partial<$ObjectToType<S>>>}
499
+ */
500
+ get partial () {
501
+ return new $Object(this.shape, true)
502
+ }
503
+
504
+ /**
505
+ * @param {any} o
506
+ * @param {ValidationError} err
507
+ * @return {o is $ObjectToType<S>}
508
+ */
509
+ check (o, err) {
510
+ if (o == null) {
511
+ /* c8 ignore next */
512
+ err?.extend(null, 'object', 'null');
513
+ return false
514
+ }
515
+ return every$1(this.shape, (vv, vk) => {
516
+ const c = (this._isPartial && !hasProperty(o, vk)) || vv.check(o[vk], err);
517
+ !c && err?.extend(vk.toString(), vv.toString(), typeof o[vk], 'Object property does not match');
518
+ return c
519
+ })
520
+ }
521
+ }
522
+
523
+ /**
524
+ * @template S
525
+ * @typedef {Schema<{ [Key in keyof S as S[Key] extends $Optional<Schema<any>> ? Key : never]?: S[Key] extends $Optional<Schema<infer Type>> ? Type : never } & { [Key in keyof S as S[Key] extends $Optional<Schema<any>> ? never : Key]: S[Key] extends Schema<infer Type> ? Type : never }>} _ObjectDefToSchema
526
+ */
527
+
528
+ // I used an explicit type annotation instead of $ObjectToType, so that the user doesn't see the
529
+ // weird type definitions when inspecting type definions.
530
+ /**
531
+ * @template {{ [key:string|symbol|number]: Schema<any> }} S
532
+ * @param {S} def
533
+ * @return {_ObjectDefToSchema<S> extends Schema<infer S> ? Schema<{ [K in keyof S]: S[K] }> : never}
534
+ */
535
+ const $object = def => /** @type {any} */ (new $Object(def));
536
+ const $$object = $constructedBy($Object);
537
+ /**
538
+ * @type {Schema<{[key:string]: any}>}
539
+ */
540
+ const $objectAny = $custom(o => o != null && (o.constructor === Object || o.constructor == null));
541
+
542
+ /**
543
+ * @template {Schema<string|number|symbol>} Keys
544
+ * @template {Schema<any>} Values
545
+ * @extends {Schema<{ [key in Unwrap<Keys>]: Unwrap<Values> }>}
546
+ */
547
+ class $Record extends Schema {
548
+ /**
549
+ * @param {Keys} keys
550
+ * @param {Values} values
551
+ */
552
+ constructor (keys, values) {
553
+ super();
554
+ this.shape = {
555
+ keys, values
556
+ };
557
+ }
558
+
559
+ /**
560
+ * @param {any} o
561
+ * @param {ValidationError} err
562
+ * @return {o is { [key in Unwrap<Keys>]: Unwrap<Values> }}
563
+ */
564
+ check (o, err) {
565
+ return o != null && every$1(o, (vv, vk) => {
566
+ const ck = this.shape.keys.check(vk, err);
567
+ /* c8 ignore next */
568
+ !ck && err?.extend(vk + '', 'Record', typeof o, ck ? 'Key doesn\'t match schema' : 'Value doesn\'t match value');
569
+ return ck && this.shape.values.check(vv, err)
570
+ })
571
+ }
572
+ }
573
+
574
+ /**
575
+ * @template {Schema<string|number|symbol>} Keys
576
+ * @template {Schema<any>} Values
577
+ * @param {Keys} keys
578
+ * @param {Values} values
579
+ * @return {CastToSchema<$Record<Keys,Values>>}
580
+ */
581
+ const $record = (keys, values) => new $Record(keys, values);
582
+ const $$record = $constructedBy($Record);
583
+
584
+ /**
585
+ * @template {Schema<any>[]} S
586
+ * @extends {Schema<{ [Key in keyof S]: S[Key] extends Schema<infer Type> ? Type : never }>}
587
+ */
588
+ class $Tuple extends Schema {
589
+ /**
590
+ * @param {S} shape
591
+ */
592
+ constructor (shape) {
593
+ super();
594
+ this.shape = shape;
595
+ }
596
+
597
+ /**
598
+ * @param {any} o
599
+ * @param {ValidationError} err
600
+ * @return {o is { [K in keyof S]: S[K] extends Schema<infer Type> ? Type : never }}
601
+ */
602
+ check (o, err) {
603
+ return o != null && every$1(this.shape, (vv, vk) => {
604
+ const c = /** @type {Schema<any>} */ (vv).check(o[vk], err);
605
+ /* c8 ignore next */
606
+ !c && err?.extend(vk.toString(), 'Tuple', typeof vv);
607
+ return c
608
+ })
609
+ }
610
+ }
611
+
612
+ /**
613
+ * @template {Array<Schema<any>>} T
614
+ * @param {T} def
615
+ * @return {CastToSchema<$Tuple<T>>}
616
+ */
617
+ const $tuple = (...def) => new $Tuple(def);
618
+ $constructedBy($Tuple);
619
+
620
+ /**
621
+ * @template {Schema<any>} S
622
+ * @extends {Schema<Array<S extends Schema<infer T> ? T : never>>}
623
+ */
624
+ class $Array extends Schema {
625
+ /**
626
+ * @param {Array<S>} v
627
+ */
628
+ constructor (v) {
629
+ super();
630
+ /**
631
+ * @type {Schema<S extends Schema<infer T> ? T : never>}
632
+ */
633
+ this.shape = v.length === 1 ? v[0] : new $Union(v);
634
+ }
635
+
636
+ /**
637
+ * @param {any} o
638
+ * @param {ValidationError} [err]
639
+ * @return {o is Array<S extends Schema<infer T> ? T : never>} o
640
+ */
641
+ check (o, err) {
642
+ const c = isArray(o) && every(o, oi => this.shape.check(oi));
643
+ /* c8 ignore next */
644
+ !c && err?.extend(null, 'Array', '');
645
+ return c
646
+ }
647
+ }
648
+
649
+ /**
650
+ * @template {Array<Schema<any>>} T
651
+ * @param {T} def
652
+ * @return {Schema<Array<T extends Array<Schema<infer S>> ? S : never>>}
653
+ */
654
+ const $array = (...def) => new $Array(def);
655
+ const $$array = $constructedBy($Array);
656
+ /**
657
+ * @type {Schema<Array<any>>}
658
+ */
659
+ const $arrayAny = $custom(o => isArray(o));
660
+
661
+ /**
662
+ * @template T
663
+ * @extends {Schema<T>}
664
+ */
665
+ class $InstanceOf extends Schema {
666
+ /**
667
+ * @param {new (...args:any) => T} constructor
668
+ * @param {((o:T) => boolean)|null} check
669
+ */
670
+ constructor (constructor, check) {
671
+ super();
672
+ this.shape = constructor;
673
+ this._c = check;
674
+ }
675
+
676
+ /**
677
+ * @param {any} o
678
+ * @param {ValidationError} err
679
+ * @return {o is T}
680
+ */
681
+ check (o, err) {
682
+ const c = o instanceof this.shape && (this._c == null || this._c(o));
683
+ /* c8 ignore next */
684
+ !c && err?.extend(null, this.shape.name, o?.constructor.name);
685
+ return c
686
+ }
687
+ }
688
+
689
+ /**
690
+ * @template T
691
+ * @param {new (...args:any) => T} c
692
+ * @param {((o:T) => boolean)|null} check
693
+ * @return {Schema<T>}
694
+ */
695
+ const $instanceOf = (c, check = null) => new $InstanceOf(c, check);
696
+ $constructedBy($InstanceOf);
697
+
698
+ const $$schema = $instanceOf(Schema);
699
+
700
+ /**
701
+ * @template {Schema<any>[]} Args
702
+ * @typedef {(...args:UnwrapArray<TuplePop<Args>>)=>Unwrap<TupleLast<Args>>} _LArgsToLambdaDef
703
+ */
704
+
705
+ /**
706
+ * @template {Array<Schema<any>>} Args
707
+ * @extends {Schema<_LArgsToLambdaDef<Args>>}
708
+ */
709
+ class $Lambda extends Schema {
710
+ /**
711
+ * @param {Args} args
712
+ */
713
+ constructor (args) {
714
+ super();
715
+ this.len = args.length - 1;
716
+ this.args = $tuple(...args.slice(-1));
717
+ this.res = args[this.len];
718
+ }
719
+
720
+ /**
721
+ * @param {any} f
722
+ * @param {ValidationError} err
723
+ * @return {f is _LArgsToLambdaDef<Args>}
724
+ */
725
+ check (f, err) {
726
+ const c = f.constructor === Function && f.length <= this.len;
727
+ /* c8 ignore next */
728
+ !c && err?.extend(null, 'function', typeof f);
729
+ return c
730
+ }
731
+ }
732
+ const $$lambda = $constructedBy($Lambda);
733
+
734
+ /**
735
+ * @type {Schema<Function>}
736
+ */
737
+ const $function = $custom(o => typeof o === 'function');
738
+
739
+ /**
740
+ * @template {Array<Schema<any>>} T
741
+ * @extends {Schema<Intersect<UnwrapArray<T>>>}
742
+ */
743
+ class $Intersection extends Schema {
744
+ /**
745
+ * @param {T} v
746
+ */
747
+ constructor (v) {
748
+ super();
749
+ /**
750
+ * @type {T}
751
+ */
752
+ this.shape = v;
753
+ }
754
+
755
+ /**
756
+ * @param {any} o
757
+ * @param {ValidationError} [err]
758
+ * @return {o is Intersect<UnwrapArray<T>>}
759
+ */
760
+ check (o, err) {
761
+ // @ts-ignore
762
+ const c = every(this.shape, check => check.check(o, err));
763
+ /* c8 ignore next */
764
+ !c && err?.extend(null, 'Intersectinon', typeof o);
765
+ return c
766
+ }
767
+ }
768
+ $constructedBy($Intersection, o => o.shape.length > 0); // Intersection with length=0 is considered "any"
769
+
770
+ /**
771
+ * @template S
772
+ * @extends {Schema<S>}
773
+ */
774
+ class $Union extends Schema {
775
+ static _dilutes = true
776
+
777
+ /**
778
+ * @param {Array<Schema<S>>} v
779
+ */
780
+ constructor (v) {
781
+ super();
782
+ this.shape = v;
783
+ }
784
+
785
+ /**
786
+ * @param {any} o
787
+ * @param {ValidationError} [err]
788
+ * @return {o is S}
789
+ */
790
+ check (o, err) {
791
+ const c = some(this.shape, (vv) => vv.check(o, err));
792
+ err?.extend(null, 'Union', typeof o);
793
+ return c
794
+ }
795
+ }
796
+
797
+ /**
798
+ * @template {Array<any>} T
799
+ * @param {T} schemas
800
+ * @return {CastToSchema<$Union<Unwrap<ReadSchema<T>>>>}
801
+ */
802
+ const $union = (...schemas) => schemas.findIndex($s => $$union.check($s)) >= 0
803
+ ? $union(...schemas.map($s => $($s)).map($s => $$union.check($s) ? $s.shape : [$s]).flat(1))
804
+ : (schemas.length === 1
805
+ ? schemas[0]
806
+ : new $Union(schemas));
807
+ const $$union = /** @type {Schema<$Union<any>>} */ ($constructedBy($Union));
808
+
809
+ const _t = () => true;
810
+ /**
811
+ * @type {Schema<any>}
812
+ */
813
+ const $any = $custom(_t);
814
+ const $$any = /** @type {Schema<Schema<any>>} */ ($constructedBy($Custom, o => o.shape === _t));
815
+
816
+ /**
817
+ * @type {Schema<bigint>}
818
+ */
819
+ const $bigint = $custom(o => typeof o === 'bigint');
820
+ const $$bigint = /** @type {Schema<Schema<BigInt>>} */ ($custom(o => o === $bigint));
821
+
822
+ /**
823
+ * @type {Schema<symbol>}
824
+ */
825
+ const $symbol = $custom(o => typeof o === 'symbol');
826
+ /** @type {Schema<Schema<Symbol>>} */ ($custom(o => o === $symbol));
827
+
828
+ /**
829
+ * @type {Schema<number>}
830
+ */
831
+ const $number = $custom(o => typeof o === 'number');
832
+ const $$number = /** @type {Schema<Schema<number>>} */ ($custom(o => o === $number));
833
+
834
+ /**
835
+ * @type {Schema<string>}
836
+ */
837
+ const $string = $custom(o => typeof o === 'string');
838
+ const $$string = /** @type {Schema<Schema<string>>} */ ($custom(o => o === $string));
839
+
840
+ /**
841
+ * @type {Schema<boolean>}
842
+ */
843
+ const $boolean = $custom(o => typeof o === 'boolean');
844
+ const $$boolean = /** @type {Schema<Schema<Boolean>>} */ ($custom(o => o === $boolean));
845
+
846
+ /**
847
+ * @type {Schema<undefined>}
848
+ */
849
+ const $undefined = $literal(undefined);
850
+ /** @type {Schema<Schema<undefined>>} */ ($constructedBy($Literal, o => o.shape.length === 1 && o.shape[0] === undefined));
851
+
852
+ /**
853
+ * @type {Schema<void>}
854
+ */
855
+ $literal(undefined);
856
+
857
+ const $null = $literal(null);
858
+ const $$null = /** @type {Schema<Schema<null>>} */ ($constructedBy($Literal, o => o.shape.length === 1 && o.shape[0] === null));
859
+
860
+ $constructedBy(Uint8Array);
861
+ /** @type {Schema<Schema<Uint8Array>>} */ ($constructedBy($ConstructedBy, o => o.shape === Uint8Array));
862
+
863
+ /**
864
+ * @type {Schema<Primitive>}
865
+ */
866
+ const $primitive = $union($number, $string, $null, $undefined, $bigint, $boolean, $symbol);
867
+
868
+ /**
869
+ * @typedef {JSON[]} JSONArray
870
+ */
871
+ /**
872
+ * @typedef {Primitive|JSONArray|{ [key:string]:JSON }} JSON
873
+ */
874
+ /**
875
+ * @type {Schema<null|number|string|boolean|JSON[]|{[key:string]:JSON}>}
876
+ */
877
+ (() => {
878
+ const $jsonArr = /** @type {$Array<$any>} */ ($array($any));
879
+ const $jsonRecord = /** @type {$Record<$string,$any>} */ ($record($string, $any));
880
+ const $json = $union($number, $string, $null, $boolean, $jsonArr, $jsonRecord);
881
+ $jsonArr.shape = $json;
882
+ $jsonRecord.shape.values = $json;
883
+ return $json
884
+ })();
885
+
886
+ /**
887
+ * @template {any} IN
888
+ * @typedef {IN extends Schema<any> ? IN
889
+ * : (IN extends string|number|boolean|null ? Schema<IN>
890
+ * : (IN extends new (...args:any[])=>any ? Schema<InstanceType<IN>>
891
+ * : (IN extends any[] ? Schema<{ [K in keyof IN]: Unwrap<ReadSchema<IN[K]>> }[number]>
892
+ * : (IN extends object ? (_ObjectDefToSchema<{[K in keyof IN]:ReadSchema<IN[K]>}> extends Schema<infer S> ? Schema<{ [K in keyof S]: S[K] }> : never)
893
+ * : never)
894
+ * )
895
+ * )
896
+ * )
897
+ * } ReadSchemaOld
898
+ */
899
+
900
+ /**
901
+ * @template {any} IN
902
+ * @typedef {[Extract<IN,Schema<any>>,Extract<IN,string|number|boolean|null>,Extract<IN,new (...args:any[])=>any>,Extract<IN,any[]>,Extract<Exclude<IN,Schema<any>|string|number|boolean|null|(new (...args:any[])=>any)|any[]>,object>] extends [infer Schemas, infer Primitives, infer Constructors, infer Arrs, infer Obj]
903
+ * ? Schema<
904
+ * (Schemas extends Schema<infer S> ? S : never)
905
+ * | Primitives
906
+ * | (Constructors extends new (...args:any[])=>any ? InstanceType<Constructors> : never)
907
+ * | (Arrs extends any[] ? { [K in keyof Arrs]: Unwrap<ReadSchema<Arrs[K]>> }[number] : never)
908
+ * | (Obj extends object ? Unwrap<(_ObjectDefToSchema<{[K in keyof Obj]:ReadSchema<Obj[K]>}> extends Schema<infer S> ? Schema<{ [K in keyof S]: S[K] }> : never)> : never)>
909
+ * : never
910
+ * } ReadSchema
911
+ */
912
+
913
+ /**
914
+ * @typedef {ReadSchema<{x:42}|{y:99}|Schema<string>|[1,2,{}]>} Q
915
+ */
916
+
917
+ /**
918
+ * @template IN
919
+ * @param {IN} o
920
+ * @return {ReadSchema<IN>}
921
+ */
922
+ const $ = o => {
923
+ if ($$schema.check(o)) {
924
+ return /** @type {any} */ (o)
925
+ } else if ($objectAny.check(o)) {
926
+ /**
927
+ * @type {any}
928
+ */
929
+ const o2 = {};
930
+ for (const k in o) {
931
+ o2[k] = $(o[k]);
932
+ }
933
+ return /** @type {any} */ ($object(o2))
934
+ } else if ($arrayAny.check(o)) {
935
+ return /** @type {any} */ ($union(...o.map($)))
936
+ } else if ($primitive.check(o)) {
937
+ return /** @type {any} */ ($literal(o))
938
+ } else if ($function.check(o)) {
939
+ return /** @type {any} */ ($constructedBy(/** @type {any} */ (o)))
940
+ }
941
+ /* c8 ignore next */
942
+ unexpectedCase();
943
+ };
944
+
945
+ /* c8 ignore start */
946
+ /**
947
+ * Assert that a variable is of this specific type.
948
+ * The assertion check is only performed in non-production environments.
949
+ *
950
+ * @type {<T>(o:any,schema:Schema<T>) => asserts o is T}
951
+ */
952
+ const assert = production
953
+ ? () => {}
954
+ : (o, schema) => {
955
+ const err = new ValidationError();
956
+ if (!schema.check(o, err)) {
957
+ throw create(`Expected value to be of type ${schema.constructor.name}.\n${err.toString()}`)
958
+ }
959
+ };
960
+ /* c8 ignore end */
961
+
962
+ /**
963
+ * @template In
964
+ * @template Out
965
+ * @typedef {{ if: Schema<In>, h: (o:In,state?:any)=>Out }} Pattern
966
+ */
967
+
968
+ /**
969
+ * @template {Pattern<any,any>} P
970
+ * @template In
971
+ * @typedef {ReturnType<Extract<P,Pattern<In extends number ? number : (In extends string ? string : In),any>>['h']>} PatternMatchResult
972
+ */
973
+
974
+ /**
975
+ * @todo move this to separate library
976
+ * @template {any} [State=undefined]
977
+ * @template {Pattern<any,any>} [Patterns=never]
978
+ */
979
+ class PatternMatcher {
980
+ /**
981
+ * @param {Schema<State>} [$state]
982
+ */
983
+ constructor ($state) {
984
+ /**
985
+ * @type {Array<Patterns>}
986
+ */
987
+ this.patterns = [];
988
+ this.$state = $state;
989
+ }
990
+
991
+ /**
992
+ * @template P
993
+ * @template R
994
+ * @param {P} pattern
995
+ * @param {(o:NoInfer<Unwrap<ReadSchema<P>>>,s:State)=>R} handler
996
+ * @return {PatternMatcher<State,Patterns|Pattern<Unwrap<ReadSchema<P>>,R>>}
997
+ */
998
+ if (pattern, handler) {
999
+ // @ts-ignore
1000
+ this.patterns.push({ if: $(pattern), h: handler });
1001
+ // @ts-ignore
1002
+ return this
1003
+ }
1004
+
1005
+ /**
1006
+ * @template R
1007
+ * @param {(o:any,s:State)=>R} h
1008
+ */
1009
+ else (h) {
1010
+ return this.if($any, h)
1011
+ }
1012
+
1013
+ /**
1014
+ * @return {State extends undefined
1015
+ * ? <In extends Unwrap<Patterns['if']>>(o:In,state?:undefined)=>PatternMatchResult<Patterns,In>
1016
+ * : <In extends Unwrap<Patterns['if']>>(o:In,state:State)=>PatternMatchResult<Patterns,In>}
1017
+ */
1018
+ done () {
1019
+ // @ts-ignore
1020
+ return /** @type {any} */ (o, s) => {
1021
+ for (let i = 0; i < this.patterns.length; i++) {
1022
+ const p = this.patterns[i];
1023
+ if (p.if.check(o)) {
1024
+ // @ts-ignore
1025
+ return p.h(o, s)
1026
+ }
1027
+ }
1028
+ throw create('Unhandled pattern')
1029
+ }
1030
+ }
1031
+ }
1032
+
1033
+ /**
1034
+ * @template [State=undefined]
1035
+ * @param {State} [state]
1036
+ * @return {PatternMatcher<State extends undefined ? undefined : Unwrap<ReadSchema<State>>>}
1037
+ */
1038
+ const match = state => new PatternMatcher(/** @type {any} */ (state));
1039
+
1040
+ /**
1041
+ * Helper function to generate a (non-exhaustive) sample set from a gives schema.
1042
+ *
1043
+ * @type {<T>(o:T,gen:prng.PRNG)=>T}
1044
+ */
1045
+ const _random = /** @type {any} */ (match(/** @type {Schema<prng.PRNG>} */ ($any))
1046
+ .if($$number, (_o, gen) => int53(gen, MIN_SAFE_INTEGER, MAX_SAFE_INTEGER))
1047
+ .if($$string, (_o, gen) => word(gen))
1048
+ .if($$boolean, (_o, gen) => bool(gen))
1049
+ .if($$bigint, (_o, gen) => BigInt(int53(gen, MIN_SAFE_INTEGER, MAX_SAFE_INTEGER)))
1050
+ .if($$union, (o, gen) => random(gen, oneOf(gen, o.shape)))
1051
+ .if($$object, (o, gen) => {
1052
+ /**
1053
+ * @type {any}
1054
+ */
1055
+ const res = {};
1056
+ for (const k in o.shape) {
1057
+ let prop = o.shape[k];
1058
+ if ($$optional.check(prop)) {
1059
+ if (bool(gen)) { continue }
1060
+ prop = prop.shape;
1061
+ }
1062
+ res[k] = _random(prop, gen);
1063
+ }
1064
+ return res
1065
+ })
1066
+ .if($$array, (o, gen) => {
1067
+ const arr = [];
1068
+ const n = int32(gen, 0, 42);
1069
+ for (let i = 0; i < n; i++) {
1070
+ arr.push(random(gen, o.shape));
1071
+ }
1072
+ return arr
1073
+ })
1074
+ .if($$literal, (o, gen) => {
1075
+ return oneOf(gen, o.shape)
1076
+ })
1077
+ .if($$null, (o, gen) => {
1078
+ return null
1079
+ })
1080
+ .if($$lambda, (o, gen) => {
1081
+ const res = random(gen, o.res);
1082
+ return () => res
1083
+ })
1084
+ .if($$any, (o, gen) => random(gen, oneOf(gen, [
1085
+ $number, $string, $null, $undefined, $bigint, $boolean,
1086
+ $array($number),
1087
+ $record($union('a', 'b', 'c'), $number)
1088
+ ])))
1089
+ .if($$record, (o, gen) => {
1090
+ /**
1091
+ * @type {any}
1092
+ */
1093
+ const res = {};
1094
+ const keysN = int53(gen, 0, 3);
1095
+ for (let i = 0; i < keysN; i++) {
1096
+ const key = random(gen, o.shape.keys);
1097
+ const val = random(gen, o.shape.values);
1098
+ res[key] = val;
1099
+ }
1100
+ return res
1101
+ })
1102
+ .done());
1103
+
1104
+ /**
1105
+ * @template S
1106
+ * @param {prng.PRNG} gen
1107
+ * @param {S} schema
1108
+ * @return {Unwrap<ReadSchema<S>>}
1109
+ */
1110
+ const random = (gen, schema) => /** @type {any} */ (_random($(schema), gen));
1111
+
1112
+ export { $, $$any, $$array, $$bigint, $$boolean, $$lambda, $$literal, $$null, $$number, $$object, $$optional, $$record, $$schema, $$string, $$union, $Array, $ConstructedBy, $Custom, $InstanceOf, $Intersection, $Lambda, $Literal, $Object, $Record, $StringTemplate, $Tuple, $Union, $any, $array, $arrayAny, $bigint, $boolean, $constructedBy, $custom, $function, $instanceOf, $literal, $null, $number, $object, $objectAny, $primitive, $record, $string, $symbol, $tuple, $undefined, $union, PatternMatcher, Schema, ValidationError, assert, match, random };
1113
+ //# sourceMappingURL=schema.js.map