@graffy/common 0.16.15-alpha.4 → 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,13 +1359,13 @@ 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 };
1289
1366
  graph.$val = true;
1290
1367
  } else if (Array.isArray(plumGraph)) {
1291
- graph = deValNull(decodeGraph(plumGraph));
1368
+ graph = decodeGraph(plumGraph);
1292
1369
  } else {
1293
1370
  throw Error("decorate.unexpected_graph");
1294
1371
  }
@@ -1361,15 +1438,6 @@ function decorate(rootGraph, rootQuery) {
1361
1438
  const result = construct(rootGraph, rootQuery);
1362
1439
  return result;
1363
1440
  }
1364
- function deValNull(graph) {
1365
- if (typeof graph !== "object" || !graph)
1366
- return graph;
1367
- if ("$val" in graph && graph.$val !== true)
1368
- return graph.$val;
1369
- for (const prop in graph)
1370
- graph[prop] = deValNull(graph[prop]);
1371
- return graph;
1372
- }
1373
1441
  function addPageMeta(graph, args) {
1374
1442
  if (args.$all) {
1375
1443
  Object.assign(graph, { $page: args, $prev: null, $next: null });
@@ -1697,81 +1765,10 @@ function makeWatcher() {
1697
1765
  }
1698
1766
  return { write, watch };
1699
1767
  }
1700
- function mergeObject(base, change) {
1701
- if (typeof change !== "object" || typeof base !== "object" || !base || !change) {
1702
- return change;
1703
- }
1704
- for (const prop in change) {
1705
- if (prop in base) {
1706
- const value = mergeObject(base[prop], change[prop]);
1707
- if (value === null) {
1708
- delete base[prop];
1709
- } else {
1710
- base[prop] = value;
1711
- }
1712
- } else {
1713
- base[prop] = change[prop];
1714
- }
1715
- }
1716
- return isEmpty(base) ? null : base;
1717
- }
1718
- function cloneObject(object) {
1719
- if (typeof object !== "object" || !object) {
1720
- return object;
1721
- }
1722
- const clone2 = {};
1723
- for (const prop in object) {
1724
- const value = cloneObject(object[prop]);
1725
- if (value === null)
1726
- continue;
1727
- clone2[prop] = value;
1728
- }
1729
- return isEmpty(clone2) ? null : clone2;
1730
- }
1731
- function wrapObject(object, path) {
1732
- if (!Array.isArray(path))
1733
- throw Error(`wrapObject.path_not_array ${path}`);
1734
- for (let i = path.length - 1; i >= 0; i--) {
1735
- const $key = path[i];
1736
- if (typeof $key === "string") {
1737
- object = { [$key]: object };
1738
- } else if (Array.isArray(object)) {
1739
- object = [{ $key, $chi: object }];
1740
- } else {
1741
- object = [{ $key, ...object }];
1742
- }
1743
- }
1744
- return object;
1745
- }
1746
- function unwrapObject(object, path) {
1747
- if (!Array.isArray(path))
1748
- throw Error(`unwrapObject.path_not_array ${path}`);
1749
- for (let i = 0; i < path.length; i++) {
1750
- if (!object || typeof object !== "object")
1751
- return;
1752
- const $key = path[i];
1753
- if (typeof $key === "string") {
1754
- if (Array.isArray(object)) {
1755
- throw Error(`unwrapObject.string_key_array:${$key}`);
1756
- }
1757
- object = object[$key];
1758
- } else {
1759
- if (!Array.isArray(object)) {
1760
- throw Error(`unwrapObject.arg_key_object:${JSON.stringify($key)}`);
1761
- }
1762
- const [page, filter] = splitArgs($key);
1763
- if (page && !page.$cursor) {
1764
- return object;
1765
- }
1766
- const target = (page == null ? void 0 : page.$cursor) ? { ...filter, $cursor: page.$cursor } : filter;
1767
- object = object.find(({ $key: $key2 }) => isEqual($key2, target));
1768
- }
1769
- }
1770
- return object;
1771
- }
1772
1768
  exports.IS_VAL = IS_VAL;
1773
1769
  exports.MAX_KEY = MAX_KEY;
1774
1770
  exports.MIN_KEY = MIN_KEY;
1771
+ exports.NULL_VAL = NULL_VAL;
1775
1772
  exports.add = add;
1776
1773
  exports.addStringify = addStringify;
1777
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,13 +1357,13 @@ 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 };
1287
1364
  graph.$val = true;
