@graffy/link 0.15.14 → 0.15.15-alpha.1

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 +42 -15
  2. package/index.mjs +42 -15
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -23,10 +23,20 @@ function linkGraph(rootGraph, defs) {
23
23
  for (const { path, def } of defs)
24
24
  linkGraphDef(rootGraph, path, def);
25
25
  return rootGraph;
26
- function linkGraphDef(graph, path, def, version = 0) {
26
+ function findChildren(node) {
27
+ if (node.children)
28
+ return node.children;
29
+ if (node.path) {
30
+ const linkedNode = common.unwrap(rootGraph, node.path);
31
+ if (Array.isArray(linkedNode))
32
+ return linkedNode;
33
+ }
34
+ throw Error("link.no_children " + JSON.stringify(node));
35
+ }
36
+ function linkGraphDef(graph, path, def, vars = {}, version = 0) {
27
37
  const [key, ...rest] = path;
28
38
  if (rest.length === 0) {
29
- const ref = makeRef(def);
39
+ const ref = makeRef(def, vars);
30
40
  const [range] = common.splitRef(def);
31
41
  const node2 = { key, path: common.encodePath(ref), version };
32
42
  if (range)
@@ -34,6 +44,15 @@ function linkGraph(rootGraph, defs) {
34
44
  common.merge(graph, [node2]);
35
45
  return;
36
46
  }
47
+ if (key[0] === "$") {
48
+ for (const node2 of graph) {
49
+ if (node2.end)
50
+ continue;
51
+ const newVars = __spreadProps(__spreadValues({}, vars), { [key.slice(1)]: node2.key });
52
+ linkGraphDef(findChildren(node2), rest, def, newVars, node2.version);
53
+ }
54
+ return;
55
+ }
37
56
  let node = graph[common.findFirst(graph, key)];
38
57
  if (!node || node.key !== key || node.end) {
39
58
  node = { key, version, value: 1 };
@@ -41,15 +60,15 @@ function linkGraph(rootGraph, defs) {
41
60
  delete node.value;
42
61
  node.children = [];
43
62
  }
44
- if (!node.children) {
45
- throw Error("linkGraph.unexpected_leaf " + key);
46
- }
47
- linkGraphDef(node.children, rest, def, node.version);
63
+ return linkGraphDef(findChildren(node), rest, def, vars, node.version);
48
64
  }
49
- function makeRef(def) {
65
+ function makeRef(def, vars) {
66
+ function getValue(key) {
67
+ return key[0] === "$" ? vars[key.slice(1)] : key;
68
+ }
50
69
  function replacePlaceholders(key) {
51
70
  if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
52
- return common.unwrap(rootGraph, key.slice(2).split("."));
71
+ return common.unwrap(rootGraph, key.slice(2).split(".").map(getValue));
53
72
  }
54
73
  if (Array.isArray(key)) {
55
74
  return key.map(replacePlaceholders);
@@ -106,9 +125,17 @@ function prepQueryLinks(rootQuery, defs) {
106
125
  for (const node of query) {
107
126
  if (!common.isBranch(node))
108
127
  continue;
109
- let usedHere = prepQueryDef(node.children, rest, def, __spreadProps(__spreadValues({}, vars), {
110
- [key.slice(1)]: node.key
111
- }), node.version);
128
+ let usedHere;
129
+ if (node.prefix) {
130
+ const pageToString = () => key;
131
+ usedHere = node.children.flatMap((subNode) => {
132
+ const pager = common.decodeArgs(subNode);
133
+ Object.defineProperty(pager, "toString", { value: pageToString });
134
+ return prefixKey(prepQueryDef(subNode.children, rest, def, __spreadProps(__spreadValues({}, vars), { [key.slice(1)]: [node.key, pager] }), node.version), key);
135
+ });
136
+ } else {
137
+ usedHere = prepQueryDef(node.children, rest, def, __spreadProps(__spreadValues({}, vars), { [key.slice(1)]: node.key }), node.version);
138
+ }
112
139
  usedHere = prefixKey(usedHere, node.key);
113
140
  used = used.concat(usedHere);
114
141
  }
@@ -121,7 +148,7 @@ function getDefQuery(def, vars, version) {
121
148
  return key[0] === "$" ? vars[key.slice(1)] : key;
122
149
  }
123
150
  function getPath(template) {
124
- return template.split(".").map(getValue);
151
+ return template.split(".").flatMap(getValue);
125
152
  }
126
153
  const defQuery = [];
127
154
  function addDefQueries(key) {
@@ -145,10 +172,10 @@ function prepareDef(def, vars) {
145
172
  }
146
173
  function replacePlaceholders(key) {
147
174
  if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
148
- return "$$" + key.slice(2).split(".").map(getValue).join(".");
175
+ return "$$" + key.slice(2).split(".").flatMap(getValue).join(".");
149
176
  }
150
177
  if (Array.isArray(key)) {
151
- return key.map(replacePlaceholders);
178
+ return key.flatMap(replacePlaceholders);
152
179
  }
153
180
  if (typeof key === "object" && key) {
154
181
  const result = {};
@@ -158,7 +185,7 @@ function prepareDef(def, vars) {
158
185
  }
159
186
  return getValue(key);
160
187
  }
161
- return def.map(replacePlaceholders);
188
+ return def.flatMap(replacePlaceholders);
162
189
  }
163
190
  var index = (defs) => (store) => {
164
191
  const prefix = store.path;
package/index.mjs CHANGED
@@ -22,10 +22,20 @@ function linkGraph(rootGraph, defs) {
22
22
  for (const { path, def } of defs)
23
23
  linkGraphDef(rootGraph, path, def);
24
24
  return rootGraph;
25
- function linkGraphDef(graph, path, def, version = 0) {
25
+ function findChildren(node) {
26
+ if (node.children)
27
+ return node.children;
28
+ if (node.path) {
29
+ const linkedNode = unwrap(rootGraph, node.path);
30
+ if (Array.isArray(linkedNode))
31
+ return linkedNode;
32
+ }
33
+ throw Error("link.no_children " + JSON.stringify(node));
34
+ }
35
+ function linkGraphDef(graph, path, def, vars = {}, version = 0) {
26
36
  const [key, ...rest] = path;
27
37
  if (rest.length === 0) {
28
- const ref = makeRef(def);
38
+ const ref = makeRef(def, vars);
29
39
  const [range] = splitRef(def);
30
40
  const node2 = { key, path: encodePath(ref), version };
31
41
  if (range)
@@ -33,6 +43,15 @@ function linkGraph(rootGraph, defs) {
33
43
  merge(graph, [node2]);
34
44
  return;
35
45
  }
46
+ if (key[0] === "$") {
47
+ for (const node2 of graph) {
48
+ if (node2.end)
49
+ continue;
50
+ const newVars = __spreadProps(__spreadValues({}, vars), { [key.slice(1)]: node2.key });
51
+ linkGraphDef(findChildren(node2), rest, def, newVars, node2.version);
52
+ }
53
+ return;
54
+ }
36
55
  let node = graph[findFirst(graph, key)];
37
56
  if (!node || node.key !== key || node.end) {
38
57
  node = { key, version, value: 1 };
@@ -40,15 +59,15 @@ function linkGraph(rootGraph, defs) {
40
59
  delete node.value;
41
60
  node.children = [];
42
61
  }
43
- if (!node.children) {
44
- throw Error("linkGraph.unexpected_leaf " + key);
45
- }
46
- linkGraphDef(node.children, rest, def, node.version);
62
+ return linkGraphDef(findChildren(node), rest, def, vars, node.version);
47
63
  }
48
- function makeRef(def) {
64
+ function makeRef(def, vars) {
65
+ function getValue(key) {
66
+ return key[0] === "$" ? vars[key.slice(1)] : key;
67
+ }
49
68
  function replacePlaceholders(key) {
50
69
  if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
51
- return unwrap(rootGraph, key.slice(2).split("."));
70
+ return unwrap(rootGraph, key.slice(2).split(".").map(getValue));
52
71
  }
53
72
  if (Array.isArray(key)) {
54
73
  return key.map(replacePlaceholders);
@@ -105,9 +124,17 @@ function prepQueryLinks(rootQuery, defs) {
105
124
  for (const node of query) {
106
125
  if (!isBranch(node))
107
126
  continue;
108
- let usedHere = prepQueryDef(node.children, rest, def, __spreadProps(__spreadValues({}, vars), {
109
- [key.slice(1)]: node.key
110
- }), node.version);
127
+ let usedHere;
128
+ if (node.prefix) {
129
+ const pageToString = () => key;
130
+ usedHere = node.children.flatMap((subNode) => {
131
+ const pager = decodeArgs(subNode);
132
+ Object.defineProperty(pager, "toString", { value: pageToString });
133
+ return prefixKey(prepQueryDef(subNode.children, rest, def, __spreadProps(__spreadValues({}, vars), { [key.slice(1)]: [node.key, pager] }), node.version), key);
134
+ });
135
+ } else {
136
+ usedHere = prepQueryDef(node.children, rest, def, __spreadProps(__spreadValues({}, vars), { [key.slice(1)]: node.key }), node.version);
137
+ }
111
138
  usedHere = prefixKey(usedHere, node.key);
112
139
  used = used.concat(usedHere);
113
140
  }
@@ -120,7 +147,7 @@ function getDefQuery(def, vars, version) {
120
147
  return key[0] === "$" ? vars[key.slice(1)] : key;
121
148
  }
122
149
  function getPath(template) {
123
- return template.split(".").map(getValue);
150
+ return template.split(".").flatMap(getValue);
124
151
  }
125
152
  const defQuery = [];
126
153
  function addDefQueries(key) {
@@ -144,10 +171,10 @@ function prepareDef(def, vars) {
144
171
  }
145
172
  function replacePlaceholders(key) {
146
173
  if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
147
- return "$$" + key.slice(2).split(".").map(getValue).join(".");
174
+ return "$$" + key.slice(2).split(".").flatMap(getValue).join(".");
148
175
  }
149
176
  if (Array.isArray(key)) {
150
- return key.map(replacePlaceholders);
177
+ return key.flatMap(replacePlaceholders);
151
178
  }
152
179
  if (typeof key === "object" && key) {
153
180
  const result = {};
@@ -157,7 +184,7 @@ function prepareDef(def, vars) {
157
184
  }
158
185
  return getValue(key);
159
186
  }
160
- return def.map(replacePlaceholders);
187
+ return def.flatMap(replacePlaceholders);
161
188
  }
162
189
  var index = (defs) => (store) => {
163
190
  const prefix = store.path;
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.14",
5
+ "version": "0.15.15-alpha.1",
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.14"
19
+ "@graffy/common": "0.15.15-alpha.1"
20
20
  }
21
21
  }