lib0 1.0.0-rc.6 → 1.0.0-rc.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lib0",
3
- "version": "1.0.0-rc.6",
3
+ "version": "1.0.0-rc.7",
4
4
  "description": "",
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -127,10 +127,6 @@ export class TextOp extends list.ListNode {
127
127
  this._fingerprint = null
128
128
  }
129
129
 
130
- get $type () {
131
- return $textOp
132
- }
133
-
134
130
  /**
135
131
  * @param {string} newVal
136
132
  */
@@ -227,10 +223,6 @@ export class InsertOp extends list.ListNode {
227
223
  this._fingerprint = null
228
224
  }
229
225
 
230
- get $type () {
231
- return $insertOp
232
- }
233
-
234
226
  /**
235
227
  * @param {ArrayContent} newVal
236
228
  */
@@ -334,10 +326,6 @@ export class DeleteOp extends list.ListNode {
334
326
  this._fingerprint = null
335
327
  }
336
328
 
337
- get $type () {
338
- return $deleteOp
339
- }
340
-
341
329
  /**
342
330
  * @return {'delete'}
343
331
  */
@@ -422,10 +410,6 @@ export class RetainOp extends list.ListNode {
422
410
  this._fingerprint = null
423
411
  }
424
412
 
425
- get $type () {
426
- return $retainOp
427
- }
428
-
429
413
  /**
430
414
  * @return {'retain'}
431
415
  */
@@ -512,10 +496,6 @@ export class ModifyOp extends list.ListNode {
512
496
  this._fingerprint = null
513
497
  }
514
498
 
515
- get $type () {
516
- return $modifyOp
517
- }
518
-
519
499
  /**
520
500
  * @return {'modify'}
521
501
  */
@@ -623,10 +603,6 @@ export class SetAttrOp {
623
603
  this._fingerprint = null
624
604
  }
625
605
 
626
- get $type () {
627
- return $setAttrOp
628
- }
629
-
630
606
  /**
631
607
  * @return {'insert'}
632
608
  */
@@ -715,10 +691,6 @@ export class DeleteAttrOp {
715
691
  this._fingerprint = null
716
692
  }
717
693
 
718
- get $type () {
719
- return $deleteAttrOp
720
- }
721
-
722
694
  /**
723
695
  * @type {'delete'}
724
696
  */
@@ -784,10 +756,6 @@ export class ModifyAttrOp {
784
756
  this._fingerprint = null
785
757
  }
786
758
 
787
- get $type () {
788
- return $modifyAttrOp
789
- }
790
-
791
759
  /**
792
760
  * @type {'modify'}
793
761
  */
@@ -839,16 +807,16 @@ export class ModifyAttrOp {
839
807
  }
840
808
  }
841
809
 
842
- export const $insertOp = /** @type {s.Schema<InsertOp<any>>} */ (s.$type('insertOp'))
843
- export const $modifyOp = /** @type {s.Schema<ModifyOp>} */ (s.$type('modifyOp'))
844
- export const $textOp = /** @type {s.Schema<TextOp>} */ (s.$type('textOp'))
845
- export const $deleteOp = /** @type {s.Schema<DeleteOp<any>>} */ (s.$type('deleteOp'))
846
- export const $retainOp = /** @type {s.Schema<RetainOp>} */ (s.$type('retainOp'))
810
+ export const $insertOp = /** @type {s.Schema<InsertOp<any>>} */ (InsertOp.prototype.$type = /** @type {s.Schema<InsertOp<any>>} */ (s.$type('d:insertOp', InsertOp)))
811
+ export const $modifyOp = ModifyOp.prototype.$type = s.$type('d:modifyOp', ModifyOp)
812
+ export const $textOp = TextOp.prototype.$type = s.$type('d:textOp', TextOp)
813
+ export const $deleteOp = /** @type {s.Schema<DeleteOp<any>>} */ (DeleteOp.prototype.$type = s.$type('d:deleteOp', DeleteOp))
814
+ export const $retainOp = RetainOp.prototype.$type = s.$type('d:retainOp', RetainOp)
847
815
  export const $anyOp = s.$union($insertOp, $deleteOp, $textOp, $modifyOp)
848
816
 
849
- export const $setAttrOp = /** @type {s.Schema<SetAttrOp<any>>} */ (s.$type('setAttrOp'))
850
- export const $modifyAttrOp = /** @type {s.Schema<ModifyAttrOp<any,string|number>>} */ (s.$type('modifyAttrOp'))
851
- export const $deleteAttrOp = /** @type {s.Schema<DeleteAttrOp<any,string|number>>} */ (s.$type('deleteAttrOp'))
817
+ export const $setAttrOp = /** @type {s.Schema<SetAttrOp<any>>} */ (SetAttrOp.prototype.$type =/** @type {s.Schema<SetAttrOp<any>>} */ (s.$type('d:setAttrOp', SetAttrOp)))
818
+ export const $modifyAttrOp = /** @type {s.Schema<ModifyAttrOp<any>>} */ (ModifyAttrOp.prototype.$type = /** @type {s.Schema<ModifyAttrOp<any>>} */ (s.$type('d:modifyAttrOp', ModifyAttrOp)))
819
+ export const $deleteAttrOp = /** @type {s.Schema<DeleteAttrOp<any>>} */ (DeleteAttrOp.prototype.$type = /** @type {s.Schema<DeleteAttrOp<any>>} */ (s.$type('d:deleteAttrOp', DeleteAttrOp)))
852
820
  export const $anyAttrOp = s.$union($setAttrOp, $deleteAttrOp, $modifyAttrOp)
853
821
 
854
822
  /**
@@ -1027,7 +995,6 @@ class DeltaData {
1027
995
  * >}
1028
996
  */
