@graffy/link 0.15.12-alpha.1 → 0.15.12-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 +67 -24
  2. package/index.mjs +68 -25
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -26,13 +26,24 @@ function linkGraph(rootGraph, defs) {
26
26
  function linkGraphDef(graph, path, def, vars = {}, version = 0) {
27
27
  const [key, ...rest] = path;
28
28
  if (rest.length === 0) {
29
- common.merge(graph, [{ key, path: common.encodePath(makeRef(def, vars)), version }]);
29
+ const ref = makeRef(def);
30
+ const [range] = common.splitRef(def);
31
+ const node = { key, path: common.encodePath(ref), version };
32
+ if (range)
33
+ node.prefix = true;
34
+ common.merge(graph, [node]);
30
35
  return;
31
36
  }
32
37
  if (key[0] !== "$") {
33
- const node = graph[common.findFirst(graph, key)];
34
- if (!node || node.key !== key || !node.children) {
35
- throw Error("linkGraph.no_path " + key);
38
+ let node = graph[common.findFirst(graph, key)];
39
+ if (!node || node.key !== key || node.end) {
40
+ node = { key, version, value: 1 };
41
+ common.merge(graph, [node]);
42
+ delete node.value;
43
+ node.children = [];
44
+ }
45
+ if (!node.children) {
46
+ throw Error("linkGraph.unexpected_leaf " + key);
36
47
  }
37
48
  linkGraphDef(node.children, rest, def, vars, node.version);
38
49
  } else {
@@ -45,16 +56,10 @@ function linkGraph(rootGraph, defs) {
45
56
  }
46
57
  }
47
58
  }
48
- function makeRef(def, vars) {
49
- function getValue(key) {
50
- return key[0] === "$" ? vars[key.slice(1)] : key;
51
- }
52
- function getPath(template) {
53
- return template.split(".").map(getValue);
54
- }
59
+ function makeRef(def) {
55
60
  function replacePlaceholders(key) {
56
61
  if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
57
- return common.unwrap(rootGraph, getPath(key.slice(2)));
62
+ return common.unwrap(rootGraph, key.slice(2).split("."));
58
63
  }
59
64
  if (Array.isArray(key)) {
60
65
  return key.map(replacePlaceholders);
@@ -65,42 +70,60 @@ function linkGraph(rootGraph, defs) {
65
70
  result[prop] = replacePlaceholders(key[prop]);
66
71
  return result;
67
72
  }
68
- return getValue(key);
73
+ return key;
69
74
  }
70
75
  const ref = def.map(replacePlaceholders);
71
76
  return ref;
72
77
  }
73
78
  }
74
79
  function prepQueryLinks(rootQuery, defs) {
75
- return defs.filter(({ path, def }) => prepQueryDef(rootQuery, path, def));
80
+ return defs.flatMap(({ path, def }) => prepQueryDef(rootQuery, path, def));
76
81
  function prepQueryDef(query, path, def, vars = {}, version = 0) {
77
82
  var _a;
78
83
  const [key, ...rest] = path;
79
84
  if (rest.length === 0) {
80
85
  const ix = common.findFirst(query, key);
81
86
  if (((_a = query[ix]) == null ? void 0 : _a.key) !== key)
82
- return false;
83
- query.splice(ix, 1);
87
+ return [];
88
+ const [{ children: subQuery }] = query.splice(ix, 1);
84
89
  common.add(rootQuery, getDefQuery(def, vars, version));
85
- return true;
90
+ const [range, filter] = common.splitRef(def);
91
+ if (range && subQuery.length) {
92
+ return subQuery.map((node) => {
93
+ return {
94
+ path: path.concat(node.key),
95
+ def: prepareDef(def.slice(0, -1).concat(__spreadValues(__spreadValues(__spreadValues({}, filter), common.decodeArgs(node)), range)), vars)
96
+ };
97
+ });
98
+ } else {
99
+ return [{ path, def: prepareDef(def, vars) }];
100
+ }
101
+ }
102
+ function prefixKey(defs2, key2) {
103
+ return defs2.map(({ path: path2, def: def2 }) => ({
104
+ path: [key2, ...path2],
105
+ def: def2
106
+ }));
86
107
  }
108
+ let used = [];
87
109
  if (key[0] !== "$") {
88
110
  const node = query[common.findFirst(query, key)];
89
111
  if (!node || node.key !== key || !node.children)
90
- return;
91
- return prepQueryDef(node.children, rest, def, vars, node.version);
112
+ return [];
113
+ used = prepQueryDef(node.children, rest, def, vars, node.version);
114
+ used = prefixKey(used, node.key);
92
115
  } else {
93
- let used = false;
94
116
  for (const node of query) {
95
117
  if (!common.isBranch(node))
96
118
  continue;
97
119
  let usedHere = prepQueryDef(node.children, rest, def, __spreadProps(__spreadValues({}, vars), {
98
120
  [key.slice(1)]: node.key
99
121
  }), node.version);
100
- used = used || usedHere;
122
+ usedHere = prefixKey(usedHere, node.key);
123
+ used = used.concat(usedHere);
101
124
  }
102
- return used;
103
125
  }
126
+ return used;
104
127
  }
105
128
  }
106
129
  function getDefQuery(def, vars, version) {
@@ -119,14 +142,34 @@ function getDefQuery(def, vars, version) {
119
142
  key.map(addDefQueries);
120
143
  }
121
144
  if (typeof key === "object" && key) {
122
- const result = {};
123
145
  for (const prop in key)
124
- result[prop] = addDefQueries(key[prop]);
146
+ addDefQueries(key[prop]);
125
147
  }
126
148
  }
127
149
  def.map(addDefQueries);
128
150
  return defQuery;
129
151
  }
152
+ function prepareDef(def, vars) {
153
+ function getValue(key) {
154
+ return key[0] === "$" ? vars[key.slice(1)] : key;
155
+ }
156
+ function replacePlaceholders(key) {
157
+ if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
158
+ return "$$" + key.slice(2).split(".").map(getValue).join(".");
159
+ }
160
+ if (Array.isArray(key)) {
161
+ return key.map(replacePlaceholders);
162
+ }
163
+ if (typeof key === "object" && key) {
164
+ const result = {};
165
+ for (const prop in key)
166
+ result[prop] = replacePlaceholders(key[prop]);
167
+ return result;
168
+ }
169
+ return getValue(key);
170
+ }
171
+ return def.map(replacePlaceholders);
172
+ }
130
173
  var index = (defs) => (store) => {
131
174
  const prefix = store.path;
132
175
  const defEntries = Object.entries(defs).map(([prop, def]) => ({
package/index.mjs CHANGED
@@ -17,7 +17,7 @@ var __spreadValues = (a, b) => {
17
17
  return a;
18
18
  };
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
- import { merge, encodePath, findFirst, isBranch, unwrap, add, wrapValue, wrap } from "@graffy/common";
20
+ import { splitRef, encodePath, merge, findFirst, isBranch, unwrap, add, decodeArgs, wrapValue, wrap } from "@graffy/common";
21
21
  function linkGraph(rootGraph, defs) {
22
22
  for (const { path, def } of defs)
23
23
  linkGraphDef(rootGraph, path, def);
@@ -25,13 +25,24 @@ function linkGraph(rootGraph, defs) {
25
25
  function linkGraphDef(graph, path, def, vars = {}, version = 0) {
26
26
  const [key, ...rest] = path;
27
27
  if (rest.length === 0) {
28
- merge(graph, [{ key, path: encodePath(makeRef(def, vars)), version }]);
28
+ const ref = makeRef(def);
29
+ const [range] = splitRef(def);
30
+ const node = { key, path: encodePath(ref), version };
31
+ if (range)
32
+ node.prefix = true;
33
+ merge(graph, [node]);
29
34
  return;
30
35
  }
31
36
  if (key[0] !== "$") {
32
- const node = graph[findFirst(graph, key)];
33
- if (!node || node.key !== key || !node.children) {
34
- throw Error("linkGraph.no_path " + key);
37
+ let node = graph[findFirst(graph, key)];
38
+ if (!node || node.key !== key || node.end) {
39
+ node = { key, version, value: 1 };
40
+ merge(graph, [node]);
41
+ delete node.value;
42
+ node.children = [];
43
+ }
44
+ if (!node.children) {
45
+ throw Error("linkGraph.unexpected_leaf " + key);
35
46
  }
36
47
  linkGraphDef(node.children, rest, def, vars, node.version);
37
48
  } else {
@@ -44,16 +55,10 @@ function linkGraph(rootGraph, defs) {
44
55
  }
45
56
  }
46
57
  }
47
- function makeRef(def, vars) {
48
- function getValue(key) {
49
- return key[0] === "$" ? vars[key.slice(1)] : key;
50
- }
51
- function getPath(template) {
52
- return template.split(".").map(getValue);
53
- }
58
+ function makeRef(def) {
54
59
  function replacePlaceholders(key) {
55
60
  if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
56
- return unwrap(rootGraph, getPath(key.slice(2)));
61
+ return unwrap(rootGraph, key.slice(2).split("."));
57
62
  }
58
63
  if (Array.isArray(key)) {
59
64
  return key.map(replacePlaceholders);
@@ -64,42 +69,60 @@ function linkGraph(rootGraph, defs) {
64
69
  result[prop] = replacePlaceholders(key[prop]);
65
70
  return result;
66
71
  }
67
- return getValue(key);
72
+ return key;
68
73
  }
69
74
  const ref = def.map(replacePlaceholders);
70
75
  return ref;
71
76
  }
72
77
  }
73
78
  function prepQueryLinks(rootQuery, defs) {
74
- return defs.filter(({ path, def }) => prepQueryDef(rootQuery, path, def));
79
+ return defs.flatMap(({ path, def }) => prepQueryDef(rootQuery, path, def));
75
80
  function prepQueryDef(query, path, def, vars = {}, version = 0) {
76
81
  var _a;
77
82
  const [key, ...rest] = path;
78
83
  if (rest.length === 0) {
79
84
  const ix = findFirst(query, key);
80
85
  if (((_a = query[ix]) == null ? void 0 : _a.key) !== key)
81
- return false;
82
- query.splice(ix, 1);
86
+ return [];
87
+ const [{ children: subQuery }] = query.splice(ix, 1);
83
88
  add(rootQuery, getDefQuery(def, vars, version));
84
- return true;
89
+ const [range, filter] = splitRef(def);
90
+ if (range && subQuery.length) {
91
+ return subQuery.map((node) => {
92
+ return {
93
+ path: path.concat(node.key),
94
+ def: prepareDef(def.slice(0, -1).concat(__spreadValues(__spreadValues(__spreadValues({}, filter), decodeArgs(node)), range)), vars)
95
+ };
96
+ });
97
+ } else {
98
+ return [{ path, def: prepareDef(def, vars) }];
99
+ }
100
+ }
101
+ function prefixKey(defs2, key2) {
102
+ return defs2.map(({ path: path2, def: def2 }) => ({
103
+ path: [key2, ...path2],
104
+ def: def2
105
+ }));
85
106
  }
107
+ let used = [];
86
108
  if (key[0] !== "$") {
87
109
  const node = query[findFirst(query, key)];
88
110
  if (!node || node.key !== key || !node.children)
89
- return;
90
- return prepQueryDef(node.children, rest, def, vars, node.version);
111
+ return [];
112
+ used = prepQueryDef(node.children, rest, def, vars, node.version);
113
+ used = prefixKey(used, node.key);
91
114
  } else {
92
- let used = false;
93
115
  for (const node of query) {
94
116
  if (!isBranch(node))
95
117
  continue;
96
118
  let usedHere = prepQueryDef(node.children, rest, def, __spreadProps(__spreadValues({}, vars), {
97
119
  [key.slice(1)]: node.key
98
120
  }), node.version);
99
- used = used || usedHere;
121
+ usedHere = prefixKey(usedHere, node.key);
122
+ used = used.concat(usedHere);
100
123
  }
101
- return used;
102
124
  }
125
+ return used;
103
126
  }
104
127
  }
105
128
  function getDefQuery(def, vars, version) {
@@ -118,14 +141,34 @@ function getDefQuery(def, vars, version) {
118
141
  key.map(addDefQueries);
119
142
  }
120
143
  if (typeof key === "object" && key) {
121
- const result = {};
122
144
  for (const prop in key)
123
- result[prop] = addDefQueries(key[prop]);
145
+ addDefQueries(key[prop]);
124
146
  }
125
147
  }
126
148
  def.map(addDefQueries);
127
149
  return defQuery;
128
150
  }
151
+ function prepareDef(def, vars) {
152
+ function getValue(key) {
153
+ return key[0] === "$" ? vars[key.slice(1)] : key;
154
+ }
155
+ function replacePlaceholders(key) {
156
+ if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
157
+ return "$$" + key.slice(2).split(".").map(getValue).join(".");
158
+ }
159
+ if (Array.isArray(key)) {
160
+ return key.map(replacePlaceholders);
161
+ }
162
+ if (typeof key === "object" && key) {
163
+ const result = {};
164
+ for (const prop in key)
165
+ result[prop] = replacePlaceholders(key[prop]);
166
+ return result;
167
+ }
168
+ return getValue(key);
169
+ }
170
+ return def.map(replacePlaceholders);
171
+ }
129
172
  var index = (defs) => (store) => {
130
173
  const prefix = store.path;
131
174
  const defEntries = Object.entries(defs).map(([prop, def]) => ({
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graffy/link",
3
3
  "description": "Graffy module for constructing links using an intuitive, declarative notation.",
4
4
  "author": "aravind (https://github.com/aravindet)",
5
- "version": "0.15.12-alpha.1",
5
+ "version": "0.15.12-alpha.2",
6
6
  "main": "./index.cjs",
7
7
  "exports": {
8
8
  "import": "./index.mjs",
@@ -16,6 +16,6 @@
16
16
  },
17
17
  "license": "Apache-2.0",
18
18
  "dependencies": {
19
- "@graffy/common": "0.15.12-alpha.1"
19
+ "@graffy/common": "0.15.12-alpha.2"
20
20
  }
21
21
  }