@statelyai/graph 0.1.0 → 0.3.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.
Files changed (52) hide show
  1. package/README.md +65 -15
  2. package/dist/{adjacency-list-CXpOCibq.mjs → adjacency-list-ITO40kmn.mjs} +34 -1
  3. package/dist/{algorithms-R35X6ro4.mjs → algorithms-NWSB2RWj.mjs} +753 -19
  4. package/dist/algorithms.d.mts +488 -11
  5. package/dist/algorithms.mjs +2 -2
  6. package/dist/converter-CchokMDg.mjs +67 -0
  7. package/dist/edge-list-CgX6bBIF.mjs +71 -0
  8. package/dist/formats/adjacency-list/index.d.mts +44 -0
  9. package/dist/formats/adjacency-list/index.mjs +3 -0
  10. package/dist/formats/converter/index.d.mts +61 -0
  11. package/dist/formats/converter/index.mjs +3 -0
  12. package/dist/formats/cytoscape/index.d.mts +83 -0
  13. package/dist/formats/cytoscape/index.mjs +135 -0
  14. package/dist/formats/d3/index.d.mts +68 -0
  15. package/dist/formats/d3/index.mjs +111 -0
  16. package/dist/formats/dot/index.d.mts +63 -0
  17. package/dist/formats/dot/index.mjs +288 -0
  18. package/dist/formats/edge-list/index.d.mts +43 -0
  19. package/dist/formats/edge-list/index.mjs +3 -0
  20. package/dist/formats/gexf/index.d.mts +9 -0
  21. package/dist/formats/gexf/index.mjs +249 -0
  22. package/dist/formats/gml/index.d.mts +65 -0
  23. package/dist/formats/gml/index.mjs +291 -0
  24. package/dist/formats/graphml/index.d.mts +9 -0
  25. package/dist/{graphml-CUTNRXqd.mjs → formats/graphml/index.mjs} +18 -4
  26. package/dist/formats/jgf/index.d.mts +79 -0
  27. package/dist/formats/jgf/index.mjs +134 -0
  28. package/dist/formats/mermaid/index.d.mts +381 -0
  29. package/dist/formats/mermaid/index.mjs +2237 -0
  30. package/dist/formats/tgf/index.d.mts +54 -0
  31. package/dist/formats/tgf/index.mjs +111 -0
  32. package/dist/index.d.mts +332 -21
  33. package/dist/index.mjs +117 -13
  34. package/dist/{indexing-BHg1VhqN.mjs → indexing-eNDrXdDA.mjs} +31 -2
  35. package/dist/queries.d.mts +430 -9
  36. package/dist/queries.mjs +472 -9
  37. package/dist/{types-XV3S5Jnh.d.mts → types-BDXC1O5b.d.mts} +37 -2
  38. package/package.json +43 -17
  39. package/dist/adjacency-list-DW-lAUe8.d.mts +0 -10
  40. package/dist/dot-BRtq3e3c.mjs +0 -59
  41. package/dist/dot-HmJeUMsj.d.mts +0 -6
  42. package/dist/edge-list-BRujEnnU.mjs +0 -39
  43. package/dist/edge-list-CJmfoNu2.d.mts +0 -10
  44. package/dist/formats/adjacency-list.d.mts +0 -2
  45. package/dist/formats/adjacency-list.mjs +0 -3
  46. package/dist/formats/dot.d.mts +0 -2
  47. package/dist/formats/dot.mjs +0 -3
  48. package/dist/formats/edge-list.d.mts +0 -2
  49. package/dist/formats/edge-list.mjs +0 -3
  50. package/dist/formats/graphml.d.mts +0 -2
  51. package/dist/formats/graphml.mjs +0 -3
  52. package/dist/graphml-CMjPzSfY.d.mts +0 -7
@@ -1,37 +1,458 @@
1
- import { c as Graph, d as GraphEdge, f as GraphNode } from "./types-XV3S5Jnh.mjs";
1
+ import { c as Graph, d as GraphEdge, p as GraphNode } from "./types-BDXC1O5b.mjs";
2
2
 
3
3
  //#region src/queries.d.ts
4
+
5
+ /**
6
+ * Returns all edges (incoming + outgoing) connected to a node.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const graph = createGraph({
11
+ * nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
12
+ * edges: [
13
+ * { id: 'e1', sourceId: 'a', targetId: 'b' },
14
+ * { id: 'e2', sourceId: 'c', targetId: 'b' },
15
+ * ],
16
+ * });
17
+ * getEdgesOf(graph, 'b');
18
+ * // => [edge e1, edge e2]
19
+ * ```
20
+ */
4
21
  declare function getEdgesOf<E>(graph: Graph<any, E>, nodeId: string): GraphEdge<E>[];