1029
997
  export class Delta extends DeltaData {
1030
- get $type () { return $deltaAny }
1031
998
  /**
1032
999
  * @type {string}
1033
1000
  */
@@ -1374,7 +1341,7 @@ export class DeltaBuilder extends Delta {
1374
1341
  }
1375
1342
 
1376
1343
  /**
1377
- * @template {Extract<DeltaConfGetAllowedChildren<Conf, FixedConf>,Delta>} NewContent
1344
+ * @template {Extract<DeltaConfGetAllowedChildren<Conf, FixedConf>,Delta|DeltaData<any,any,any,any>|DeltaBuilder>} NewContent
1378
1345
  * @param {NewContent} modify
1379
1346
  * @param {FormattingAttributes?} formatting
1380
1347
  * @param {Attribution?} attribution
@@ -1958,24 +1925,39 @@ export class $Delta extends s.Schema {
1958
1925
  }
1959
1926
 
1960
1927
  /**
1961
- * @template {s.Schema<string>|string|Array<string>} [NodeNameSchema=s.Schema<any>]
1962
- * @template {s.Schema<{ [key: string|number]: any }>|{ [key:string|number]:any }} [AttrsSchema=s.Schema<{}>]
1963
- * @template {any} [ChildrenSchema=s.Schema<never>]
1964
- * @template {boolean} [HasText=false]
1965
- * @template {boolean} [RecursiveChildren=false]
1966
- * @template {{ [k:string]:any }} [Formats={[k:string]:any}]
1967
- * @param {object} opts
1968
- * @param {NodeNameSchema?} [opts.name]
1969
- * @param {AttrsSchema?} [opts.attrs] What key-value pairs are included.
1970
- * @param {ChildrenSchema?} [opts.children] The type of content in `insertOp`
1971
- * @param {HasText} [opts.text] Whether this delta contains text using `textOp`
1972
- * @param {Formats} [opts.formats]
1973
- * @param {RecursiveChildren} [opts.recursiveChildren]
1974
- * @return {[s.ReadSchemaUnwrapped<NodeNameSchema>,s.ReadSchemaUnwrapped<AttrsSchema>,s.ReadSchemaUnwrapped<ChildrenSchema>] extends [infer NodeName, infer Attrs, infer Children] ? s.Schema<Delta<PrettifyDeltaConf<(import('../ts.js').TypeIsAny<NodeName, {}, { name: NodeName }> &
1975
- * ([keyof Attrs] extends [never] ? {} : { attrs: Attrs }) &
1976
- * ([Children] extends [never] ? {} : { children: Children }) &
1977
- * (HasText extends false ? {} : { text: HasText }) &
1978
- * (RecursiveChildren extends false ? {} : { recursiveChildren: RecursiveChildren })) extends infer DC extends DeltaConf ? DC : never>>> : never}
1928
+ * @typedef {{
1929
+ * name?: s.Schema<string>|string|Array<string>,
1930
+ * attrs?: s.Schema<{ [key: string|number]: any }>|{ [key:string|number]:any },
1931
+ * children?: any,
1932
+ * text?: boolean,
1933
+ * recursiveChildren?: boolean,
1934
+ * formats?: { [k:string]: any }
1935
+ * }} ReadableDeltaConf
1936
+ */
1937
+
1938
+ /**
1939
+ * Transforms a ReadableDeltaConf (the input shape of `$delta(spec)`) to a DeltaConf.
1940
+ * @template {ReadableDeltaConf} DConfSpec
1941
+ * @typedef {[
1942
+ * DConfSpec extends {name: infer N} ? s.ReadSchemaUnwrapped<N> : any,
1943
+ * DConfSpec extends {attrs: infer A} ? s.ReadSchemaUnwrapped<A> : {},
1944
+ * DConfSpec extends {children: infer C} ? s.ReadSchemaUnwrapped<C> : never
1945
+ * ] extends [infer NodeName, infer Attrs, infer Children]
1946
+ * ? PrettifyDeltaConf<(
1947
+ * import('../ts.js').TypeIsAny<NodeName, {}, { name: NodeName }> &
1948
+ * ([keyof Attrs] extends [never] ? {} : { attrs: Attrs }) &
1949
+ * ([Children] extends [never] ? {} : { children: Children }) &
1950
+ * (DConfSpec extends {text: true} ? { text: true } : {}) &
1951
+ * (DConfSpec extends {recursiveChildren: true} ? { recursiveChildren: true } : {})
1952
+ * ) extends infer DC extends DeltaConf ? DC : never>
1953
+ * : never
1954
+ * } ReadDeltaConf
1955
+ */
1956
+
1957
+ /**
1958
+ * @template {ReadableDeltaConf} [DeltaConf={}]
1959
+ * @param {DeltaConf} opts
1960
+ * @return {s.Schema<Delta<ReadDeltaConf<DeltaConf>>>}
1979
1961
  */
1980
1962
  export const $delta = ({ name, attrs, children, text, formats, recursiveChildren: recursive }) => /** @type {any} */ (new $Delta(
1981
1963
  /** @type {any} */ (name == null ? s.$any : s.$(name)),
@@ -1988,7 +1970,7 @@ export const $delta = ({ name, attrs, children, text, formats, recursiveChildren
1988
1970
 
1989
1971
  export const $$delta = /* @__PURE__ */s.$constructedBy($Delta)
1990
1972
 
1991
- export const $deltaAny = /** @type {s.Schema<DeltaAny>} */ (/* @__PURE__ */s.$type('delta'))
1973
+ export const $deltaAny = /* @__PURE__ *//** @type {s.Schema<Delta<any>>} */ (Delta.prototype.$type = s.$type('d:delta', Delta))
1992
1974
  export const $deltaBuilderAny = /** @type {s.Schema<DeltaBuilderAny>} */ (/* @__PURE__ */s.$custom(o => $deltaAny.check(o) && !o.isDone))
1993
1975
 
1994
1976
  /**
@@ -2028,7 +2010,7 @@ export const mergeDeltas = (a, b) => {
2028
2010
  * @return {DeltaBuilder<Conf>}
2029
2011
  */
2030
2012
  export const random = (gen, $d, conf = {}) => {
2031
- let { source = null, minChildOps = 1, maxChildOps = 9 } = conf
2013
+ const { source = null, minChildOps = 1, maxChildOps = 9 } = conf
2032
2014
  let sourceLen = source == null ? 0 : source.childCnt
2033
2015
  const { $name, $attrs, $children, hasText, $formats: $formats_ } = /** @type {$Delta<any>} */ (/** @type {any} */ ($d)).shape
2034
2016
  const d = s.$$any.check($name) ? create($deltaAny) : create(s.random(gen, $name), $deltaAny)
@@ -2225,7 +2207,6 @@ class _DiffStringWrapper {
2225
2207
  }
2226
2208
  }
2227
2209
 
2228
-
2229
2210
  /*
2230
2211
  * Delta Diffing approach - optimized for performance and creating readable deltas
2231
2212
  *
@@ -2293,27 +2274,31 @@ export const diff = (d1, d2) => {
2293
2274
  */
2294
2275
  const cs2 = []
2295
2276
  // if right is null, then we already matched everything
2296
- while (left1 !== null && right1 !== null && left1 !== right1.next) {
2297
- if ($textOp.check(left1)) {
2298
- cs1.push(left1.insert)
2299
- } else if ($insertOp.check(left1)) {
2300
- cs1.push(...left1.insert.map(ins => typeof ins === 'string' ? new _DiffStringWrapper(ins) : ins))
2301
- } else {
2302
- error.unexpectedCase()
2277
+ if (right1 != null) {
2278
+ while (left1 !== null && left1 !== right1.next) {
2279
+ if ($textOp.check(left1)) {
2280
+ cs1.push(left1.insert)
2281
+ } else if ($insertOp.check(left1)) {
2282
+ cs1.push(...left1.insert.map(ins => typeof ins === 'string' ? new _DiffStringWrapper(ins) : ins))
2283
+ } else {
2284
+ error.unexpectedCase()
2285
+ }
2286
+ formattingNeedsDiff ||= left1.format != null
2287
+ left1 = left1.next
2303
2288
  }
2304
- formattingNeedsDiff ||= left1.format != null
2305
- left1 = left1.next
2306
2289
  }
2307
- while (left2 !== null && right2 !== null && left2 !== right2.next) {
2308
- if ($textOp.check(left2)) {
2309
- cs2.push(left2.insert)
2310
- } else if ($insertOp.check(left2)) {
2311
- cs2.push(...left2.insert.map(ins => typeof ins === 'string' ? new _DiffStringWrapper(ins) : ins))
2312
- } else {
2313
- error.unexpectedCase()
2290
+ if (right2 != null) {
2291
+ while (left2 !== null && left2 !== right2.next) {
2292
+ if ($textOp.check(left2)) {
2293
+ cs2.push(left2.insert)
2294
+ } else if ($insertOp.check(left2)) {
2295
+ cs2.push(...left2.insert.map(ins => typeof ins === 'string' ? new _DiffStringWrapper(ins) : ins))
2296
+ } else {
2297
+ error.unexpectedCase()
2298
+ }
2299
+ formattingNeedsDiff ||= left2.format != null
2300
+ left2 = left2.next
2314
2301
  }
2315
- formattingNeedsDiff ||= left2.format != null
2316
- left2 = left2.next
2317
2302
  }
2318
2303
  const changeset1 = [{
2319
2304
  index: commonPrefixOffset,
@@ -62,7 +62,7 @@ export const diffSplitBy = (a, b, regexp) => {
62
62
  *
63
63
  * @experimental
64
64
  */
65
- export const smartSplitRegex = /[^\p{L}\p{N}\p{Extended_Pictographic}\p{Emoji_Modifier}\u200D\uFE0F]/gu
65
+ export const smartSplitRegex = /[^\p{L}\p{N}\p{Extended_Pictographic}\p{Emoji_Modifier}\u200D\uFE0F]/gu // eslint-disable-line no-misleading-character-class
66
66
 
67
67
  /**
68
68
  * Sensible default for diffing strings using patience (it's fast though).
package/src/schema.js CHANGED
@@ -4,7 +4,6 @@
4
4
  * Simple & efficient schemas for your data.
5
5
  */
6
6
 
7
- import * as obj from './object.js'
8
7
  import * as arr from './array.js'
9
8
  import * as error from './error.js'
10
9
  import * as equalityTraits from './trait/equality.js'
@@ -14,6 +13,7 @@ import * as prng from './prng.js'
14
13
  import * as number from './number.js'
15
14
  import * as map from './map.js'
16
15
  import { global } from './environment.js'
16
+ import * as object from './object.js'
17
17
 
18
18
  /**
19
19
  * @typedef {string|number|bigint|boolean|null|undefined|symbol} Primitive
@@ -109,8 +109,8 @@ const _extendsShapeHelper = (a, b) => {
109
109
  return arr.every(a, aitem =>
110
110
  arr.some(b, bitem => _extendsShapeHelper(aitem, bitem))
111
111
  )
112
- } else if (obj.isObject(a)) {
113
- return obj.every(a, (aitem, akey) =>
112
+ } else if (object.isObject(a)) {
113
+ return object.every(a, (aitem, akey) =>
114
114
  _extendsShapeHelper(aitem, b[akey])
115
115
  )
116
116
  }
@@ -129,11 +129,14 @@ export const extendsShape = (a, b) => {
129
129
  return /** @type {any} */ (a.constructor)._dilutes ? _extendsShapeHelper(bShape, aShape) : _extendsShapeHelper(aShape, bShape)
130
130
  }
131
131
 
132
+ const schemaSymbol = Symbol.for('schema:Schema')
133
+
132
134
  /**
133
135
  * @template T
134
136
  * @implements {equalityTraits.EqualityTrait}
135
137
  */
136
138
  export class Schema {
139
+ get isSchema () { return schemaSymbol }
137
140
  // this.shape must not be defined on Schema. Otherwise typecheck on metatypes (e.g. $$object) won't work as expected anymore
138
141
  /**
139
142
  * If true, the more things are added to the shape the more objects this schema will accept (e.g.
@@ -221,8 +224,6 @@ export class Schema {
221
224
  * "Always." - Snape, talking about type safety
222
225
  *
223
226
  * Ensures that a variable is a a specific type. Returns the value, or throws an exception if the assertion check failed.
224
- * Use this if you know that the type is of a specific type and you just want to convince the type
225
- * system.
226
227
  *
227
228
  * Can be useful when defining lambdas: `s.lambda(s.$number, s.$number).expect((n) => n + 1)`
228
229
  *
@@ -235,6 +236,75 @@ export class Schema {
235
236
  }
236
237
  }
237
238
 
239
+ /**
240
+ * @extends {Schema<any>}
241
+ */
242
+ export class $Type extends Schema {
243
+ /**
244
+ * @param {string} name
245
+ */
246
+ constructor (name) {
247
+ super()
248
+ /**
249
+ * @type {string}
250
+ */
251
+ this.name = name
252
+ this.typeSymbol = Symbol('schema:' + name)
253
+ }
254
+
255
+ /**
256
+ * @param {any} o
257
+ * @param {ValidationError} err
258
+ * @return {o is any}
259
+ */
260
+ check (o, err) {
261
+ const c = o != null && o.$type === this
262
+ /* c8 ignore next */
263
+ !c && err != null && err.extend(null, 'type check on', o?.constructor.name, 'failed')
264
+ return c
265
+ }
266
+ }
267
+
268
+ /**
269
+ * A shared gobal namespace to store schemas across lib0 installations
270
+ */
271
+ const schemaStore = /* @__PURE__ */(() => {
272
+ const _ns = '_lib0@v1/$'
273
+ return (global[_ns] = (global[_ns] || new Map()))
274
+ })()
275
+
276
+ /**
277
+ * Define a schema for a type that can validate instances across duplicated module installations.
278
+ *
279
+ * You may only assign one "$type" to any class.
280
+ *
281
+ * The recommended way to assign a type to a class is by doing. While not strictly necessary - it ensures proper typing.
282
+ *
283
+ * const $a = A.prototype.$type = s.$type(':a', A)
284
+ *
285
+ * Use $type if you need to validate duplicated classes from other (duplicate) installations of your
286
+ * js module. Don't use this only if needed and not as some "standard practice".
287
+ *
288
+ * It is good practice to define a $type property on the type constructor to enable efficient
289
+ * typechecks `myClassInstance.$type === $myClass`
290
+ *
291
+ * @template {new (...args:any[])=>any} T
292
+ * @param {string} uniqueName - a good name would be `{projectPrefix}:{ConstructorName}[@v{version}]`
293
+ * @param {T|{ [K:string|number|symbol]:any }|null} TypeConstructor
294
+ * @return {Schema<InstanceType<T>>}
295
+ */
296
+ /* @__NO_SIDE_EFFECTS__ */
297
+ export const $type = (uniqueName, TypeConstructor) => {
298
+ const type = map.setIfUndefined(schemaStore, uniqueName, () => new $Type(uniqueName))
299
+ if (TypeConstructor) {
300
+ /* c8 ignore next */
301
+ if (TypeConstructor.prototype && object.hasProperty(TypeConstructor.prototype, '$type')) { error.unexpectedCase() }
302
+ (TypeConstructor.prototype || TypeConstructor).$type = type
303
+ }
304
+ return /** @type {any} */ (type)
305
+ }
306
+ export const $$type = $Type.prototype.$type = $type('s:$type', $Type)
307
+
238
308
  /**
239
309
  * @template {(new (...args:any[]) => any) | ((...args:any[]) => any)} Constr
240
310
  * @typedef {Constr extends ((...args:any[]) => infer T) ? T : (Constr extends (new (...args:any[]) => any) ? InstanceType<Constr> : never)} Instance
@@ -261,6 +331,7 @@ export class $ConstructedBy extends Schema {
261
331
  * @return {o is C extends ((...args:any[]) => infer T) ? T : (C extends (new (...args:any[]) => any) ? InstanceType<C> : never)} o
262
332
  */
263
333
  check (o, err = undefined) {
334
+ /* c8 ignore next */
264
335
  const c = o?.constructor === this.shape && (this._c == null || this._c(o))
265
336
  /* c8 ignore next */
266
337
  !c && err?.extend(null, this.shape.name, o?.constructor.name, o?.constructor !== this.shape ? 'Constructor match failed' : 'Check failed')
@@ -276,7 +347,7 @@ export class $ConstructedBy extends Schema {
276
347
  */
277
348
  /* @__NO_SIDE_EFFECTS__ */
278
349
  export const $constructedBy = (c, check = null) => new $ConstructedBy(c, check)
279
- export const $$constructedBy = $constructedBy($ConstructedBy)
350
+ export const $$constructedBy = $type('s:$ConstructedBy', $ConstructedBy)
280
351
 
281
352
  /**
282
353
  * Check custom properties on any object. You may want to overwrite the generated Schema<any>.
@@ -314,46 +385,7 @@ export class $Custom extends Schema {
314
385
  */
315
386
  /* @__NO_SIDE_EFFECTS__ */
316
387
  export const $custom = (check) => new $Custom(check)
317
- export const $$custom = $constructedBy($Custom)
318
-
319
- /**
320
- * @extends {Schema<{$type: Schema<any>}>}
321
- */
322
- export class $Type extends Schema {
323
- /**
324
- * @param {any} o
325
- * @param {ValidationError} err
326
- * @return {o is any}
327
- */
328
- check (o, err) {
329
- const c = o && (o.$type === this)
330
- /* c8 ignore next */
331
- !c && err?.extend(null, 'type check on', o?.constructor.name, 'failed')
332
- return c
333
- }
334
- }
335
-
336
- /**
337
- * A shared gobal namespace to store schemas across lib0 installations
338
- */
339
- const schemaStore = /* @__PURE__ */(() => {
340
- const _ns = '_lib0@v1/$'
341
- return (global[_ns] = (global[_ns] || new Map()))
342
- })()
343
-
344
- /**
345
- * Define a schema for a type that can validate instances across duplicated module installations.
346
- *
347
- * Use this if you need to validate duplicated classes from other (duplicate) installations of your
348
- * js module. Don't use this only if needed and not as some "standard practice".
349
- *
350
- * @param {string} uniqueName
351
- * @param {number} version
352
- * @return {Schema<{ $type: Schema<any> }>}
353
- */
354
- /* @__NO_SIDE_EFFECTS__ */
355
- export const $type = (uniqueName, version = 0) => map.setIfUndefined(schemaStore, `${uniqueName}@v${version}`, () => new $Type())
356
- export const $$type = $constructedBy($Type)
388
+ export const $$custom = $type('s:$Custom', $Custom)
357
389
 
358
390
  /**
359
391
  * @template {Primitive} T
@@ -377,7 +409,7 @@ export class $Literal extends Schema {
377
409
  check (o, err) {
378
410
  const c = this.shape.some(a => a === o)
379
411
  /* c8 ignore next */
380
- !c && err?.extend(null, this.shape.join(' | '), o.toString())
412
+ !c && err?.extend(null, this.shape.join(' | '), o + '')
381
413
  return c
382
414
  }
383
415
  }
@@ -389,7 +421,7 @@ export class $Literal extends Schema {
389
421
  */
390
422
  /* @__NO_SIDE_EFFECTS__ */
391
423
  export const $literal = (...literals) => new $Literal(literals)
392
- export const $$literal = $constructedBy($Literal)
424
+ export const $$literal = $type('s:$Literal', $Literal)
393
425
 
394
426
  /**
395
427
  * @template {Array<string|Schema<string|number>>} Ts
@@ -428,7 +460,7 @@ const _schemaStringTemplateToRegex = s => {
428
460
  if ($$union.check(s)) {
429
461
  return s.shape.map(_schemaStringTemplateToRegex).flat(1)
430
462
  }
431
- /* c8 ignore next 2 */
463
+ /* c8 ignore next 3 */
432
464
  // unexpected schema structure (only supports unions and string in literal types)
433
465
  error.unexpectedCase()
434
466
  }
@@ -455,7 +487,7 @@ export class $StringTemplate extends Schema {
455
487
  check (o, err) {
456
488
  const c = this._r.exec(o) != null
457
489
  /* c8 ignore next */
458
- !c && err?.extend(null, this._r.toString(), o.toString(), 'String doesn\'t match string template.')
490
+ !c && err?.extend(null, this._r.toString(), o+'', 'String doesn\'t match string template.')
459
491
  return c
460
492
  }
461
493
  }
@@ -467,7 +499,7 @@ export class $StringTemplate extends Schema {
467
499
  */
468
500
  /* @__NO_SIDE_EFFECTS__ */
469
501
  export const $stringTemplate = (...literals) => new $StringTemplate(literals)
470
- export const $$stringTemplate = $constructedBy($StringTemplate)
502
+ export const $$stringTemplate = $type('s:$StringTemplate', $StringTemplate)
471
503
 
472
504
  /**
473
505
  * @template {Schema<any>} S
@@ -494,7 +526,7 @@ export class $Optional extends Schema {
494
526
  return c
495
527
  }
496
528
  }
497
- export const $$optional = $constructedBy($Optional)
529
+ export const $$optional = $type('s:$Optional', $Optional)
498
530
 
499
531
  /**
500
532
  * @extends Schema<never>
@@ -516,7 +548,7 @@ class $Never extends Schema {
516
548
  * @type {Schema<never>}
517
549
  */
518
550
  export const $never = /* @__PURE__ */new $Never()
519
- export const $$never = /* @__PURE__ */$constructedBy($Never)
551
+ export const $$never = /* @__PURE__ */$type('s:$Never', $Never)
520
552
 
521
553
  /**
522
554
  * @template {{ [key: string|symbol|number]: Schema<any> }} S
@@ -561,8 +593,8 @@ export class $Object extends Schema {
561
593
  err?.extend(null, 'object', 'null')
562
594
  return false
563
595
  }
564
- return obj.every(this.shape, (vv, vk) => {
565
- const c = (this._isPartial && !obj.hasProperty(o, vk)) || vv.check(o[vk], err)
596
+ return object.every(this.shape, (vv, vk) => {
597
+ const c = (this._isPartial && !object.hasProperty(o, vk)) || vv.check(o[vk], err)
566
598
  !c && err?.extend(vk.toString(), vv.toString(), typeof o[vk], 'Object property does not match')
567
599
  return c
568
600
  })
@@ -581,7 +613,7 @@ export class $Object extends Schema {
581
613
  */
582
614
  /* @__NO_SIDE_EFFECTS__ */
583
615
  export const $object = def => /** @type {any} */ (new $Object(def))
584
- export const $$object = /* @__PURE__ */$constructedBy($Object)
616
+ export const $$object = /* @__PURE__ */$type('s:$Object', $Object)
585
617
 
586
618
  /**
587
619
  * @template {{ [key:string|symbol|number]: any }} S
@@ -619,7 +651,7 @@ export class $Record extends Schema {
619
651
  * @return {o is { [key in Unwrap<Keys>]: Unwrap<Values> }}
620
652
  */
621
653
  check (o, err) {
622
- return o != null && obj.every(o, (vv, vk) => {
654
+ return o != null && object.every(o, (vv, vk) => {
623
655
  const ck = this.shape.keys.check(vk, err)
624
656
  /* c8 ignore next */
625
657
  !ck && err?.extend(vk + '', 'Record', typeof o, ck ? 'Key doesn\'t match schema' : 'Value doesn\'t match value')
@@ -637,7 +669,7 @@ export class $Record extends Schema {
637
669
  */
638
670
  /* @__NO_SIDE_EFFECTS__ */
639
671
  export const $record = (keys, values) => new $Record(keys, values)
640
- export const $$record = /* @__PURE__ */$constructedBy($Record)
672
+ export const $$record = /* @__PURE__ */$type('s:$Record', $Record)
641
673
 
642
674
  /**
643
675
  * @template {Schema<any>[]} S
@@ -658,7 +690,7 @@ export class $Tuple extends Schema {
658
690
  * @return {o is { [K in keyof S]: S[K] extends Schema<infer Type> ? Type : never }}
659
691
  */
660
692
  check (o, err) {
661
- return o != null && obj.every(this.shape, (vv, vk) => {
693
+ return o != null && object.every(this.shape, (vv, vk) => {
662
694
  const c = /** @type {Schema<any>} */ (vv).check(o[vk], err)
663
695
  /* c8 ignore next */
664
696
  !c && err?.extend(vk.toString(), 'Tuple', typeof vv)
@@ -674,7 +706,7 @@ export class $Tuple extends Schema {
674
706
  */
675
707
  /* @__NO_SIDE_EFFECTS__ */
676
708
  export const $tuple = (...def) => new $Tuple(def)
677
- export const $$tuple = /* @__PURE__ */$constructedBy($Tuple)
709
+ export const $$tuple = /* @__PURE__ */$type('s:$Tuple', $Tuple)
678
710
 
679
711
  /**
680
712
  * @template {Schema<any>} S
@@ -712,7 +744,7 @@ export class $Array extends Schema {
712
744
  */
713
745
  /* @__NO_SIDE_EFFECTS__ */
714
746
  export const $array = (...def) => new $Array(def)
715
- export const $$array = /* @__PURE__ */$constructedBy($Array)
747
+ export const $$array = /* @__PURE__ */$type('s:$Array', $Array)
716
748
  /**
717
749
  * @type {Schema<Array<any>>}
718
750
  */
@@ -754,9 +786,9 @@ export class $InstanceOf extends Schema {
754
786
  */
755
787
  /* @__NO_SIDE_EFFECTS__ */
756
788
  export const $instanceOf = (c, check = null) => new $InstanceOf(c, check)
757
- export const $$instanceOf = /* @__PURE__ */$constructedBy($InstanceOf)
789
+ export const $$instanceOf = /* @__PURE__ */$type('s:$InstanceOf', $InstanceOf)
758
790
 
759
- export const $$schema = /* @__PURE__ */$instanceOf(Schema)
791
+ export const $$schema = /** @type {Schema<Schema<any>>} */ (Schema.prototype.$type = $custom(o => o?.isSchema === schemaSymbol))
760
792
 
761
793
  /**
762
794
  * @template {Schema<any>[]} Args
@@ -798,7 +830,7 @@ export class $Lambda extends Schema {
798
830
  */
799
831
  /* @__NO_SIDE_EFFECTS__ */
800
832
  export const $lambda = (...args) => new $Lambda(args.length > 0 ? args : [$void])
801
- export const $$lambda = /* @__PURE__ */$constructedBy($Lambda)
833
+ export const $$lambda = /* @__PURE__ */$type('s:$Lambda', $Lambda)
802
834
 
803
835
  /**
804
836
  * @type {Schema<Function>}
@@ -842,7 +874,7 @@ export class $Intersection extends Schema {
842
874
  */
843
875
  /* @__NO_SIDE_EFFECTS__ */
844
876
  export const $intersect = (...def) => new $Intersection(def)
845
- export const $$intersect = /* @__PURE__ */$constructedBy($Intersection, o => o.shape.length > 0) // Intersection with length=0 is considered "any"
877
+ export const $$intersect = /* @__PURE__ */$type('s:Intersection', $Intersection)
846
878
 
847
879
  /**
848
880
  * @template S
@@ -880,7 +912,7 @@ export const $union = (...schemas) => schemas.findIndex($s => $$union.check($s))
880
912
  : (schemas.length === 1
881
913
  ? schemas[0]
882
914
  : new $Union(schemas.map($)))
883
- export const $$union = /** @type {Schema<$Union<any>>} */ (/* @__PURE__ */$constructedBy($Union))
915
+ export const $$union = /** @type {Schema<$Union<any>>} */ (/* @__PURE__ */$type('s:$Union', $Union))
884
916
 
885
917
  /**
886
918
  * @template {any} IN
@@ -925,15 +957,13 @@ export const $ = o => {
925
957
  } else if ($function.check(o)) {
926
958
  return /** @type {any} */ ($constructedBy(/** @type {any} */ (o)))
927
959
  }
928
- /* c8 ignore next */
960
+ /* c8 ignore next 2 */
929
961
  error.unexpectedCase()
930
962
  }
931
963
 
932
964
  /* c8 ignore start */
933
965
  /**
934
966
  * Assert that a variable is of this specific type.
935
- * The assertion check is only performed in non-production environments.
936
- *
937
967
  * @type {<T>(o:any,schema:Schema<T>) => asserts o is T}
938
968
  */
939
969
  /* @__NO_SIDE_EFFECTS__ */
@@ -945,6 +975,13 @@ export const assert = (o, schema) => {
945
975
  }
946
976
  /* c8 ignore end */
947
977
 
978
+ /**
979
+ * Assert that a variable is of this specific type.
980
+ * @type {<T>(o:any,schema:Schema<T>) => asserts o is T}
981
+ */
982
+ /* @__NO_SIDE_EFFECTS__ */
983
+ export const assertNoCheck = (_o, _schema) => { }
984
+
948
985
  /* @__NO_SIDE_EFFECTS__ */
949
986
  const _t = () => true
950
987
  /**
@@ -952,58 +989,58 @@ const _t = () => true
952
989
  */
953
990
  /* @__NO_SIDE_EFFECTS__ */
954
991
  export const $any = $custom(_t)
955
- export const $$any = /** @type {Schema<Schema<any>>} */ (/* @__PURE__ */$constructedBy($Custom, o => o.shape === _t))
992
+ export const $$any = /** @type {Schema<Schema<any>>} */ (/* @__PURE__ */$type('s:$any', $any))
956
993
 
957
994
  /**
958
995
  * @type {Schema<bigint>}
959
996
  */
960
997
  export const $bigint = /* @__PURE__ */$custom(o => typeof o === 'bigint')
961
- export const $$bigint = /** @type {Schema<Schema<BigInt>>} */ (/* @__PURE__ */$custom(o => o === $bigint))
998
+ export const $$bigint = /** @type {Schema<Schema<BigInt>>} */ (/* @__PURE__ */$type('s:$bigint', $bigint))
962
999
 
963
1000
  /**
964
1001
  * @type {Schema<symbol>}
965
1002
  */
966
1003
  export const $symbol = /* @__PURE__ */$custom(o => typeof o === 'symbol')
967
- export const $$symbol = /** @type {Schema<Schema<Symbol>>} */ (/* @__PURE__ */$custom(o => o === $symbol))
1004
+ export const $$symbol = /** @type {Schema<Schema<Symbol>>} */ (/* @__PURE__ */$type('s:$symbol', $symbol))
968
1005
 
969
1006
  /**
970
1007
  * @type {Schema<number>}
971
1008
  */
972
1009
  export const $number = /* @__PURE__ */$custom(o => typeof o === 'number')
973
- export const $$number = /** @type {Schema<Schema<number>>} */ (/* @__PURE__ */$custom(o => o === $number))
1010
+ export const $$number = /** @type {Schema<Schema<number>>} */ (/* @__PURE__ */$type('s:$number', $number))
974
1011
 
975
1012
  /**
976
1013
  * @type {Schema<string>}
977
1014
  */
978
1015
  export const $string = /* @__PURE__ */$custom(o => typeof o === 'string')
979
- export const $$string = /** @type {Schema<Schema<string>>} */ (/* @__PURE__ */$custom(o => o === $string))
1016
+ export const $$string = /** @type {Schema<Schema<string>>} */ (/* @__PURE__ */$type('s:$string', $string))
980
1017
 
981
1018
  /**
982
1019
  * @type {Schema<boolean>}
983
1020
  */
984
1021
  export const $boolean = /* @__PURE__ */$custom(o => typeof o === 'boolean')
985
- export const $$boolean = /** @type {Schema<Schema<Boolean>>} */ (/* @__PURE__ */$custom(o => o === $boolean))
1022
+ export const $$boolean = /** @type {Schema<Schema<Boolean>>} */ (/* @__PURE__ */$type('s:$boolean', $boolean))
986
1023
 
987
1024
  /**
988
1025
  * @type {Schema<undefined>}
989
1026
  */
990
1027
  export const $undefined = /* @__PURE__ */$literal(undefined)
991
- export const $$undefined = /** @type {Schema<Schema<undefined>>} */ (/* @__PURE__ */$constructedBy($Literal, o => o.shape.length === 1 && o.shape[0] === undefined))
1028
+ export const $$undefined = /** @type {Schema<Schema<undefined>>} */ (/* @__PURE__ */$type('s:$undefined', $undefined))
992
1029
 
993
1030
  /**
994
1031
  * @type {Schema<void>}
995
1032
  */
996
- export const $void = $literal(undefined)
1033
+ export const $void = $undefined
997
1034
  export const $$void = /** @type {Schema<Schema<void>>} */ ($$undefined)
998
1035
 
999
1036
  export const $null = $literal(null)
1000
- export const $$null = /** @type {Schema<Schema<null>>} */ ($constructedBy($Literal, o => o.shape.length === 1 && o.shape[0] === null))
1037
+ export const $$null = /** @type {Schema<Schema<null>>} */ ($type('s:$null', $null))
1001
1038
 
1002
1039
  export const $uint8Array = $constructedBy(Uint8Array)
1003
- export const $$uint8Array = /** @type {Schema<Schema<Uint8Array>>} */ ($constructedBy($ConstructedBy, o => o.shape === Uint8Array))
1040
+ export const $$uint8Array = /** @type {Schema<Schema<Uint8Array>>} */ ($type('s:$uint8Array', $uint8Array))
1004
1041
 
1005
1042
  export const $promise = $constructedBy(Promise)
1006
- export const $$promise = /** @type {Schema<Schema<Uint8Array>>} */ ($constructedBy($ConstructedBy, o => o.shape === Promise))
1043
+ export const $$promise = /** @type {Schema<Schema<Uint8Array>>} */ ($type('s:$promise', $promise))
1007
1044
 
1008
1045
  /**
1009
1046
  * Primitive JavaScript types: immutable, non-object value that is stored and compared directly by
@@ -1038,6 +1075,16 @@ export const $json = /* @__PURE__ */(() => {
1038
1075
  * @typedef {{ if: Schema<In>, h: (o:In,state?:any)=>Out }} Pattern
1039
1076
  */
1040
1077
 
1078
+ /**
1079
+ * @template {Pattern<any,any>} Ps
1080
+ * @template S
1081
+ * @typedef {import('./ts.js').UnionToIntersection<Ps extends Pattern<infer In, infer Out> ? [S] extends [undefined] ? (o: In) => Out : (o: In, state: S) => Out : never>
1082
+ * & ([S] extends [undefined]
1083
+ * ? (o: Ps extends Pattern<infer In, any> ? In : never) => (Ps extends Pattern<any, infer Out> ? Out : never)
1084
+ * : (o: Ps extends Pattern<infer In, any> ? In : never, state: S) => (Ps extends Pattern<any, infer Out> ? Out : never))
1085
+ * } PatternMatcherFn
1086
+ */
1087
+
1041
1088
  const unhandledPatternError = 'Unhandled pattern'
1042
1089
 
1043
1090
  /**
@@ -1089,9 +1136,7 @@ export class PatternMatcher {
1089
1136
  }
1090
1137
 
1091
1138
  /**
1092
- * @return {(
1093
- * Patterns extends Pattern<infer In, infer Out> ? (k: State extends undefined ? (o: In) => Out : (o: In, state: State) => Out) => void : never
1094
- * ) extends (k: infer I) => void ? I : never}
1139
+ * @return {PatternMatcherFn<Patterns, State>}
1095
1140
  */
1096
1141
  done () {
1097
1142
  // @ts-ignore
@@ -1121,10 +1166,11 @@ export const match = state => new PatternMatcher(/** @type {any} */ (state))
1121
1166
  *
1122
1167
  * @type {<T>(o:T,opts:any)=>ReadSchemaUnwrapped<T>}
1123
1168
  */
1124
- const _random = /* @__PURE__ */ (() => match({ gen: /** @type {Schema<prng.PRNG>} */ ($any), fallback: $lambda($any,$any,$any).optional })
1169
+ const _random = /* @__PURE__ */ (() => match({ gen: /** @type {Schema<prng.PRNG>} */ ($any), fallback: $lambda($any, $any, $any).optional })
1125
1170
  .if($$number, (_o, { gen }) => prng.oneOf(gen, [-1, 0, 1, prng.int53(gen, number.MIN_SAFE_INTEGER, number.MAX_SAFE_INTEGER)]))
1126
1171
  .if($$string, (_o, { gen }) => prng.word(gen))
1127
1172
  .if($$boolean, (_o, { gen }) => prng.bool(gen))
1173
+ .if($$undefined, (_o) => undefined)
1128
1174
  .if($$bigint, (_o, { gen }) => BigInt(prng.int53(gen, number.MIN_SAFE_INTEGER, number.MAX_SAFE_INTEGER)))
1129
1175
  .if($$union, (o, opts) => _random(prng.oneOf(opts.gen, o.shape), opts))
1130
1176
  .if($$object, (o, opts) => {
package/src/ts.js CHANGED
@@ -45,6 +45,17 @@
45
45
  * @typedef {{[K in keyof OBJ as K extends keyof Renames ? Renames[K] : K extends Renames[keyof Renames] ? never : K]: OBJ[K]}} PropsRename
46
46
  */
47
47
 
48
+ /**
49
+ * Convert a union type to an intersection type.
50
+ *
51
+ * @example
52
+ * // { a: string } & { b: number }
53
+ * type R = UnionToIntersection<{ a: string } | { b: number }>
54
+ *
55
+ * @template U
56
+ * @typedef {(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never} UnionToIntersection
57
+ */
58
+
48
59
  /**
49
60
  * Merge two objects, keeping only keys present in both with intersected value types.
50
61
  * e.g. IntersectObjects<{a:string,b:string|number},{b:string,c:number}> = {b:string}
@@ -64,7 +64,6 @@ export class TextOp extends list.ListNode {
64
64
  * @type {string?}
65
65
  */
66
66
  _fingerprint: string | null;
67
- get $type(): s.Schema<TextOp>;
68
67
  /**
69
68
  * @param {string} newVal
70
69
  */
@@ -90,6 +89,7 @@ export class TextOp extends list.ListNode {
90
89
  * @return {TextOp}
91
90
  */
92
91
  clone(start?: number, end?: number): TextOp;
92
+ $type: s.Schema<TextOp>;
93
93
  /**
94
94
  * @param {TextOp} other
95
95
  */
@@ -124,7 +124,6 @@ export class InsertOp<ArrayContent extends unknown> extends list.ListNode {
124
124
  * @type {string?}
125
125
  */
126
126
  _fingerprint: string | null;
127
- get $type(): s.Schema<InsertOp<any>>;
128
127
  /**
129
128
  * @param {ArrayContent} newVal
130
129
  */
@@ -155,6 +154,7 @@ export class InsertOp<ArrayContent extends unknown> extends list.ListNode {
155
154
  * @return {InsertOp<ArrayContent>}
156
155
  */
157
156
  clone(start?: number, end?: number): InsertOp<ArrayContent>;
157
+ $type: s.Schema<InsertOp<any>>;
158
158
  /**
159
159
  * @param {InsertOp<ArrayContent>} other
160
160
  */
@@ -175,7 +175,6 @@ export class DeleteOp<Conf extends DeltaConf = {}> extends list.ListNode {
175
175
  * @type {string|null}
176
176
  */
177
177
  _fingerprint: string | null;
178
- get $type(): s.Schema<DeleteOp<any>>;
179
178
  /**
180
179
  * @return {'delete'}
181
180
  */
@@ -199,6 +198,7 @@ export class DeleteOp<Conf extends DeltaConf = {}> extends list.ListNode {
199
198
  * @return {DeleteOp}
200
199
  */
201
200
  clone(start?: number, end?: number): DeleteOp;
201
+ $type: s.Schema<DeleteOp<any>>;
202
202
  /**
203
203
  * @param {DeleteOp} other
204
204
  */
@@ -230,7 +230,6 @@ export class RetainOp extends list.ListNode {
230
230
  * @type {string|null}
231
231
  */
232
232
  _fingerprint: string | null;
233
- get $type(): s.Schema<RetainOp>;
234
233
  /**
235
234
  * @return {'retain'}
236
235
  */
@@ -249,6 +248,7 @@ export class RetainOp extends list.ListNode {
249
248
  */
250
249
  toJSON(): DeltaListOpJSON;
251
250
  clone(start?: number, end?: number): RetainOp;
251
+ $type: s.Schema<RetainOp>;
252
252
  /**
253
253
  * @param {RetainOp} other
254
254
  */
@@ -285,7 +285,6 @@ export class ModifyOp<DTypes extends Delta = DeltaAny> extends list.ListNode {
285
285
  * @type {string|null}
286
286
  */
287
287
  _fingerprint: string | null;
288
- get $type(): s.Schema<ModifyOp<DeltaAny>>;
289
288
  /**
290
289
  * @return {'modify'}
291
290
  */
@@ -311,6 +310,7 @@ export class ModifyOp<DTypes extends Delta = DeltaAny> extends list.ListNode {
311
310
  * @return {ModifyOp<DTypes>}
312
311
  */
313
312
  clone(): ModifyOp<DTypes>;
313
+ $type: s.Schema<ModifyOp<Delta<any>>>;
314
314
  /**
315
315
  * @param {ModifyOp<any>} other
316
316
  */
@@ -352,7 +352,6 @@ export class SetAttrOp<V extends unknown = any, K extends string | number = any>
352
352
  * @type {string|null}
353
353
  */
354
354
  _fingerprint: string | null;
355
- get $type(): s.Schema<SetAttrOp<any, any>>;
356
355
  /**
357
356
  * @return {'insert'}
358
357
  */
@@ -374,6 +373,7 @@ export class SetAttrOp<V extends unknown = any, K extends string | number = any>
374
373
  * @return {SetAttrOp<V,K>}
375
374
  */
376
375
  clone(): SetAttrOp<V, K>;
376
+ $type: s.Schema<SetAttrOp<any, any>>;
377
377
  /**
378
378
  * @param {SetAttrOp<V>} other
379
379
  */
@@ -403,7 +403,6 @@ export class DeleteAttrOp<V = any, K extends string | number = string | number>
403
403
  * @type {string|null}
404
404
  */
405
405
  _fingerprint: string | null;
406
- get $type(): s.Schema<DeleteAttrOp<any, string | number>>;
407
406
  /**
408
407
  * @type {'delete'}
409
408
  */
@@ -415,6 +414,7 @@ export class DeleteAttrOp<V = any, K extends string | number = string | number>
415
414
  */
416
415
  toJSON(): DeltaAttrOpJSON;
417
416
  clone(): DeleteAttrOp<V, K>;
417
+ $type: s.Schema<DeleteAttrOp<any, string | number>>;
418
418
  /**
419
419
  * @param {DeleteAttrOp<V>} other
420
420
  */
@@ -444,7 +444,6 @@ export class ModifyAttrOp<Modifier extends DeltaAny = DeltaAny, K extends string
444
444
  * @type {string|null}
445
445
  */
446
446
  _fingerprint: string | null;
447
- get $type(): s.Schema<ModifyAttrOp<any, string | number>>;
448
447
  /**
449
448
  * @type {'modify'}
450
449
  */
@@ -462,21 +461,22 @@ export class ModifyAttrOp<Modifier extends DeltaAny = DeltaAny, K extends string
462
461
  * @return {ModifyAttrOp<Modifier,K>}
463
462
  */
464
463
  clone(): ModifyAttrOp<Modifier, K>;
464
+ $type: s.Schema<ModifyAttrOp<any, string>>;
465
465
  /**
466
466
  * @param {ModifyAttrOp<Modifier>} other
467
467
  */
468
468
  [equalityTrait.EqualityTraitSymbol](other: ModifyAttrOp<Modifier>): boolean;
469
469
  }
470
470
  export const $insertOp: s.Schema<InsertOp<any>>;
471
- export const $modifyOp: s.Schema<ModifyOp>;
471
+ export const $modifyOp: s.Schema<ModifyOp<Delta<any>>>;
472
472
  export const $textOp: s.Schema<TextOp>;
473
473
  export const $deleteOp: s.Schema<DeleteOp<any>>;
474
474
  export const $retainOp: s.Schema<RetainOp>;
475
- export const $anyOp: s.Schema<TextOp | InsertOp<any> | DeleteOp<any> | ModifyOp<DeltaAny>>;
475
+ export const $anyOp: s.Schema<TextOp | InsertOp<any> | DeleteOp<any> | ModifyOp<Delta<any>>>;
476
476
  export const $setAttrOp: s.Schema<SetAttrOp<any>>;
477
- export const $modifyAttrOp: s.Schema<ModifyAttrOp<any, string | number>>;
478
- export const $deleteAttrOp: s.Schema<DeleteAttrOp<any, string | number>>;
479
- export const $anyAttrOp: s.Schema<SetAttrOp<any, any> | DeleteAttrOp<any, string | number> | ModifyAttrOp<any, string | number>>;
477
+ export const $modifyAttrOp: s.Schema<ModifyAttrOp<any>>;
478
+ export const $deleteAttrOp: s.Schema<DeleteAttrOp<any>>;
479
+ export const $anyAttrOp: s.Schema<SetAttrOp<any, any> | DeleteAttrOp<any, string | number> | ModifyAttrOp<any, string>>;
480
480
  export function $setAttrOpWith<Content extends fingerprintTrait.Fingerprintable>($content: s.Schema<Content>): s.Schema<SetAttrOp<Content>>;
481
481
  export function $insertOpWith<Content extends fingerprintTrait.Fingerprintable>($content: s.Schema<Content>): s.Schema<InsertOp<Content>>;
482
482
  export function $modifyOpWith<Modify extends DeltaAny>($content: s.Schema<Modify>): s.Schema<ModifyOp<Modify>>;
@@ -503,7 +503,6 @@ export class Delta<Conf extends DeltaConf = {}> extends DeltaData<DeltaConfGetNa
503
503
  * @param {s.Schema<Delta<any>>?} $schema
504
504
  */
505
505
  constructor(name: string | null, $schema: s.Schema<Delta<any>> | null);
506
- get $type(): s.Schema<DeltaAny>;
507
506
  /**
508
507
  * @type {string}
509
508
  */
@@ -526,6 +525,7 @@ export class Delta<Conf extends DeltaConf = {}> extends DeltaData<DeltaConfGetNa
526
525
  * @return {Delta<Conf>}
527
526
  */
528
527
  done(markAsDone?: boolean): Delta<Conf>;
528
+ $type: s.Schema<Delta<any>>;
529
529
  [fingerprintTrait.FingerprintTraitSymbol](): string;
530
530
  /**
531
531
  * @param {any} other
@@ -590,13 +590,13 @@ export class DeltaBuilder<Conf extends DeltaConf = {}, FixedConf extends boolean
590
590
  text: true;
591
591
  })>, FixedConf>;
592
592
  /**
593
- * @template {Extract<DeltaConfGetAllowedChildren<Conf, FixedConf>,Delta>} NewContent
593
+ * @template {Extract<DeltaConfGetAllowedChildren<Conf, FixedConf>,Delta|DeltaData<any,any,any,any>|DeltaBuilder>} NewContent
594
594
  * @param {NewContent} modify
595
595
  * @param {FormattingAttributes?} formatting
596
596
  * @param {Attribution?} attribution
597
597
  * @return {DeltaBuilder<DeltaConfOverwrite<Conf, {children: DeltaConfGetChildren<Conf>|NewContent}>, FixedConf>}
598
598
  */
599
- modify<NewContent extends Extract<DeltaConfGetAllowedChildren<Conf, FixedConf>, Delta>>(modify: NewContent, formatting?: FormattingAttributes | null, attribution?: Attribution | null): DeltaBuilder<DeltaConfOverwrite<Conf, {
599
+ modify<NewContent extends Extract<DeltaConfGetAllowedChildren<Conf, FixedConf>, Delta | DeltaData<any, any, any, any> | DeltaBuilder>>(modify: NewContent, formatting?: FormattingAttributes | null, attribution?: Attribution | null): DeltaBuilder<DeltaConfOverwrite<Conf, {
600
600
  children: DeltaConfGetChildren<Conf> | NewContent;
601
601
  }>, FixedConf>;
602
602
  /**
@@ -729,34 +729,9 @@ export class $Delta<Conf extends DeltaConf> extends s.Schema<Delta<Conf>> {
729
729
  }>;
730
730
  };
731
731
  }
732
- export function $delta<NodeNameSchema extends s.Schema<string> | string | Array<string> = s.Schema<any>, AttrsSchema extends s.Schema<{
733
- [key: string | number]: any;
734
- }> | {
735
- [key: string | number]: any;
736
- } = s.Schema<{}>, ChildrenSchema extends unknown = s.Schema<never>, HasText extends boolean = false, RecursiveChildren extends boolean = false, Formats extends {
737
- [k: string]: any;
738
- } = {
739
- [k: string]: any;
740
- }>({ name, attrs, children, text, formats, recursiveChildren: recursive }: {
741
- name?: NodeNameSchema | null | undefined;
742
- attrs?: AttrsSchema | null | undefined;
743
- children?: ChildrenSchema | null | undefined;
744
- text?: HasText | undefined;
745
- formats?: Formats | undefined;
746
- recursiveChildren?: RecursiveChildren | undefined;
747
- }): [s.ReadSchemaUnwrapped<NodeNameSchema>, s.ReadSchemaUnwrapped<AttrsSchema>, s.ReadSchemaUnwrapped<ChildrenSchema>] extends [infer NodeName_1, infer Attrs_1, infer Children_1] ? s.Schema<Delta<PrettifyDeltaConf<(import("../ts.js").TypeIsAny<NodeName_1, {}, {
748
- name: NodeName_1;
749
- }> & ([keyof Attrs_1] extends [never] ? {} : {
750
- attrs: Attrs_1;
751
- }) & ([Children_1] extends [never] ? {} : {
752
- children: Children_1;
753
- }) & (HasText extends false ? {} : {
754
- text: HasText;
755
- }) & (RecursiveChildren extends false ? {} : {
756
- recursiveChildren: RecursiveChildren;
757
- })) extends infer DC extends DeltaConf ? DC : never>>> : never;
732
+ export function $delta<DeltaConf extends ReadableDeltaConf = {}>({ name, attrs, children, text, formats, recursiveChildren: recursive }: DeltaConf): s.Schema<Delta<ReadDeltaConf<DeltaConf>>>;
758
733
  export const $$delta: s.Schema<$Delta<DeltaConf>>;
759
- export const $deltaAny: s.Schema<DeltaAny>;
734
+ export const $deltaAny: s.Schema<Delta<any>>;
760
735
  export const $deltaBuilderAny: s.Schema<DeltaBuilderAny>;
761
736
  export function mergeAttrs<T extends {
762
737
  [key: string]: any;
@@ -998,6 +973,44 @@ export type DeltaConfigGetRecursiveAttrs<Conf_1 extends DeltaConf> = Conf_1 exte
998
973
  export type _SanifyDelta<V> = V extends never ? never : (import("../ts.js").TypeIsAny<V, any, V extends Delta<infer Conf_1> ? Delta<Conf_1> : V>);
999
974
  export type PrettifyDeltaConf<Conf_1 extends DeltaConf> = import("../ts.js").Prettify<{ [K in keyof Conf_1]: K extends "attrs" ? import("../ts.js").Prettify<{ [KA in keyof Conf_1[K]]: _SanifyDelta<Conf_1[K][KA]>; }, 1> : (K extends "children" ? _SanifyDelta<Conf_1[K]> : Conf_1[K]); }, 1>;
1000
975
  export type DeltaConfOverwrite<D1 extends DeltaConf, D2> = (import("../ts.js").TypeIsAny<D1, any, PrettifyDeltaConf<{ [K in (keyof D1 | keyof D2)]: K extends keyof D2 ? D2[K] : (K extends keyof D1 ? D1[K] : never); }>> & {}) extends infer DC extends DeltaConf ? DC : never;
976
+ export type ReadableDeltaConf = {
977
+ name?: s.Schema<string> | string | Array<string>;
978
+ attrs?: s.Schema<{
979
+ [key: string | number]: any;
980
+ }> | {
981
+ [key: string | number]: any;
982
+ };
983
+ children?: any;
984
+ text?: boolean;
985
+ recursiveChildren?: boolean;
986
+ formats?: {
987
+ [k: string]: any;
988
+ };
989
+ };
990
+ /**
991
+ * Transforms a ReadableDeltaConf (the input shape of `$delta(spec)`) to a DeltaConf.
992
+ */
993
+ export type ReadDeltaConf<DConfSpec extends ReadableDeltaConf> = [DConfSpec extends {
994
+ name: infer N;
995
+ } ? s.ReadSchemaUnwrapped<N> : any, DConfSpec extends {
996
+ attrs: infer A;
997
+ } ? s.ReadSchemaUnwrapped<A> : {}, DConfSpec extends {
998
+ children: infer C;
999
+ } ? s.ReadSchemaUnwrapped<C> : never] extends [infer NodeName_1, infer Attrs_1, infer Children_1] ? PrettifyDeltaConf<(import("../ts.js").TypeIsAny<NodeName_1, {}, {
1000
+ name: NodeName_1;
1001
+ }> & ([keyof Attrs_1] extends [never] ? {} : {
1002
+ attrs: Attrs_1;
1003
+ }) & ([Children_1] extends [never] ? {} : {
1004
+ children: Children_1;
1005
+ }) & (DConfSpec extends {
1006
+ text: true;
1007
+ } ? {
1008
+ text: true;
1009
+ } : {}) & (DConfSpec extends {
1010
+ recursiveChildren: true;
1011
+ } ? {
1012
+ recursiveChildren: true;
1013
+ } : {})) extends infer DC extends DeltaConf ? DC : never> : never;
1001
1014
  import * as s from '../schema.js';
1002
1015
  import * as list from '../list.js';
1003
1016
  import * as equalityTrait from '../trait/equality.js';
package/types/schema.d.ts CHANGED
@@ -71,6 +71,7 @@ export class Schema<T> implements equalityTraits.EqualityTrait {
71
71
  * @protected
72
72
  */
73
73
  protected static _dilutes: boolean;
74
+ get isSchema(): symbol;
74
75
  /**
75
76
  * Overwrite this when necessary. By default, we only check the `shape` property which every shape
76
77
  * should have.
@@ -120,8 +121,6 @@ export class Schema<T> implements equalityTraits.EqualityTrait {
120
121
  * "Always." - Snape, talking about type safety
121
122
  *
122
123
  * Ensures that a variable is a a specific type. Returns the value, or throws an exception if the assertion check failed.
123
- * Use this if you know that the type is of a specific type and you just want to convince the type
124
- * system.
125
124
  *
126
125
  * Can be useful when defining lambdas: `s.lambda(s.$number, s.$number).expect((n) => n + 1)`
127
126
  *
@@ -129,11 +128,37 @@ export class Schema<T> implements equalityTraits.EqualityTrait {
129
128
  * @return {o extends T ? T : never}
130
129
  */
131
130
  expect(o: T): T extends T ? T : never;
131
+ $type: Schema<Schema<any>>;
132
132
  /**
133
133
  * @param {object} other
134
134
  */
135
135
  [equalityTraits.EqualityTraitSymbol](other: object): boolean;
136
136
  }
137
+ /**
138
+ * @extends {Schema<any>}
139
+ */
140
+ export class $Type extends Schema<any> {
141
+ /**
142
+ * @param {string} name
143
+ */
144
+ constructor(name: string);
145
+ /**
146
+ * @type {string}
147
+ */
148
+ name: string;
149
+ typeSymbol: symbol;
150
+ /**
151
+ * @param {any} o
152
+ * @param {ValidationError} err
153
+ * @return {o is any}
154
+ */
155
+ check(o: any, err: ValidationError): o is any;
156
+ $type: Schema<$Type>;
157
+ }
158
+ export function $type<T extends new (...args: any[]) => any>(uniqueName: string, TypeConstructor: T | {
159
+ [K: string | number | symbol]: any;
160
+ } | null): Schema<InstanceType<T>>;
161
+ export const $$type: Schema<$Type>;
137
162
  /**
138
163
  * @template {(new (...args:any[]) => any) | ((...args:any[]) => any)} Constr
139
164
  * @typedef {Constr extends ((...args:any[]) => infer T) ? T : (Constr extends (new (...args:any[]) => any) ? InstanceType<Constr> : never)} Instance
@@ -182,24 +207,6 @@ export class $Custom extends Schema<any> {
182
207
  }
183
208
  export function $custom(check: (o: any) => boolean): Schema<any>;
184
209
  export const $$custom: Schema<$Custom>;
185
- /**
186
- * @extends {Schema<{$type: Schema<any>}>}
187
- */
188
- export class $Type extends Schema<{
189
- $type: Schema<any>;
190
- }> {
191
- constructor();
192
- /**
193
- * @param {any} o
194
- * @param {ValidationError} err
195
- * @return {o is any}
196
- */
197
- check(o: any, err: ValidationError): o is any;
198
- }
199
- export function $type(uniqueName: string, version?: number): Schema<{
200
- $type: Schema<any>;
201
- }>;
202
- export const $$type: Schema<$Type>;
203
210
  /**
204
211
  * @template {Primitive} T
205
212
  * @extends {Schema<T>}
@@ -383,7 +390,7 @@ export class $InstanceOf<T> extends Schema<T> {
383
390
  }
384
391
  export function $instanceOf<T>(c: new (...args: any) => T, check?: ((o: T) => boolean) | null): Schema<T>;
385
392
  export const $$instanceOf: Schema<$InstanceOf<unknown>>;
386
- export const $$schema: Schema<Schema<unknown>>;
393
+ export const $$schema: Schema<Schema<any>>;
387
394
  /**
388
395
  * @template {Schema<any>[]} Args
389
396
  * @typedef {(...args:UnwrapArray<TuplePop<Args>>)=>Unwrap<TupleLast<Args>>} _LArgsToLambdaDef
@@ -445,11 +452,14 @@ export const $$union: Schema<$Union<any>>;
445
452
  export function $<IN>(o: IN): Schema<import("./ts.js").TypeIsAny<IN, any, ReadSchemaUnwrapped<IN>>>;
446
453
  /**
447
454
  * Assert that a variable is of this specific type.
448
- * The assertion check is only performed in non-production environments.
449
- *
450
455
  * @type {<T>(o:any,schema:Schema<T>) => asserts o is T}
451
456
  */
452
457
  export const assert: <T>(o: any, schema: Schema<T>) => asserts o is T;
458
+ /**
459
+ * Assert that a variable is of this specific type.
460
+ * @type {<T>(o:any,schema:Schema<T>) => asserts o is T}
461
+ */
462
+ export const assertNoCheck: <T>(o: any, schema: Schema<T>) => asserts o is T;
453
463
  /**
454
464
  * @type {Schema<any>}
455
465
  */
@@ -546,13 +556,11 @@ export class PatternMatcher<State extends unknown = undefined, Patterns extends
546
556
  /**
547
557
  * @param {string} errorMessage
548
558
  */
549
- error(errorMessage?: string): (Patterns | Pattern<any, never> extends infer T ? T extends Patterns | Pattern<any, never> ? T extends Pattern<infer In, infer Out> ? (k: State extends undefined ? (o: In) => Out : (o: In, state: State) => Out) => void : never : never : never) | ((k: State extends undefined ? (o: any) => never : (o: any, state: State) => never) => void) extends (k: infer I) => void ? I : never;
559
+ error(errorMessage?: string): PatternMatcherFn<Patterns | Pattern<any, never>, State>;
550
560
  /**
551
- * @return {(
552
- * Patterns extends Pattern<infer In, infer Out> ? (k: State extends undefined ? (o: In) => Out : (o: In, state: State) => Out) => void : never
553
- * ) extends (k: infer I) => void ? I : never}
561
+ * @return {PatternMatcherFn<Patterns, State>}
554
562
  */
555
- done(): (Patterns extends Pattern<infer In, infer Out> ? (k: State extends undefined ? (o: In) => Out : (o: In, state: State) => Out) => void : never) extends (k: infer I) => void ? I : never;
563
+ done(): PatternMatcherFn<Patterns, State>;
556
564
  }
557
565
  export function match<State = undefined>(state?: State): PatternMatcher<State extends undefined ? undefined : ReadSchemaUnwrapped<State>>;
558
566
  export function random<S>(gen: prng.PRNG, schema: S, fallback?: (gen: prng.PRNG, o: Schema<any>) => any): ReadSchemaUnwrapped<S>;
@@ -584,6 +592,7 @@ export type Pattern<In, Out> = {
584
592
  if: Schema<In>;
585
593
  h: (o: In, state?: any) => Out;
586
594
  };
595
+ export type PatternMatcherFn<Ps extends Pattern<any, any>, S> = import("./ts.js").UnionToIntersection<Ps extends Pattern<infer In, infer Out> ? [S] extends [undefined] ? (o: In) => Out : (o: In, state: S) => Out : never> & ([S] extends [undefined] ? (o: Ps extends Pattern<infer In, any> ? In : never) => (Ps extends Pattern<any, infer Out> ? Out : never) : (o: Ps extends Pattern<infer In, any> ? In : never, state: S) => (Ps extends Pattern<any, infer Out> ? Out : never));
587
596
  import * as equalityTraits from './trait/equality.js';
588
597
  /**
589
598
  * @extends Schema<never>
package/types/ts.d.ts CHANGED
@@ -19,6 +19,10 @@ export type KeyOf<Obj extends {
19
19
  export type PropsRename<OBJ extends { [K in string | number]: any; }, Renames extends {
20
20
  [K: string | number]: string | number;
21
21
  }> = { [K in keyof OBJ as K extends keyof Renames ? Renames[K] : K extends Renames[keyof Renames] ? never : K]: OBJ[K]; };
22
+ /**
23
+ * Convert a union type to an intersection type.
24
+ */
25
+ export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
22
26
  /**
23
27
  * Merge two objects, keeping only keys present in both with intersected value types.
24
28
  * e.g. IntersectObjects<{a:string,b:string|number},{b:string,c:number}> = {b:string}