@statelyai/graph 0.3.0 → 0.4.0
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 +12 -11
- package/dist/{adjacency-list-A4_Eiwj3.mjs → adjacency-list-Bv4tfiM3.mjs} +33 -0
- package/dist/{algorithms-DBU7nmIV.mjs → algorithms-CnTmuX9t.mjs} +682 -24
- package/dist/algorithms.d.mts +479 -10
- package/dist/algorithms.mjs +1 -1
- package/dist/converter-C5DlzzHs.mjs +67 -0
- package/dist/{edge-list-DuHMz8hf.mjs → edge-list-R1SUbHwe.mjs} +32 -0
- package/dist/formats/adjacency-list/index.d.mts +35 -1
- package/dist/formats/adjacency-list/index.mjs +1 -1
- package/dist/formats/converter/index.d.mts +37 -3
- package/dist/formats/converter/index.mjs +1 -1
- package/dist/formats/cytoscape/index.d.mts +50 -2
- package/dist/formats/cytoscape/index.mjs +53 -5
- package/dist/formats/d3/index.d.mts +48 -2
- package/dist/formats/d3/index.mjs +48 -2
- package/dist/formats/dot/index.d.mts +56 -2
- package/dist/formats/dot/index.mjs +55 -2
- package/dist/formats/edge-list/index.d.mts +34 -1
- package/dist/formats/edge-list/index.mjs +1 -1
- package/dist/formats/gexf/index.d.mts +1 -1
- package/dist/formats/gexf/index.mjs +5 -5
- package/dist/formats/gml/index.d.mts +58 -2
- package/dist/formats/gml/index.mjs +59 -4
- package/dist/formats/graphml/index.d.mts +1 -1
- package/dist/formats/graphml/index.mjs +2 -2
- package/dist/formats/jgf/index.d.mts +51 -2
- package/dist/formats/jgf/index.mjs +54 -5
- package/dist/formats/mermaid/index.d.mts +201 -8
- package/dist/formats/mermaid/index.mjs +365 -26
- package/dist/formats/tgf/index.d.mts +47 -2
- package/dist/formats/tgf/index.mjs +46 -2
- package/dist/formats/xyflow/index.d.mts +73 -0
- package/dist/formats/xyflow/index.mjs +133 -0
- package/dist/index.d.mts +320 -14
- package/dist/index.mjs +117 -9
- package/dist/{indexing-BFFVMnjF.mjs → indexing-DitHphT7.mjs} +37 -7
- package/dist/queries.d.mts +353 -8
- package/dist/queries.mjs +359 -15
- package/dist/{types-B6Tpeerk.d.mts → types-Bq_fmLwW.d.mts} +15 -4
- package/package.json +3 -1
- package/dist/converter-DnbeyE_p.mjs +0 -33
package/dist/queries.mjs
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
|
-
import { t as getIndex } from "./indexing-
|
|
1
|
+
import { t as getIndex } from "./indexing-DitHphT7.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/queries.ts
|
|
4
|
+
/**
|
|
5
|
+
* Returns all edges (incoming + outgoing) connected to a node.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const graph = createGraph({
|
|
10
|
+
* nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
|
|
11
|
+
* edges: [
|
|
12
|
+
* { id: 'e1', sourceId: 'a', targetId: 'b' },
|
|
13
|
+
* { id: 'e2', sourceId: 'c', targetId: 'b' },
|
|
14
|
+
* ],
|
|
15
|
+
* });
|
|
16
|
+
* getEdgesOf(graph, 'b');
|
|
17
|
+
* // => [edge e1, edge e2]
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
4
20
|
function getEdgesOf(graph, nodeId) {
|
|
5
21
|
const idx = getIndex(graph);
|
|
6
22
|
const outIds = idx.outEdges.get(nodeId) ?? [];
|
|
@@ -18,14 +34,60 @@ function getEdgesOf(graph, nodeId) {
|
|
|
18
34
|
}
|
|
19
35
|
return result;
|
|
20
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Returns incoming edges to a node.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* const graph = createGraph({
|
|
43
|
+
* nodes: [{ id: 'a' }, { id: 'b' }],
|
|
44
|
+
* edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
|
|
45
|
+
* });
|
|
46
|
+
* getInEdges(graph, 'b');
|
|
47
|
+
* // => [edge e1]
|
|
48
|
+
* getInEdges(graph, 'a');
|
|
49
|
+
* // => []
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
21
52
|
function getInEdges(graph, nodeId) {
|
|
22
53
|
const idx = getIndex(graph);
|
|
23
54
|
return (idx.inEdges.get(nodeId) ?? []).map((eid) => graph.edges[idx.edgeById.get(eid)]);
|
|
24
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns outgoing edges from a node.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const graph = createGraph({
|
|
62
|
+
* nodes: [{ id: 'a' }, { id: 'b' }],
|
|
63
|
+
* edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
|
|
64
|
+
* });
|
|
65
|
+
* getOutEdges(graph, 'a');
|
|
66
|
+
* // => [edge e1]
|
|
67
|
+
* getOutEdges(graph, 'b');
|
|
68
|
+
* // => []
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
25
71
|
function getOutEdges(graph, nodeId) {
|
|
26
72
|
const idx = getIndex(graph);
|
|
27
73
|
return (idx.outEdges.get(nodeId) ?? []).map((eid) => graph.edges[idx.edgeById.get(eid)]);
|
|
28
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Returns the edge from `sourceId` to `targetId`, or `undefined` if none exists.
|
|
77
|
+
* For undirected graphs, checks both directions.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* const graph = createGraph({
|
|
82
|
+
* nodes: [{ id: 'a' }, { id: 'b' }],
|
|
83
|
+
* edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
|
|
84
|
+
* });
|
|
85
|
+
* getEdgeBetween(graph, 'a', 'b');
|
|
86
|
+
* // => edge e1
|
|
87
|
+
* getEdgeBetween(graph, 'b', 'a');
|
|
88
|
+
* // => undefined (directed graph)
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
29
91
|
function getEdgeBetween(graph, sourceId, targetId) {
|
|
30
92
|
const idx = getIndex(graph);
|
|
31
93
|
const outIds = idx.outEdges.get(sourceId) ?? [];
|
|
@@ -43,6 +105,22 @@ function getEdgeBetween(graph, sourceId, targetId) {
|
|
|
43
105
|
}
|
|
44
106
|
}
|
|
45
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Returns direct successor nodes (targets of outgoing edges).
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* const graph = createGraph({
|
|
114
|
+
* nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
|
|
115
|
+
* edges: [
|
|
116
|
+
* { id: 'e1', sourceId: 'a', targetId: 'b' },
|
|
117
|
+
* { id: 'e2', sourceId: 'a', targetId: 'c' },
|
|
118
|
+
* ],
|
|
119
|
+
* });
|
|
120
|
+
* getSuccessors(graph, 'a');
|
|
121
|
+
* // => [node b, node c]
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
46
124
|
function getSuccessors(graph, nodeId) {
|
|
47
125
|
const idx = getIndex(graph);
|
|
48
126
|
const edgeIds = idx.outEdges.get(nodeId) ?? [];
|
|
@@ -58,6 +136,22 @@ function getSuccessors(graph, nodeId) {
|
|
|
58
136
|
}
|
|
59
137
|
return result;
|
|
60
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Returns direct predecessor nodes (sources of incoming edges).
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* const graph = createGraph({
|
|
145
|
+
* nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
|
|
146
|
+
* edges: [
|
|
147
|
+
* { id: 'e1', sourceId: 'a', targetId: 'c' },
|
|
148
|
+
* { id: 'e2', sourceId: 'b', targetId: 'c' },
|
|
149
|
+
* ],
|
|
150
|
+
* });
|
|
151
|
+
* getPredecessors(graph, 'c');
|
|
152
|
+
* // => [node a, node b]
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
61
155
|
function getPredecessors(graph, nodeId) {
|
|
62
156
|
const idx = getIndex(graph);
|
|
63
157
|
const edgeIds = idx.inEdges.get(nodeId) ?? [];
|
|
@@ -73,6 +167,22 @@ function getPredecessors(graph, nodeId) {
|
|
|
73
167
|
}
|
|
74
168
|
return result;
|
|
75
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* Returns all neighbor nodes (successors + predecessors).
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* const graph = createGraph({
|
|
176
|
+
* nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
|
|
177
|
+
* edges: [
|
|
178
|
+
* { id: 'e1', sourceId: 'a', targetId: 'b' },
|
|
179
|
+
* { id: 'e2', sourceId: 'c', targetId: 'b' },
|
|
180
|
+
* ],
|
|
181
|
+
* });
|
|
182
|
+
* getNeighbors(graph, 'b');
|
|
183
|
+
* // => [node a, node c]
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
76
186
|
function getNeighbors(graph, nodeId) {
|
|
77
187
|
const idx = getIndex(graph);
|
|
78
188
|
const ids = /* @__PURE__ */ new Set();
|
|
@@ -80,6 +190,23 @@ function getNeighbors(graph, nodeId) {
|
|
|
80
190
|
for (const eid of idx.inEdges.get(nodeId) ?? []) ids.add(graph.edges[idx.edgeById.get(eid)].sourceId);
|
|
81
191
|
return [...ids].map((id) => graph.nodes[idx.nodeById.get(id)]).filter(Boolean);
|
|
82
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* Returns the total degree of a node (inDegree + outDegree).
|
|
195
|
+
* For undirected graphs, each edge is counted once.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* const graph = createGraph({
|
|
200
|
+
* nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
|
|
201
|
+
* edges: [
|
|
202
|
+
* { id: 'e1', sourceId: 'a', targetId: 'b' },
|
|
203
|
+
* { id: 'e2', sourceId: 'c', targetId: 'b' },
|
|
204
|
+
* ],
|
|
205
|
+
* });
|
|
206
|
+
* getDegree(graph, 'b'); // => 2
|
|
207
|
+
* getDegree(graph, 'a'); // => 1
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
83
210
|
function getDegree(graph, nodeId) {
|
|
84
211
|
const idx = getIndex(graph);
|
|
85
212
|
if (graph.type === "undirected") {
|
|
@@ -89,32 +216,110 @@ function getDegree(graph, nodeId) {
|
|
|
89
216
|
}
|
|
90
217
|
return (idx.inEdges.get(nodeId)?.length ?? 0) + (idx.outEdges.get(nodeId)?.length ?? 0);
|
|
91
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* Returns the in-degree of a node (number of incoming edges).
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* const graph = createGraph({
|
|
225
|
+
* nodes: [{ id: 'a' }, { id: 'b' }],
|
|
226
|
+
* edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
|
|
227
|
+
* });
|
|
228
|
+
* getInDegree(graph, 'b'); // => 1
|
|
229
|
+
* getInDegree(graph, 'a'); // => 0
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
92
232
|
function getInDegree(graph, nodeId) {
|
|
93
233
|
return getIndex(graph).inEdges.get(nodeId)?.length ?? 0;
|
|
94
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
* Returns the out-degree of a node (number of outgoing edges).
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* const graph = createGraph({
|
|
241
|
+
* nodes: [{ id: 'a' }, { id: 'b' }],
|
|
242
|
+
* edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
|
|
243
|
+
* });
|
|
244
|
+
* getOutDegree(graph, 'a'); // => 1
|
|
245
|
+
* getOutDegree(graph, 'b'); // => 0
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
95
248
|
function getOutDegree(graph, nodeId) {
|
|
96
249
|
return getIndex(graph).outEdges.get(nodeId)?.length ?? 0;
|
|
97
250
|
}
|
|
251
|
+
/**
|
|
252
|
+
* Returns direct children of a node in the hierarchy.
|
|
253
|
+
* Pass `null` to get root-level nodes.
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```ts
|
|
257
|
+
* const graph = createGraph({
|
|
258
|
+
* nodes: [
|
|
259
|
+
* { id: 'parent' },
|
|
260
|
+
* { id: 'child1', parentId: 'parent' },
|
|
261
|
+
* { id: 'child2', parentId: 'parent' },
|
|
262
|
+
* ],
|
|
263
|
+
* });
|
|
264
|
+
* getChildren(graph, 'parent');
|
|
265
|
+
* // => [node child1, node child2]
|
|
266
|
+
* getChildren(graph, null);
|
|
267
|
+
* // => [node parent]
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
98
270
|
function getChildren(graph, nodeId) {
|
|
99
271
|
const idx = getIndex(graph);
|
|
100
272
|
return (idx.childNodes.get(nodeId) ?? []).map((id) => graph.nodes[idx.nodeById.get(id)]).filter(Boolean);
|
|
101
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* Returns the parent node in the hierarchy, or `undefined` if root-level.
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* ```ts
|
|
279
|
+
* const graph = createGraph({
|
|
280
|
+
* nodes: [
|
|
281
|
+
* { id: 'parent' },
|
|
282
|
+
* { id: 'child', parentId: 'parent' },
|
|
283
|
+
* ],
|
|
284
|
+
* });
|
|
285
|
+
* getParent(graph, 'child');
|
|
286
|
+
* // => node parent
|
|
287
|
+
* getParent(graph, 'parent');
|
|
288
|
+
* // => undefined
|
|
289
|
+
* ```
|
|
290
|
+
*/
|
|
102
291
|
function getParent(graph, nodeId) {
|
|
103
292
|
const idx = getIndex(graph);
|
|
104
293
|
const ni = idx.nodeById.get(nodeId);
|
|
105
294
|
if (ni === void 0) return void 0;
|
|
106
295
|
const node = graph.nodes[ni];
|
|
107
|
-
if (node.parentId
|
|
296
|
+
if (!node.parentId) return void 0;
|
|
108
297
|
const pi = idx.nodeById.get(node.parentId);
|
|
109
298
|
return pi !== void 0 ? graph.nodes[pi] : void 0;
|
|
110
299
|
}
|
|
300
|
+
/**
|
|
301
|
+
* Returns all ancestors from the node up to the root (nearest parent first).
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* ```ts
|
|
305
|
+
* const graph = createGraph({
|
|
306
|
+
* nodes: [
|
|
307
|
+
* { id: 'root' },
|
|
308
|
+
* { id: 'mid', parentId: 'root' },
|
|
309
|
+
* { id: 'leaf', parentId: 'mid' },
|
|
310
|
+
* ],
|
|
311
|
+
* });
|
|
312
|
+
* getAncestors(graph, 'leaf');
|
|
313
|
+
* // => [node mid, node root]
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
111
316
|
function getAncestors(graph, nodeId) {
|
|
112
317
|
const idx = getIndex(graph);
|
|
113
318
|
const result = [];
|
|
114
319
|
let ni = idx.nodeById.get(nodeId);
|
|
115
320
|
if (ni === void 0) return result;
|
|
116
321
|
let current = graph.nodes[ni];
|
|
117
|
-
while (current && current.parentId
|
|
322
|
+
while (current && current.parentId) {
|
|
118
323
|
const pi = idx.nodeById.get(current.parentId);
|
|
119
324
|
if (pi === void 0) break;
|
|
120
325
|
const p = graph.nodes[pi];
|
|
@@ -123,6 +328,22 @@ function getAncestors(graph, nodeId) {
|
|
|
123
328
|
}
|
|
124
329
|
return result;
|
|
125
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* Returns all descendants recursively (depth-first).
|
|
333
|
+
*
|
|
334
|
+
* @example
|
|
335
|
+
* ```ts
|
|
336
|
+
* const graph = createGraph({
|
|
337
|
+
* nodes: [
|
|
338
|
+
* { id: 'root' },
|
|
339
|
+
* { id: 'child', parentId: 'root' },
|
|
340
|
+
* { id: 'grandchild', parentId: 'child' },
|
|
341
|
+
* ],
|
|
342
|
+
* });
|
|
343
|
+
* getDescendants(graph, 'root');
|
|
344
|
+
* // => [node child, node grandchild]
|
|
345
|
+
* ```
|
|
346
|
+
*/
|
|
126
347
|
function getDescendants(graph, nodeId) {
|
|
127
348
|
const idx = getIndex(graph);
|
|
128
349
|
const result = [];
|
|
@@ -139,26 +360,87 @@ function getDescendants(graph, nodeId) {
|
|
|
139
360
|
collect(nodeId);
|
|
140
361
|
return result;
|
|
141
362
|
}
|
|
363
|
+
/**
|
|
364
|
+
* Returns all root nodes (nodes with no parent).
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```ts
|
|
368
|
+
* const graph = createGraph({
|
|
369
|
+
* nodes: [
|
|
370
|
+
* { id: 'root1' },
|
|
371
|
+
* { id: 'root2' },
|
|
372
|
+
* { id: 'child', parentId: 'root1' },
|
|
373
|
+
* ],
|
|
374
|
+
* });
|
|
375
|
+
* getRoots(graph);
|
|
376
|
+
* // => [node root1, node root2]
|
|
377
|
+
* ```
|
|
378
|
+
*/
|
|
142
379
|
function getRoots(graph) {
|
|
143
380
|
const idx = getIndex(graph);
|
|
144
381
|
return idx.childNodes.get(null)?.map((id) => graph.nodes[idx.nodeById.get(id)]).filter(Boolean) ?? [];
|
|
145
382
|
}
|
|
146
|
-
/**
|
|
383
|
+
/**
|
|
384
|
+
* Whether a node has children (is a compound/group node).
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* ```ts
|
|
388
|
+
* const graph = createGraph({
|
|
389
|
+
* nodes: [
|
|
390
|
+
* { id: 'parent' },
|
|
391
|
+
* { id: 'child', parentId: 'parent' },
|
|
392
|
+
* ],
|
|
393
|
+
* });
|
|
394
|
+
* isCompound(graph, 'parent'); // => true
|
|
395
|
+
* isCompound(graph, 'child'); // => false
|
|
396
|
+
* ```
|
|
397
|
+
*/
|
|
147
398
|
function isCompound(graph, nodeId) {
|
|
148
399
|
return (getIndex(graph).childNodes.get(nodeId) ?? []).length > 0;
|
|
149
400
|
}
|
|
150
|
-
/**
|
|
401
|
+
/**
|
|
402
|
+
* Whether a node has no children (is a leaf/atomic node).
|
|
403
|
+
*
|
|
404
|
+
* @example
|
|
405
|
+
* ```ts
|
|
406
|
+
* const graph = createGraph({
|
|
407
|
+
* nodes: [
|
|
408
|
+
* { id: 'parent' },
|
|
409
|
+
* { id: 'child', parentId: 'parent' },
|
|
410
|
+
* ],
|
|
411
|
+
* });
|
|
412
|
+
* isLeaf(graph, 'child'); // => true
|
|
413
|
+
* isLeaf(graph, 'parent'); // => false
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
151
416
|
function isLeaf(graph, nodeId) {
|
|
152
417
|
return !isCompound(graph, nodeId);
|
|
153
418
|
}
|
|
154
|
-
/**
|
|
419
|
+
/**
|
|
420
|
+
* Depth of a node in the hierarchy (root = 0).
|
|
421
|
+
* Returns -1 if the node is not found.
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
* ```ts
|
|
425
|
+
* const graph = createGraph({
|
|
426
|
+
* nodes: [
|
|
427
|
+
* { id: 'root' },
|
|
428
|
+
* { id: 'child', parentId: 'root' },
|
|
429
|
+
* { id: 'grandchild', parentId: 'child' },
|
|
430
|
+
* ],
|
|
431
|
+
* });
|
|
432
|
+
* getDepth(graph, 'root'); // => 0
|
|
433
|
+
* getDepth(graph, 'child'); // => 1
|
|
434
|
+
* getDepth(graph, 'grandchild'); // => 2
|
|
435
|
+
* ```
|
|
436
|
+
*/
|
|
155
437
|
function getDepth(graph, nodeId) {
|
|
156
438
|
const idx = getIndex(graph);
|
|
157
439
|
let d = 0;
|
|
158
440
|
let ni = idx.nodeById.get(nodeId);
|
|
159
441
|
if (ni === void 0) return -1;
|
|
160
442
|
let current = graph.nodes[ni];
|
|
161
|
-
while (current.parentId
|
|
443
|
+
while (current.parentId) {
|
|
162
444
|
d++;
|
|
163
445
|
const pi = idx.nodeById.get(current.parentId);
|
|
164
446
|
if (pi === void 0) break;
|
|
@@ -166,17 +448,49 @@ function getDepth(graph, nodeId) {
|
|
|
166
448
|
}
|
|
167
449
|
return d;
|
|
168
450
|
}
|
|
169
|
-
/**
|
|
451
|
+
/**
|
|
452
|
+
* Sibling nodes (same parentId, excluding the node itself).
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* ```ts
|
|
456
|
+
* const graph = createGraph({
|
|
457
|
+
* nodes: [
|
|
458
|
+
* { id: 'parent' },
|
|
459
|
+
* { id: 'a', parentId: 'parent' },
|
|
460
|
+
* { id: 'b', parentId: 'parent' },
|
|
461
|
+
* { id: 'c', parentId: 'parent' },
|
|
462
|
+
* ],
|
|
463
|
+
* });
|
|
464
|
+
* getSiblings(graph, 'a');
|
|
465
|
+
* // => [node b, node c]
|
|
466
|
+
* ```
|
|
467
|
+
*/
|
|
170
468
|
function getSiblings(graph, nodeId) {
|
|
171
469
|
const idx = getIndex(graph);
|
|
172
470
|
const ni = idx.nodeById.get(nodeId);
|
|
173
471
|
if (ni === void 0) return [];
|
|
174
472
|
const node = graph.nodes[ni];
|
|
175
|
-
return (idx.childNodes.get(node.parentId) ?? []).filter((id) => id !== nodeId).map((id) => graph.nodes[idx.nodeById.get(id)]).filter(Boolean);
|
|
473
|
+
return (idx.childNodes.get(node.parentId ?? null) ?? []).filter((id) => id !== nodeId).map((id) => graph.nodes[idx.nodeById.get(id)]).filter(Boolean);
|
|
176
474
|
}
|
|
177
475
|
/**
|
|
178
|
-
* Least Common Ancestor
|
|
476
|
+
* Least Common Ancestor -- deepest proper ancestor of all given nodes.
|
|
179
477
|
* A proper ancestor excludes the input nodes themselves.
|
|
478
|
+
*
|
|
479
|
+
* @example
|
|
480
|
+
* ```ts
|
|
481
|
+
* const graph = createGraph({
|
|
482
|
+
* nodes: [
|
|
483
|
+
* { id: 'root' },
|
|
484
|
+
* { id: 'a', parentId: 'root' },
|
|
485
|
+
* { id: 'b', parentId: 'root' },
|
|
486
|
+
* { id: 'a1', parentId: 'a' },
|
|
487
|
+
* ],
|
|
488
|
+
* });
|
|
489
|
+
* getLCA(graph, 'a1', 'b');
|
|
490
|
+
* // => node root
|
|
491
|
+
* getLCA(graph, 'a', 'b');
|
|
492
|
+
* // => node root
|
|
493
|
+
* ```
|
|
180
494
|
*/
|
|
181
495
|
function getLCA(graph, ...nodeIds) {
|
|
182
496
|
if (nodeIds.length === 0) return void 0;
|
|
@@ -186,7 +500,7 @@ function getLCA(graph, ...nodeIds) {
|
|
|
186
500
|
let ni$1 = idx.nodeById.get(id);
|
|
187
501
|
if (ni$1 === void 0) return result;
|
|
188
502
|
let current = graph.nodes[ni$1];
|
|
189
|
-
while (current.parentId
|
|
503
|
+
while (current.parentId) {
|
|
190
504
|
result.push(current.parentId);
|
|
191
505
|
const pi = idx.nodeById.get(current.parentId);
|
|
192
506
|
if (pi === void 0) break;
|
|
@@ -250,7 +564,7 @@ function getRelativeDistanceMap(graph, parentId) {
|
|
|
250
564
|
let sourceId = null;
|
|
251
565
|
if (parentId !== null) {
|
|
252
566
|
const pi = idx.nodeById.get(parentId);
|
|
253
|
-
if (pi !== void 0) sourceId = graph.nodes[pi].initialNodeId;
|
|
567
|
+
if (pi !== void 0) sourceId = graph.nodes[pi].initialNodeId ?? null;
|
|
254
568
|
} else sourceId = graph.initialNodeId;
|
|
255
569
|
if (!sourceId) return {};
|
|
256
570
|
const siblingSet = new Set(idx.childNodes.get(parentId) ?? []);
|
|
@@ -323,14 +637,44 @@ function getRelativeDistance(graph, nodeId) {
|
|
|
323
637
|
const ni = getIndex(graph).nodeById.get(nodeId);
|
|
324
638
|
if (ni === void 0) return void 0;
|
|
325
639
|
const node = graph.nodes[ni];
|
|
326
|
-
return getRelativeDistanceMap(graph, node.parentId)[nodeId];
|
|
640
|
+
return getRelativeDistanceMap(graph, node.parentId ?? null)[nodeId];
|
|
327
641
|
}
|
|
328
|
-
/**
|
|
642
|
+
/**
|
|
643
|
+
* Nodes with no incoming edges (inDegree 0).
|
|
644
|
+
*
|
|
645
|
+
* @example
|
|
646
|
+
* ```ts
|
|
647
|
+
* const graph = createGraph({
|
|
648
|
+
* nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
|
|
649
|
+
* edges: [
|
|
650
|
+
* { id: 'e1', sourceId: 'a', targetId: 'b' },
|
|
651
|
+
* { id: 'e2', sourceId: 'b', targetId: 'c' },
|
|
652
|
+
* ],
|
|
653
|
+
* });
|
|
654
|
+
* getSources(graph);
|
|
655
|
+
* // => [node a]
|
|
656
|
+
* ```
|
|
657
|
+
*/
|
|
329
658
|
function getSources(graph) {
|
|
330
659
|
const idx = getIndex(graph);
|
|
331
660
|
return graph.nodes.filter((n) => (idx.inEdges.get(n.id)?.length ?? 0) === 0);
|
|
332
661
|
}
|
|
333
|
-
/**
|
|
662
|
+
/**
|
|
663
|
+
* Nodes with no outgoing edges (outDegree 0).
|
|
664
|
+
*
|
|
665
|
+
* @example
|
|
666
|
+
* ```ts
|
|
667
|
+
* const graph = createGraph({
|
|
668
|
+
* nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
|
|
669
|
+
* edges: [
|
|
670
|
+
* { id: 'e1', sourceId: 'a', targetId: 'b' },
|
|
671
|
+
* { id: 'e2', sourceId: 'b', targetId: 'c' },
|
|
672
|
+
* ],
|
|
673
|
+
* });
|
|
674
|
+
* getSinks(graph);
|
|
675
|
+
* // => [node c]
|
|
676
|
+
* ```
|
|
677
|
+
*/
|
|
334
678
|
function getSinks(graph) {
|
|
335
679
|
const idx = getIndex(graph);
|
|
336
680
|
return graph.nodes.filter((n) => (idx.outEdges.get(n.id)?.length ?? 0) === 0);
|
|
@@ -67,8 +67,8 @@ interface Graph<TNodeData = any, TEdgeData = any, TGraphData = any> {
|
|
|
67
67
|
interface GraphNode<TNodeData = any> {
|
|
68
68
|
type: 'node';
|
|
69
69
|
id: string;
|
|
70
|
-
parentId
|
|
71
|
-
initialNodeId
|
|
70
|
+
parentId?: string | null;
|
|
71
|
+
initialNodeId?: string | null;
|
|
72
72
|
label: string;
|
|
73
73
|
data: TNodeData;
|
|
74
74
|
x?: number;
|
|
@@ -94,7 +94,7 @@ interface GraphEdge<TEdgeData = any> {
|
|
|
94
94
|
style?: Record<string, string | number>;
|
|
95
95
|
}
|
|
96
96
|
interface VisualNode<TNodeData = any> extends Omit<GraphNode<TNodeData>, keyof EntityRect>, EntityRect {
|
|
97
|
-
shape
|
|
97
|
+
shape?: string;
|
|
98
98
|
}
|
|
99
99
|
interface VisualEdge<TEdgeData = any> extends Omit<GraphEdge<TEdgeData>, keyof EntityRect>, EntityRect {}
|
|
100
100
|
interface VisualGraph<TNodeData = any, TEdgeData = any, TGraphData = any> extends Omit<Graph<TNodeData, TEdgeData, TGraphData>, 'nodes' | 'edges'> {
|
|
@@ -234,6 +234,17 @@ interface GraphFormatConverter<TSerial> {
|
|
|
234
234
|
/** Convert from the serialized format to a Graph. */
|
|
235
235
|
from(input: TSerial): Graph;
|
|
236
236
|
}
|
|
237
|
+
/**
|
|
238
|
+
* A bidirectional converter between `VisualGraph` and a serialized format.
|
|
239
|
+
*
|
|
240
|
+
* Use this for formats that carry position/size data (e.g. xyflow, cytoscape).
|
|
241
|
+
*/
|
|
242
|
+
interface VisualGraphFormatConverter<TSerial> {
|
|
243
|
+
/** Convert a VisualGraph to the serialized format. */
|
|
244
|
+
to(graph: VisualGraph): TSerial;
|
|
245
|
+
/** Convert from the serialized format to a VisualGraph. */
|
|
246
|
+
from(input: TSerial): VisualGraph;
|
|
247
|
+
}
|
|
237
248
|
interface TransitionOptions<TState, TEvent> {
|
|
238
249
|
/** Initial state to begin BFS exploration from. */
|
|
239
250
|
initialState: TState;
|
|
@@ -251,4 +262,4 @@ interface TransitionOptions<TState, TEvent> {
|
|
|
251
262
|
id?: string;
|
|
252
263
|
}
|
|
253
264
|
//#endregion
|
|
254
|
-
export { TraversalOptions as C,
|
|
265
|
+
export { TraversalOptions as C, VisualGraphFormatConverter as D, VisualGraphConfig as E, VisualNode as O, TransitionOptions as S, VisualGraph as T, MSTOptions as _, EntitiesConfig as a, PathOptions as b, Graph as c, GraphEdge as d, GraphFormatConverter as f, GraphStep as g, GraphPath as h, EdgeConfig as i, GraphConfig as l, GraphPatch as m, DeleteNodeOptions as n, EntitiesUpdate as o, GraphNode as p, EdgeChange as r, EntityRect as s, AllPairsShortestPathsOptions as t, GraphDiff as u, NodeChange as v, VisualEdge as w, SinglePathOptions as x, NodeConfig as y };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statelyai/graph",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"description": "A TypeScript-first graph library with plain JSON-serializable objects",
|
|
6
6
|
"author": "David Khourshid <david@stately.ai>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"./jgf": "./dist/formats/jgf/index.mjs",
|
|
37
37
|
"./mermaid": "./dist/formats/mermaid/index.mjs",
|
|
38
38
|
"./tgf": "./dist/formats/tgf/index.mjs",
|
|
39
|
+
"./xyflow": "./dist/formats/xyflow/index.mjs",
|
|
39
40
|
"./queries": "./dist/queries.mjs",
|
|
40
41
|
"./schemas": "./dist/schemas.mjs",
|
|
41
42
|
"./package.json": "./package.json"
|
|
@@ -53,6 +54,7 @@
|
|
|
53
54
|
"@types/d3-array": "^3.2.2",
|
|
54
55
|
"@types/d3-force": "^3.0.10",
|
|
55
56
|
"@types/node": "^25.0.3",
|
|
57
|
+
"@xyflow/system": "^0.0.75",
|
|
56
58
|
"bumpp": "^10.3.2",
|
|
57
59
|
"cytoscape": "^3.33.1",
|
|
58
60
|
"d3-array": "^3.2.4",
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { n as toAdjacencyList, t as fromAdjacencyList } from "./adjacency-list-A4_Eiwj3.mjs";
|
|
2
|
-
import { n as toEdgeList, t as fromEdgeList } from "./edge-list-DuHMz8hf.mjs";
|
|
3
|
-
|
|
4
|
-
//#region src/formats/converter/index.ts
|
|
5
|
-
/**
|
|
6
|
-
* Create a `GraphFormatConverter` from a pair of `to`/`from` functions.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```ts
|
|
10
|
-
* import { createFormatConverter } from '@statelyai/graph';
|
|
11
|
-
*
|
|
12
|
-
* const yamlConverter = createFormatConverter(
|
|
13
|
-
* (graph) => toYAML(graph),
|
|
14
|
-
* (yaml) => fromYAML(yaml),
|
|
15
|
-
* );
|
|
16
|
-
*
|
|
17
|
-
* const yaml = yamlConverter.to(graph);
|
|
18
|
-
* const graph = yamlConverter.from(yaml);
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
function createFormatConverter(to, from) {
|
|
22
|
-
return {
|
|
23
|
-
to,
|
|
24
|
-
from
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
/** Bidirectional converter for adjacency-list format (`Record<string, string[]>`). */
|
|
28
|
-
const adjacencyListConverter = createFormatConverter(toAdjacencyList, fromAdjacencyList);
|
|
29
|
-
/** Bidirectional converter for edge-list format (`[source, target][]`). */
|
|
30
|
-
const edgeListConverter = createFormatConverter(toEdgeList, fromEdgeList);
|
|
31
|
-
|
|
32
|
-
//#endregion
|
|
33
|
-
export { createFormatConverter as n, edgeListConverter as r, adjacencyListConverter as t };
|