@graffy/link 0.16.20-alpha.10 → 0.16.20-alpha.12

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 +40 -20
  2. package/index.mjs +40 -20
  3. package/package.json +3 -3
package/index.cjs CHANGED
@@ -11,7 +11,8 @@ function linkGraph(rootGraph, defs) {
11
11
  const realRef = makeRef(value, vars);
12
12
  const node = { key: realPath.pop(), path: common.encodePath(realRef), version };
13
13
  const [range] = common.splitRef(value);
14
- if (range) node.prefix = true;
14
+ if (range)
15
+ node.prefix = true;
15
16
  let target = rootGraph;
16
17
  do {
17
18
  const key = realPath.shift();
@@ -22,7 +23,8 @@ function linkGraph(rootGraph, defs) {
22
23
  }
23
24
  target = nextTarget.path ? common.unwrap(rootGraph, nextTarget.path) : nextTarget.children;
24
25
  } while (target && realPath.length);
25
- if (!target) return;
26
+ if (!target)
27
+ return;
26
28
  common.merge(target, realPath.length ? common.wrap([node], realPath, version) : [node]);
27
29
  }
28
30
  }
@@ -32,13 +34,15 @@ function linkGraph(rootGraph, defs) {
32
34
  return lookupValues(rootGraph, key.slice(2).split("."));
33
35
  }
34
36
  if (Array.isArray(key)) {
35
- if (!key.length) return [{ value: [], vars: {} }];
37
+ if (!key.length)
38
+ return [{ value: [], vars: {} }];
36
39
  return unbraid(key.map(getChoices));
37
40
  }
