@graffy/link 0.15.25 → 0.16.0-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.
- package/index.cjs +41 -27
- package/index.mjs +42 -28
- package/package.json +2 -2
package/index.cjs
CHANGED
|
@@ -12,14 +12,14 @@ function linkGraph(rootGraph, defs) {
|
|
|
12
12
|
const realPath = makeRef(path, vars);
|
|
13
13
|
const realRef = makeRef(value, vars);
|
|
14
14
|
const node = { key: realPath.pop(), path: common.encodePath(realRef), version };
|
|
15
|
-
const [range] = common.splitRef(
|
|
15
|
+
const [range] = common.splitRef(value);
|
|
16
16
|
if (range)
|
|
17
17
|
node.prefix = true;
|
|
18
18
|
let target = rootGraph;
|
|
19
19
|
do {
|
|
20
20
|
const key = realPath.shift();
|
|
21
21
|
const nextTarget = target[common.findFirst(target, key)];
|
|
22
|
-
if (!nextTarget || nextTarget.key !==
|
|
22
|
+
if (!nextTarget || common.cmp(nextTarget.key, key) !== 0 || nextTarget.end) {
|
|
23
23
|
realPath.unshift(key);
|
|
24
24
|
break;
|
|
25
25
|
}
|
|
@@ -75,8 +75,9 @@ function linkGraph(rootGraph, defs) {
|
|
|
75
75
|
return recurse(node2, rest, newVars);
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
const encodedKey = common.encodeArgs(key).key;
|
|
79
|
+
let node = graph[common.findFirst(graph, encodedKey)];
|
|
80
|
+
if (!node || common.cmp(node.key, encodedKey) !== 0 || node.end)
|
|
80
81
|
return [];
|
|
81
82
|
return recurse(node, rest, vars);
|
|
82
83
|
}
|
|
@@ -128,7 +129,7 @@ function makeRef(def, vars) {
|
|
|
128
129
|
if (Array.isArray(key)) {
|
|
129
130
|
return key.map(replacePlaceholders);
|
|
130
131
|
}
|
|
131
|
-
if (typeof key === "object" && key) {
|
|
132
|
+
if (typeof key === "object" && !ArrayBuffer.isView(key) && key) {
|
|
132
133
|
const result = {};
|
|
133
134
|
for (const prop in key) {
|
|
134
135
|
result[replacePlaceholders(prop)] = replacePlaceholders(key[prop]);
|
|
@@ -137,13 +138,11 @@ function makeRef(def, vars) {
|
|
|
137
138
|
}
|
|
138
139
|
return getValue(key);
|
|
139
140
|
}
|
|
140
|
-
|
|
141
|
-
return ref;
|
|
141
|
+
return common.encodePath(def.map(replacePlaceholders));
|
|
142
142
|
}
|
|
143
143
|
function prepQueryLinks(rootQuery, defs) {
|
|
144
144
|
return defs.flatMap(({ path, def }) => prepQueryDef(rootQuery, path, def));
|
|
145
145
|
function prepQueryDef(query, path, def, vars = {}, version = 0) {
|
|
146
|
-
var _a;
|
|
147
146
|
function addDefQuery(subQuery) {
|
|
148
147
|
common.add(rootQuery, getDefQuery(def, vars, version));
|
|
149
148
|
const [range, filter] = common.splitRef(def);
|
|
@@ -155,7 +154,7 @@ function prepQueryLinks(rootQuery, defs) {
|
|
|
155
154
|
);
|
|
156
155
|
}
|
|
157
156
|
return {
|
|
158
|
-
path: path.concat(node.
|
|
157
|
+
path: path.concat(node.prefix ? common.decodeArgs(node) : common.MIN_KEY),
|
|
159
158
|
def: prepareDef(
|
|
160
159
|
def.slice(0, -1).concat({
|
|
161
160
|
...filter,
|
|
@@ -176,22 +175,19 @@ function prepQueryLinks(rootQuery, defs) {
|
|
|
176
175
|
def: def2
|
|
177
176
|
}));
|
|
178
177
|
}
|
|
179
|
-
function makePager(pager) {
|
|
180
|
-
Object.defineProperty(pager, "toString", { value: () => key });
|
|
181
|
-
return pager;
|
|
182
|
-
}
|
|
183
178
|
if (!Array.isArray(query) || !query.length)
|
|
184
179
|
return [];
|
|
185
180
|
const [key, ...rest] = path;
|
|
181
|
+
const encodedKey = common.encodeArgs(key).key;
|
|
186
182
|
if (rest.length === 0) {
|
|
187
183
|
if (key[0] === "$") {
|
|
188
184
|
return query.splice(0).flatMap((node) => {
|
|
189
|
-
vars[key.slice(1)] =
|
|
185
|
+
vars[key.slice(1)] = common.decodeArgs(node);
|
|
190
186
|
return addDefQuery(node.children);
|
|
191
187
|
});
|
|
192
188
|
} else {
|
|
193
|
-
const ix = common.findFirst(query,
|
|
194
|
-
if (
|
|
189
|
+
const ix = common.findFirst(query, encodedKey);
|
|
190
|
+
if (!query[ix] || common.cmp(query[ix].key, encodedKey) !== 0)
|
|
195
191
|
return [];
|
|
196
192
|
const [{ children: subQuery }] = query.splice(ix, 1);
|
|
197
193
|
return addDefQuery(subQuery);
|
|
@@ -205,13 +201,18 @@ function prepQueryLinks(rootQuery, defs) {
|
|
|
205
201
|
let usedHere;
|
|
206
202
|
if (node.prefix) {
|
|
207
203
|
usedHere = node.children.flatMap((subNode) => {
|
|
208
|
-
const pager = makePager(common.decodeArgs(subNode));
|
|
209
204
|
return prefixKey(
|
|
210
205
|
prepQueryDef(
|
|
211
206
|
subNode.children,
|
|
212
207
|
rest,
|
|
213
208
|
def,
|
|
214
|
-
{
|
|
209
|
+
{
|
|
210
|
+
...vars,
|
|
211
|
+
[key.slice(1)]: {
|
|
212
|
+
...common.decodeArgs(node),
|
|
213
|
+
...common.decodeArgs(subNode)
|
|
214
|
+
}
|
|
215
|
+
},
|
|
215
216
|
node.version
|
|
216
217
|
),
|
|
217
218
|
key
|
|
@@ -222,19 +223,21 @@ function prepQueryLinks(rootQuery, defs) {
|
|
|
222
223
|
node.children,
|
|
223
224
|
rest,
|
|
224
225
|
def,
|
|
225
|
-
{ ...vars, [key.slice(1)]: node
|
|
226
|
+
{ ...vars, [key.slice(1)]: common.decodeArgs(node) },
|
|
226
227
|
node.version
|
|
227
228
|
);
|
|
228
229
|
}
|
|
229
|
-
|
|
230
|
+
if (!node.prefix)
|
|
231
|
+
usedHere = prefixKey(usedHere, common.decodeArgs(node));
|
|
230
232
|
used = used.concat(usedHere);
|
|
231
233
|
}
|
|
232
234
|
} else {
|
|
233
|
-
const node = query[common.findFirst(query,
|
|
234
|
-
if (!node || node.key !==
|
|
235
|
+
const node = query[common.findFirst(query, encodedKey)];
|
|
236
|
+
if (!node || common.cmp(node.key, encodedKey) !== 0 || !node.children)
|
|
235
237
|
return [];
|
|
236
238
|
used = prepQueryDef(node.children, rest, def, vars, node.version);
|
|
237
|
-
|
|
239
|
+
if (!node.prefix)
|
|
240
|
+
used = prefixKey(used, common.decodeArgs(node));
|
|
238
241
|
}
|
|
239
242
|
for (let i = 0; i < query.length; i++) {
|
|
240
243
|
if (query[i].children && query[i].children.length === 0) {
|
|
@@ -255,7 +258,14 @@ function getDefQuery(def, vars, version) {
|
|
|
255
258
|
const defQuery = [];
|
|
256
259
|
function addDefQueries(key) {
|
|
257
260
|
if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
|
|
258
|
-
|
|
261
|
+
const path = getPath(key.slice(2));
|
|
262
|
+
let porcelainQuery = { $key: path.pop() };
|
|
263
|
+
let $key;
|
|
264
|
+
while ($key = path.pop()) {
|
|
265
|
+
porcelainQuery = { $key, $chi: [porcelainQuery] };
|
|
266
|
+
}
|
|
267
|
+
const query = common.encodeQuery(porcelainQuery, version);
|
|
268
|
+
common.add(defQuery, query);
|
|
259
269
|
}
|
|
260
270
|
if (Array.isArray(key)) {
|
|
261
271
|
key.map(addDefQueries);
|
|
@@ -274,7 +284,11 @@ function prepareDef(def, vars) {
|
|
|
274
284
|
function getValue(key) {
|
|
275
285
|
if (typeof key !== "string")
|
|
276
286
|
return key;
|
|
277
|
-
|
|
287
|
+
if (key[0] === "$" && key.slice(1) in vars) {
|
|
288
|
+
const value = vars[key.slice(1)];
|
|
289
|
+
return typeof value === "object" && value !== null ? key : value;
|
|
290
|
+
}
|
|
291
|
+
return key;
|
|
278
292
|
}
|
|
279
293
|
function replacePlaceholders(key) {
|
|
280
294
|
if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
|
|
@@ -297,7 +311,7 @@ function prepareDef(def, vars) {
|
|
|
297
311
|
}
|
|
298
312
|
const log = debug__default.default("graffy:link");
|
|
299
313
|
const index = (defs) => (store) => {
|
|
300
|
-
const prefix = store.path;
|
|
314
|
+
const prefix = common.encodePath(store.path);
|
|
301
315
|
const defEntries = Object.entries(defs).map(([prop, def]) => ({
|
|
302
316
|
path: prop.split("."),
|
|
303
317
|
def
|
|
@@ -320,6 +334,6 @@ const index = (defs) => (store) => {
|
|
|
320
334
|
});
|
|
321
335
|
};
|
|
322
336
|
function clone(tree) {
|
|
323
|
-
return
|
|
337
|
+
return common.deserialize(common.serialize(tree));
|
|
324
338
|
}
|
|
325
339
|
module.exports = index;
|
package/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { encodePath, splitRef, findFirst, unwrap, merge, wrap, splitArgs, decodeArgs, isBranch, add,
|
|
1
|
+
import { encodePath, splitRef, findFirst, cmp, unwrap, merge, wrap, splitArgs, encodeArgs, decodeArgs, isBranch, add, MIN_KEY, encodeQuery, finalize, deserialize, serialize } from "@graffy/common";
|
|
2
2
|
import debug from "debug";
|
|
3
3
|
function linkGraph(rootGraph, defs) {
|
|
4
4
|
let version = rootGraph[0].version;
|
|
@@ -9,14 +9,14 @@ function linkGraph(rootGraph, defs) {
|
|
|
9
9
|
const realPath = makeRef(path, vars);
|
|
10
10
|
const realRef = makeRef(value, vars);
|
|
11
11
|
const node = { key: realPath.pop(), path: encodePath(realRef), version };
|
|
12
|
-
const [range] = splitRef(
|
|
12
|
+
const [range] = splitRef(value);
|
|
13
13
|
if (range)
|
|
14
14
|
node.prefix = true;
|
|
15
15
|
let target = rootGraph;
|
|
16
16
|
do {
|
|
17
17
|
const key = realPath.shift();
|
|
18
18
|
const nextTarget = target[findFirst(target, key)];
|
|
19
|
-
if (!nextTarget || nextTarget.key !==
|
|
19
|
+
if (!nextTarget || cmp(nextTarget.key, key) !== 0 || nextTarget.end) {
|
|
20
20
|
realPath.unshift(key);
|
|
21
21
|
break;
|
|
22
22
|
}
|
|
@@ -72,8 +72,9 @@ function linkGraph(rootGraph, defs) {
|
|
|
72
72
|
return recurse(node2, rest, newVars);
|
|
73
73
|
});
|
|
74
74
|
}
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
const encodedKey = encodeArgs(key).key;
|
|
76
|
+
let node = graph[findFirst(graph, encodedKey)];
|
|
77
|
+
if (!node || cmp(node.key, encodedKey) !== 0 || node.end)
|
|
77
78
|
return [];
|
|
78
79
|
return recurse(node, rest, vars);
|
|
79
80
|
}
|
|
@@ -125,7 +126,7 @@ function makeRef(def, vars) {
|
|
|
125
126
|
if (Array.isArray(key)) {
|
|
126
127
|
return key.map(replacePlaceholders);
|
|
127
128
|
}
|
|
128
|
-
if (typeof key === "object" && key) {
|
|
129
|
+
if (typeof key === "object" && !ArrayBuffer.isView(key) && key) {
|
|
129
130
|
const result = {};
|
|
130
131
|
for (const prop in key) {
|
|
131
132
|
result[replacePlaceholders(prop)] = replacePlaceholders(key[prop]);
|
|
@@ -134,13 +135,11 @@ function makeRef(def, vars) {
|
|
|
134
135
|
}
|
|
135
136
|
return getValue(key);
|
|
136
137
|
}
|
|
137
|
-
|
|
138
|
-
return ref;
|
|
138
|
+
return encodePath(def.map(replacePlaceholders));
|
|
139
139
|
}
|
|
140
140
|
function prepQueryLinks(rootQuery, defs) {
|
|
141
141
|
return defs.flatMap(({ path, def }) => prepQueryDef(rootQuery, path, def));
|
|
142
142
|
function prepQueryDef(query, path, def, vars = {}, version = 0) {
|
|
143
|
-
var _a;
|
|
144
143
|
function addDefQuery(subQuery) {
|
|
145
144
|
add(rootQuery, getDefQuery(def, vars, version));
|
|
146
145
|
const [range, filter] = splitRef(def);
|
|
@@ -152,7 +151,7 @@ function prepQueryLinks(rootQuery, defs) {
|
|
|
152
151
|
);
|
|
153
152
|
}
|
|
154
153
|
return {
|
|
155
|
-
path: path.concat(node.
|
|
154
|
+
path: path.concat(node.prefix ? decodeArgs(node) : MIN_KEY),
|
|
156
155
|
def: prepareDef(
|
|
157
156
|
def.slice(0, -1).concat({
|
|
158
157
|
...filter,
|
|
@@ -173,22 +172,19 @@ function prepQueryLinks(rootQuery, defs) {
|
|
|
173
172
|
def: def2
|
|
174
173
|
}));
|
|
175
174
|
}
|
|
176
|
-
function makePager(pager) {
|
|
177
|
-
Object.defineProperty(pager, "toString", { value: () => key });
|
|
178
|
-
return pager;
|
|
179
|
-
}
|
|
180
175
|
if (!Array.isArray(query) || !query.length)
|
|
181
176
|
return [];
|
|
182
177
|
const [key, ...rest] = path;
|
|
178
|
+
const encodedKey = encodeArgs(key).key;
|
|
183
179
|
if (rest.length === 0) {
|
|
184
180
|
if (key[0] === "$") {
|
|
185
181
|
return query.splice(0).flatMap((node) => {
|
|
186
|
-
vars[key.slice(1)] =
|
|
182
|
+
vars[key.slice(1)] = decodeArgs(node);
|
|
187
183
|
return addDefQuery(node.children);
|
|
188
184
|
});
|
|
189
185
|
} else {
|
|
190
|
-
const ix = findFirst(query,
|
|
191
|
-
if (
|
|
186
|
+
const ix = findFirst(query, encodedKey);
|
|
187
|
+
if (!query[ix] || cmp(query[ix].key, encodedKey) !== 0)
|
|
192
188
|
return [];
|
|
193
189
|
const [{ children: subQuery }] = query.splice(ix, 1);
|
|
194
190
|
return addDefQuery(subQuery);
|
|
@@ -202,13 +198,18 @@ function prepQueryLinks(rootQuery, defs) {
|
|
|
202
198
|
let usedHere;
|
|
203
199
|
if (node.prefix) {
|
|
204
200
|
usedHere = node.children.flatMap((subNode) => {
|
|
205
|
-
const pager = makePager(decodeArgs(subNode));
|
|
206
201
|
return prefixKey(
|
|
207
202
|
prepQueryDef(
|
|
208
203
|
subNode.children,
|
|
209
204
|
rest,
|
|
210
205
|
def,
|
|
211
|
-
{
|
|
206
|
+
{
|
|
207
|
+
...vars,
|
|
208
|
+
[key.slice(1)]: {
|
|
209
|
+
...decodeArgs(node),
|
|
210
|
+
...decodeArgs(subNode)
|
|
211
|
+
}
|
|
212
|
+
},
|
|
212
213
|
node.version
|
|
213
214
|
),
|
|
214
215
|
key
|
|
@@ -219,19 +220,21 @@ function prepQueryLinks(rootQuery, defs) {
|
|
|
219
220
|
node.children,
|
|
220
221
|
rest,
|
|
221
222
|
def,
|
|
222
|
-
{ ...vars, [key.slice(1)]: node
|
|
223
|
+
{ ...vars, [key.slice(1)]: decodeArgs(node) },
|
|
223
224
|
node.version
|
|
224
225
|
);
|
|
225
226
|
}
|
|
226
|
-
|
|
227
|
+
if (!node.prefix)
|
|
228
|
+
usedHere = prefixKey(usedHere, decodeArgs(node));
|
|
227
229
|
used = used.concat(usedHere);
|
|
228
230
|
}
|
|
229
231
|
} else {
|
|
230
|
-
const node = query[findFirst(query,
|
|
231
|
-
if (!node || node.key !==
|
|
232
|
+
const node = query[findFirst(query, encodedKey)];
|
|
233
|
+
if (!node || cmp(node.key, encodedKey) !== 0 || !node.children)
|
|
232
234
|
return [];
|
|
233
235
|
used = prepQueryDef(node.children, rest, def, vars, node.version);
|
|
234
|
-
|
|
236
|
+
if (!node.prefix)
|
|
237
|
+
used = prefixKey(used, decodeArgs(node));
|
|
235
238
|
}
|
|
236
239
|
for (let i = 0; i < query.length; i++) {
|
|
237
240
|
if (query[i].children && query[i].children.length === 0) {
|
|
@@ -252,7 +255,14 @@ function getDefQuery(def, vars, version) {
|
|
|
252
255
|
const defQuery = [];
|
|
253
256
|
function addDefQueries(key) {
|
|
254
257
|
if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
|
|
255
|
-
|
|
258
|
+
const path = getPath(key.slice(2));
|
|
259
|
+
let porcelainQuery = { $key: path.pop() };
|
|
260
|
+
let $key;
|
|
261
|
+
while ($key = path.pop()) {
|
|
262
|
+
porcelainQuery = { $key, $chi: [porcelainQuery] };
|
|
263
|
+
}
|
|
264
|
+
const query = encodeQuery(porcelainQuery, version);
|
|
265
|
+
add(defQuery, query);
|
|
256
266
|
}
|
|
257
267
|
if (Array.isArray(key)) {
|
|
258
268
|
key.map(addDefQueries);
|
|
@@ -271,7 +281,11 @@ function prepareDef(def, vars) {
|
|
|
271
281
|
function getValue(key) {
|
|
272
282
|
if (typeof key !== "string")
|
|
273
283
|
return key;
|
|
274
|
-
|
|
284
|
+
if (key[0] === "$" && key.slice(1) in vars) {
|
|
285
|
+
const value = vars[key.slice(1)];
|
|
286
|
+
return typeof value === "object" && value !== null ? key : value;
|
|
287
|
+
}
|
|
288
|
+
return key;
|
|
275
289
|
}
|
|
276
290
|
function replacePlaceholders(key) {
|
|
277
291
|
if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
|
|
@@ -294,7 +308,7 @@ function prepareDef(def, vars) {
|
|
|
294
308
|
}
|
|
295
309
|
const log = debug("graffy:link");
|
|
296
310
|
const index = (defs) => (store) => {
|
|
297
|
-
const prefix = store.path;
|
|
311
|
+
const prefix = encodePath(store.path);
|
|
298
312
|
const defEntries = Object.entries(defs).map(([prop, def]) => ({
|
|
299
313
|
path: prop.split("."),
|
|
300
314
|
def
|
|
@@ -317,7 +331,7 @@ const index = (defs) => (store) => {
|
|
|
317
331
|
});
|
|
318
332
|
};
|
|
319
333
|
function clone(tree) {
|
|
320
|
-
return
|
|
334
|
+
return deserialize(serialize(tree));
|
|
321
335
|
}
|
|
322
336
|
export {
|
|
323
337
|
index as default
|
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.
|
|
5
|
+
"version": "0.16.0-alpha.1",
|
|
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.
|
|
19
|
+
"@graffy/common": "0.16.0-alpha.1",
|
|
20
20
|
"debug": "^4.3.3"
|
|
21
21
|
}
|
|
22
22
|
}
|