lib0 0.2.108 → 0.2.110
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/array.d.ts +2 -2
- package/array.d.ts.map +1 -1
- package/array.js +5 -6
- package/bin/0serve.js +9 -1
- package/bin/gentesthtml.js +16 -2
- package/coverage/tmp/coverage-19938-1752497708602-0.json +1 -0
- package/coverage/tmp/coverage-19939-1752497665435-0.json +1 -0
- package/diff/patience.d.ts +16 -0
- package/diff/patience.d.ts.map +1 -0
- package/diff/patience.js +226 -0
- package/diff/patience.test.d.ts +4 -0
- package/diff/patience.test.d.ts.map +1 -0
- package/dist/aes-gcm.cjs +4 -4
- package/dist/{array-f0f52786.cjs → array-f5410bdd.cjs} +6 -7
- package/dist/array-f5410bdd.cjs.map +1 -0
- package/dist/array.cjs +1 -1
- package/dist/array.d.ts +2 -2
- package/dist/array.d.ts.map +1 -1
- package/dist/{broadcastchannel-f721f1d5.cjs → broadcastchannel-7479dcb7.cjs} +2 -2
- package/dist/{broadcastchannel-f721f1d5.cjs.map → broadcastchannel-7479dcb7.cjs.map} +1 -1
- package/dist/broadcastchannel.cjs +9 -9
- package/dist/{buffer-5c744eee.cjs → buffer-cba90761.cjs} +6 -6
- package/dist/{buffer-5c744eee.cjs.map → buffer-cba90761.cjs.map} +1 -1
- package/dist/buffer.cjs +8 -8
- package/dist/cache.cjs +3 -3
- package/dist/component.cjs +5 -5
- package/dist/{decoding-7b1393a8.cjs → decoding-9732ddf2.cjs} +3 -3
- package/dist/{decoding-7b1393a8.cjs.map → decoding-9732ddf2.cjs.map} +1 -1
- package/dist/decoding.cjs +4 -4
- package/dist/diff/patience.d.ts +16 -0
- package/dist/diff/patience.d.ts.map +1 -0
- package/dist/diff/patience.test.d.ts +4 -0
- package/dist/diff/patience.test.d.ts.map +1 -0
- package/dist/{diff-6c8c103c.cjs → diff-63d13d0f.cjs} +2 -2
- package/dist/{diff-6c8c103c.cjs.map → diff-63d13d0f.cjs.map} +1 -1
- package/dist/diff.cjs +4 -4
- package/dist/{encoding-364fc682.cjs → encoding-62f41e96.cjs} +3 -3
- package/dist/{encoding-364fc682.cjs.map → encoding-62f41e96.cjs.map} +1 -1
- package/dist/encoding.cjs +3 -3
- package/dist/{environment-e8406042.cjs → environment-2789c99d.cjs} +3 -3
- package/dist/{environment-e8406042.cjs.map → environment-2789c99d.cjs.map} +1 -1
- package/dist/environment.cjs +5 -5
- package/dist/{function-a29189eb.cjs → function-6c17de82.cjs} +3 -3
- package/dist/{function-a29189eb.cjs.map → function-6c17de82.cjs.map} +1 -1
- package/dist/function.cjs +3 -3
- package/dist/index.cjs +12 -12
- package/dist/indexeddbV2.cjs +1 -1
- package/dist/jwt.cjs +8 -8
- package/dist/list.cjs +3 -3
- package/dist/logging.cjs +5 -5
- package/dist/logging.common.cjs +5 -5
- package/dist/logging.node.cjs +5 -5
- package/dist/{object-315a738b.cjs → object-491858d1.cjs} +33 -7
- package/dist/object-491858d1.cjs.map +1 -0
- package/dist/object.cjs +3 -1
- package/dist/object.cjs.map +1 -1
- package/dist/object.d.ts +28 -12
- package/dist/object.d.ts.map +1 -1
- package/dist/object.test.d.ts +1 -0
- package/dist/object.test.d.ts.map +1 -1
- package/dist/observable.cjs +1 -1
- package/dist/patience.cjs +236 -0
- package/dist/patience.cjs.map +1 -0
- package/dist/performance.node.cjs +3 -3
- package/dist/pledge.cjs +1 -1
- package/dist/{prng-706a5e42.cjs → prng-6cd9a82d.cjs} +3 -3
- package/dist/{prng-706a5e42.cjs.map → prng-6cd9a82d.cjs.map} +1 -1
- package/dist/prng.cjs +9 -9
- package/dist/prng.test.d.ts.map +1 -1
- package/dist/rabin-gf2-polynomial.cjs +8 -8
- package/dist/rabin-uncached.cjs +8 -8
- package/dist/rabin.cjs +8 -8
- package/dist/random.test.d.ts.map +1 -1
- package/dist/schema.cjs +509 -0
- package/dist/schema.cjs.map +1 -0
- package/dist/schema.d.ts +297 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.test.d.ts +3 -0
- package/dist/schema.test.d.ts.map +1 -0
- package/dist/sort.test.d.ts.map +1 -1
- package/dist/{string-42009272.cjs → string-04e96f2d.cjs} +2 -2
- package/dist/{string-42009272.cjs.map → string-04e96f2d.cjs.map} +1 -1
- package/dist/string.cjs +2 -2
- package/dist/testing.cjs +10 -11
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.ts +0 -1
- package/dist/testing.d.ts.map +1 -1
- package/dist/url.cjs +1 -1
- package/dist/{websocket-3a481d20.cjs → websocket-2c325505.cjs} +1 -1
- package/dist/{websocket-3a481d20.cjs.map → websocket-2c325505.cjs.map} +1 -1
- package/dist/websocket.cjs +2 -2
- package/object.d.ts +28 -12
- package/object.d.ts.map +1 -1
- package/object.js +27 -5
- package/object.test.d.ts +1 -0
- package/object.test.d.ts.map +1 -1
- package/package.json +17 -5
- package/prng.test.d.ts.map +1 -1
- package/random.test.d.ts.map +1 -1
- package/schema.d.ts +297 -0
- package/schema.d.ts.map +1 -0
- package/schema.js +470 -0
- package/schema.test.d.ts +3 -0
- package/schema.test.d.ts.map +1 -0
- package/sort.test.d.ts.map +1 -1
- package/test.html +1 -0
- package/test.js +5 -2
- package/testing.d.ts +0 -1
- package/testing.d.ts.map +1 -1
- package/testing.js +0 -2
- package/coverage/tmp/coverage-24213-1747233250173-0.json +0 -1
- package/dist/array-f0f52786.cjs.map +0 -1
- package/dist/object-315a738b.cjs.map +0 -1
package/schema.js
ADDED
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @experimental WIP
|
|
3
|
+
*
|
|
4
|
+
* Simple & efficient schemas for your data.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as obj from './object.js'
|
|
8
|
+
import * as arr from './array.js'
|
|
9
|
+
import * as error from './error.js'
|
|
10
|
+
import * as env from './environment.js'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {string|number|bigint|boolean|null|undefined} LiteralType
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {{ [k:string|number|symbol]: any }} AnyObject
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @template T
|
|
22
|
+
* @typedef {T extends $Schema<infer X> ? X : T} TypeOfSchema
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @template {readonly unknown[]} T
|
|
27
|
+
* @typedef {T extends []
|
|
28
|
+
* ? {}
|
|
29
|
+
* : T extends [infer First]
|
|
30
|
+
* ? First
|
|
31
|
+
* : T extends [infer First, ...infer Rest]
|
|
32
|
+
* ? First & Intersect<Rest>
|
|
33
|
+
* : never
|
|
34
|
+
* } Intersect
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @template {readonly unknown[]} T
|
|
39
|
+
* @typedef {T extends readonly [$Schema<infer First>, ...infer Rest] ? [First, ...ExtractTypesFromSchemaArray<Rest>] : [] } ExtractTypesFromSchemaArray
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @template T
|
|
44
|
+
* @typedef {T extends $Schema<infer S> ? $Schema<S> : never} CastToSchema
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @template {unknown[]} Arr
|
|
49
|
+
* @typedef {Arr extends [...unknown[], infer L] ? L : never} TupleLast
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @template {unknown[]} Arr
|
|
54
|
+
* @typedef {Arr extends [...infer Fs, unknown] ? Fs : never} TuplePop
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
const schemaSymbol = Symbol('0schema')
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @template T
|
|
61
|
+
*/
|
|
62
|
+
export class $Schema {
|
|
63
|
+
get [schemaSymbol] () { return true }
|
|
64
|
+
/**
|
|
65
|
+
* Use `schema.validate(obj)` with a typed parameter that is already of typed to be an instance of
|
|
66
|
+
* Schema. Validate will check the structure of the parameter and return true iff the instance
|
|
67
|
+
* really is an instance of Schema.
|
|
68
|
+
*
|
|
69
|
+
* @param {T} o
|
|
70
|
+
* @return {boolean}
|
|
71
|
+
*/
|
|
72
|
+
validate (o) {
|
|
73
|
+
return this.check(o)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/* c8 ignore start */
|
|
77
|
+
/**
|
|
78
|
+
* Similar to validate, but this method accepts untyped parameters.
|
|
79
|
+
*
|
|
80
|
+
* @param {any} _o
|
|
81
|
+
* @return {_o is T}
|
|
82
|
+
*/
|
|
83
|
+
check (_o) {
|
|
84
|
+
error.methodUnimplemented()
|
|
85
|
+
}
|
|
86
|
+
/* c8 ignore stop */
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @type {$Schema<T?>}
|
|
90
|
+
*/
|
|
91
|
+
get nullable () {
|
|
92
|
+
return union(this, null_)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @type {$Schema<T|undefined>}
|
|
97
|
+
*/
|
|
98
|
+
get optional () {
|
|
99
|
+
return union(this, undefined_)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Cast a variable to a specific type. Returns the casted value, or throws an exception otherwise.
|
|
104
|
+
* Use this if you know that the type is of a specific type and you just want to convince the type
|
|
105
|
+
* system.
|
|
106
|
+
*
|
|
107
|
+
* **Do not rely on these error messages!**
|
|
108
|
+
* Performs an assertion check only if not in a production environment.
|
|
109
|
+
*
|
|
110
|
+
* @param {any} o
|
|
111
|
+
* @return {o extends T ? T : never}
|
|
112
|
+
*/
|
|
113
|
+
cast (o) {
|
|
114
|
+
assert(o, this)
|
|
115
|
+
return o
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Ensures that a variable is a a specific type. Returns the value, or throws an exception if the assertion check failed.
|
|
120
|
+
* Use this if you know that the type is of a specific type and you just want to convince the type
|
|
121
|
+
* system.
|
|
122
|
+
*
|
|
123
|
+
* Can be useful when defining lambdas: `s.lambda(s.$number, s.$void).ensure((n) => n + 1)`
|
|
124
|
+
*
|
|
125
|
+
* **Do not rely on these error messages!**
|
|
126
|
+
* Performs an assertion check if not in a production environment.
|
|
127
|
+
*
|
|
128
|
+
* @param {T} o
|
|
129
|
+
* @return {o extends T ? T : never}
|
|
130
|
+
*/
|
|
131
|
+
ensure (o) {
|
|
132
|
+
assert(o, this)
|
|
133
|
+
return o
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @template {(new (...args:any[]) => any) | ((...args:any[]) => any)} C
|
|
139
|
+
* @extends {$Schema<C extends ((...args:any[]) => infer T) ? T : (C extends (new (...args:any[]) => any) ? InstanceType<C> : never)>}
|
|
140
|
+
*/
|
|
141
|
+
export class $ConstructedBy extends $Schema {
|
|
142
|
+
/**
|
|
143
|
+
* @param {C} c
|
|
144
|
+
*/
|
|
145
|
+
constructor (c) {
|
|
146
|
+
super()
|
|
147
|
+
this.v = c
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @param {any} o
|
|
152
|
+
* @return {o is C extends ((...args:any[]) => infer T) ? T : (C extends (new (...args:any[]) => any) ? InstanceType<C> : never)} o
|
|
153
|
+
*/
|
|
154
|
+
check (o) {
|
|
155
|
+
return o?.constructor === this.v
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* @template {(new (...args:any[]) => any) | ((...args:any[]) => any)} C
|
|
161
|
+
* @param {C} c
|
|
162
|
+
* @return {CastToSchema<$ConstructedBy<C>>}
|
|
163
|
+
*/
|
|
164
|
+
export const constructedBy = c => new $ConstructedBy(c)
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* @template {LiteralType} T
|
|
168
|
+
* @extends {$Schema<T>}
|
|
169
|
+
*/
|
|
170
|
+
export class $Literal extends $Schema {
|
|
171
|
+
/**
|
|
172
|
+
* @param {Array<T>} literals
|
|
173
|
+
*/
|
|
174
|
+
constructor (literals) {
|
|
175
|
+
super()
|
|
176
|
+
this.v = literals
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* @param {any} o
|
|
181
|
+
* @return {o is T}
|
|
182
|
+
*/
|
|
183
|
+
check (o) {
|
|
184
|
+
return this.v.some(a => a === o)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @template {LiteralType[]} T
|
|
190
|
+
* @param {T} literals
|
|
191
|
+
* @return {CastToSchema<$Literal<T[number]>>}
|
|
192
|
+
*/
|
|
193
|
+
export const literal = (...literals) => new $Literal(literals)
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @template {{[key:string|symbol|number]: $Schema<any>}} S
|
|
197
|
+
* @extends {$Schema<{ [Key in keyof S]: S[Key] extends $Schema<infer Type> ? Type : never }>}
|
|
198
|
+
*/
|
|
199
|
+
export class $Object extends $Schema {
|
|
200
|
+
/**
|
|
201
|
+
* @param {S} v
|
|
202
|
+
*/
|
|
203
|
+
constructor (v) {
|
|
204
|
+
super()
|
|
205
|
+
this.v = v
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* @param {any} o
|
|
210
|
+
* @return {o is { [K in keyof S]: S[K] extends $Schema<infer Type> ? Type : never }}
|
|
211
|
+
*/
|
|
212
|
+
check (o) {
|
|
213
|
+
return o != null && obj.every(this.v, (vv, vk) => vv.check(o[vk]))
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* @template {{ [key:string|symbol|number]: $Schema<any> }} T
|
|
219
|
+
* @param {T} def
|
|
220
|
+
* @return {CastToSchema<$Object<T>>}
|
|
221
|
+
*/
|
|
222
|
+
export const object = def => new $Object(def)
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* @template {$Schema<any>[]} S
|
|
226
|
+
* @extends {$Schema<{ [Key in keyof S]: S[Key] extends $Schema<infer Type> ? Type : never }>}
|
|
227
|
+
*/
|
|
228
|
+
export class $Tuple extends $Schema {
|
|
229
|
+
/**
|
|
230
|
+
* @param {S} v
|
|
231
|
+
*/
|
|
232
|
+
constructor (v) {
|
|
233
|
+
super()
|
|
234
|
+
this.v = v
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @param {any} o
|
|
239
|
+
* @return {o is { [K in keyof S]: S[K] extends $Schema<infer Type> ? Type : never }}
|
|
240
|
+
*/
|
|
241
|
+
check (o) {
|
|
242
|
+
return o != null && obj.every(this.v, (vv, vk) => /** @type {$Schema<any>} */ (vv).check(o[vk]))
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* @template {Array<$Schema<any>>} T
|
|
248
|
+
* @param {T} def
|
|
249
|
+
* @return {CastToSchema<$Tuple<T>>}
|
|
250
|
+
*/
|
|
251
|
+
export const tuple = (...def) => new $Tuple(def)
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* @template {$Schema<any>} S
|
|
255
|
+
* @extends {$Schema<Array<S extends $Schema<infer T> ? T : never>>}
|
|
256
|
+
*/
|
|
257
|
+
export class $Array extends $Schema {
|
|
258
|
+
/**
|
|
259
|
+
* @param {Array<S>} v
|
|
260
|
+
*/
|
|
261
|
+
constructor (v) {
|
|
262
|
+
super()
|
|
263
|
+
/**
|
|
264
|
+
* @type {$Schema<S extends $Schema<infer T> ? T : never>}
|
|
265
|
+
*/
|
|
266
|
+
this.v = v.length === 1 ? v[0] : new $Union(v)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* @param {any} o
|
|
271
|
+
* @return {o is Array<S extends $Schema<infer T> ? T : never>} o
|
|
272
|
+
*/
|
|
273
|
+
check (o) {
|
|
274
|
+
return arr.isArray(o) && arr.every(o, oi => this.v.check(oi))
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* @template {Array<$Schema<any>>} T
|
|
280
|
+
* @param {T} def
|
|
281
|
+
* @return {$Schema<Array<T extends Array<$Schema<infer S>> ? S : never>>}
|
|
282
|
+
*/
|
|
283
|
+
export const array = (...def) => new $Array(def)
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* @template T
|
|
287
|
+
* @extends {$Schema<T>}
|
|
288
|
+
*/
|
|
289
|
+
export class $InstanceOf extends $Schema {
|
|
290
|
+
/**
|
|
291
|
+
* @param {new (...args:any) => T} constructor
|
|
292
|
+
*/
|
|
293
|
+
constructor (constructor) {
|
|
294
|
+
super()
|
|
295
|
+
this.v = constructor
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* @param {any} o
|
|
300
|
+
* @return {o is T}
|
|
301
|
+
*/
|
|
302
|
+
check (o) {
|
|
303
|
+
return o instanceof this.v
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* @template T
|
|
309
|
+
* @param {new (...args:any) => T} c
|
|
310
|
+
* @return {$Schema<T>}
|
|
311
|
+
*/
|
|
312
|
+
export const instance = c => new $InstanceOf(c)
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* @template {$Schema<any>[]} Args
|
|
316
|
+
* @typedef {(...args:ExtractTypesFromSchemaArray<TuplePop<Args>>)=>TypeOfSchema<TupleLast<Args>>} _LArgsToLambdaDef
|
|
317
|
+
*/
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* @template {Array<$Schema<any>>} Args
|
|
321
|
+
* @extends {$Schema<_LArgsToLambdaDef<Args>>}
|
|
322
|
+
*/
|
|
323
|
+
export class $Lambda extends $Schema {
|
|
324
|
+
/**
|
|
325
|
+
* @param {Args} args
|
|
326
|
+
*/
|
|
327
|
+
constructor (args) {
|
|
328
|
+
super()
|
|
329
|
+
this.len = args.length - 1
|
|
330
|
+
this.args = tuple(...args.slice(-1))
|
|
331
|
+
this.res = args[this.len]
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* @param {any} f
|
|
336
|
+
* @return {f is _LArgsToLambdaDef<Args>}
|
|
337
|
+
*/
|
|
338
|
+
check (f) {
|
|
339
|
+
return f.constructor === Function && f.length <= this.len
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* @template {$Schema<any>[]} Args
|
|
345
|
+
* @param {Args} args
|
|
346
|
+
* @return {$Schema<(...args:ExtractTypesFromSchemaArray<TuplePop<Args>>)=>TypeOfSchema<TupleLast<Args>>>}
|
|
347
|
+
*/
|
|
348
|
+
export const lambda = (...args) => new $Lambda(args.length > 0 ? args : [void_])
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* @template {Array<$Schema<any>>} T
|
|
352
|
+
* @extends {$Schema<Intersect<ExtractTypesFromSchemaArray<T>>>}
|
|
353
|
+
*/
|
|
354
|
+
export class $Intersection extends $Schema {
|
|
355
|
+
/**
|
|
356
|
+
* @param {T} v
|
|
357
|
+
*/
|
|
358
|
+
constructor (v) {
|
|
359
|
+
super()
|
|
360
|
+
/**
|
|
361
|
+
* @type {T}
|
|
362
|
+
*/
|
|
363
|
+
this.v = v
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* @param {any} o
|
|
368
|
+
* @return {o is Intersect<ExtractTypesFromSchemaArray<T>>}
|
|
369
|
+
*/
|
|
370
|
+
check (o) {
|
|
371
|
+
// @ts-ignore
|
|
372
|
+
return arr.every(this.v, check => check.check(o))
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* @template {$Schema<any>[]} T
|
|
378
|
+
* @param {T} def
|
|
379
|
+
* @return {CastToSchema<$Intersection<T>>}
|
|
380
|
+
*/
|
|
381
|
+
export const intersect = (...def) => new $Intersection(def)
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* @template S
|
|
385
|
+
* @extends {$Schema<S>}
|
|
386
|
+
*/
|
|
387
|
+
export class $Union extends $Schema {
|
|
388
|
+
/**
|
|
389
|
+
* @param {Array<$Schema<S>>} v
|
|
390
|
+
*/
|
|
391
|
+
constructor (v) {
|
|
392
|
+
super()
|
|
393
|
+
this.v = v
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* @param {any} o
|
|
398
|
+
* @return {o is S}
|
|
399
|
+
*/
|
|
400
|
+
check (o) {
|
|
401
|
+
return arr.some(this.v, (vv) => vv.check(o))
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
static schema = constructedBy($Union)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* @template {Array<$Schema<any>>} T
|
|
409
|
+
* @param {T} def
|
|
410
|
+
* @return {CastToSchema<$Union<T extends Array<$Schema<infer S>> ? S : never>>}
|
|
411
|
+
*/
|
|
412
|
+
export const union = (...def) => $Union.schema.check(def[0]) ? new $Union([...def[0].v, ...def.slice(1)]) : new $Union(def)
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* @type {$Schema<any>}
|
|
416
|
+
*/
|
|
417
|
+
export const any = intersect()
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* @type {$Schema<bigint>}
|
|
421
|
+
*/
|
|
422
|
+
export const bigint = constructedBy(BigInt)
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* @type {$Schema<Symbol>}
|
|
426
|
+
*/
|
|
427
|
+
export const symbol = constructedBy(Symbol)
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* @type {$Schema<number>}
|
|
431
|
+
*/
|
|
432
|
+
export const number = constructedBy(Number)
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* @type {$Schema<string>}
|
|
436
|
+
*/
|
|
437
|
+
export const string = constructedBy(String)
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* @type {$Schema<undefined>}
|
|
441
|
+
*/
|
|
442
|
+
const undefined_ = literal(undefined)
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* @type {$Schema<void>}
|
|
446
|
+
*/
|
|
447
|
+
const void_ = undefined_
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* @type {$Schema<null>}
|
|
451
|
+
*/
|
|
452
|
+
const null_ = literal(null)
|
|
453
|
+
|
|
454
|
+
export { null_ as null, void_ as void, undefined_ as undefined }
|
|
455
|
+
|
|
456
|
+
/* c8 ignore start */
|
|
457
|
+
/**
|
|
458
|
+
* Assert that a variable is of this specific type.
|
|
459
|
+
* The assertion check is only performed in non-production environments.
|
|
460
|
+
*
|
|
461
|
+
* @type {<T>(o:any,schema:$Schema<T>) => asserts o is T}
|
|
462
|
+
*/
|
|
463
|
+
export const assert = env.production
|
|
464
|
+
? () => {}
|
|
465
|
+
: (o, schema) => {
|
|
466
|
+
if (!schema.check(o)) {
|
|
467
|
+
throw error.create(`Expected value to be of type ${schema.constructor.name}.`)
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
/* c8 ignore end */
|
package/schema.test.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.test.d.ts","sourceRoot":"","sources":["schema.test.js"],"names":[],"mappings":"AAOO,iCAFI,EAAE,QAAQ,QAqNpB;mBA1NkB,cAAc"}
|
package/sort.test.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sort.test.d.ts","sourceRoot":"","sources":["sort.test.js"],"names":[],"mappings":"AAoEO,kCAFI,EAAE,QAAQ,
|
|
1
|
+
{"version":3,"file":"sort.test.d.ts","sourceRoot":"","sources":["sort.test.js"],"names":[],"mappings":"AAoEO,kCAFI,EAAE,QAAQ,QAoBpB;AAKM,mCAFI,EAAE,QAAQ,QAoBpB;AAKM,mCAFI,EAAE,QAAQ,QAoBpB;AAKM,yCAFI,EAAE,QAAQ,QAoBpB;AAKM,+CAFI,EAAE,QAAQ,QA4BpB;mBAzLkB,cAAc"}
|
package/test.html
CHANGED
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"lib0/decoding.js": "./decoding.js",
|
|
39
39
|
"lib0/dist/decoding.cjs": "./dist/decoding.cjs",
|
|
40
40
|
"lib0/decoding": "./decoding.js",
|
|
41
|
+
"lib0/diff/patience": "./diff/patience.js",
|
|
41
42
|
"lib0/diff.js": "./diff.js",
|
|
42
43
|
"lib0/dist/diff.cjs": "./dist/diff.cjs",
|
|
43
44
|
"lib0/diff": "./diff.js",
|
package/test.js
CHANGED
|
@@ -8,6 +8,7 @@ import * as logging from './logging.test.js'
|
|
|
8
8
|
import * as string from './string.test.js'
|
|
9
9
|
import * as encoding from './encoding.test.js'
|
|
10
10
|
import * as diff from './diff.test.js'
|
|
11
|
+
import * as patienceDiff from './diff/patience.test.js'
|
|
11
12
|
import * as testing from './testing.test.js'
|
|
12
13
|
import * as indexeddb from './indexeddb.test.js'
|
|
13
14
|
import * as indexeddbV2 from './indexeddbV2.test.js'
|
|
@@ -38,7 +39,7 @@ import * as list from './list.test.js'
|
|
|
38
39
|
import * as cache from './cache.test.js'
|
|
39
40
|
import * as symbol from './symbol.test.js'
|
|
40
41
|
import * as traits from './traits.test.js'
|
|
41
|
-
|
|
42
|
+
import * as schema from './schema.test.js'
|
|
42
43
|
import { isBrowser, isNode } from './environment.js'
|
|
43
44
|
|
|
44
45
|
/* c8 ignore next */
|
|
@@ -56,6 +57,7 @@ runTests({
|
|
|
56
57
|
string,
|
|
57
58
|
encoding,
|
|
58
59
|
diff,
|
|
60
|
+
patienceDiff,
|
|
59
61
|
testing,
|
|
60
62
|
indexeddb,
|
|
61
63
|
indexeddbV2,
|
|
@@ -84,7 +86,8 @@ runTests({
|
|
|
84
86
|
list,
|
|
85
87
|
cache,
|
|
86
88
|
symbol,
|
|
87
|
-
traits
|
|
89
|
+
traits,
|
|
90
|
+
schema
|
|
88
91
|
}).then(success => {
|
|
89
92
|
/* c8 ignore next */
|
|
90
93
|
if (isNode) {
|
package/testing.d.ts
CHANGED
package/testing.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["testing.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["testing.js"],"names":[],"mappings":"AA6DA,gCAAiD;AAGjD,oCAAmG;AAEnG;IACE;;;OAGG;IACH,wBAHW,MAAM,YACN,MAAM,EAmBhB;IAhBC;;OAEG;IACH,YAFU,MAAM,CAEY;IAC5B;;OAEG;IACH,UAFU,MAAM,CAEQ;IACxB;;;;OAIG;IACH,MAFU,IAAI,MAAM,EAAC,GAAG,CAAC,CAEJ;IACrB,qBAAiB;IACjB,wBAAiB;IAGnB,kBAGC;IAED;;OAEG;IAEH,mBAOC;IAED;;;;OAIG;IACH,sBAMC;CACF;AAED,oCAA6E;AAgBtE,gCANI,MAAM,QACN,MAAM,YACG,QAAQ,KAAE,IAAI,GAAC,QAAQ,GAAG,CAAC,KACpC,MAAM,iBACN,MAAM,oBA0EhB;AAcM,sCAHI,MAAM,SACN,MAAM,QAEwF;AAYlG,2BAFI,MAAM,QAE6B;AAE9C,yDAAoC;AAEpC,8EAA0C;AAoBnC,mCAHI,MAAM,eACM,GAAG,OAAE,IAAI,QAS/B;AAoBM,wCAHI,MAAM,eACM,GAAG,OAAE,QAAQ,GAAG,CAAC,iBASvC;AAoBM,qCAJI,MAAM,eACM,GAAG,OAAE,IAAI,GACpB,MAAM,CAYjB;AAoBM,0CAJI,MAAM,eACM,GAAG,OAAE,QAAQ,GAAG,CAAC,GAC5B,QAAQ,MAAM,CAAC,CAY1B;AASM,4EAFK,OAAO,CAYlB;AAQM,kCALI,MAAM,KACN,MAAM,gCAUhB;AASM,mFAA2F;AA8H3F,oGAFa,GAAG,0BAAK,MAAM,QAAC,GAAG,KAAE,OAAO,wBAE8E;AAUtH,gHAA8H;AAK9H,6CAFgB,GAAG,OAAE,QAAQ,GAAG,CAAC,iBASvC;AAMM,mCAHgB,GAAG,OAAE,IAAI,QAW/B;AAMM,wCAHgB,GAAG,OAAE,QAAQ,GAAG,CAAC,iBAWvC;AAKM;QAFW,MAAM;YAAS,MAAM,UAAW,QAAQ,KAAE,IAAI,GAAC,QAAQ,GAAG,CAAC;;qBA2C5E;AAQM,6BAHI,MAAM,SAMhB;AAQM,4BAHI,OAAO,QAOjB;sBA9kBqB,WAAW"}
|
package/testing.js
CHANGED
|
@@ -59,8 +59,6 @@ import * as promise from './promise.js'
|
|
|
59
59
|
import * as performance from 'lib0/performance'
|
|
60
60
|
import * as traits from './traits.js'
|
|
61
61
|
|
|
62
|
-
export { production } from './environment.js'
|
|
63
|
-
|
|
64
62
|
export const extensive = env.hasConf('extensive')
|
|
65
63
|
|
|
66
64
|
/* c8 ignore next */
|