22
+ /**
23
+ * Returns incoming edges to a node.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * const graph = createGraph({
28
+ * nodes: [{ id: 'a' }, { id: 'b' }],
29
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
30
+ * });
31
+ * getInEdges(graph, 'b');
32
+ * // => [edge e1]
33
+ * getInEdges(graph, 'a');
34
+ * // => []
35
+ * ```
36
+ */
5
37
  declare function getInEdges<E>(graph: Graph<any, E>, nodeId: string): GraphEdge<E>[];
38
+ /**
39
+ * Returns outgoing edges from a node.
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * const graph = createGraph({
44
+ * nodes: [{ id: 'a' }, { id: 'b' }],
45
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
46
+ * });
47
+ * getOutEdges(graph, 'a');
48
+ * // => [edge e1]
49
+ * getOutEdges(graph, 'b');
50
+ * // => []
51
+ * ```
52
+ */
6
53
  declare function getOutEdges<E>(graph: Graph<any, E>, nodeId: string): GraphEdge<E>[];
54
+ /**
55
+ * Returns the edge from `sourceId` to `targetId`, or `undefined` if none exists.
56
+ * For undirected graphs, checks both directions.
57
+ *
58
+ * @example
59
+ * ```ts
60
+ * const graph = createGraph({
61
+ * nodes: [{ id: 'a' }, { id: 'b' }],
62
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
63
+ * });
64
+ * getEdgeBetween(graph, 'a', 'b');
65
+ * // => edge e1
66
+ * getEdgeBetween(graph, 'b', 'a');
67
+ * // => undefined (directed graph)
68
+ * ```
69
+ */
7
70
  declare function getEdgeBetween<E>(graph: Graph<any, E>, sourceId: string, targetId: string): GraphEdge<E> | undefined;
71
+ /**
72
+ * Returns direct successor nodes (targets of outgoing edges).
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const graph = createGraph({
77
+ * nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
78
+ * edges: [
79
+ * { id: 'e1', sourceId: 'a', targetId: 'b' },
80
+ * { id: 'e2', sourceId: 'a', targetId: 'c' },
81
+ * ],
82
+ * });
83
+ * getSuccessors(graph, 'a');
84
+ * // => [node b, node c]
85
+ * ```
86
+ */
8
87
  declare function getSuccessors<N>(graph: Graph<N>, nodeId: string): GraphNode<N>[];
88
+ /**
89
+ * Returns direct predecessor nodes (sources of incoming edges).
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * const graph = createGraph({
94
+ * nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
95
+ * edges: [
96
+ * { id: 'e1', sourceId: 'a', targetId: 'c' },
97
+ * { id: 'e2', sourceId: 'b', targetId: 'c' },
98
+ * ],
99
+ * });
100
+ * getPredecessors(graph, 'c');
101
+ * // => [node a, node b]
102
+ * ```
103
+ */
9
104
  declare function getPredecessors<N>(graph: Graph<N>, nodeId: string): GraphNode<N>[];
105
+ /**
106
+ * Returns all neighbor nodes (successors + predecessors).
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * const graph = createGraph({
111
+ * nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
112
+ * edges: [
113
+ * { id: 'e1', sourceId: 'a', targetId: 'b' },
114
+ * { id: 'e2', sourceId: 'c', targetId: 'b' },
115
+ * ],
116
+ * });
117
+ * getNeighbors(graph, 'b');
118
+ * // => [node a, node c]
119
+ * ```
120
+ */
10
121
  declare function getNeighbors<N>(graph: Graph<N>, nodeId: string): GraphNode<N>[];
122
+ /**
123
+ * Returns the total degree of a node (inDegree + outDegree).
124
+ * For undirected graphs, each edge is counted once.
125
+ *
126
+ * @example
127
+ * ```ts
128
+ * const graph = createGraph({
129
+ * nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
130
+ * edges: [
131
+ * { id: 'e1', sourceId: 'a', targetId: 'b' },
132
+ * { id: 'e2', sourceId: 'c', targetId: 'b' },
133
+ * ],
134
+ * });
135
+ * getDegree(graph, 'b'); // => 2
136
+ * getDegree(graph, 'a'); // => 1
137
+ * ```
138
+ */
11
139
  declare function getDegree(graph: Graph, nodeId: string): number;
