@graffy/link 0.16.19 → 0.16.20-alpha.10

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