@graffy/common 0.16.0-alpha.1 → 0.16.0-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.
package/index.cjs CHANGED
@@ -639,10 +639,10 @@ function getNewer(node, base) {
639
639
  }
640
640
  }
641
641
  const IS_VAL = Symbol("IS_VAL");
642
- function makeNode(seg, props) {
642
+ function makeNode(seg, props2) {
643
643
  if (ArrayBuffer.isView(seg))
644
- return { key: seg, ...props };
645
- return { ...seg, ...props };
644
+ return { key: seg, ...props2 };
645
+ return { ...seg, ...props2 };
646
646
  }
647
647
  function wrapValue(value, path, version = 0) {
648
648
  const node = makeNode(path[path.length - 1], { value, version });
@@ -1202,7 +1202,7 @@ function decorate(rootGraph, rootQuery) {
1202
1202
  query = [query];
1203
1203
  let graph;
1204
1204
  if (query.$ref) {
1205
- const { $ref, ...props } = query;
1205
+ const { $ref, ...props2 } = query;
1206
1206
  const [range, filter] = splitRef($ref);
1207
1207
  const path = encode$2($ref);
1208
1208
  const targetPlumGraph = unwrap(rootGraph, path);
@@ -1211,7 +1211,7 @@ function decorate(rootGraph, rootQuery) {
1211
1211
  targetPlumGraph[PRE] = filter;
1212
1212
  graph = construct(
1213
1213
  targetPlumGraph,
1214
- range ? { $key: range, ...props } : props
1214
+ range ? { $key: range, ...props2 } : props2
1215
1215
  );
1216
1216
  Object.defineProperty(graph, "$ref", { value: $ref });
1217
1217
  }
@@ -1221,8 +1221,8 @@ function decorate(rootGraph, rootQuery) {
1221
1221
  if (!(item == null ? void 0 : item.$key)) {
1222
1222
  return construct(descend(plumGraph, i), item);
1223
1223
  }
1224
- const { $key, $chi, ...props } = item;
1225
- const subQuery = $chi || (isEmpty(props) ? 1 : props);
1224
+ const { $key, $chi, ...props2 } = item;
1225
+ const subQuery = $chi || (isEmpty(props2) ? 1 : props2);
1226
1226
  if (!isPlainObject($key) || !splitArgs($key)[0]) {
1227
1227
  return construct(descend(plumGraph, $key), subQuery);
1228
1228
  }
@@ -1388,25 +1388,84 @@ function decode(string, start = 0) {
1388
1388
  }
1389
1389
  return addStringify(new Uint8Array(buffer));
1390
1390
  }
1391
- function serialize(obj) {
1392
- return JSON.stringify(obj, (_key, value) => {
1393
- return ArrayBuffer.isView(value) ? "\0" + encode$1(value) : value;
1394
- });
1391
+ const props = [
1392
+ "end",
1393
+ "version",
1394
+ "limit",
1395
+ "value",
1396
+ "path",
1397
+ "prefix",
1398
+ "children"
1399
+ ];
1400
+ function serializeKey(key) {
1401
+ if (key[0] === STR)
1402
+ return decode$4(key);
1403
+ return "\0" + encode$1(key);
1404
+ }
1405
+ function deserializeKey(key) {
1406
+ if (key[0] === "\0")
1407
+ return decode(key.slice(1));
1408
+ return encode$4(key);
1409
+ }
1410
+ function serializeNodes(children, parentVersion) {
1411
+ const array = children.map(
1412
+ (node) => props.reduce(
1413
+ (array2, prop, i) => {
1414
+ if (!(prop in node))
1415
+ return array2;
1416
+ let value = node[prop];
1417
+ if (prop === "version" && value === parentVersion)
1418
+ return array2;
1419
+ if (prop === "children")
1420
+ value = serializeNodes(value, node.version);
1421
+ if (prop === "end")
1422
+ value = serializeKey(value);
1423
+ if (prop === "path")
1424
+ value = value.map(serializeKey);
1425
+ array2[1] |= 1 << i;
1426
+ array2.push(value);
1427
+ return array2;
1428
+ },
1429
+ [serializeKey(node.key), 0]
1430
+ )
1431
+ );
1432
+ return array;
1433
+ }
1434
+ function deserializeNodes(children, parentVersion) {
1435
+ const node = children.map(
1436
+ ([key, type, ...values]) => props.reduce(
1437
+ (node2, prop, i) => {
1438
+ if (!(type & 1 << i))
1439
+ return node2;
1440
+ let value = values.shift();
1441
+ if (prop === "children")
1442
+ value = deserializeNodes(value, node2.version);
1443
+ if (prop === "end")
1444
+ value = deserializeKey(value);
1445
+ if (prop === "path")
1446
+ value = value.map(deserializeKey);
1447
+ node2[prop] = value;
1448
+ return node2;
1449
+ },
1450
+ { key: deserializeKey(key), version: parentVersion }
1451
+ )
1452
+ );
1453
+ return node;
1454
+ }
1455
+ function serialize(payload) {
1456
+ return JSON.stringify(serializeNodes(payload));
1395
1457
  }
1396
1458
  function deserialize(str) {
1397
- return JSON.parse(
1398
- str,
1399
- (_key, value) => typeof value === "string" && value[0] === "\0" ? decode(value.slice(1)) : value
1400
- );
1459
+ return deserializeNodes(JSON.parse(str));
1401
1460
  }
1402
1461
  const ROOT_KEY = Symbol();
1403
1462
  function encode(value, { version, isGraph } = {}) {
1404
1463
  var _a;
1405
1464
  const links = [];
1406
- function pushLink($ref, $ver, props, $val, $chi) {
1465
+ function pushLink($ref, $ver, props2, $val, $chi) {
1407
1466
  const [range, _] = splitRef($ref);
1408
- let children = !isEmpty(props) ? makeNode2(
1409
- range ? [{ $key: range, ...props }] : props,
1467
+ let children = !isEmpty(props2) ? makeNode2(
1468
+ range ? [{ $key: range, ...props2 }] : props2,
1410
1469
  void 0,
1411
1470
  $ver
1412
1471
  ).children : isDef($chi) ? makeNode2(
@@ -1425,12 +1484,12 @@ function encode(value, { version, isGraph } = {}) {
1425
1484
  return;
1426
1485
  if (typeof object === "object" && object && isEmpty(object))
1427
1486
  return;
1428
- const { $key, $ver, $ref, $val, $chi, $put, ...props } = object || {};
1487
+ const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
1429
1488
  if (isDef($ver))
1430
1489
  ver = $ver;
1431
1490
  if (isPlainObject($key)) {
1432
1491
  const [page, filter] = splitArgs($key);
1433
- if (isGraph && page && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props))) {
1492
+ if (isGraph && page && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props2))) {
1434
1493
  const node2 = makeNode2({ ...object, $key: filter || {} }, key, ver);
1435
1494
  if (!filter)
1436
1495
  node2.key = MIN_KEY;
@@ -1464,12 +1523,12 @@ function encode(value, { version, isGraph } = {}) {
1464
1523
  node.children = [makeNode2(object, void 0, ver)].filter(Boolean);
1465
1524
  return node;
1466
1525
  } else if ($ref) {
1467
- pushLink($ref, node.version, props, $val, $chi);
1526
+ pushLink($ref, node.version, props2, $val, $chi);
1468
1527
  if (!isGraph)
1469
1528
  return;
1470
1529
  node.path = encode$2($ref);
1471
1530
  } else if ($val === true) {
1472
- node.value = props;
1531
+ node.value = props2;
1473
1532
  } else if (isDef($val)) {
1474
1533
  node.value = $val;
1475
1534
  } else if (typeof object !== "object") {
@@ -1488,7 +1547,7 @@ function encode(value, { version, isGraph } = {}) {
1488
1547
  node.children = children;
1489
1548
  }
1490
1549
  } else {
1491
- const children = Object.keys(props).sort().map((key2) => makeNode2(object[key2], key2, ver)).filter(Boolean);
1550
+ const children = Object.keys(props2).sort().map((key2) => makeNode2(object[key2], key2, ver)).filter(Boolean);
1492
1551
  if (children.length) {
1493
1552
  node.children = children;
1494
1553
  } else if (isGraph) {
package/index.mjs CHANGED
@@ -634,10 +634,10 @@ function getNewer(node, base) {
634
634
  }
635
635
  }
636
636
  const IS_VAL = Symbol("IS_VAL");
637
- function makeNode(seg, props) {
637
+ function makeNode(seg, props2) {
638
638
  if (ArrayBuffer.isView(seg))
639
- return { key: seg, ...props };
640
- return { ...seg, ...props };
639
+ return { key: seg, ...props2 };
640
+ return { ...seg, ...props2 };
641
641
  }
642
642
  function wrapValue(value, path, version = 0) {
643
643
  const node = makeNode(path[path.length - 1], { value, version });
@@ -1197,7 +1197,7 @@ function decorate(rootGraph, rootQuery) {
1197
1197
  query = [query];
1198
1198
  let graph;
1199
1199
  if (query.$ref) {
1200
- const { $ref, ...props } = query;
1200
+ const { $ref, ...props2 } = query;
1201
1201
  const [range, filter] = splitRef($ref);
1202
1202
  const path = encode$2($ref);
1203
1203
  const targetPlumGraph = unwrap(rootGraph, path);
@@ -1206,7 +1206,7 @@ function decorate(rootGraph, rootQuery) {
1206
1206
  targetPlumGraph[PRE] = filter;
1207
1207
  graph = construct(
1208
1208
  targetPlumGraph,
1209
- range ? { $key: range, ...props } : props
1209
+ range ? { $key: range, ...props2 } : props2
1210
1210
  );
1211
1211
  Object.defineProperty(graph, "$ref", { value: $ref });
1212
1212
  }
@@ -1216,8 +1216,8 @@ function decorate(rootGraph, rootQuery) {
1216
1216
  if (!(item == null ? void 0 : item.$key)) {
1217
1217
  return construct(descend(plumGraph, i), item);
1218
1218
  }
1219
- const { $key, $chi, ...props } = item;
1220
- const subQuery = $chi || (isEmpty(props) ? 1 : props);
1219
+ const { $key, $chi, ...props2 } = item;
1220
+ const subQuery = $chi || (isEmpty(props2) ? 1 : props2);
1221
1221
  if (!isPlainObject($key) || !splitArgs($key)[0]) {
1222
1222
  return construct(descend(plumGraph, $key), subQuery);
1223
1223
  }
@@ -1383,25 +1383,84 @@ function decode(string, start = 0) {
1383
1383
  }
1384
1384
  return addStringify(new Uint8Array(buffer));
1385
1385
  }
1386
- function serialize(obj) {
1387
- return JSON.stringify(obj, (_key, value) => {
1388
- return ArrayBuffer.isView(value) ? "\0" + encode$1(value) : value;
1389
- });
1386
+ const props = [
1387
+ "end",
1388
+ "version",
1389
+ "limit",
1390
+ "value",
1391
+ "path",
1392
+ "prefix",
1393
+ "children"
1394
+ ];
1395
+ function serializeKey(key) {
1396
+ if (key[0] === STR)
1397
+ return decode$4(key);
1398
+ return "\0" + encode$1(key);
1399
+ }
1400
+ function deserializeKey(key) {
1401
+ if (key[0] === "\0")
1402
+ return decode(key.slice(1));
1403
+ return encode$4(key);
1404
+ }
1405
+ function serializeNodes(children, parentVersion) {
1406
+ const array = children.map(
1407
+ (node) => props.reduce(
1408
+ (array2, prop, i) => {
1409
+ if (!(prop in node))
1410
+ return array2;
1411
+ let value = node[prop];
1412
+ if (prop === "version" && value === parentVersion)
1413
+ return array2;
1414
+ if (prop === "children")
1415
+ value = serializeNodes(value, node.version);
1416
+ if (prop === "end")
1417
+ value = serializeKey(value);
1418
+ if (prop === "path")
1419
+ value = value.map(serializeKey);
1420
+ array2[1] |= 1 << i;
1421
+ array2.push(value);
1422
+ return array2;
1423
+ },
1424
+ [serializeKey(node.key), 0]
1425
+ )
1426
+ );
1427
+ return array;
1428
+ }
1429
+ function deserializeNodes(children, parentVersion) {
1430
+ const node = children.map(
1431
+ ([key, type, ...values]) => props.reduce(
1432
+ (node2, prop, i) => {
1433
+ if (!(type & 1 << i))
1434
+ return node2;
1435
+ let value = values.shift();
1436
+ if (prop === "children")
1437
+ value = deserializeNodes(value, node2.version);
1438
+ if (prop === "end")
1439
+ value = deserializeKey(value);
1440
+ if (prop === "path")
1441
+ value = value.map(deserializeKey);
1442
+ node2[prop] = value;
1443
+ return node2;
1444
+ },
1445
+ { key: deserializeKey(key), version: parentVersion }
1446
+ )
1447
+ );
1448
+ return node;
1449
+ }
1450
+ function serialize(payload) {
1451
+ return JSON.stringify(serializeNodes(payload));
1390
1452
  }
1391
1453
  function deserialize(str) {
1392
- return JSON.parse(
1393
- str,
1394
- (_key, value) => typeof value === "string" && value[0] === "\0" ? decode(value.slice(1)) : value
1395
- );
1454
+ return deserializeNodes(JSON.parse(str));
1396
1455
  }
1397
1456
  const ROOT_KEY = Symbol();
1398
1457
  function encode(value, { version, isGraph } = {}) {
1399
1458
  var _a;
1400
1459
  const links = [];
1401
- function pushLink($ref, $ver, props, $val, $chi) {
1460
+ function pushLink($ref, $ver, props2, $val, $chi) {
1402
1461
  const [range, _] = splitRef($ref);
1403
- let children = !isEmpty(props) ? makeNode2(
1404
- range ? [{ $key: range, ...props }] : props,
1462
+ let children = !isEmpty(props2) ? makeNode2(
1463
+ range ? [{ $key: range, ...props2 }] : props2,
1405
1464
  void 0,
1406
1465
  $ver
1407
1466
  ).children : isDef($chi) ? makeNode2(
@@ -1420,12 +1479,12 @@ function encode(value, { version, isGraph } = {}) {
1420
1479
  return;
1421
1480
  if (typeof object === "object" && object && isEmpty(object))
1422
1481
  return;
1423
- const { $key, $ver, $ref, $val, $chi, $put, ...props } = object || {};
1482
+ const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
1424
1483
  if (isDef($ver))
1425
1484
  ver = $ver;
1426
1485
  if (isPlainObject($key)) {
1427
1486
  const [page, filter] = splitArgs($key);
1428
- if (isGraph && page && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props))) {
1487
+ if (isGraph && page && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props2))) {
1429
1488
  const node2 = makeNode2({ ...object, $key: filter || {} }, key, ver);
1430
1489
  if (!filter)
1431
1490
  node2.key = MIN_KEY;
@@ -1459,12 +1518,12 @@ function encode(value, { version, isGraph } = {}) {
1459
1518
  node.children = [makeNode2(object, void 0, ver)].filter(Boolean);
1460
1519
  return node;
1461
1520
  } else if ($ref) {
1462
- pushLink($ref, node.version, props, $val, $chi);
1521
+ pushLink($ref, node.version, props2, $val, $chi);
1463
1522
  if (!isGraph)
1464
1523
  return;
1465
1524
  node.path = encode$2($ref);
1466
1525
  } else if ($val === true) {
1467
- node.value = props;
1526
+ node.value = props2;
1468
1527
  } else if (isDef($val)) {
1469
1528
  node.value = $val;
1470
1529
  } else if (typeof object !== "object") {
@@ -1483,7 +1542,7 @@ function encode(value, { version, isGraph } = {}) {
1483
1542
  node.children = children;
1484
1543
  }
1485
1544
  } else {
1486
- const children = Object.keys(props).sort().map((key2) => makeNode2(object[key2], key2, ver)).filter(Boolean);
1545
+ const children = Object.keys(props2).sort().map((key2) => makeNode2(object[key2], key2, ver)).filter(Boolean);
1487
1546
  if (children.length) {
1488
1547
  node.children = children;
1489
1548
  } else if (isGraph) {
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.0-alpha.1",
5
+ "version": "0.16.0-alpha.2",
6
6
  "main": "./index.cjs",
7
7
  "exports": {
8
8
  "import": "./index.mjs",
@@ -18,6 +18,6 @@
18
18
  "dependencies": {
19
19
  "lodash": "^4.17.19",
20
20
  "merge-async-iterators": "^0.2.1",
21
- "@graffy/stream": "0.16.0-alpha.1"
21
+ "@graffy/stream": "0.16.0-alpha.2"
22
22
  }
23
23
  }
@@ -1,2 +1,2 @@
1
- export function serialize(obj: any): string;
1
+ export function serialize(payload: any): string;
2
2
  export function deserialize(str: any): any;