140
+ /**
141
+ * Returns the in-degree of a node (number of incoming edges).
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * const graph = createGraph({
146
+ * nodes: [{ id: 'a' }, { id: 'b' }],
147
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
148
+ * });
149
+ * getInDegree(graph, 'b'); // => 1
150
+ * getInDegree(graph, 'a'); // => 0
151
+ * ```
152
+ */
12
153
  declare function getInDegree(graph: Graph, nodeId: string): number;
154
+ /**
155
+ * Returns the out-degree of a node (number of outgoing edges).
156
+ *
157
+ * @example
158
+ * ```ts
159
+ * const graph = createGraph({
160
+ * nodes: [{ id: 'a' }, { id: 'b' }],
161
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
162
+ * });
163
+ * getOutDegree(graph, 'a'); // => 1
164
+ * getOutDegree(graph, 'b'); // => 0
165
+ * ```
166
+ */
13
167
  declare function getOutDegree(graph: Graph, nodeId: string): number;
168
+ /**
169
+ * Returns direct children of a node in the hierarchy.
170
+ * Pass `null` to get root-level nodes.
171
+ *
172
+ * @example
173
+ * ```ts
174
+ * const graph = createGraph({
175
+ * nodes: [
176
+ * { id: 'parent' },
177
+ * { id: 'child1', parentId: 'parent' },
178
+ * { id: 'child2', parentId: 'parent' },
179
+ * ],
180
+ * });
181
+ * getChildren(graph, 'parent');
182
+ * // => [node child1, node child2]
183
+ * getChildren(graph, null);
184
+ * // => [node parent]
185
+ * ```
186
+ */
14
187
  declare function getChildren<N>(graph: Graph<N>, nodeId: string | null): GraphNode<N>[];
188
+ /**
189
+ * Returns the parent node in the hierarchy, or `undefined` if root-level.
190
+ *
191
+ * @example
192
+ * ```ts
193
+ * const graph = createGraph({
194
+ * nodes: [
195
+ * { id: 'parent' },
196
+ * { id: 'child', parentId: 'parent' },
197
+ * ],
198
+ * });
199
+ * getParent(graph, 'child');
200
+ * // => node parent
201
+ * getParent(graph, 'parent');
202
+ * // => undefined
203
+ * ```
204
+ */
15
205
  declare function getParent<N>(graph: Graph<N>, nodeId: string): GraphNode<N> | undefined;
206
+ /**
207
+ * Returns all ancestors from the node up to the root (nearest parent first).
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * const graph = createGraph({
212
+ * nodes: [
213
+ * { id: 'root' },
214
+ * { id: 'mid', parentId: 'root' },
215
+ * { id: 'leaf', parentId: 'mid' },
216
+ * ],
217
+ * });
218
+ * getAncestors(graph, 'leaf');
219
+ * // => [node mid, node root]
220
+ * ```
221
+ */
16
222
  declare function getAncestors<N>(graph: Graph<N>, nodeId: string): GraphNode<N>[];
223
+ /**
224
+ * Returns all descendants recursively (depth-first).
225
+ *
226
+ * @example
227
+ * ```ts
228
+ * const graph = createGraph({
229
+ * nodes: [
230
+ * { id: 'root' },
231
+ * { id: 'child', parentId: 'root' },
232
+ * { id: 'grandchild', parentId: 'child' },
233
+ * ],
234
+ * });
235
+ * getDescendants(graph, 'root');
236
+ * // => [node child, node grandchild]
237
+ * ```
238
+ */
17
239
  declare function getDescendants<N>(graph: Graph<N>, nodeId: string): GraphNode<N>[];
240
+ /**
241
+ * Returns all root nodes (nodes with no parent, i.e. `parentId === null`).
242
+ *
243
+ * @example
244
+ * ```ts
245
+ * const graph = createGraph({
246
+ * nodes: [
247
+ * { id: 'root1' },
248
+ * { id: 'root2' },
249
+ * { id: 'child', parentId: 'root1' },
250
+ * ],
251
+ * });
252
+ * getRoots(graph);
253
+ * // => [node root1, node root2]
254
+ * ```
255
+ */
18
256
  declare function getRoots<N>(graph: Graph<N>): GraphNode<N>[];
19
- /** Whether a node has children (is a compound/group node). */
257
+ /**
258
+ * Whether a node has children (is a compound/group node).
259
+ *
260
+ * @example
261
+ * ```ts
262
+ * const graph = createGraph({
263
+ * nodes: [
264
+ * { id: 'parent' },
265
+ * { id: 'child', parentId: 'parent' },
266
+ * ],
267
+ * });
268
+ * isCompound(graph, 'parent'); // => true
269
+ * isCompound(graph, 'child'); // => false
270
+ * ```
271
+ */
20
272
  declare function isCompound(graph: Graph, nodeId: string): boolean;
