lib0 1.0.0-rc.4 → 1.0.0-rc.6

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.4",
3
+ "version": "1.0.0-rc.6",
4
4
  "description": "",
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -288,7 +288,7 @@
288
288
  "trace-opt": "clear && node --trace-opt src/test.js",
289
289
  "lint": "tsc && standard",
290
290
  "gendocs": "node ./bin/gendocs.js",
291
- "np": "np --test-script dist",
291
+ "release": "npm run dist && np --test-script dist",
292
292
  "version": "npm run clean && npm run dist && git add README.md",
293
293
  "postpublish": "npm run clean",
294
294
  "gentesthtml": "node ./src/bin/gentesthtml.js --script src/test.js > test.html"
@@ -346,7 +346,7 @@ export class DeleteOp extends list.ListNode {
346
346
  }
347
347
 
348
348
  get length () {
349
- return 0
349
+ return this.delete
350
350
  }
351
351
 
352
352
  get fingerprint () {
@@ -1972,7 +1972,7 @@ export class $Delta extends s.Schema {
1972
1972
  * @param {Formats} [opts.formats]
1973
1973
  * @param {RecursiveChildren} [opts.recursiveChildren]
1974
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
- * ({} extends Attrs ? {} : { attrs: Attrs }) &
1975
+ * ([keyof Attrs] extends [never] ? {} : { attrs: Attrs }) &
1976
1976
  * ([Children] extends [never] ? {} : { children: Children }) &
1977
1977
  * (HasText extends false ? {} : { text: HasText }) &
1978
1978
  * (RecursiveChildren extends false ? {} : { recursiveChildren: RecursiveChildren })) extends infer DC extends DeltaConf ? DC : never>>> : never}
