@graffy/common 0.16.15-alpha.3 → 0.16.15-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.cjs CHANGED
@@ -514,6 +514,81 @@ function isOlder(node, version) {
514
514
  function isNewer(node, version) {
515
515
  return typeof node.version !== "undefined" && node.version > version;
516
516
  }
517
+ const NULL_VAL = {};
518
+ Object.defineProperty(NULL_VAL, "$val", { value: null });
519
+ Object.defineProperty(NULL_VAL, "toJSON", { value: () => null });
520
+ function mergeObject(base, change) {
521
+ if (typeof change !== "object" || typeof base !== "object" || !base || !change) {
522
+ return change;
523
+ }
524
+ for (const prop in change) {
525
+ if (prop in base) {
526
+ const value = mergeObject(base[prop], change[prop]);
527
+ if (value === null) {
528
+ delete base[prop];
529
+ } else {
530
+ base[prop] = value;
531
+ }
532
+ } else {
533
+ base[prop] = change[prop];
534
+ }
535
+ }
536
+ return isEmpty(base) ? null : base;
537
+ }
538
+ function cloneObject(object) {
539
+ if (typeof object !== "object" || !object) {
540
+ return object;
541
+ }
542
+ const clone2 = {};
543
+ for (const prop in object) {
544
+ const value = cloneObject(object[prop]);
545
+ if (value === null)
546
+ continue;
547
+ clone2[prop] = value;
548
+ }
549
+ return isEmpty(clone2) ? null : clone2;
550
+ }
551
+ function wrapObject(object, path) {
552
+ if (!Array.isArray(path))
553
+ throw Error(`wrapObject.path_not_array ${path}`);
554
+ for (let i = path.length - 1; i >= 0; i--) {
555
+ const $key = path[i];
556
+ if (typeof $key === "string") {
557
+ object = { [$key]: object };
558
+ } else if (Array.isArray(object)) {
559
+ object = [{ $key, $chi: object }];
560
+ } else {
561
+ object = [{ $key, ...object }];
562
+ }
563
+ }
564
+ return object;
565
+ }
566
+ function unwrapObject(object, path) {
567
+ if (!Array.isArray(path))
568
+ throw Error(`unwrapObject.path_not_array ${path}`);
569
+ for (let i = 0; i < path.length; i++) {
570
+ if (!object || typeof object !== "object")
571
+ return;
572
+ const $key = path[i];
573
+ if (typeof $key === "string") {
574
+ if (Array.isArray(object)) {
575
+ throw Error(`unwrapObject.string_key_array:${$key}`);
576
+ }
577
+ object = object[$key];
578
+ } else {
579
+ if (!Array.isArray(object)) {
580
+ throw Error(`unwrapObject.arg_key_object:${JSON.stringify($key)}`);
581
+ }
582
+ const [page, filter] = splitArgs($key);
583
+ if (page && !page.$cursor) {
584
+ return object;
585
+ }
586
+ const target = (page == null ? void 0 : page.$cursor) ? { ...filter, $cursor: page.$cursor } : filter;
587
+ object = object.find(({ $key: $key2 }) => isEqual($key2, target));
588
+ }
589
+ }
590
+ return object;
591
+ }
517
592
  function add(base, diff) {
518
593
  let changed = false;
519
594
  let index = 0;
@@ -702,7 +777,9 @@ function unwrap(tree, path) {
702
777
  function getNodeValue(node) {
703
778
  if (node.children)
704
779
  return node.children;
705
- if (node.value && typeof node.value === "object") {
780
+ if (node.value === null)
781
+ return NULL_VAL;
782
+ if (typeof node.value === "object") {
706
783
  node.value[IS_VAL] = true;
707
784
  }
708
785
  return node.value;
@@ -1124,7 +1201,7 @@ function decode$1(nodes = [], { isGraph } = {}) {
1124
1201
  delete item.$key;
1125
1202
  delete item.$val;
1126
1203
  if ($val === null) {
1127
- $val = { $val };
1204
+ $val = NULL_VAL;
1128
1205
  } else if (typeof $val === "object") {
1129
1206
  $val = clone$1($val);
1130
1207
  Object.defineProperty($val, "$val", { value: true });
@@ -1224,8 +1301,8 @@ const REF = Symbol();
1224
1301
  const PRE = Symbol();
1225
1302
  function decorate(rootGraph, rootQuery) {
1226
1303
  function construct(plumGraph, query) {
1227
- if (plumGraph === null)
1228
- return null;
1304
+ if (plumGraph === null || plumGraph === NULL_VAL)
1305
+ return plumGraph;
1229
1306
  if (!isDef(plumGraph))
1230
1307
  plumGraph = [];
1231
1308
  if (query.$key)
@@ -1282,7 +1359,7 @@ function decorate(rootGraph, rootQuery) {
1282
1359
  } else if (query) {
1283
1360
  if (Array.isArray(plumGraph) && !plumGraph.length) {
1284
1361
  graph = void 0;
1285
- } else if (typeof plumGraph !== "object" || !plumGraph) {
1362
+ } else if (typeof plumGraph !== "object") {
1286
1363
  graph = plumGraph;
1287
1364
  } else if (plumGraph[IS_VAL]) {
1288
1365
  graph = Array.isArray(plumGraph) ? plumGraph.slice(0) : { ...plumGraph };
@@ -1688,81 +1765,10 @@ function makeWatcher() {
1688
1765
  }
1689
1766
  return { write, watch };
1690
1767
  }
1691
- function mergeObject(base, change) {
1692
- if (typeof change !== "object" || typeof base !== "object" || !base || !change) {
1693
- return change;
1694
- }
1695
- for (const prop in change) {
1696
- if (prop in base) {
1697
- const value = mergeObject(base[prop], change[prop]);
1698
- if (value === null) {
1699
- delete base[prop];
1700
- } else {
1701
- base[prop] = value;
1702
- }
1703
- } else {
1704
- base[prop] = change[prop];
1705
- }
1706
- }
1707
- return isEmpty(base) ? null : base;
1708
- }
1709
- function cloneObject(object) {
1710
- if (typeof object !== "object" || !object) {
1711
- return object;
1712
- }
1713
- const clone2 = {};
1714
- for (const prop in object) {
1715
- const value = cloneObject(object[prop]);
1716
- if (value === null)
1717
- continue;
1718
- clone2[prop] = value;
1719
- }
1720
- return isEmpty(clone2) ? null : clone2;
1721
- }
1722
- function wrapObject(object, path) {
1723
- if (!Array.isArray(path))
1724
- throw Error(`wrapObject.path_not_array ${path}`);
1725
- for (let i = path.length - 1; i >= 0; i--) {
1726
- const $key = path[i];
1727
- if (typeof $key === "string") {
1728
- object = { [$key]: object };
1729
- } else if (Array.isArray(object)) {
1730
- object = [{ $key, $chi: object }];
1731
- } else {
1732
- object = [{ $key, ...object }];
1733
- }
1734
- }
1735
- return object;
1736
- }
1737
- function unwrapObject(object, path) {
1738
- if (!Array.isArray(path))
1739
- throw Error(`unwrapObject.path_not_array ${path}`);
1740
- for (let i = 0; i < path.length; i++) {
1741
- if (!object || typeof object !== "object")
1742
- return;
1743
- const $key = path[i];
1744
- if (typeof $key === "string") {
1745
- if (Array.isArray(object)) {
1746
- throw Error(`unwrapObject.string_key_array:${$key}`);
1747
- }
1748
- object = object[$key];
1749
- } else {
1750
- if (!Array.isArray(object)) {
1751
- throw Error(`unwrapObject.arg_key_object:${JSON.stringify($key)}`);
1752
- }
1753
- const [page, filter] = splitArgs($key);
1754
- if (page && !page.$cursor) {
1755
- return object;
1756
- }
1757
- const target = (page == null ? void 0 : page.$cursor) ? { ...filter, $cursor: page.$cursor } : filter;
1758
- object = object.find(({ $key: $key2 }) => isEqual($key2, target));
1759
- }
1760
- }
1761
- return object;
1762
- }
1763
1768
  exports.IS_VAL = IS_VAL;
1764
1769
  exports.MAX_KEY = MAX_KEY;
1765
1770
  exports.MIN_KEY = MIN_KEY;
1771
+ exports.NULL_VAL = NULL_VAL;
1766
1772
  exports.add = add;
1767
1773
  exports.addStringify = addStringify;
1768
1774
  exports.clone = clone$1;
package/index.mjs CHANGED
@@ -512,6 +512,81 @@ function isOlder(node, version) {
512
512
  function isNewer(node, version) {
513
513
  return typeof node.version !== "undefined" && node.version > version;
514
514
  }
515
+ const NULL_VAL = {};
516
+ Object.defineProperty(NULL_VAL, "$val", { value: null });
517
+ Object.defineProperty(NULL_VAL, "toJSON", { value: () => null });
518
+ function mergeObject(base, change) {
519
+ if (typeof change !== "object" || typeof base !== "object" || !base || !change) {
520
+ return change;
521
+ }
522
+ for (const prop in change) {
523
+ if (prop in base) {
524
+ const value = mergeObject(base[prop], change[prop]);
525
+ if (value === null) {
526
+ delete base[prop];
527
+ } else {
528
+ base[prop] = value;
529
+ }
530
+ } else {
531
+ base[prop] = change[prop];
532
+ }
533
+ }
534
+ return isEmpty(base) ? null : base;
535
+ }
536
+ function cloneObject(object) {
537
+ if (typeof object !== "object" || !object) {
538
+ return object;
539
+ }
540
+ const clone2 = {};
541
+ for (const prop in object) {
542
+ const value = cloneObject(object[prop]);
543
+ if (value === null)
544
+ continue;
545
+ clone2[prop] = value;
546
+ }
547
+ return isEmpty(clone2) ? null : clone2;
548
+ }
549
+ function wrapObject(object, path) {
550
+ if (!Array.isArray(path))
551
+ throw Error(`wrapObject.path_not_array ${path}`);
552
+ for (let i = path.length - 1; i >= 0; i--) {
553
+ const $key = path[i];
554
+ if (typeof $key === "string") {
555
+ object = { [$key]: object };
556
+ } else if (Array.isArray(object)) {
557
+ object = [{ $key, $chi: object }];
558
+ } else {
559
+ object = [{ $key, ...object }];
560
+ }
561
+ }
562
+ return object;
563
+ }
564
+ function unwrapObject(object, path) {
565
+ if (!Array.isArray(path))
566
+ throw Error(`unwrapObject.path_not_array ${path}`);
567
+ for (let i = 0; i < path.length; i++) {
568
+ if (!object || typeof object !== "object")
569
+ return;
570
+ const $key = path[i];
571
+ if (typeof $key === "string") {
572
+ if (Array.isArray(object)) {
573
+ throw Error(`unwrapObject.string_key_array:${$key}`);
574
+ }
575
+ object = object[$key];
576
+ } else {
577
+ if (!Array.isArray(object)) {
578
+ throw Error(`unwrapObject.arg_key_object:${JSON.stringify($key)}`);
579
+ }
580
+ const [page, filter] = splitArgs($key);
581
+ if (page && !page.$cursor) {
582
+ return object;
583
+ }
584
+ const target = (page == null ? void 0 : page.$cursor) ? { ...filter, $cursor: page.$cursor } : filter;
585
+ object = object.find(({ $key: $key2 }) => isEqual($key2, target));
586
+ }
587
+ }
588
+ return object;
589
+ }
515
590
  function add(base, diff) {
516
591
  let changed = false;
517
592
  let index = 0;
@@ -700,7 +775,9 @@ function unwrap(tree, path) {
700
775
  function getNodeValue(node) {
701
776
  if (node.children)
702
777
  return node.children;
703
- if (node.value && typeof node.value === "object") {
778
+ if (node.value === null)
779
+ return NULL_VAL;
780
+ if (typeof node.value === "object") {
704
781
  node.value[IS_VAL] = true;
705
782
  }
706
783
  return node.value;
@@ -1122,7 +1199,7 @@ function decode$1(nodes = [], { isGraph } = {}) {
1122
1199
  delete item.$key;
1123
1200
  delete item.$val;
1124
1201
  if ($val === null) {
1125
- $val = { $val };
1202
+ $val = NULL_VAL;
1126
1203
  } else if (typeof $val === "object") {
1127
1204
  $val = clone$1($val);
1128
1205
  Object.defineProperty($val, "$val", { value: true });
@@ -1222,8 +1299,8 @@ const REF = Symbol();
1222
1299
  const PRE = Symbol();
1223
1300
  function decorate(rootGraph, rootQuery) {
1224
1301
  function construct(plumGraph, query) {
1225
- if (plumGraph === null)
1226
- return null;
1302
+ if (plumGraph === null || plumGraph === NULL_VAL)
1303
+ return plumGraph;
1227
1304
  if (!isDef(plumGraph))
1228
1305
  plumGraph = [];
1229
1306
  if (query.$key)
@@ -1280,7 +1357,7 @@ function decorate(rootGraph, rootQuery) {
1280
1357
  } else if (query) {
1281
1358
  if (Array.isArray(plumGraph) && !plumGraph.length) {
1282
1359
  graph = void 0;
1283
- } else if (typeof plumGraph !== "object" || !plumGraph) {
1360
+ } else if (typeof plumGraph !== "object") {
1284
1361
  graph = plumGraph;
1285
1362
  } else if (plumGraph[IS_VAL]) {
1286
1363
  graph = Array.isArray(plumGraph) ? plumGraph.slice(0) : { ...plumGraph };
@@ -1686,82 +1763,11 @@ function makeWatcher() {
1686
1763
  }
1687
1764
  return { write, watch };
1688
1765
  }
1689
- function mergeObject(base, change) {
1690
- if (typeof change !== "object" || typeof base !== "object" || !base || !change) {
1691
- return change;
1692
- }
1693
- for (const prop in change) {
1694
- if (prop in base) {
1695
- const value = mergeObject(base[prop], change[prop]);
1696
- if (value === null) {
1697
- delete base[prop];
1698
- } else {
1699
- base[prop] = value;
1700
- }
1701
- } else {
1702
- base[prop] = change[prop];
1703
- }
1704
- }
1705
- return isEmpty(base) ? null : base;
1706
- }
1707
- function cloneObject(object) {
1708
- if (typeof object !== "object" || !object) {
1709
- return object;
1710
- }
1711
- const clone2 = {};
1712
- for (const prop in object) {
1713
- const value = cloneObject(object[prop]);
1714
- if (value === null)
1715
- continue;
1716
- clone2[prop] = value;
1717
- }
1718
- return isEmpty(clone2) ? null : clone2;
1719
- }
1720
- function wrapObject(object, path) {
1721
- if (!Array.isArray(path))
1722
- throw Error(`wrapObject.path_not_array ${path}`);
1723
- for (let i = path.length - 1; i >= 0; i--) {
1724
- const $key = path[i];
1725
- if (typeof $key === "string") {
1726
- object = { [$key]: object };
1727
- } else if (Array.isArray(object)) {
1728
- object = [{ $key, $chi: object }];
1729
- } else {
1730
- object = [{ $key, ...object }];
1731
- }
1732
- }
1733
- return object;
1734
- }
1735
- function unwrapObject(object, path) {
1736
- if (!Array.isArray(path))
1737
- throw Error(`unwrapObject.path_not_array ${path}`);
1738
- for (let i = 0; i < path.length; i++) {
1739
- if (!object || typeof object !== "object")
1740
- return;
1741
- const $key = path[i];
1742
- if (typeof $key === "string") {
1743
- if (Array.isArray(object)) {
1744
- throw Error(`unwrapObject.string_key_array:${$key}`);
1745
- }
1746
- object = object[$key];
1747
- } else {
1748
- if (!Array.isArray(object)) {
1749
- throw Error(`unwrapObject.arg_key_object:${JSON.stringify($key)}`);
1750
- }
1751
- const [page, filter] = splitArgs($key);
1752
- if (page && !page.$cursor) {
1753
- return object;
1754
- }
1755
- const target = (page == null ? void 0 : page.$cursor) ? { ...filter, $cursor: page.$cursor } : filter;
1756
- object = object.find(({ $key: $key2 }) => isEqual($key2, target));
1757
- }
1758
- }
1759
- return object;
1760
- }
1761
1766
  export {
1762
1767
  IS_VAL,
1763
1768
  MAX_KEY,
1764
1769
  MIN_KEY,
1770
+ NULL_VAL,
1765
1771
  add,
1766
1772
  addStringify,
1767
1773
  clone$1 as clone,
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graffy/common",
3
3
  "description": "Common libraries that used by various Graffy modules.",
4
4
  "author": "aravind (https://github.com/aravindet)",
5
- "version": "0.16.15-alpha.3",
5
+ "version": "0.16.15-alpha.5",
6
6
  "main": "./index.cjs",
7
7
  "exports": {
8
8
  "import": "./index.mjs",
@@ -19,6 +19,6 @@
19
19
  "lodash": "^4.17.19",
20
20
  "debug": "^4.3.3",
21
21
  "merge-async-iterators": "^0.2.1",
22
- "@graffy/stream": "0.16.15-alpha.3"
22
+ "@graffy/stream": "0.16.15-alpha.5"
23
23
  }
24
24
  }
package/types/object.d.ts CHANGED
@@ -2,3 +2,6 @@ export function mergeObject(base: any, change: any): any;
2
2
  export function cloneObject(object: any): any;
3
3
  export function wrapObject(object: any, path: any): any;
4
4
  export function unwrapObject(object: any, path: any): any;
5
+ export namespace NULL_VAL {
6
+ let $val: any;
7
+ }