@graffy/common 0.16.15-alpha.5 → 0.16.16

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,81 +514,6 @@ 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
- }
592
517
  function add(base, diff) {
593
518
  let changed = false;
594
519
  let index = 0;
@@ -777,9 +702,7 @@ function unwrap(tree, path) {
777
702
  function getNodeValue(node) {
778
703
  if (node.children)
779
704
  return node.children;
780
- if (node.value === null)
781
- return NULL_VAL;
782
- if (typeof node.value === "object") {
705
+ if (node.value && typeof node.value === "object") {
783
706
  node.value[IS_VAL] = true;
784
707
  }
785
708
  return node.value;
@@ -1201,7 +1124,7 @@ function decode$1(nodes = [], { isGraph } = {}) {
1201
1124
  delete item.$key;
1202
1125
  delete item.$val;
1203
1126
  if ($val === null) {
1204
- $val = NULL_VAL;
1127
+ $val = { $val };
1205
1128
  } else if (typeof $val === "object") {
1206
1129
  $val = clone$1($val);
1207
1130
  Object.defineProperty($val, "$val", { value: true });
@@ -1301,8 +1224,8 @@ const REF = Symbol();
1301
1224
  const PRE = Symbol();
1302
1225
  function decorate(rootGraph, rootQuery) {
1303
1226
  function construct(plumGraph, query) {
1304
- if (plumGraph === null || plumGraph === NULL_VAL)
1305
- return plumGraph;
1227
+ if (plumGraph === null)
1228
+ return null;
1306
1229
  if (!isDef(plumGraph))
1307
1230
  plumGraph = [];
1308
1231
  if (query.$key)
@@ -1359,13 +1282,13 @@ function decorate(rootGraph, rootQuery) {
1359
1282
  } else if (query) {
1360
1283
  if (Array.isArray(plumGraph) && !plumGraph.length) {
1361
1284
  graph = void 0;
1362
- } else if (typeof plumGraph !== "object") {
1285
+ } else if (typeof plumGraph !== "object" || !plumGraph) {
1363
1286
  graph = plumGraph;
1364
1287
  } else if (plumGraph[IS_VAL]) {
1365
1288
  graph = Array.isArray(plumGraph) ? plumGraph.slice(0) : { ...plumGraph };
1366
1289
  graph.$val = true;
1367
1290
  } else if (Array.isArray(plumGraph)) {
1368
- graph = decodeGraph(plumGraph);
1291
+ graph = deValNull(decodeGraph(plumGraph));
1369
1292
  } else {
1370
1293
  throw Error("decorate.unexpected_graph");
1371
1294
  }
@@ -1438,6 +1361,15 @@ function decorate(rootGraph, rootQuery) {
1438
1361
  const result = construct(rootGraph, rootQuery);
1439
1362
  return result;
1440
1363
  }
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
+ }
1441
1373
  function addPageMeta(graph, args) {
1442
1374
  if (args.$all) {
1443
1375
  Object.assign(graph, { $page: args, $prev: null, $next: null });
@@ -1765,10 +1697,81 @@ function makeWatcher() {
1765
1697
  }
1766
1698
  return { write, watch };
1767
1699
  }
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
+ }
1768
1772
  exports.IS_VAL = IS_VAL;
1769
1773
  exports.MAX_KEY = MAX_KEY;
1770
1774
  exports.MIN_KEY = MIN_KEY;
1771
- exports.NULL_VAL = NULL_VAL;
1772
1775
  exports.add = add;
1773
1776
  exports.addStringify = addStringify;
1774
1777
  exports.clone = clone$1;
package/index.mjs CHANGED
@@ -512,81 +512,6 @@ 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
- }
590
515
  function add(base, diff) {
591
516
  let changed = false;
592
517
  let index = 0;
@@ -775,9 +700,7 @@ function unwrap(tree, path) {
775
700
  function getNodeValue(node) {
776
701
  if (node.children)
777
702
  return node.children;
778
- if (node.value === null)
779
- return NULL_VAL;
780
- if (typeof node.value === "object") {
703
+ if (node.value && typeof node.value === "object") {
781
704
  node.value[IS_VAL] = true;
782
705
  }
783
706
  return node.value;
@@ -1199,7 +1122,7 @@ function decode$1(nodes = [], { isGraph } = {}) {
1199
1122
  delete item.$key;
1200
1123
  delete item.$val;
1201
1124
  if ($val === null) {
1202
- $val = NULL_VAL;
1125
+ $val = { $val };
1203
1126
  } else if (typeof $val === "object") {
1204
1127
  $val = clone$1($val);
1205
1128
  Object.defineProperty($val, "$val", { value: true });
@@ -1299,8 +1222,8 @@ const REF = Symbol();
1299
1222
  const PRE = Symbol();
1300
1223
  function decorate(rootGraph, rootQuery) {
1301
1224
  function construct(plumGraph, query) {
1302
- if (plumGraph === null || plumGraph === NULL_VAL)
1303
- return plumGraph;
1225
+ if (plumGraph === null)
1226
+ return null;
1304
1227
  if (!isDef(plumGraph))
1305
1228
  plumGraph = [];
1306
1229
  if (query.$key)
@@ -1357,13 +1280,13 @@ function decorate(rootGraph, rootQuery) {
1357
1280
  } else if (query) {
1358
1281
  if (Array.isArray(plumGraph) && !plumGraph.length) {
1359
1282
  graph = void 0;
1360
- } else if (typeof plumGraph !== "object") {
1283
+ } else if (typeof plumGraph !== "object" || !plumGraph) {
1361
1284
  graph = plumGraph;
1362
1285
  } else if (plumGraph[IS_VAL]) {
1363
1286
  graph = Array.isArray(plumGraph) ? plumGraph.slice(0) : { ...plumGraph };
1364
1287
  graph.$val = true;
1365
1288
  } else if (Array.isArray(plumGraph)) {
1366
- graph = decodeGraph(plumGraph);
1289
+ graph = deValNull(decodeGraph(plumGraph));
1367
1290
  } else {
1368
1291
  throw Error("decorate.unexpected_graph");
1369
1292
  }
@@ -1436,6 +1359,15 @@ function decorate(rootGraph, rootQuery) {
1436
1359
  const result = construct(rootGraph, rootQuery);
1437
1360
  return result;
1438
1361
  }
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
+ }
1439
1371
  function addPageMeta(graph, args) {
1440
1372
  if (args.$all) {
1441
1373
  Object.assign(graph, { $page: args, $prev: null, $next: null });
@@ -1763,11 +1695,82 @@ function makeWatcher() {
1763
1695
  }
1764
1696
  return { write, watch };
1765
1697
  }
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
+ }
1766
1770
  export {
1767
1771
  IS_VAL,
1768
1772
  MAX_KEY,
1769
1773
  MIN_KEY,
1770
- NULL_VAL,
1771
1774
  add,
1772
1775
  addStringify,
1773
1776
  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.5",
5
+ "version": "0.16.16",
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.5"
22
+ "@graffy/stream": "0.16.16"
23
23
  }
24
24
  }
package/types/object.d.ts CHANGED
@@ -2,6 +2,3 @@ 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
- }