1288
1365
  } else if (Array.isArray(plumGraph)) {
1289
- graph = deValNull(decodeGraph(plumGraph));
1366
+ graph = decodeGraph(plumGraph);
1290
1367
  } else {
1291
1368
  throw Error("decorate.unexpected_graph");
1292
1369
  }
@@ -1359,15 +1436,6 @@ function decorate(rootGraph, rootQuery) {
1359
1436
  const result = construct(rootGraph, rootQuery);
1360
1437
  return result;
1361
1438
  }
1362
- 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]);
1369
- return graph;
1370
- }
1371
1439
  function addPageMeta(graph, args) {
1372
1440
  if (args.$all) {
1373
1441
  Object.assign(graph, { $page: args, $prev: null, $next: null });
@@ -1695,82 +1763,11 @@ function makeWatcher() {
1695
1763
  }
1696
1764
  return { write, watch };
1697
1765
  }
1698
- function mergeObject(base, change) {
1699
- if (typeof change !== "object" || typeof base !== "object" || !base || !change) {
1700
- return change;
1701
- }
1702
- for (const prop in change) {
1703
- if (prop in base) {
1704
- const value = mergeObject(base[prop], change[prop]);
1705
- if (value === null) {
1706
- delete base[prop];
1707
- } else {
1708
- base[prop] = value;
1709
- }
1710
- } else {
1711
- base[prop] = change[prop];
1712
- }
1713
- }
1714
- return isEmpty(base) ? null : base;
1715
- }
1716
- function cloneObject(object) {
1717
- if (typeof object !== "object" || !object) {
1718
- return object;
1719
- }
1720
- const clone2 = {};
1721
- for (const prop in object) {
1722
- const value = cloneObject(object[prop]);
1723
- if (value === null)
1724
- continue;
1725
- clone2[prop] = value;
1726
- }
1727
- return isEmpty(clone2) ? null : clone2;
1728
- }
1729
- function wrapObject(object, path) {
1730
- if (!Array.isArray(path))
1731
- throw Error(`wrapObject.path_not_array ${path}`);
1732
- for (let i = path.length - 1; i >= 0; i--) {
1733
- const $key = path[i];
1734
- if (typeof $key === "string") {
1735
- object = { [$key]: object };
1736
- } else if (Array.isArray(object)) {
1737
- object = [{ $key, $chi: object }];
1738
- } else {
1739
- object = [{ $key, ...object }];
1740
- }
1741
- }
1742
- return object;
1743
- }
1744
- function unwrapObject(object, path) {
1745
- if (!Array.isArray(path))
1746
- throw Error(`unwrapObject.path_not_array ${path}`);
1747
- for (let i = 0; i < path.length; i++) {
1748
- if (!object || typeof object !== "object")
1749
- return;
1750
- const $key = path[i];
1751
- if (typeof $key === "string") {
1752
- if (Array.isArray(object)) {
1753
- throw Error(`unwrapObject.string_key_array:${$key}`);
1754
- }
1755
- object = object[$key];
1756
- } else {
1757
- if (!Array.isArray(object)) {
1758
- throw Error(`unwrapObject.arg_key_object:${JSON.stringify($key)}`);
1759
- }
1760
- const [page, filter] = splitArgs($key);
1761
- if (page && !page.$cursor) {
1762
- return object;
1763
- }
1764
- const target = (page == null ? void 0 : page.$cursor) ? { ...filter, $cursor: page.$cursor } : filter;
1765
- object = object.find(({ $key: $key2 }) => isEqual($key2, target));
1766
- }
1767
- }
1768
- return object;
1769
- }
1770
1766
  export {
1771
1767
  IS_VAL,
1772
1768
  MAX_KEY,
1773
1769
  MIN_KEY,
1770
+ NULL_VAL,
1774
1771
  add,
1775
1772
  addStringify,
1776
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.4",
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.4"
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
+ }