@graffy/link 0.15.8
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/Readme.md +5 -0
- package/index.cjs +148 -0
- package/index.mjs +147 -0
- package/package.json +21 -0
- package/types/index.d.ts +2 -0
- package/types/linkGraph.d.ts +1 -0
- package/types/prepQueryLinks.d.ts +1 -0
package/Readme.md
ADDED
package/index.cjs
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __spreadValues = (a, b) => {
|
|
10
|
+
for (var prop in b || (b = {}))
|
|
11
|
+
if (__hasOwnProp.call(b, prop))
|
|
12
|
+
__defNormalProp(a, prop, b[prop]);
|
|
13
|
+
if (__getOwnPropSymbols)
|
|
14
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
15
|
+
if (__propIsEnum.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
}
|
|
18
|
+
return a;
|
|
19
|
+
};
|
|
20
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
21
|
+
var common = require("@graffy/common");
|
|
22
|
+
function linkGraph(rootGraph, defs) {
|
|
23
|
+
for (const { path, def } of defs)
|
|
24
|
+
linkGraphDef(rootGraph, path, def);
|
|
25
|
+
return rootGraph;
|
|
26
|
+
function linkGraphDef(graph, path, def, vars = {}, version = 0) {
|
|
27
|
+
const [key, ...rest] = path;
|
|
28
|
+
if (rest.length === 0) {
|
|
29
|
+
common.merge(graph, [{ key, path: common.encodePath(makeRef(def, vars)), version }]);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
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);
|
|
36
|
+
}
|
|
37
|
+
linkGraphDef(node.children, rest, def, vars, node.version);
|
|
38
|
+
} else {
|
|
39
|
+
for (const node of graph) {
|
|
40
|
+
if (!common.isBranch(node))
|
|
41
|
+
continue;
|
|
42
|
+
linkGraphDef(node.children, rest, def, __spreadProps(__spreadValues({}, vars), {
|
|
43
|
+
[key.slice(1)]: node.key
|
|
44
|
+
}), node.version);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
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
|
+
}
|
|
55
|
+
function replacePlaceholders(key) {
|
|
56
|
+
if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
|
|
57
|
+
return common.unwrap(rootGraph, getPath(key.slice(2)));
|
|
58
|
+
}
|
|
59
|
+
if (Array.isArray(key)) {
|
|
60
|
+
return key.map(replacePlaceholders);
|
|
61
|
+
}
|
|
62
|
+
if (typeof key === "object" && key) {
|
|
63
|
+
const result = {};
|
|
64
|
+
for (const prop in key)
|
|
65
|
+
result[prop] = replacePlaceholders(key[prop]);
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
return getValue(key);
|
|
69
|
+
}
|
|
70
|
+
const ref = def.map(replacePlaceholders);
|
|
71
|
+
return ref;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function prepQueryLinks(rootQuery, defs) {
|
|
75
|
+
return defs.filter(({ path, def }) => prepQueryDef(rootQuery, path, def));
|
|
76
|
+
function prepQueryDef(query, path, def, vars = {}, version = 0) {
|
|
77
|
+
var _a;
|
|
78
|
+
const [key, ...rest] = path;
|
|
79
|
+
if (rest.length === 0) {
|
|
80
|
+
const ix = common.findFirst(query, key);
|
|
81
|
+
if (((_a = query[ix]) == null ? void 0 : _a.key) !== key)
|
|
82
|
+
return false;
|
|
83
|
+
query.splice(ix, 1);
|
|
84
|
+
common.add(rootQuery, getDefQuery(def, vars, version));
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
if (key[0] !== "$") {
|
|
88
|
+
const node = query[common.findFirst(query, key)];
|
|
89
|
+
if (!node || node.key !== key || !node.children)
|
|
90
|
+
return;
|
|
91
|
+
return prepQueryDef(node.children, rest, def, vars, node.version);
|
|
92
|
+
} else {
|
|
93
|
+
let used = false;
|
|
94
|
+
for (const node of query) {
|
|
95
|
+
if (!common.isBranch(node))
|
|
96
|
+
continue;
|
|
97
|
+
let usedHere = prepQueryDef(node.children, rest, def, __spreadProps(__spreadValues({}, vars), {
|
|
98
|
+
[key.slice(1)]: node.key
|
|
99
|
+
}), node.version);
|
|
100
|
+
used = used || usedHere;
|
|
101
|
+
}
|
|
102
|
+
return used;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function getDefQuery(def, vars, version) {
|
|
107
|
+
function getValue(key) {
|
|
108
|
+
return key[0] === "$" ? vars[key.slice(1)] : key;
|
|
109
|
+
}
|
|
110
|
+
function getPath(template) {
|
|
111
|
+
return template.split(".").map(getValue);
|
|
112
|
+
}
|
|
113
|
+
const defQuery = [];
|
|
114
|
+
function addDefQueries(key) {
|
|
115
|
+
if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
|
|
116
|
+
common.add(defQuery, common.wrapValue(1, getPath(key.slice(2)), version));
|
|
117
|
+
}
|
|
118
|
+
if (Array.isArray(key)) {
|
|
119
|
+
key.map(addDefQueries);
|
|
120
|
+
}
|
|
121
|
+
if (typeof key === "object" && key) {
|
|
122
|
+
const result = {};
|
|
123
|
+
for (const prop in key)
|
|
124
|
+
result[prop] = addDefQueries(key[prop]);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
def.map(addDefQueries);
|
|
128
|
+
return defQuery;
|
|
129
|
+
}
|
|
130
|
+
var index = (defs) => (store) => {
|
|
131
|
+
const prefix = store.path;
|
|
132
|
+
const defEntries = Object.entries(defs).map(([prop, def]) => ({
|
|
133
|
+
path: prop.split("."),
|
|
134
|
+
def
|
|
135
|
+
}));
|
|
136
|
+
store.on("read", async (query, options, next) => {
|
|
137
|
+
const unwrappedQuery = clone(common.unwrap(query, prefix));
|
|
138
|
+
const usedDefs = prepQueryLinks(unwrappedQuery, defEntries);
|
|
139
|
+
const result = await next(common.wrap(unwrappedQuery, prefix), options);
|
|
140
|
+
const unwrappedResult = common.unwrap(result, prefix);
|
|
141
|
+
linkGraph(unwrappedResult, usedDefs);
|
|
142
|
+
return result;
|
|
143
|
+
});
|
|
144
|
+
};
|
|
145
|
+
function clone(tree) {
|
|
146
|
+
return JSON.parse(JSON.stringify(tree));
|
|
147
|
+
}
|
|
148
|
+
module.exports = index;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import { merge, encodePath, findFirst, isBranch, unwrap, add, wrapValue, wrap } from "@graffy/common";
|
|
21
|
+
function linkGraph(rootGraph, defs) {
|
|
22
|
+
for (const { path, def } of defs)
|
|
23
|
+
linkGraphDef(rootGraph, path, def);
|
|
24
|
+
return rootGraph;
|
|
25
|
+
function linkGraphDef(graph, path, def, vars = {}, version = 0) {
|
|
26
|
+
const [key, ...rest] = path;
|
|
27
|
+
if (rest.length === 0) {
|
|
28
|
+
merge(graph, [{ key, path: encodePath(makeRef(def, vars)), version }]);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
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);
|
|
35
|
+
}
|
|
36
|
+
linkGraphDef(node.children, rest, def, vars, node.version);
|
|
37
|
+
} else {
|
|
38
|
+
for (const node of graph) {
|
|
39
|
+
if (!isBranch(node))
|
|
40
|
+
continue;
|
|
41
|
+
linkGraphDef(node.children, rest, def, __spreadProps(__spreadValues({}, vars), {
|
|
42
|
+
[key.slice(1)]: node.key
|
|
43
|
+
}), node.version);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
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
|
+
}
|
|
54
|
+
function replacePlaceholders(key) {
|
|
55
|
+
if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
|
|
56
|
+
return unwrap(rootGraph, getPath(key.slice(2)));
|
|
57
|
+
}
|
|
58
|
+
if (Array.isArray(key)) {
|
|
59
|
+
return key.map(replacePlaceholders);
|
|
60
|
+
}
|
|
61
|
+
if (typeof key === "object" && key) {
|
|
62
|
+
const result = {};
|
|
63
|
+
for (const prop in key)
|
|
64
|
+
result[prop] = replacePlaceholders(key[prop]);
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
return getValue(key);
|
|
68
|
+
}
|
|
69
|
+
const ref = def.map(replacePlaceholders);
|
|
70
|
+
return ref;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function prepQueryLinks(rootQuery, defs) {
|
|
74
|
+
return defs.filter(({ path, def }) => prepQueryDef(rootQuery, path, def));
|
|
75
|
+
function prepQueryDef(query, path, def, vars = {}, version = 0) {
|
|
76
|
+
var _a;
|
|
77
|
+
const [key, ...rest] = path;
|
|
78
|
+
if (rest.length === 0) {
|
|
79
|
+
const ix = findFirst(query, key);
|
|
80
|
+
if (((_a = query[ix]) == null ? void 0 : _a.key) !== key)
|
|
81
|
+
return false;
|
|
82
|
+
query.splice(ix, 1);
|
|
83
|
+
add(rootQuery, getDefQuery(def, vars, version));
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
if (key[0] !== "$") {
|
|
87
|
+
const node = query[findFirst(query, key)];
|
|
88
|
+
if (!node || node.key !== key || !node.children)
|
|
89
|
+
return;
|
|
90
|
+
return prepQueryDef(node.children, rest, def, vars, node.version);
|
|
91
|
+
} else {
|
|
92
|
+
let used = false;
|
|
93
|
+
for (const node of query) {
|
|
94
|
+
if (!isBranch(node))
|
|
95
|
+
continue;
|
|
96
|
+
let usedHere = prepQueryDef(node.children, rest, def, __spreadProps(__spreadValues({}, vars), {
|
|
97
|
+
[key.slice(1)]: node.key
|
|
98
|
+
}), node.version);
|
|
99
|
+
used = used || usedHere;
|
|
100
|
+
}
|
|
101
|
+
return used;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function getDefQuery(def, vars, version) {
|
|
106
|
+
function getValue(key) {
|
|
107
|
+
return key[0] === "$" ? vars[key.slice(1)] : key;
|
|
108
|
+
}
|
|
109
|
+
function getPath(template) {
|
|
110
|
+
return template.split(".").map(getValue);
|
|
111
|
+
}
|
|
112
|
+
const defQuery = [];
|
|
113
|
+
function addDefQueries(key) {
|
|
114
|
+
if (typeof key === "string" && key[0] === "$" && key[1] === "$") {
|
|
115
|
+
add(defQuery, wrapValue(1, getPath(key.slice(2)), version));
|
|
116
|
+
}
|
|
117
|
+
if (Array.isArray(key)) {
|
|
118
|
+
key.map(addDefQueries);
|
|
119
|
+
}
|
|
120
|
+
if (typeof key === "object" && key) {
|
|
121
|
+
const result = {};
|
|
122
|
+
for (const prop in key)
|
|
123
|
+
result[prop] = addDefQueries(key[prop]);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
def.map(addDefQueries);
|
|
127
|
+
return defQuery;
|
|
128
|
+
}
|
|
129
|
+
var index = (defs) => (store) => {
|
|
130
|
+
const prefix = store.path;
|
|
131
|
+
const defEntries = Object.entries(defs).map(([prop, def]) => ({
|
|
132
|
+
path: prop.split("."),
|
|
133
|
+
def
|
|
134
|
+
}));
|
|
135
|
+
store.on("read", async (query, options, next) => {
|
|
136
|
+
const unwrappedQuery = clone(unwrap(query, prefix));
|
|
137
|
+
const usedDefs = prepQueryLinks(unwrappedQuery, defEntries);
|
|
138
|
+
const result = await next(wrap(unwrappedQuery, prefix), options);
|
|
139
|
+
const unwrappedResult = unwrap(result, prefix);
|
|
140
|
+
linkGraph(unwrappedResult, usedDefs);
|
|
141
|
+
return result;
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
function clone(tree) {
|
|
145
|
+
return JSON.parse(JSON.stringify(tree));
|
|
146
|
+
}
|
|
147
|
+
export { index as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@graffy/link",
|
|
3
|
+
"description": "Graffy module for constructing links using an intuitive, declarative notation.",
|
|
4
|
+
"author": "aravind (https://github.com/aravindet)",
|
|
5
|
+
"version": "0.15.8",
|
|
6
|
+
"main": "./index.cjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
"import": "./index.mjs",
|
|
9
|
+
"require": "./index.cjs"
|
|
10
|
+
},
|
|
11
|
+
"module": "./index.mjs",
|
|
12
|
+
"types": "./types/index.d.ts",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/usegraffy/graffy.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "Apache-2.0",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@graffy/common": "0.15.8"
|
|
20
|
+
}
|
|
21
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function linkGraph(rootGraph: any, defs: any): any;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function prepQueryLinks(rootQuery: any, defs: any): any;
|