@graffy/common 0.16.19 → 0.16.20-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.cjs +155 -309
  2. package/index.mjs +155 -309
  3. package/package.json +4 -4
package/index.mjs CHANGED
@@ -17,12 +17,10 @@ function err(message, { cause = null, ...args } = {}) {
17
17
  throw e;
18
18
  }
19
19
  function errIf(message, condition, args) {
20
- if (condition)
21
- err(message, args);
20
+ if (condition) err(message, args);
22
21
  }
23
22
  function isEmpty(object) {
24
- for (const _ in object)
25
- return false;
23
+ for (const _ in object) return false;
26
24
  return true;
27
25
  }
28
26
  function isDef(value) {
@@ -40,15 +38,11 @@ function clone$1(obj) {
40
38
  function cmp(a, b) {
41
39
  const l = a.length < b.length ? a.length : b.length;
42
40
  for (let i = 0; i < l; i++) {
43
- if (a[i] < b[i])
44
- return -1;
45
- if (a[i] > b[i])
46
- return 1;
47
- }
48
- if (a.length < b.length)
49
- return -1;
50
- if (a.length > b.length)
51
- return 1;
41
+ if (a[i] < b[i]) return -1;
42
+ if (a[i] > b[i]) return 1;
43
+ }
44
+ if (a.length < b.length) return -1;
45
+ if (a.length > b.length) return 1;
52
46
  return 0;
53
47
  }
54
48
  function find(items, compare2, first = 0, last = items.length) {
@@ -69,8 +63,7 @@ function find(items, compare2, first = 0, last = items.length) {
69
63
  }
70
64
  function stringify() {
71
65
  var _a;
72
- if ((this == null ? void 0 : this.length) === 0)
73
- return "·";
66
+ if ((this == null ? void 0 : this.length) === 0) return "·";
74
67
  let str = "";
75
68
  let bull = false;
76
69
  (_a = this == null ? void 0 : this.forEach) == null ? void 0 : _a.call(this, (value, i) => {
@@ -86,10 +79,8 @@ function stringify() {
86
79
  }
87
80
  const inspectSymbol = Symbol.for("nodejs.util.inspect.custom");
88
81
  function addStringify(buffer) {
89
- if (!isDebugMode)
90
- return buffer;
91
- if ("toJSON" in buffer || inspectSymbol in buffer)
92
- return buffer;
82
+ if (!isDebugMode) return buffer;
83
+ if ("toJSON" in buffer || inspectSymbol in buffer) return buffer;
93
84
  buffer.toJSON = stringify;
94
85
  buffer.toString = stringify;
95
86
  buffer[inspectSymbol] = stringify;
@@ -162,31 +153,22 @@ function encodeObject(object) {
162
153
  ];
163
154
  }
164
155
  function encodeParts(value) {
165
- if (value === null)
166
- return [NULL];
167
- if (value === false)
168
- return [FALSE];
169
- if (value === true)
170
- return [TRUE];
171
- if (typeof value === "number")
172
- return [NUM, encode$6(value)];
173
- if (typeof value === "string")
174
- return [STR, encode$5(value), END];
175
- if (Array.isArray(value))
176
- return encodeArray(value);
177
- if (typeof value === "object")
178
- return encodeObject(value);
156
+ if (value === null) return [NULL];
157
+ if (value === false) return [FALSE];
158
+ if (value === true) return [TRUE];
159
+ if (typeof value === "number") return [NUM, encode$6(value)];
160
+ if (typeof value === "string") return [STR, encode$5(value), END];
161
+ if (Array.isArray(value)) return encodeArray(value);
162
+ if (typeof value === "object") return encodeObject(value);
179
163
  return [NULL];
180
164
  }
181
165
  function encode$4(value) {
182
166
  const parts = encodeParts(value);
183
- while (parts[parts.length - 1] === END)
184
- parts.pop();
167
+ while (parts[parts.length - 1] === END) parts.pop();
185
168
  const lastPart = parts[parts.length - 1];
186
169
  if (typeof lastPart !== "number") {
187
170
  let end = lastPart.length - 1;
188
- while (end >= 0 && !lastPart[end])
189
- end--;
171
+ while (end >= 0 && !lastPart[end]) end--;
190
172
  if (lastPart[end] !== 255) {
191
173
  parts[parts.length - 1] = lastPart.slice(0, end + 1);
192
174
  } else {
@@ -217,18 +199,15 @@ function decode$4(buffer) {
217
199
  const stack = [[]];
218
200
  function readString() {
219
201
  const start = i;
220
- while (i < buffer.length && buffer[i] !== END)
221
- i++;
202
+ while (i < buffer.length && buffer[i] !== END) i++;
222
203
  const str = decode$5(buffer.subarray(start, i));
223
204
  i++;
224
205
  return str;
225
206
  }
226
207
  function pushToken(type, value) {
227
208
  const current = stack[stack.length - 1];
228
- if (type === ARR || type === OBJ)
229
- stack.push(value);
230
- if (!current)
231
- return;
209
+ if (type === ARR || type === OBJ) stack.push(value);
210
+ if (!current) return;
232
211
  if (Array.isArray(current)) {
233
212
  current.push(value);
234
213
  } else {
@@ -281,10 +260,8 @@ function decode$4(buffer) {
281
260
  return stack[0][0];
282
261
  }
283
262
  function keyStep(key) {
284
- if (isMinKey(key))
285
- return { key, step: 1 };
286
- if (isMaxKey(key))
287
- return { key, step: -1 };
263
+ if (isMinKey(key)) return { key, step: 1 };
264
+ if (isMaxKey(key)) return { key, step: -1 };
288
265
  const l = key.length - 1;
289
266
  let newKey;
290
267
  let step;
@@ -307,8 +284,7 @@ function keyStep(key) {
307
284
  return { key: newKey, step };
308
285
  }
309
286
  function keyBefore(key) {
310
- if (isMinKey(key) || isMaxKey(key))
311
- return key;
287
+ if (isMinKey(key) || isMaxKey(key)) return key;
312
288
  const l = key.length - 1;
313
289
  let newKey;
314
290
  if (key[l] === 0) {
@@ -323,8 +299,7 @@ function keyBefore(key) {
323
299
  return newKey;
324
300
  }
325
301
  function keyAfter(key) {
326
- if (isMaxKey(key))
327
- return key;
302
+ if (isMaxKey(key)) return key;
328
303
  const l = key.length - 1;
329
304
  let newKey;
330
305
  if (key[l] === 255) {
@@ -340,8 +315,7 @@ function keyAfter(key) {
340
315
  }
341
316
  function decodeBound(bound) {
342
317
  const { key, step } = keyStep(bound);
343
- if (isMinKey(key) || isMaxKey(key))
344
- return { step };
318
+ if (isMinKey(key) || isMaxKey(key)) return { step };
345
319
  const value = decode$4(key);
346
320
  return { key: value, step };
347
321
  }
@@ -358,20 +332,17 @@ const pageProps = {
358
332
  function splitArgs(arg) {
359
333
  const page = {};
360
334
  const filter = {};
361
- for (const p in arg)
362
- (p in pageProps ? page : filter)[p] = arg[p];
335
+ for (const p in arg) (p in pageProps ? page : filter)[p] = arg[p];
363
336
  return [
364
337
  isEmpty(page) ? void 0 : page,
365
338
  isEmpty(filter) ? void 0 : filter
366
339
  ];
367
340
  }
368
341
  function encode$3(arg) {
369
- if (!isPlainObject(arg))
370
- return { key: encode$4(arg) };
342
+ if (!isPlainObject(arg)) return { key: encode$4(arg) };
371
343
  const [page, filter] = splitArgs(arg);
372
344
  errIf("page_and_filter", page && filter, arg);
373
- if (!page)
374
- return { key: encode$4(filter || {}) };
345
+ if (!page) return { key: encode$4(filter || {}) };
375
346
  const { $cursor, ...range } = page;
376
347
  const { $first, $all, $last, $after, $before, $since, $until } = range;
377
348
  const hasRange = !isEmpty(range);
@@ -382,23 +353,15 @@ function encode$3(arg) {
382
353
  errIf("before_and_until", isDef($before) && isDef($until), arg);
383
354
  errIf("cursor_and_range_arg", isDef($cursor) && hasRange, arg);
384
355
  let [key, end] = hasRange ? [MIN_KEY, MAX_KEY] : [];
385
- if (isDef($cursor))
386
- key = encode$4($cursor);
387
- if (isDef($after))
388
- key = keyAfter(encode$4($after));
389
- if (isDef($before))
390
- end = keyBefore(encode$4($before));
391
- if (isDef($since))
392
- key = encode$4($since);
393
- if (isDef($until))
394
- end = encode$4($until);
395
- if (isDef($last))
396
- [key, end] = [end, key];
356
+ if (isDef($cursor)) key = encode$4($cursor);
357
+ if (isDef($after)) key = keyAfter(encode$4($after));
358
+ if (isDef($before)) end = keyBefore(encode$4($before));
359
+ if (isDef($since)) key = encode$4($since);
360
+ if (isDef($until)) end = encode$4($until);
361
+ if (isDef($last)) [key, end] = [end, key];
397
362
  const node = { key };
398
- if (isDef(end))
399
- node.end = end;
400
- if ($first || $last)
401
- node.limit = $first || $last;
363
+ if (isDef(end)) node.end = end;
364
+ if ($first || $last) node.limit = $first || $last;
402
365
  return node;
403
366
  }
404
367
  function decode$3(node) {
@@ -406,8 +369,7 @@ function decode$3(node) {
406
369
  errIf("no_key", !isDef(key));
407
370
  errIf("limit_without_end", isDef(limit) && !isDef(end));
408
371
  const kParts = decodeBound(key);
409
- if (!isDef(end) || cmp(key, end) === 0)
410
- return kParts.key;
372
+ if (!isDef(end) || cmp(key, end) === 0) return kParts.key;
411
373
  const eParts = decodeBound(end);
412
374
  const reverse = cmp(key, end) > 0;
413
375
  const [lower, upper] = reverse ? [eParts, kParts] : [kParts, eParts];
@@ -428,25 +390,21 @@ function decode$3(node) {
428
390
  const PATH_SEPARATOR = ".";
429
391
  function encode$2(path) {
430
392
  if (typeof path === "string") {
431
- if (!path.length || path === PATH_SEPARATOR)
432
- return [];
393
+ if (!path.length || path === PATH_SEPARATOR) return [];
433
394
  path = path.split(PATH_SEPARATOR);
434
395
  }
435
396
  if (!Array.isArray(path)) {
436
397
  throw Error(`encodePath.invalid:${JSON.stringify(path)}`);
437
398
  }
438
399
  function encodeSegment(seg) {
439
- if (ArrayBuffer.isView(seg))
440
- return seg;
400
+ if (ArrayBuffer.isView(seg)) return seg;
441
401
  const node = encode$3(seg);
442
- if (node.end)
443
- return node;
402
+ if (node.end) return node;
444
403
  return node.key;
445
404
  }
446
405
  if (isPlainObject(path[path.length - 1])) {
447
406
  const [page, filter] = splitArgs(path[path.length - 1]);
448
- if (page)
449
- path = path.slice(0, -1).concat([filter || MIN_KEY]);
407
+ if (page) path = path.slice(0, -1).concat([filter || MIN_KEY]);
450
408
  }
451
409
  return path.map(encodeSegment);
452
410
  }
@@ -457,11 +415,9 @@ function decode$2(path) {
457
415
  return path.map((key) => decode$3({ key }));
458
416
  }
459
417
  function splitRef($ref) {
460
- if (!Array.isArray($ref))
461
- return [];
418
+ if (!Array.isArray($ref)) return [];
462
419
  const tail = $ref[$ref.length - 1];
463
- if (!isPlainObject(tail))
464
- return [];
420
+ if (!isPlainObject(tail)) return [];
465
421
  return splitArgs(tail);
466
422
  }
467
423
  let customAlphabet = (alphabet, defaultSize = 21) => {
@@ -482,8 +438,7 @@ function findFirst(children, target, first, last) {
482
438
  ({ key, end }) => {
483
439
  const keyCmp = cmp(key, target);
484
440
  const endCmp = end && cmp(end, target);
485
- if (end && keyCmp < 0 && endCmp >= 0)
486
- return 0;
441
+ if (end && keyCmp < 0 && endCmp >= 0) return 0;
487
442
  return keyCmp;
488
443
  },
489
444
  first,
@@ -551,20 +506,17 @@ function compare(node) {
551
506
  };
552
507
  }
553
508
  function compareValue(a, b) {
554
- if (a === b)
555
- return 0;
509
+ if (a === b) return 0;
556
510
  return a < b ? -1 : 1;
557
511
  }
558
512
  function clone(node) {
559
513
  const copy = { ...node };
560
- if (node.children)
561
- copy.children = node.children.map((child) => clone(child));
514
+ if (node.children) copy.children = node.children.map((child) => clone(child));
562
515
  return copy;
563
516
  }
564
517
  function merge(current, changes) {
565
518
  let index = 0;
566
- if (typeof changes === "undefined")
567
- return current;
519
+ if (typeof changes === "undefined") return current;
568
520
  for (const change of changes) {
569
521
  index = isRange(change) ? insertRange$1(current, change, index) : insertNode$1(current, change, index);
570
522
  }
@@ -587,8 +539,7 @@ function insertRange$1(current, change, start = 0) {
587
539
  return keyIx + insertions.length - 1;
588
540
  }
589
541
  function mergeRanges$1(base, node) {
590
- if (node.version < base.version)
591
- [node, base] = [base, node];
542
+ if (node.version < base.version) [node, base] = [base, node];
592
543
  return [
593
544
  cmp(base.key, node.key) < 0 && { ...base, end: keyBefore(node.key) },
594
545
  node,
@@ -596,8 +547,7 @@ function mergeRanges$1(base, node) {
596
547
  ].filter(Boolean);
597
548
  }
598
549
  function insertNode$1(current, change, start = 0) {
599
- if (!current)
600
- throw new Error(`merge.insertNode: ${current}`);
550
+ if (!current) throw new Error(`merge.insertNode: ${current}`);
601
551
  const key = change.key;
602
552
  const index = findFirst(current, key, start);
603
553
  const node = current[index];
@@ -611,8 +561,7 @@ function insertNodeIntoRange$1(current, index, change) {
611
561
  const key = change.key;
612
562
  const range = current[index];
613
563
  const newChange = getNewer(change, range);
614
- if (!newChange)
615
- return;
564
+ if (!newChange) return;
616
565
  const insertions = [
617
566
  cmp(range.key, key) < 0 && { ...range, end: keyBefore(key) },
618
567
  newChange,
@@ -630,11 +579,9 @@ function updateNode$1(current, index, change) {
630
579
  current[index] = newNode || change;
631
580
  } else {
632
581
  const newChange = getNewer(change, node);
633
- if (newChange)
634
- current[index] = newChange;
582
+ if (newChange) current[index] = newChange;
635
583
  }
636
- if (change.prefix)
637
- current[index].prefix = true;
584
+ if (change.prefix) current[index].prefix = true;
638
585
  return index + 1;
639
586
  }
640
587
  function getNewer(node, base) {
@@ -648,8 +595,7 @@ function getNewer(node, base) {
648
595
  }
649
596
  const IS_VAL = Symbol("IS_VAL");
650
597
  function makeNode(seg, props2) {
651
- if (ArrayBuffer.isView(seg))
652
- return { key: seg, ...props2 };
598
+ if (ArrayBuffer.isView(seg)) return { key: seg, ...props2 };
653
599
  return { ...seg, ...props2 };
654
600
  }
655
601
  function wrapValue(value, path, version = 0) {
@@ -657,74 +603,56 @@ function wrapValue(value, path, version = 0) {
657
603
  return wrap([node], path.slice(0, -1), version);
658
604
  }
659
605
  function wrap(children, path, version = 0, prefix = false) {
660
- if (!Array.isArray(path))
661
- throw Error(`wrap.path_not_array ${path}`);
662
- if (!path.length)
663
- return children;
606
+ if (!Array.isArray(path)) throw Error(`wrap.path_not_array ${path}`);
607
+ if (!path.length) return children;
664
608
  let i = path.length - 1;
665
609
  if (!Array.isArray(children)) {
666
610
  children = [makeNode(path[i--], { value: children, version })];
667
611
  } else {
668
- if (!children.length)
669
- return;
612
+ if (!children.length) return;
670
613
  children = [makeNode(path[i--], { children, version })];
671
614
  }
672
- if (prefix)
673
- children[0].prefix = true;
674
- while (i >= 0)
675
- children = [makeNode(path[i--], { children, version })];
615
+ if (prefix) children[0].prefix = true;
616
+ while (i >= 0) children = [makeNode(path[i--], { children, version })];
676
617
  return children;
677
618
  }
678
619
  function unwrap(tree, path) {
679
- if (!Array.isArray(path))
680
- throw Error(`unwrap.path_not_array ${path}`);
620
+ if (!Array.isArray(path)) throw Error(`unwrap.path_not_array ${path}`);
681
621
  let children = tree;
682
622
  let node = { children };
683
623
  for (let i = 0; i < path.length; i++) {
684
624
  const key = path[i];
685
- if (!ArrayBuffer.isView(key))
686
- throw Error("unwrap.ranges_unsupported");
625
+ if (!ArrayBuffer.isView(key)) throw Error("unwrap.ranges_unsupported");
687
626
  children = node.children;
688
- if (!children)
689
- return null;
627
+ if (!children) return null;
690
628
  node = children[findFirst(children, key)];
691
- if (!node || cmp(node.key, key) > 0)
692
- return void 0;
693
- if (isRange(node))
694
- return null;
695
- if (node.path)
696
- return unwrap(tree, node.path.concat(path.slice(i + 1)));
629
+ if (!node || cmp(node.key, key) > 0) return void 0;
630
+ if (isRange(node)) return null;
631
+ if (node.path) return unwrap(tree, node.path.concat(path.slice(i + 1)));
697
632
  }
698
633
  return getNodeValue(node);
699
634
  }
700
635
  function getNodeValue(node) {
701
- if (node.children)
702
- return node.children;
636
+ if (node.children) return node.children;
703
637
  if (node.value && typeof node.value === "object") {
704
638
  node.value[IS_VAL] = true;
705
639
  }
706
640
  return node.value;
707
641
  }
708
642
  function remove(children, path) {
709
- if (!Array.isArray(path))
710
- throw Error(`del.path_not_array ${path}`);
711
- if (!children)
712
- return null;
713
- if (!path.length)
714
- return [];
643
+ if (!Array.isArray(path)) throw Error(`del.path_not_array ${path}`);
644
+ if (!children) return null;
645
+ if (!path.length) return [];
715
646
  const key = path[0];
716
647
  const ix = findFirst(children, key);
717
648
  const node = children[ix];
718
- if (!node || cmp(node.key, key) > 0 || isRange(node))
719
- return children;
649
+ if (!node || cmp(node.key, key) > 0 || isRange(node)) return children;
720
650
  if (path.length === 1) {
721
651
  return children.slice(0, ix).concat(children.slice(ix + 1));
722
652
  }
723
- if (!isBranch(node))
724
- return children;
653
+ if (!isBranch(node)) return children;
725
654
  const filteredChildren = remove(node.children, path.slice(1));
726
- if (filteredChildren === children)
727
- return children;
655
+ if (filteredChildren === children) return children;
728
656
  const filteredNode = filteredChildren.length ? { ...node, children: filteredChildren } : [];
729
657
  return children.slice(0, ix).concat(filteredNode, children.slice(ix + 1));
730
658
  }
@@ -741,8 +669,7 @@ class Result {
741
669
  this.unknown.push(node);
742
670
  }
743
671
  addLinked(children) {
744
- if (this.root !== this)
745
- return this.root.addLinked(children);
672
+ if (this.root !== this) return this.root.addLinked(children);
746
673
  this.linked = this.linked || [];
747
674
  add(this.linked, children);
748
675
  }
@@ -784,10 +711,8 @@ function sliceNode(graph, query, result) {
784
711
  }
785
712
  } else if (isBranch(graph) && isBranch(query)) {
786
713
  const { known, unknown } = slice(graph.children, query.children, root);
787
- if (known)
788
- result.addKnown({ ...graph, children: known });
789
- if (unknown)
790
- result.addUnknown({ ...query, children: unknown });
714
+ if (known) result.addKnown({ ...graph, children: known });
715
+ if (unknown) result.addUnknown({ ...query, children: unknown });
791
716
  } else if (isLink(graph)) {
792
717
  result.addKnown(graph);
793
718
  if (graph.prefix && isRange(query)) {
@@ -819,17 +744,14 @@ function sliceRange(graph, query, result) {
819
744
  const step = cmp(key, end) < 0 ? 1 : -1;
820
745
  if (isMinKey(graph[0].key) && graph[0].prefix && graph[0].children) {
821
746
  const { known, unknown } = slice(graph[0].children, [query], result.root);
822
- if (known)
823
- result.addKnown({ ...graph[0], children: known });
824
- if (unknown)
825
- result.addUnknown({ ...query[0], children: unknown });
747
+ if (known) result.addKnown({ ...graph[0], children: known });
748
+ if (unknown) result.addUnknown({ ...query[0], children: unknown });
826
749
  return;
827
750
  }
828
751
  if (cmp(key, end) < 0) {
829
752
  for (let i = findFirst(graph, key); cmp(key, end) <= 0 && limit > 0; i++) {
830
753
  const node = graph[i];
831
- if (!node || cmp(key, node.key) < 0 || isOlder(node, version))
832
- break;
754
+ if (!node || cmp(key, node.key) < 0 || isOlder(node, version)) break;
833
755
  if (isRange(node)) {
834
756
  result.addKnown(getOverlap(node, key, end));
835
757
  } else {
@@ -858,8 +780,7 @@ function sliceRange(graph, query, result) {
858
780
  }
859
781
  }
860
782
  function getOverlap(node, key, end) {
861
- if (cmp(node.key, key) >= 0 && cmp(node.end, end) <= 0)
862
- return node;
783
+ if (cmp(node.key, key) >= 0 && cmp(node.end, end) <= 0) return node;
863
784
  return {
864
785
  ...node,
865
786
  key: cmp(node.key, key) > 0 ? node.key : key,
@@ -914,8 +835,7 @@ function insertRange(current, change, result, start = 0) {
914
835
  version: change.version
915
836
  });
916
837
  }
917
- if (appliedChange.length)
918
- result.push(...appliedChange);
838
+ if (appliedChange.length) result.push(...appliedChange);
919
839
  const insertions = [change];
920
840
  for (let i = keyIx; i < endIx; i++) {
921
841
  const node = current[i];
@@ -929,8 +849,7 @@ function insertRange(current, change, result, start = 0) {
929
849
  return keyIx + insertions.length - 1;
930
850
  }
931
851
  function mergeRanges(base, node) {
932
- if (node.version < base.version)
933
- [node, base] = [base, node];
852
+ if (node.version < base.version) [node, base] = [base, node];
934
853
  return [
935
854
  cmp(base.key, node.key) < 0 && { ...base, end: keyBefore(node.key) },
936
855
  node,
@@ -951,8 +870,7 @@ function insertNodeIntoRange(current, index, change, result) {
951
870
  const range = current[index];
952
871
  const newChange = getNewerChange(change, range);
953
872
  const newNode = getNewerNode(change, range);
954
- if (!newChange)
955
- return;
873
+ if (!newChange) return;
956
874
  result.push(newChange);
957
875
  const insertions = [
958
876
  cmp(range.key, key) < 0 && { ...range, end: keyBefore(key) },
@@ -968,18 +886,15 @@ function updateNode(current, index, change, result) {
968
886
  const nextResult = [];
969
887
  node.version = change.version;
970
888
  sieve(node.children, change.children, nextResult);
971
- if (nextResult.length)
972
- result.push({ ...change, children: nextResult });
889
+ if (nextResult.length) result.push({ ...change, children: nextResult });
973
890
  } else if (isBranch(node)) {
974
891
  const newNode = getNewerNode(node, change);
975
892
  current[index] = newNode || change;
976
- if (!newNode)
977
- result.push(change);
893
+ if (!newNode) result.push(change);
978
894
  } else {
979
895
  const newChange = getNewerChange(change, node);
980
896
  const newNode = getNewerNode(change, node);
981
- if (newNode)
982
- current[index] = newNode;
897
+ if (newNode) current[index] = newNode;
983
898
  if (newChange && (change.value !== node.value || !isPathEqual(change.path, node.path))) {
984
899
  result.push(newChange);
985
900
  }
@@ -987,15 +902,11 @@ function updateNode(current, index, change, result) {
987
902
  return index + 1;
988
903
  }
989
904
  function isPathEqual(first, second) {
990
- if (!(first || second))
991
- return true;
992
- if (!(first && second))
993
- return false;
994
- if (first.length !== second.length)
995
- return false;
905
+ if (!(first || second)) return true;
906
+ if (!(first && second)) return false;
907
+ if (first.length !== second.length) return false;
996
908
  for (let i = 0; i < first.length; i++) {
997
- if (first[i] !== second[i])
998
- return false;
909
+ if (first[i] !== second[i]) return false;
999
910
  }
1000
911
  return true;
1001
912
  }
@@ -1019,10 +930,8 @@ function getNewerChange(node, base) {
1019
930
  }
1020
931
  function setVersion(graph, version, onlyIfZero = false) {
1021
932
  for (const node of graph) {
1022
- if (!(onlyIfZero && node.version))
1023
- node.version = version;
1024
- if (node.children)
1025
- setVersion(node.children, version, onlyIfZero);
933
+ if (!(onlyIfZero && node.version)) node.version = version;
934
+ if (node.children) setVersion(node.children, version, onlyIfZero);
1026
935
  }
1027
936
  return graph;
1028
937
  }
@@ -1031,8 +940,7 @@ function getKnown(graph, version = 0) {
1031
940
  for (const { key, end, children } of graph) {
1032
941
  const node = { key, version };
1033
942
  if (end) {
1034
- if (cmp(end, key) !== 0)
1035
- node.end = end;
943
+ if (cmp(end, key) !== 0) node.end = end;
1036
944
  node.value = 1;
1037
945
  }
1038
946
  if (children) {
@@ -1047,10 +955,8 @@ function getKnown(graph, version = 0) {
1047
955
  function finalize(graph, query, version = Date.now()) {
1048
956
  let result = [{ key: MIN_KEY, end: MAX_KEY, version: 0 }];
1049
957
  merge(result, graph);
1050
- if (query)
1051
- result = slice(result, query).known || [];
1052
- if (version !== false)
1053
- result = setVersion(result, version, true);
958
+ if (query) result = slice(result, query).known || [];
959
+ if (version !== false) result = setVersion(result, version, true);
1054
960
  return result;
1055
961
  }
1056
962
  const PRE_CHI_PUT = Symbol("PREFIX_CHILDREN_$PUT");
@@ -1061,10 +967,8 @@ function decode$1(nodes = [], { isGraph } = {}) {
1061
967
  let allNums = true;
1062
968
  function pushResult(...objects) {
1063
969
  for (const object of objects) {
1064
- if (isDef(object) && !Number.isInteger(object.$key))
1065
- allNums = false;
1066
- if (isDef(object) && typeof object.$key !== "string")
1067
- allStrs = false;
970
+ if (isDef(object) && !Number.isInteger(object.$key)) allNums = false;
971
+ if (isDef(object) && typeof object.$key !== "string") allStrs = false;
1068
972
  }
1069
973
  result.push(...objects);
1070
974
  }
@@ -1093,22 +997,17 @@ function decode$1(nodes = [], { isGraph } = {}) {
1093
997
  return false;
1094
998
  }
1095
999
  for (const node of nodes2) {
1096
- if (isGraph && addPutRange(node))
1097
- continue;
1000
+ if (isGraph && addPutRange(node)) continue;
1098
1001
  if (isPrefix(node)) {
1099
1002
  const decodedChildren = decodePrefixNode(node);
1100
1003
  if (PRE_CHI_PUT in decodedChildren) {
1101
1004
  prefixChildPuts.push(...decodedChildren[PRE_CHI_PUT]);
1102
1005
  }
1103
1006
  pushResult(...decodedChildren);
1104
- } else if (isGraph && isRange(node))
1105
- pushResult(decodeRangeNode(node));
1106
- else if (isBranch(node))
1107
- pushResult(decodeBranchNode(node));
1108
- else if (isLink(node))
1109
- pushResult(decodeLinkNode(node));
1110
- else
1111
- pushResult(decodeLeafNode(node));
1007
+ } else if (isGraph && isRange(node)) pushResult(decodeRangeNode(node));
1008
+ else if (isBranch(node)) pushResult(decodeBranchNode(node));
1009
+ else if (isLink(node)) pushResult(decodeLinkNode(node));
1010
+ else pushResult(decodeLeafNode(node));
1112
1011
  }
1113
1012
  if (allStrs || allNums && putRanges.length === 1 && cmp(putRanges[0].key, 0) === 0 && cmp(putRanges[0].end, Number.POSITIVE_INFINITY) === 0) {
1114
1013
  result = result.reduce(
@@ -1148,8 +1047,7 @@ function decode$1(nodes = [], { isGraph } = {}) {
1148
1047
  }
1149
1048
  function decodePrefixNode(node) {
1150
1049
  let args = decode$3(node);
1151
- if (!args)
1152
- args = {};
1050
+ if (!args) args = {};
1153
1051
  if (typeof args === "string") {
1154
1052
  throw Error(`decode.unencoded_prefix: ${args}`);
1155
1053
  }
@@ -1222,12 +1120,9 @@ const REF = Symbol();
1222
1120
  const PRE = Symbol();
1223
1121
  function decorate(rootGraph, rootQuery) {
1224
1122
  function construct(plumGraph, query) {
1225
- if (plumGraph === null)
1226
- return null;
1227
- if (!isDef(plumGraph))
1228
- plumGraph = [];
1229
- if (query.$key)
1230
- query = [query];
1123
+ if (plumGraph === null) return null;
1124
+ if (!isDef(plumGraph)) plumGraph = [];
1125
+ if (query.$key) query = [query];
1231
1126
  let graph;
1232
1127
  if (query.$ref) {
1233
1128
  const { $ref, ...props2 } = query;
@@ -1235,8 +1130,7 @@ function decorate(rootGraph, rootQuery) {
1235
1130
  const path = encode$2($ref);
1236
1131
  const targetPlumGraph = unwrap(rootGraph, path);
1237
1132
  if (targetPlumGraph) {
1238
- if (range)
1239
- targetPlumGraph[PRE] = filter;
1133
+ if (range) targetPlumGraph[PRE] = filter;
1240
1134
  graph = construct(
1241
1135
  targetPlumGraph,
1242
1136
  range ? { $key: range, ...props2 } : props2
@@ -1270,8 +1164,7 @@ function decorate(rootGraph, rootQuery) {
1270
1164
  return subResult;
1271
1165
  });
1272
1166
  });
1273
- if (pageKey)
1274
- addPageMeta(graph, pageKey);
1167
+ if (pageKey) addPageMeta(graph, pageKey);
1275
1168
  } else if (typeof query === "object") {
1276
1169
  graph = {};
1277
1170
  for (const prop in query) {
@@ -1300,27 +1193,21 @@ function decorate(rootGraph, rootQuery) {
1300
1193
  }
1301
1194
  function descend(children, $key) {
1302
1195
  const key = ArrayBuffer.isView($key) ? $key : encode$3($key).key;
1303
- if (!Array.isArray(children))
1304
- return null;
1196
+ if (!Array.isArray(children)) return null;
1305
1197
  const ix = findFirst(children, key);
1306
1198
  const node = children[ix];
1307
- if (!node)
1308
- return;
1309
- if (isRange(node) && node.end >= key)
1310
- return null;
1311
- if (cmp(node.key, key) !== 0)
1312
- return;
1199
+ if (!node) return;
1200
+ if (isRange(node) && node.end >= key) return null;
1201
+ if (cmp(node.key, key) !== 0) return;
1313
1202
  const result2 = getValue(node);
1314
- if (node.prefix)
1315
- result2[PRE] = $key;
1203
+ if (node.prefix) result2[PRE] = $key;
1316
1204
  return result2;
1317
1205
  }
1318
1206
  function getValue(node) {
1319
1207
  let result2;
1320
1208
  if (node.path) {
1321
1209
  result2 = unwrap(rootGraph, node.path);
1322
- if (typeof result2 === "object" && result2)
1323
- result2[REF] = node.path;
1210
+ if (typeof result2 === "object" && result2) result2[REF] = node.path;
1324
1211
  } else {
1325
1212
  result2 = getNodeValue(node);
1326
1213
  }
@@ -1339,33 +1226,26 @@ function decorate(rootGraph, rootQuery) {
1339
1226
  let result2;
1340
1227
  if (cmp(key, end) < 0) {
1341
1228
  for (let n = 0; i < children.length && n < limit; i++) {
1342
- if (!isRange(children[i]))
1343
- n++;
1229
+ if (!isRange(children[i])) n++;
1344
1230
  }
1345
1231
  result2 = children.slice(ix, i);
1346
1232
  } else {
1347
1233
  for (let n = 0; i >= 0 && n < limit; i--) {
1348
- if (!isRange(children[i]))
1349
- n++;
1234
+ if (!isRange(children[i])) n++;
1350
1235
  }
1351
1236
  result2 = children.slice(i + 1, ix + 1);
1352
1237
  }
1353
- if (children[REF])
1354
- result2[REF] = children[REF];
1355
- if (children[PRE])
1356
- result2[PRE] = children[PRE];
1238
+ if (children[REF]) result2[REF] = children[REF];
1239
+ if (children[PRE]) result2[PRE] = children[PRE];
1357
1240
  return result2;
1358
1241
  }
1359
1242
  const result = construct(rootGraph, rootQuery);
1360
1243
  return result;
1361
1244
  }
1362
1245
  function deValNull(graph) {
1363
- if (typeof graph !== "object" || !graph)
1364
- return graph;
1365
- if ("$val" in graph && graph.$val !== true)
1366
- return graph.$val;
1367
- for (const prop in graph)
1368
- graph[prop] = deValNull(graph[prop]);
1246
+ if (typeof graph !== "object" || !graph) return graph;
1247
+ if ("$val" in graph && graph.$val !== true) return graph.$val;
1248
+ for (const prop in graph) graph[prop] = deValNull(graph[prop]);
1369
1249
  return graph;
1370
1250
  }
1371
1251
  function addPageMeta(graph, args) {
@@ -1418,8 +1298,7 @@ function decode(string, start = 0) {
1418
1298
  for (let i = start; i < string.length; i += 4) {
1419
1299
  let value = (getChar(string, i) << 18) + (getChar(string, i + 1) << 12) + (getChar(string, i + 2) << 6) + getChar(string, i + 3);
1420
1300
  for (let j = i * 3 / 4 + 2; j >= i * 3 / 4; j--) {
1421
- if (j < view.byteLength)
1422
- view.setUint8(j, value & 255);
1301
+ if (j < view.byteLength) view.setUint8(j, value & 255);
1423
1302
  value = value >> 8 | 0;
1424
1303
  }
1425
1304
  }
@@ -1444,27 +1323,20 @@ function serializeKey(key) {
1444
1323
  return `\0${encode$1(key)}`;
1445
1324
  }
1446
1325
  function deserializeKey(key) {
1447
- if (key[0] === "\0")
1448
- return decode(key.slice(1));
1326
+ if (key[0] === "\0") return decode(key.slice(1));
1449
1327
  return encode$4(key);
1450
1328
  }
1451
1329
  function pack(children, parentVersion) {
1452
- if (!Array.isArray(children))
1453
- return children;
1330
+ if (!Array.isArray(children)) return children;
1454
1331
  const array = children.map(
1455
1332
  (node) => props.reduce(
1456
1333
  (array2, prop, i) => {
1457
- if (!(prop in node))
1458
- return array2;
1334
+ if (!(prop in node)) return array2;
1459
1335
  let value = node[prop];
1460
- if (prop === "version" && value === parentVersion)
1461
- return array2;
1462
- if (prop === "children")
1463
- value = pack(value, node.version);
1464
- if (prop === "end")
1465
- value = serializeKey(value);
1466
- if (prop === "path")
1467
- value = value.map(serializeKey);
1336
+ if (prop === "version" && value === parentVersion) return array2;
1337
+ if (prop === "children") value = pack(value, node.version);
1338
+ if (prop === "end") value = serializeKey(value);
1339
+ if (prop === "path") value = value.map(serializeKey);
1468
1340
  array2[1] |= 1 << i;
1469
1341
  array2.push(value);
1470
1342
  return array2;
@@ -1475,20 +1347,15 @@ function pack(children, parentVersion) {
1475
1347
  return array;
1476
1348
  }
1477
1349
  function unpack(children, parentVersion) {
1478
- if (!Array.isArray(children))
1479
- return children;
1350
+ if (!Array.isArray(children)) return children;
1480
1351
  const node = children.map(
1481
1352
  ([key, type, ...values]) => props.reduce(
1482
1353
  (node2, prop, i) => {
1483
- if (!(type & 1 << i))
1484
- return node2;
1354
+ if (!(type & 1 << i)) return node2;
1485
1355
  let value = values.shift();
1486
- if (prop === "children")
1487
- value = unpack(value, node2.version);
1488
- if (prop === "end")
1489
- value = deserializeKey(value);
1490
- if (prop === "path")
1491
- value = value.map(deserializeKey);
1356
+ if (prop === "children") value = unpack(value, node2.version);
1357
+ if (prop === "end") value = deserializeKey(value);
1358
+ if (prop === "path") value = value.map(deserializeKey);
1492
1359
  node2[prop] = value;
1493
1360
  return node2;
1494
1361
  },
@@ -1512,8 +1379,7 @@ function encode(value, { version, isGraph } = {}) {
1512
1379
  const combine = isGraph ? merge : add;
1513
1380
  function makeNode2(object, key, ver, parentPuts = []) {
1514
1381
  var _a2;
1515
- if (!isDef(object))
1516
- return;
1382
+ if (!isDef(object)) return;
1517
1383
  const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
1518
1384
  if (typeof object === "object" && object && !Array.isArray(object)) {
1519
1385
  object = {
@@ -1530,10 +1396,8 @@ function encode(value, { version, isGraph } = {}) {
1530
1396
  ...props2
1531
1397
  };
1532
1398
  }
1533
- if (typeof object === "object" && object && isEmpty(object))
1534
- return;
1535
- if (isDef($ver))
1536
- ver = $ver;
1399
+ if (typeof object === "object" && object && isEmpty(object)) return;
1400
+ if (isDef($ver)) ver = $ver;
1537
1401
  if (isPlainObject($key)) {
1538
1402
  const [page, filter] = splitArgs($key);
1539
1403
  if (page) {
@@ -1542,19 +1406,16 @@ function encode(value, { version, isGraph } = {}) {
1542
1406
  object.$key = filter || {};
1543
1407
  object.$put = foundPuts;
1544
1408
  const node2 = makeNode2(object, key, ver);
1545
- if (!filter)
1546
- node2.key = MIN_KEY;
1409
+ if (!filter) node2.key = MIN_KEY;
1547
1410
  node2.prefix = true;
1548
1411
  return node2;
1549
1412
  }
1550
1413
  if ((!isDef(key) || Number.isInteger(key)) && (filter || isDef(page.$cursor))) {
1551
1414
  object.$key = isDef(page.$cursor) ? page.$cursor : page;
1552
1415
  const wrapper = { $key: filter || {}, $chi: [object] };
1553
- if (isGraph)
1554
- wrapper.$put = foundPuts;
1416
+ if (isGraph) wrapper.$put = foundPuts;
1555
1417
  const node2 = makeNode2(wrapper, key, ver);
1556
- if (!filter)
1557
- node2.key = MIN_KEY;
1418
+ if (!filter) node2.key = MIN_KEY;
1558
1419
  node2.prefix = true;
1559
1420
  return node2;
1560
1421
  }
@@ -1580,8 +1441,7 @@ function encode(value, { version, isGraph } = {}) {
1580
1441
  } else if (isDef($put)) {
1581
1442
  classifyPut($put);
1582
1443
  }
1583
- if (isDef($key) && (Number.isInteger(key) || !isDef(key)))
1584
- key = $key;
1444
+ if (isDef($key) && (Number.isInteger(key) || !isDef(key))) key = $key;
1585
1445
  const node = key === ROOT_KEY || !isDef(key) ? {} : encode$3(key);
1586
1446
  node.version = ver;
1587
1447
  if (object === null) {
@@ -1593,8 +1453,7 @@ function encode(value, { version, isGraph } = {}) {
1593
1453
  return node;
1594
1454
  } else if ($ref) {
1595
1455
  pushLink($ref, node.version, props2, $val, $chi);
1596
- if (!isGraph)
1597
- return;
1456
+ if (!isGraph) return;
1598
1457
  node.path = encode$2($ref);
1599
1458
  } else if ($val === true) {
1600
1459
  node.value = Array.isArray(object) ? clone$1(object) : props2;
@@ -1619,16 +1478,12 @@ function encode(value, { version, isGraph } = {}) {
1619
1478
  const children = Object.keys(props2).sort().map((key2) => makeNode2(object[key2], key2, ver)).filter(Boolean);
1620
1479
  if (children.length) {
1621
1480
  node.children = children;
1622
- } else if (putRange.length)
1623
- ;
1481
+ } else if (putRange.length) ;
1624
1482
  else if (isGraph) {
1625
- if (!(isDef($key) || isDef($put)))
1626
- return;
1627
- if (node.key && !node.end)
1628
- node.end = node.key;
1483
+ if (!(isDef($key) || isDef($put))) return;
1484
+ if (node.key && !node.end) node.end = node.key;
1629
1485
  } else {
1630
- if (!isDef($key))
1631
- return;
1486
+ if (!isDef($key)) return;
1632
1487
  node.value = 1;
1633
1488
  }
1634
1489
  }
@@ -1647,8 +1502,7 @@ function encode(value, { version, isGraph } = {}) {
1647
1502
  return node;
1648
1503
  }
1649
1504
  }
1650
- if (value == null ? void 0 : value.$key)
1651
- value = [value];
1505
+ if (value == null ? void 0 : value.$key) value = [value];
1652
1506
  const result = ((_a = makeNode2(value, ROOT_KEY, version)) == null ? void 0 : _a.children) || [];
1653
1507
  while (links.length) {
1654
1508
  combine(result, [links.pop()]);
@@ -1668,13 +1522,11 @@ async function* mergeStreams(...streams) {
1668
1522
  if (firstValues.some((value) => typeof value === "undefined")) {
1669
1523
  yield void 0;
1670
1524
  for (const value of firstValues) {
1671
- if (typeof value !== "undefined")
1672
- yield value;
1525
+ if (typeof value !== "undefined") yield value;
1673
1526
  }
1674
1527
  } else {
1675
1528
  const merged = [];
1676
- for (const value of firstValues)
1677
- merge(merged, value);
1529
+ for (const value of firstValues) merge(merged, value);
1678
1530
  yield merged;
1679
1531
  }
1680
1532
  yield* mergeIterators(streams);
@@ -1682,14 +1534,12 @@ async function* mergeStreams(...streams) {
1682
1534
  function makeWatcher() {
1683
1535
  const listeners = /* @__PURE__ */ new Set();
1684
1536
  function write(change) {
1685
- for (const push of listeners)
1686
- push(change);
1537
+ for (const push of listeners) push(change);
1687
1538
  }
1688
1539
  function watch(...args) {
1689
1540
  return makeStream((push, _end) => {
1690
1541
  listeners.add(push);
1691
- if (args.length)
1692
- Promise.resolve(args[0]).then(push);
1542
+ if (args.length) Promise.resolve(args[0]).then(push);
1693
1543
  return () => listeners.delete(push);
1694
1544
  });
1695
1545
  }
@@ -1720,15 +1570,13 @@ function cloneObject(object) {
1720
1570
  const clone2 = {};
1721
1571
  for (const prop in object) {
1722
1572
  const value = cloneObject(object[prop]);
1723
- if (value === null)
1724
- continue;
1573
+ if (value === null) continue;
1725
1574
  clone2[prop] = value;
1726
1575
  }
1727
1576
  return isEmpty(clone2) ? null : clone2;
1728
1577
  }
1729
1578
  function wrapObject(object, path) {
1730
- if (!Array.isArray(path))
1731
- throw Error(`wrapObject.path_not_array ${path}`);
1579
+ if (!Array.isArray(path)) throw Error(`wrapObject.path_not_array ${path}`);
1732
1580
  for (let i = path.length - 1; i >= 0; i--) {
1733
1581
  const $key = path[i];
1734
1582
  if (typeof $key === "string") {
@@ -1742,11 +1590,9 @@ function wrapObject(object, path) {
1742
1590
  return object;
1743
1591
  }
1744
1592
  function unwrapObject(object, path) {
1745
- if (!Array.isArray(path))
1746
- throw Error(`unwrapObject.path_not_array ${path}`);
1593
+ if (!Array.isArray(path)) throw Error(`unwrapObject.path_not_array ${path}`);
1747
1594
  for (let i = 0; i < path.length; i++) {
1748
- if (!object || typeof object !== "object")
1749
- return;
1595
+ if (!object || typeof object !== "object") return;
1750
1596
  const $key = path[i];
1751
1597
  if (typeof $key === "string") {
1752
1598
  if (Array.isArray(object)) {