21
- /** Whether a node has no children (is a leaf/atomic node). */
273
+ /**
274
+ * Whether a node has no children (is a leaf/atomic node).
275
+ *
276
+ * @example
277
+ * ```ts
278
+ * const graph = createGraph({
279
+ * nodes: [
280
+ * { id: 'parent' },
281
+ * { id: 'child', parentId: 'parent' },
282
+ * ],
283
+ * });
284
+ * isLeaf(graph, 'child'); // => true
285
+ * isLeaf(graph, 'parent'); // => false
286
+ * ```
287
+ */
22
288
  declare function isLeaf(graph: Graph, nodeId: string): boolean;
23
- /** Depth of a node in the hierarchy (root = 0). */
289
+ /**
290
+ * Depth of a node in the hierarchy (root = 0).
291
+ * Returns -1 if the node is not found.
292
+ *
293
+ * @example
294
+ * ```ts
295
+ * const graph = createGraph({
296
+ * nodes: [
297
+ * { id: 'root' },
298
+ * { id: 'child', parentId: 'root' },
299
+ * { id: 'grandchild', parentId: 'child' },
300
+ * ],
301
+ * });
302
+ * getDepth(graph, 'root'); // => 0
303
+ * getDepth(graph, 'child'); // => 1
304
+ * getDepth(graph, 'grandchild'); // => 2
305
+ * ```
306
+ */
24
307
  declare function getDepth(graph: Graph, nodeId: string): number;
25
- /** Sibling nodes (same parentId, excluding the node itself). */
308
+ /**
309
+ * Sibling nodes (same parentId, excluding the node itself).
310
+ *
311
+ * @example
312
+ * ```ts
313
+ * const graph = createGraph({
314
+ * nodes: [
315
+ * { id: 'parent' },
316
+ * { id: 'a', parentId: 'parent' },
317
+ * { id: 'b', parentId: 'parent' },
318
+ * { id: 'c', parentId: 'parent' },
319
+ * ],
320
+ * });
321
+ * getSiblings(graph, 'a');
322
+ * // => [node b, node c]
323
+ * ```
324
+ */
26
325
  declare function getSiblings<N>(graph: Graph<N>, nodeId: string): GraphNode<N>[];
27
326
  /**
28
- * Least Common Ancestor deepest proper ancestor of all given nodes.
327
+ * Least Common Ancestor -- deepest proper ancestor of all given nodes.
29
328
  * A proper ancestor excludes the input nodes themselves.
329
+ *
330
+ * @example
331
+ * ```ts
332
+ * const graph = createGraph({
333
+ * nodes: [
334
+ * { id: 'root' },
335
+ * { id: 'a', parentId: 'root' },
336
+ * { id: 'b', parentId: 'root' },
337
+ * { id: 'a1', parentId: 'a' },
338
+ * ],
339
+ * });
340
+ * getLCA(graph, 'a1', 'b');
341
+ * // => node root
342
+ * getLCA(graph, 'a', 'b');
343
+ * // => node root
344
+ * ```
30
345
  */
31
346
  declare function getLCA<N>(graph: Graph<N>, ...nodeIds: string[]): GraphNode<N> | undefined;