38
41
  if (typeof key === "object" && key) {
39
42
  const [range = {}, filter = {}] = common.splitArgs(key);
40
43
  const entries = Object.entries(filter).flat();
41
- if (!entries.length) return [{ value: {}, vars: {} }];
44
+ if (!entries.length)
45
+ return [{ value: {}, vars: {} }];
42
46
  const strands = unbraid(entries.map(getChoices));
43
47
  return strands.map(({ value, vars }) => ({
44
48
  value: {
@@ -63,28 +67,34 @@ function linkGraph(rootGraph, defs) {
63
67
  const [key, ...rest] = path;
64
68
  if (key[0] === "$") {
65
69
  return graph.flatMap((node2) => {
66
- if (node2.end) return [];
70
+ if (node2.end)
71
+ return [];
67
72
  const newVars = { ...vars, [key.slice(1)]: node2.key };
68
73
  return recurse(node2, rest, newVars);
69
74
  });
70
75
  }
71
76
  const encodedKey = common.encodeArgs(key).key;
72
77
  const node = graph[common.findFirst(graph, encodedKey)];
73
- if (!node || common.cmp(node.key, encodedKey) !== 0 || node.end) return [];
78
+ if (!node || common.cmp(node.key, encodedKey) !== 0 || node.end)
79
+ return [];
74
80
  return recurse(node, rest, vars);
75
81
  }
76
82
  function recurse(node, path, vars) {
77
- if (!path.length) return [{ value: node.value, vars }];
78
- if (node.children) return lookupValues(node.children, path, vars);
83
+ if (!path.length)
84
+ return [{ value: node.value, vars }];
85
+ if (node.children)
86
+ return lookupValues(node.children, path, vars);
79
87
  if (node.path) {
80
88
  const linked = common.unwrap(rootGraph, node.path);
81
- if (Array.isArray(linked)) return lookupValues(linked, path, vars);
89
+ if (Array.isArray(linked))
90
+ return lookupValues(linked, path, vars);
82
91
  }
83
92
  throw Error(`link.no_children ${JSON.stringify(node)}`);
84
93
  }
85
94
  }
86
95
  function unbraid(braid) {
87
- if (!braid.length) return [];
96
+ if (!braid.length)
97
+ return [];
88
98
  const [options, ...rest] = braid;
89
99
  if (!rest.length) {
90
100
  return options.map((option) => ({
@@ -102,13 +112,15 @@ function unbraid(braid) {
102
112
  }
103
113
  function isCompatible(oVars, sVars) {
104
114
  for (const name in oVars) {
105
- if (name in sVars && oVars[name] !== sVars[name]) return false;
115
+ if (name in sVars && oVars[name] !== sVars[name])
116
+ return false;
106
117
  }
107
118
  return true;
108
119
  }
109
120
  function makeRef(def, vars) {
110
121
  function getValue(key) {
111
- if (typeof key !== "string") return key;
122
+ if (typeof key !== "string")
123
+ return key;
112
124
  return key[0] === "$" && key.slice(1) in vars ? vars[key.slice(1)] : key;
113
125
  }
114
126
  function replacePlaceholders(key) {
@@ -164,7 +176,8 @@ function prepQueryLinks(rootQuery, defs) {
164
176
  def: def2
165
177
  }));
166
178
  }
167
- if (!(Array.isArray(query) && query.length)) return [];
179
+ if (!(Array.isArray(query) && query.length))
180
+ return [];
168
181
  const [key, ...rest] = path;
169
182
  const encodedKey = common.encodeArgs(key).key;
170
183
  if (rest.length === 0) {
@@ -175,14 +188,16 @@ function prepQueryLinks(rootQuery, defs) {
175
188
  });
176
189
  }
177
190
  const ix = common.findFirst(query, encodedKey);
178
- if (!query[ix] || common.cmp(query[ix].key, encodedKey) !== 0) return [];
191
+ if (!query[ix] || common.cmp(query[ix].key, encodedKey) !== 0)
192
+ return [];
179
193
  const [{ children: subQuery }] = query.splice(ix, 1);
180
194
  return addDefQuery(subQuery);
181
195
  }
182
196
  let used = [];
183
197
  if (key[0] === "$") {
184
198
  for (const node of query) {
185
- if (!common.isBranch(node)) continue;
199
+ if (!common.isBranch(node))
200
+ continue;
186
201
  let usedHere;
187
202
  if (node.prefix) {
188
203
  usedHere = node.children.flatMap((subNode) => {
@@ -212,14 +227,17 @@ function prepQueryLinks(rootQuery, defs) {
212
227
  node.version
213
228
  );
214
229
  }
215
- if (!node.prefix) usedHere = prefixKey(usedHere, common.decodeArgs(node));
230
+ if (!node.prefix)
231
+ usedHere = prefixKey(usedHere, common.decodeArgs(node));
216
232
  used = used.concat(usedHere);
217
233
  }
218
234
  } else {
219
235
  const node = query[common.findFirst(query, encodedKey)];
220
- if (!node || common.cmp(node.key, encodedKey) !== 0 || !node.children) return [];
236
+ if (!node || common.cmp(node.key, encodedKey) !== 0 || !node.children)
237
+ return [];
221
238
  used = prepQueryDef(node.children, rest, def, vars, node.version);
222
- if (!node.prefix) used = prefixKey(used, common.decodeArgs(node));
239
+ if (!node.prefix)
240
+ used = prefixKey(used, common.decodeArgs(node));
223
241
  }
224
242
  for (let i = 0; i < query.length; i++) {
225
243
  if (query[i].children && query[i].children.length === 0) {
@@ -264,7 +282,8 @@ function getDefQuery(def, vars, version) {
264
282
  }
265
283
  function prepareDef(def, vars) {
266
284
  function getValue(key) {
267
- if (typeof key !== "string") return key;
285
+ if (typeof key !== "string")
286
+ return key;
268
287
  if (key[0] === "$" && key.slice(1) in vars) {
269
288
  const value = vars[key.slice(1)];
270
289
  return typeof value === "object" && value !== null ? key : value;
@@ -300,7 +319,8 @@ const index = (defs) => (store) => {
300
319
  store.on("read", async (query, options, next) => {
301
320
  const unwrappedQuery = clone(common.unwrap(query, prefix));
302
321
  const usedDefs = prepQueryLinks(unwrappedQuery, defEntries);
303
- if (!usedDefs.length) return next(query, options);
322
+ if (!usedDefs.length)
323
+ return next(query, options);
304
324
  const result = await next(common.wrap(unwrappedQuery, prefix), options);
305
325
  const version = result[0].version;
306
326
  const unwrappedResult = common.unwrap(result, prefix);
package/index.mjs CHANGED
@@ -10,7 +10,8 @@ function linkGraph(rootGraph, defs) {
10
10
  const realRef = makeRef(value, vars);
11
11
  const node = { key: realPath.pop(), path: encodePath(realRef), version };
12
12
  const [range] = splitRef(value);
13
- if (range) node.prefix = true;
13
+ if (range)
14
+ node.prefix = true;
14
15
  let target = rootGraph;
15
16
  do {
16
17
  const key = realPath.shift();
@@ -21,7 +22,8 @@ function linkGraph(rootGraph, defs) {
21
22
  }
22
23
  target = nextTarget.path ? unwrap(rootGraph, nextTarget.path) : nextTarget.children;
23
24
  } while (target && realPath.length);
24
- if (!target) return;
25
+ if (!target)
26
+ return;
25
27
  merge(target, realPath.length ? wrap([node], realPath, version) : [node]);
26
28
  }
27
29
  }
@@ -31,13 +33,15 @@ function linkGraph(rootGraph, defs) {
31
33
  return lookupValues(rootGraph, key.slice(2).split("."));
32
34
  }
33
35
  if (Array.isArray(key)) {
34
- if (!key.length) return [{ value: [], vars: {} }];
36
+ if (!key.length)
37
+ return [{ value: [], vars: {} }];
35
38
  return unbraid(key.map(getChoices));
36
39
  }
37
40
  if (typeof key === "object" && key) {
38
41
  const [range = {}, filter = {}] = splitArgs(key);
39
42
  const entries = Object.entries(filter).flat();
40
- if (!entries.length) return [{ value: {}, vars: {} }];
43
+ if (!entries.length)
44
+ return [{ value: {}, vars: {} }];
41
45
  const strands = unbraid(entries.map(getChoices));
42
46
  return strands.map(({ value, vars }) => ({
43
47
  value: {
@@ -62,28 +66,34 @@ function linkGraph(rootGraph, defs) {
62
66
  const [key, ...rest] = path;
63
67
  if (key[0] === "$") {
64
68
  return graph.flatMap((node2) => {
65
- if (node2.end) return [];
69
+ if (node2.end)
70
+ return [];
66
71
  const newVars = { ...vars, [key.slice(1)]: node2.key };
67
72
  return recurse(node2, rest, newVars);
68
73
  });
69
74
  }
70
75
  const encodedKey = encodeArgs(key).key;
71
76
  const node = graph[findFirst(graph, encodedKey)];
72
- if (!node || cmp(node.key, encodedKey) !== 0 || node.end) return [];
77
+ if (!node || cmp(node.key, encodedKey) !== 0 || node.end)
78
+ return [];
73
79
  return recurse(node, rest, vars);
74
80
  }
75
81
  function recurse(node, path, vars) {
76
- if (!path.length) return [{ value: node.value, vars }];
77
- if (node.children) return lookupValues(node.children, path, vars);
82
+ if (!path.length)
83
+ return [{ value: node.value, vars }];
84
+ if (node.children)
85
+ return lookupValues(node.children, path, vars);
78
86
  if (node.path) {
79
87
  const linked = unwrap(rootGraph, node.path);
80
- if (Array.isArray(linked)) return lookupValues(linked, path, vars);
88
+ if (Array.isArray(linked))
89
+ return lookupValues(linked, path, vars);
81
90
  }
82
91
  throw Error(`link.no_children ${JSON.stringify(node)}`);
83
92
  }
84
93
  }
85
94
  function unbraid(braid) {
86
- if (!braid.length) return [];
95
+ if (!braid.length)
96
+ return [];
87
97
  const [options, ...rest] = braid;
88
98
  if (!rest.length) {
89
99
  return options.map((option) => ({
@@ -101,13 +111,15 @@ function unbraid(braid) {
101
111
  }
102
112
  function isCompatible(oVars, sVars) {
103
113
  for (const name in oVars) {
104
- if (name in sVars && oVars[name] !== sVars[name]) return false;
114
+ if (name in sVars && oVars[name] !== sVars[name])
115
+ return false;
105
116
  }
106
117
  return true;
107
118
  }
108
119
  function makeRef(def, vars) {
109
120
  function getValue(key) {
110
- if (typeof key !== "string") return key;
121
+ if (typeof key !== "string")
122
+ return key;
111
123
  return key[0] === "$" && key.slice(1) in vars ? vars[key.slice(1)] : key;
112
124
  }
113
125
  function replacePlaceholders(key) {
@@ -163,7 +175,8 @@ function prepQueryLinks(rootQuery, defs) {
163
175
  def: def2
164
176
  }));
165
177
  }
166
- if (!(Array.isArray(query) && query.length)) return [];
178
+ if (!(Array.isArray(query) && query.length))
179
+ return [];
167
180
  const [key, ...rest] = path;
168
181
  const encodedKey = encodeArgs(key).key;
169
182
  if (rest.length === 0) {
@@ -174,14 +187,16 @@ function prepQueryLinks(rootQuery, defs) {
174
187
  });
175
188
  }
176
189
  const ix = findFirst(query, encodedKey);
177
- if (!query[ix] || cmp(query[ix].key, encodedKey) !== 0) return [];
190
+ if (!query[ix] || cmp(query[ix].key, encodedKey) !== 0)
191
+ return [];
178
192
  const [{ children: subQuery }] = query.splice(ix, 1);
179
193
  return addDefQuery(subQuery);
180
194
  }
181
195
  let used = [];
182
196
  if (key[0] === "$") {
183
197
  for (const node of query) {
184
- if (!isBranch(node)) continue;
198
+ if (!isBranch(node))
199
+ continue;
185
200
  let usedHere;
186
201
  if (node.prefix) {
187
202
  usedHere = node.children.flatMap((subNode) => {
@@ -211,14 +226,17 @@ function prepQueryLinks(rootQuery, defs) {
211
226
  node.version
212
227
  );
213
228
  }
214
- if (!node.prefix) usedHere = prefixKey(usedHere, decodeArgs(node));
229
+ if (!node.prefix)
230
+ usedHere = prefixKey(usedHere, decodeArgs(node));
215
231
  used = used.concat(usedHere);
216
232
  }
217
233
  } else {
218
234
  const node = query[findFirst(query, encodedKey)];
219
- if (!node || cmp(node.key, encodedKey) !== 0 || !node.children) return [];
235
+ if (!node || cmp(node.key, encodedKey) !== 0 || !node.children)
236
+ return [];
220
237
  used = prepQueryDef(node.children, rest, def, vars, node.version);
221
- if (!node.prefix) used = prefixKey(used, decodeArgs(node));
238
+ if (!node.prefix)
239
+ used = prefixKey(used, decodeArgs(node));
222
240
  }
223
241
  for (let i = 0; i < query.length; i++) {
224
242
  if (query[i].children && query[i].children.length === 0) {
@@ -263,7 +281,8 @@ function getDefQuery(def, vars, version) {
263
281
  }
264
282
  function prepareDef(def, vars) {
265
283
  function getValue(key) {
266
- if (typeof key !== "string") return key;
284
+ if (typeof key !== "string")
285
+ return key;
267
286
  if (key[0] === "$" && key.slice(1) in vars) {
268
287
  const value = vars[key.slice(1)];
269
288
  return typeof value === "object" && value !== null ? key : value;
@@ -299,7 +318,8 @@ const index = (defs) => (store) => {
299
318
  store.on("read", async (query, options, next) => {
300
319
  const unwrappedQuery = clone(unwrap(query, prefix));
301
320
  const usedDefs = prepQueryLinks(unwrappedQuery, defEntries);
302
- if (!usedDefs.length) return next(query, options);
321
+ if (!usedDefs.length)
322
+ return next(query, options);
303
323
  const result = await next(wrap(unwrappedQuery, prefix), options);
304
324
  const version = result[0].version;
305
325
  const unwrappedResult = unwrap(result, prefix);
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.16.20-alpha.10",
5
+ "version": "0.16.20-alpha.12",
6
6
  "main": "./index.cjs",
7
7
  "exports": {
8
8
  "import": "./index.mjs",
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "license": "Apache-2.0",
18
18
  "dependencies": {
19
- "@graffy/common": "0.16.20-alpha.10",
20
- "debug": "^4.3.7"
19
+ "@graffy/common": "0.16.20-alpha.12",
20
+ "debug": "^4.3.3"
21
21
  }
22
22
  }