@@ -2325,7 +2325,7 @@ export const diff = (d1, d2) => {
2325
2325
  // split by alphanumerics and others
2326
2326
  const changeset3 = diffChangesetWithSeparator(changeset2, patience.smartSplitRegex)
2327
2327
  // split all
2328
- const changeset4 = diffChangesetWithSeparator(changeset3, /./g) // @todo this part should use myers diff
2328
+ const changeset4 = diffChangesetWithSeparator(changeset3, /./g)
2329
2329
  applyChangesetToDelta(d, changeset4)
2330
2330
  if (formattingNeedsDiff) {
2331
2331
  const formattingDiff = create()
@@ -16,7 +16,7 @@ import * as array from '../array.js'
16
16
  *
17
17
  * @param {Array<string>} as
18
18
  * @param {Array<string>} bs
19
- * @return {Array<{ index: number, remove: Array<string>, insert: Array<string>}>} changeset @todo should use delta instead
19
+ * @return {Array<{ index: number, remove: Array<string>, insert: Array<string>}>} changeset
20
20
  */
21
21
  export const diff = (as, bs) => {
22
22
  const {
@@ -35,9 +35,11 @@ export const diff = (as, bs) => {
35
35
  export const diffSplitBy = (a, b, regexp) => {
36
36
  const isStringSeparator = typeof regexp === 'string'
37
37
  const separator = /** @type {string} */ (isStringSeparator ? regexp : '')
38
- regexp = new RegExp(regexp, 'g')
39
- const as = splitByRegexp(a, regexp, !isStringSeparator)
40
- const bs = splitByRegexp(b, regexp, !isStringSeparator)
38
+ if (isStringSeparator) {
39
+ regexp = new RegExp(regexp, 'g')
40
+ }
41
+ const as = splitByRegexp(a, /** @type {RegExp} */ (regexp), !isStringSeparator)
42
+ const bs = splitByRegexp(b, /** @type {RegExp} */ (regexp), !isStringSeparator)
41
43
  const changes = diff(as, bs)
42
44
  let prevSplitIndex = 0
43
45
  let prevStringIndex = 0
@@ -54,11 +56,13 @@ export const diffSplitBy = (a, b, regexp) => {
54
56
  }
55
57
 
56
58
  /**
57
- * For internal use.
59
+ * Idea: match all "non-word" characters and keep them separate (spaces, comma, exclamation marks, ..).
60
+ * Anything that can be classivied as a word (emoji, char, number) will be bundled together. All other tokens are kept
61
+ * individually. "hello, world!!!!" => ['hello', ',', ' ', 'world', '!', '!']
58
62
  *
59
63
  * @experimental
60
64
  */
61
- export const smartSplitRegex = /[^\p{L}\p{N}]+/gu
65
+ export const smartSplitRegex = /[^\p{L}\p{N}\p{Extended_Pictographic}\p{Emoji_Modifier}\u200D\uFE0F]/gu
62
66
 
63
67
  /**
64
68
  * Sensible default for diffing strings using patience (it's fast though).
@@ -76,7 +80,7 @@ export const diffAuto = (a, b) =>
76
80
  remove: dd.remove,
77
81
  index: dd.index + d.index
78
82
  }))
79
- ).flat()
83
+ ).flat(1)
80
84
 
81
85
  /**
82
86
  * @param {Array<string>} as
package/src/schema.js CHANGED
@@ -876,12 +876,75 @@ export class $Union extends Schema {
876
876
  */
877
877
  /* @__NO_SIDE_EFFECTS__ */
878
878
  export const $union = (...schemas) => schemas.findIndex($s => $$union.check($s)) >= 0
879
- ? $union(...schemas.map($s => $($s)).map($s => $$union.check($s) ? $s.shape : [$s]).flat(1))
879
+ ? $union(...schemas.map($).map($s => $$union.check($s) ? $s.shape : [$s]).flat(1))
880
880
  : (schemas.length === 1
881
881
  ? schemas[0]
882
- : new $Union(schemas))
882
+ : new $Union(schemas.map($)))
883
883
  export const $$union = /** @type {Schema<$Union<any>>} */ (/* @__PURE__ */$constructedBy($Union))
884
884
 
885
+ /**
886
+ * @template {any} IN
887
+ * @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]
888
+ * ? (
889
+ * (Schemas extends Schema<infer S> ? S : never)
890
+ * | Primitives
891
+ * | (Constructors extends new (...args:any[])=>any ? InstanceType<Constructors> : never)
892
+ * | (Arrs extends any[] ? { [K in keyof Arrs]: ReadSchemaUnwrapped<Arrs[K]> }[keyof Arrs & number] : never)
893
+ * | (Obj extends object ? _ObjectDefToSchema<{[K in keyof Obj]:Schema<ReadSchemaUnwrapped<Obj[K]>>}> : never))
894
+ * : never
895
+ * } ReadSchemaUnwrapped
896
+ */
897
+
898
+ /**
899
+ * @template {any} IN
900
+ * @typedef {Schema<ReadSchemaUnwrapped<IN>>} ReadSchema
901
+ */
902
+
903
+ /**
904
+ * @template IN
905
+ * @param {IN} o
906
+ * @return {Schema<import('./ts.js').TypeIsAny<IN,any,ReadSchemaUnwrapped<IN>>>}
907
+ */
908
+ /* @__NO_SIDE_EFFECTS__ */
909
+ export const $ = o => {
910
+ if ($$schema.check(o)) {
911
+ return /** @type {any} */ (o)
912
+ } else if ($objectAny.check(o)) {
913
+ /**
914
+ * @type {any}
915
+ */
916
+ const o2 = {}
917
+ for (const k in o) {
918
+ o2[k] = $(o[k])
919
+ }
920
+ return /** @type {any} */ ($object(o2))
921
+ } else if ($arrayAny.check(o)) {
922
+ return /** @type {any} */ ($union(...o.map($)))
923
+ } else if ($primitive.check(o)) {
924
+ return /** @type {any} */ ($literal(o))
925
+ } else if ($function.check(o)) {
926
+ return /** @type {any} */ ($constructedBy(/** @type {any} */ (o)))
927
+ }
928
+ /* c8 ignore next */
929
+ error.unexpectedCase()
930
+ }
931
+
932
+ /* c8 ignore start */
933
+ /**
934
+ * Assert that a variable is of this specific type.
935
+ * The assertion check is only performed in non-production environments.
936
+ *
937
+ * @type {<T>(o:any,schema:Schema<T>) => asserts o is T}
938
+ */
939
+ /* @__NO_SIDE_EFFECTS__ */
940
+ export const assert = (o, schema) => {
941
+ const err = new ValidationError()
942
+ if (!schema.check(o, err)) {
943
+ throw error.create(`Expected value to be of type ${schema.constructor.name}.\n${err.toString()}`)
944
+ }
945
+ }
946
+ /* c8 ignore end */
947
+
885
948
  /* @__NO_SIDE_EFFECTS__ */
886
949
  const _t = () => true
887
950
  /**
@@ -969,69 +1032,6 @@ export const $json = /* @__PURE__ */(() => {
969
1032
  return $json
970
1033
  })()
971
1034
 
972
- /**
973
- * @template {any} IN
974
- * @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]
975
- * ? (
976
- * (Schemas extends Schema<infer S> ? S : never)
977
- * | Primitives
978
- * | (Constructors extends new (...args:any[])=>any ? InstanceType<Constructors> : never)
979
- * | (Arrs extends any[] ? { [K in keyof Arrs]: ReadSchemaUnwrapped<Arrs[K]> }[keyof Arrs & number] : never)
980
- * | (Obj extends object ? _ObjectDefToSchema<{[K in keyof Obj]:Schema<ReadSchemaUnwrapped<Obj[K]>>}> : never))
981
- * : never
982
- * } ReadSchemaUnwrapped
983
- */
984
-
985
- /**
986
- * @template {any} IN
987
- * @typedef {Schema<ReadSchemaUnwrapped<IN>>} ReadSchema
988
- */
989
-
990
- /**
991
- * @template IN
992
- * @param {IN} o
993
- * @return {Schema<import('./ts.js').TypeIsAny<IN,any,ReadSchemaUnwrapped<IN>>>}
994
- */
995
- /* @__NO_SIDE_EFFECTS__ */
996
- export const $ = o => {
997
- if ($$schema.check(o)) {
998
- return /** @type {any} */ (o)
999
- } else if ($objectAny.check(o)) {
1000
- /**
1001
- * @type {any}
1002
- */
1003
- const o2 = {}
1004
- for (const k in o) {
1005
- o2[k] = $(o[k])
1006
- }
1007
- return /** @type {any} */ ($object(o2))
1008
- } else if ($arrayAny.check(o)) {
1009
- return /** @type {any} */ ($union(...o.map($)))
1010
- } else if ($primitive.check(o)) {
1011
- return /** @type {any} */ ($literal(o))
1012
- } else if ($function.check(o)) {
1013
- return /** @type {any} */ ($constructedBy(/** @type {any} */ (o)))
1014
- }
1015
- /* c8 ignore next */
1016
- error.unexpectedCase()
1017
- }
1018
-
1019
- /* c8 ignore start */
1020
- /**
1021
- * Assert that a variable is of this specific type.
1022
- * The assertion check is only performed in non-production environments.
1023
- *
1024
- * @type {<T>(o:any,schema:Schema<T>) => asserts o is T}
1025
- */
1026
- /* @__NO_SIDE_EFFECTS__ */
1027
- export const assert = (o, schema) => {
1028
- const err = new ValidationError()
1029
- if (!schema.check(o, err)) {
1030
- throw error.create(`Expected value to be of type ${schema.constructor.name}.\n${err.toString()}`)
1031
- }
1032
- }
1033
- /* c8 ignore end */
1034
-
1035
1035
  /**
1036
1036
  * @template In
1037
1037
  * @template Out
@@ -746,7 +746,7 @@ export function $delta<NodeNameSchema extends s.Schema<string> | string | Array<
746
746
  recursiveChildren?: RecursiveChildren | undefined;
747
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
748
  name: NodeName_1;
749
- }> & ({} extends Attrs_1 ? {} : {
749
+ }> & ([keyof Attrs_1] extends [never] ? {} : {
750
750
  attrs: Attrs_1;
751
751
  }) & ([Children_1] extends [never] ? {} : {
752
752
  children: Children_1;
@@ -9,7 +9,9 @@ export function diffSplitBy(a: string, b: string, regexp: RegExp | string): {
9
9
  insert: string;
10
10
  }[];
11
11
  /**
12
- * For internal use.
12
+ * Idea: match all "non-word" characters and keep them separate (spaces, comma, exclamation marks, ..).
13
+ * Anything that can be classivied as a word (emoji, char, number) will be bundled together. All other tokens are kept
14
+ * individually. "hello, world!!!!" => ['hello', ',', ' ', 'world', '!', '!']
13
15
  *
14
16
  * @experimental
15
17
  */
package/types/schema.d.ts CHANGED
@@ -442,6 +442,14 @@ export class $Union<S> extends Schema<S> {
442
442
  }
443
443
  export function $union<T extends Array<any>>(...schemas: T): CastToSchema<$Union<ReadSchemaUnwrapped<T>>>;
444
444
  export const $$union: Schema<$Union<any>>;
445
+ export function $<IN>(o: IN): Schema<import("./ts.js").TypeIsAny<IN, any, ReadSchemaUnwrapped<IN>>>;
446
+ /**
447
+ * Assert that a variable is of this specific type.
448
+ * The assertion check is only performed in non-production environments.
449
+ *
450
+ * @type {<T>(o:any,schema:Schema<T>) => asserts o is T}
451
+ */
452
+ export const assert: <T>(o: any, schema: Schema<T>) => asserts o is T;
445
453
  /**
446
454
  * @type {Schema<any>}
447
455
  */
@@ -507,14 +515,6 @@ export const $primitive: Schema<Primitive>;
507
515
  export const $json: Schema<null | number | string | boolean | JSON[] | {
508
516
  [key: string]: JSON;
509
517
  }>;
510
- export function $<IN>(o: IN): Schema<import("./ts.js").TypeIsAny<IN, any, ReadSchemaUnwrapped<IN>>>;
511
- /**
512
- * Assert that a variable is of this specific type.
513
- * The assertion check is only performed in non-production environments.
514
- *
515
- * @type {<T>(o:any,schema:Schema<T>) => asserts o is T}
516
- */
517
- export const assert: <T>(o: any, schema: Schema<T>) => asserts o is T;
518
518
  /**
519
519
  * @todo move this to separate library
520
520
  * @template {any} [State=undefined]
@@ -574,12 +574,12 @@ export type $ObjectToType<S extends {
574
574
  }> = { [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; };
575
575
  export type _ObjectDefToSchema<S> = import("./ts.js").Prettify<{ [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; }, 1>;
576
576
  export type _LArgsToLambdaDef<Args extends Schema<any>[]> = (...args: UnwrapArray<TuplePop<Args>>) => Unwrap<TupleLast<Args>>;
577
+ export type ReadSchemaUnwrapped<IN extends unknown> = [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] ? ((Schemas extends Schema<infer S> ? S : never) | Primitives | (Constructors extends new (...args: any[]) => any ? InstanceType<Constructors> : never) | (Arrs extends any[] ? { [K in keyof Arrs]: ReadSchemaUnwrapped<Arrs[K]>; }[keyof Arrs & number] : never) | (Obj extends object ? _ObjectDefToSchema<{ [K in keyof Obj]: Schema<ReadSchemaUnwrapped<Obj[K]>>; }> : never)) : never;
578
+ export type ReadSchema<IN extends unknown> = Schema<ReadSchemaUnwrapped<IN>>;
577
579
  export type JSONArray = JSON[];
578
580
  export type JSON = Primitive | JSONArray | {
579
581
  [key: string]: JSON;
580
582
  };
581
- export type ReadSchemaUnwrapped<IN extends unknown> = [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] ? ((Schemas extends Schema<infer S> ? S : never) | Primitives | (Constructors extends new (...args: any[]) => any ? InstanceType<Constructors> : never) | (Arrs extends any[] ? { [K in keyof Arrs]: ReadSchemaUnwrapped<Arrs[K]>; }[keyof Arrs & number] : never) | (Obj extends object ? _ObjectDefToSchema<{ [K in keyof Obj]: Schema<ReadSchemaUnwrapped<Obj[K]>>; }> : never)) : never;
582
- export type ReadSchema<IN extends unknown> = Schema<ReadSchemaUnwrapped<IN>>;
583
583
  export type Pattern<In, Out> = {
584
584
  if: Schema<In>;
585
585
  h: (o: In, state?: any) => Out;