32
- /** Nodes with no incoming edges (inDegree 0). */
347
+ /**
348
+ * Returns a map of nodeId → shortest-path distance for all sibling nodes
349
+ * (same parentId). Distance is measured from the parent's `initialNodeId`
350
+ * (or `graph.initialNodeId` for root-level nodes).
351
+ *
352
+ * Only follows edges between siblings. Unreachable siblings are omitted.
353
+ *
354
+ * @example Root-level nodes (uses `graph.initialNodeId`):
355
+ * ```ts
356
+ * const graph = createGraph({
357
+ * initialNodeId: 'a',
358
+ * nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
359
+ * edges: [
360
+ * { id: 'e1', sourceId: 'a', targetId: 'b' },
361
+ * { id: 'e2', sourceId: 'b', targetId: 'c' },
362
+ * ],
363
+ * });
364
+ * getRelativeDistanceMap(graph, null);
365
+ * // => { a: 0, b: 1, c: 2 }
366
+ * ```
367
+ *
368
+ * @example Nested nodes (uses parent's `initialNodeId`):
369
+ * ```ts
370
+ * const graph = createGraph({
371
+ * nodes: [
372
+ * { id: 'parent', initialNodeId: 's1' },
373
+ * { id: 's1', parentId: 'parent' },
374
+ * { id: 's2', parentId: 'parent' },
375
+ * { id: 's3', parentId: 'parent' },
376
+ * ],
377
+ * edges: [
378
+ * { id: 'e1', sourceId: 's1', targetId: 's2' },
379
+ * { id: 'e2', sourceId: 's2', targetId: 's3' },
380
+ * ],
381
+ * });
382
+ * getRelativeDistanceMap(graph, 'parent');
383
+ * // => { s1: 0, s2: 1, s3: 2 }
384
+ * ```
385
+ */
386
+ declare function getRelativeDistanceMap(graph: Graph, parentId: string | null): Record<string, number>;
387
+ /**
388
+ * Returns the shortest-path distance of a node from its parent's initial node.
389
+ * Automatically scopes to the node's sibling group (same `parentId`).
390
+ *
391
+ * Returns `undefined` if the node is not found or unreachable.
392
+ *
393
+ * @example
394
+ * ```ts
395
+ * const graph = createGraph({
396
+ * initialNodeId: 'a',
397
+ * nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
398
+ * edges: [
399
+ * { id: 'e1', sourceId: 'a', targetId: 'b' },
400
+ * { id: 'e2', sourceId: 'b', targetId: 'c' },
401
+ * ],
402
+ * });
403
+ * getRelativeDistance(graph, 'a'); // => 0
404
+ * getRelativeDistance(graph, 'b'); // => 1
405
+ * getRelativeDistance(graph, 'c'); // => 2
406
+ * ```
407
+ *
408
+ * @example Nested nodes:
409
+ * ```ts
410
+ * const graph = createGraph({
411
+ * nodes: [
412
+ * { id: 'parent', initialNodeId: 's1' },
413
+ * { id: 's1', parentId: 'parent' },
414
+ * { id: 's2', parentId: 'parent' },
415
+ * ],
416
+ * edges: [{ id: 'e1', sourceId: 's1', targetId: 's2' }],
417
+ * });
418
+ * getRelativeDistance(graph, 's1'); // => 0
419
+ * getRelativeDistance(graph, 's2'); // => 1
420
+ * ```
421
+ */
422
+ declare function getRelativeDistance(graph: Graph, nodeId: string): number | undefined;
423
+ /**
424
+ * Nodes with no incoming edges (inDegree 0).
425
+ *
426
+ * @example
427
+ * ```ts
428
+ * const graph = createGraph({
429
+ * nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
430
+ * edges: [
431
+ * { id: 'e1', sourceId: 'a', targetId: 'b' },
432
+ * { id: 'e2', sourceId: 'b', targetId: 'c' },
433
+ * ],
434
+ * });
435
+ * getSources(graph);
436
+ * // => [node a]
437
+ * ```
438
+ */
33
439
  declare function getSources<N>(graph: Graph<N>): GraphNode<N>[];
34
- /** Nodes with no outgoing edges (outDegree 0). */
440
+ /**
441
+ * Nodes with no outgoing edges (outDegree 0).
442
+ *
443
+ * @example
444
+ * ```ts
445
+ * const graph = createGraph({
446
+ * nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }],
447
+ * edges: [
448
+ * { id: 'e1', sourceId: 'a', targetId: 'b' },
449
+ * { id: 'e2', sourceId: 'b', targetId: 'c' },
450
+ * ],
451
+ * });
452
+ * getSinks(graph);
453
+ * // => [node c]
454
+ * ```
455
+ */
35
456
  declare function getSinks<N>(graph: Graph<N>): GraphNode<N>[];
36
457
  //#endregion
37
- export { getAncestors, getChildren, getDegree, getDepth, getDescendants, getEdgeBetween, getEdgesOf, getInDegree, getInEdges, getLCA, getNeighbors, getOutDegree, getOutEdges, getParent, getPredecessors, getRoots, getSiblings, getSinks, getSources, getSuccessors, isCompound, isLeaf };
458
+ export { getAncestors, getChildren, getDegree, getDepth, getDescendants, getEdgeBetween, getEdgesOf, getInDegree, getInEdges, getLCA, getNeighbors, getOutDegree, getOutEdges, getParent, getPredecessors, getRelativeDistance, getRelativeDistanceMap, getRoots, getSiblings, getSinks, getSources, getSuccessors, isCompound, isLeaf };