@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
@@ -0,0 +1,71 @@
1
+ //#region src/formats/edge-list/index.ts
2
+ /**
3
+ * Converts a graph to an edge list of `[source, target]` tuples.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { createGraph, toEdgeList } from '@statelyai/graph';
8
+ *
9
+ * const graph = createGraph({
10
+ * nodes: { a: {}, b: {}, c: {} },
11
+ * edges: [{ source: 'a', target: 'b' }, { source: 'b', target: 'c' }],
12
+ * });
13
+ *
14
+ * toEdgeList(graph);
15
+ * // [['a', 'b'], ['b', 'c']]
16
+ * ```
17
+ */
18
+ function toEdgeList(graph) {
19
+ return graph.edges.map((e) => [e.sourceId, e.targetId]);
20
+ }
21
+ /**
22
+ * Parses an edge list of `[source, target]` tuples into a graph.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * import { fromEdgeList } from '@statelyai/graph';
27
+ *
28
+ * const graph = fromEdgeList([
29
+ * ['a', 'b'],
30
+ * ['b', 'c'],
31
+ * ]);
32
+ *
33
+ * graph.nodes; // [{id: 'a', ...}, {id: 'b', ...}, {id: 'c', ...}]
34
+ * graph.edges; // [{sourceId: 'a', targetId: 'b', ...}, ...]
35
+ * ```
36
+ */
37
+ function fromEdgeList(edges, options) {
38
+ const directed = options?.directed ?? true;
39
+ const nodeIds = /* @__PURE__ */ new Set();
40
+ for (const [source, target] of edges) {
41
+ nodeIds.add(source);
42
+ nodeIds.add(target);
43
+ }
44
+ const nodes = [...nodeIds].map((id) => ({
45
+ type: "node",
46
+ id,
47
+ parentId: null,
48
+ initialNodeId: null,
49
+ label: "",
50
+ data: void 0
51
+ }));
52
+ const edgeObjects = edges.map(([sourceId, targetId], i) => ({
53
+ type: "edge",
54
+ id: `e${i}`,
55
+ sourceId,
56
+ targetId,
57
+ label: "",
58
+ data: void 0
59
+ }));
60
+ return {
61
+ id: options?.id ?? "",
62
+ type: directed ? "directed" : "undirected",
63
+ initialNodeId: null,
64
+ nodes,
65
+ edges: edgeObjects,
66
+ data: void 0
67
+ };
68
+ }
69
+
70
+ //#endregion
71
+ export { toEdgeList as n, fromEdgeList as t };
@@ -0,0 +1,44 @@
1
+ import { c as Graph } from "../../types-BDXC1O5b.mjs";
2
+
3
+ //#region src/formats/adjacency-list/index.d.ts
4
+
5
+ /**
6
+ * Converts a graph to an adjacency list representation.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { createGraph, toAdjacencyList } from '@statelyai/graph';
11
+ *
12
+ * const graph = createGraph({
13
+ * nodes: { a: {}, b: {}, c: {} },
14
+ * edges: [{ source: 'a', target: 'b' }, { source: 'a', target: 'c' }],
15
+ * });
16
+ *
17
+ * toAdjacencyList(graph);
18
+ * // { a: ['b', 'c'], b: [], c: [] }
19
+ * ```
20
+ */
21
+ declare function toAdjacencyList(graph: Graph): Record<string, string[]>;
22
+ /**
23
+ * Parses an adjacency list into a graph.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * import { fromAdjacencyList } from '@statelyai/graph';
28
+ *
29
+ * const graph = fromAdjacencyList({
30
+ * a: ['b', 'c'],
31
+ * b: ['c'],
32
+ * c: [],
33
+ * });
34
+ *
35
+ * graph.nodes; // [{id: 'a', ...}, {id: 'b', ...}, {id: 'c', ...}]
36
+ * graph.edges; // [{sourceId: 'a', targetId: 'b', ...}, ...]
37
+ * ```
38
+ */
39
+ declare function fromAdjacencyList(adj: Record<string, string[]>, options?: {
40
+ directed?: boolean;
41
+ id?: string;
42
+ }): Graph;
43
+ //#endregion
44
+ export { fromAdjacencyList, toAdjacencyList };
@@ -0,0 +1,3 @@
1
+ import { n as toAdjacencyList, t as fromAdjacencyList } from "../../adjacency-list-ITO40kmn.mjs";
2
+
3
+ export { fromAdjacencyList, toAdjacencyList };
@@ -0,0 +1,61 @@
1
+ import { c as Graph, f as GraphFormatConverter } from "../../types-BDXC1O5b.mjs";
2
+
3
+ //#region src/formats/converter/index.d.ts
4
+
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
+ declare function createFormatConverter<TSerial>(to: (graph: Graph) => TSerial, from: (input: TSerial) => Graph): GraphFormatConverter<TSerial>;
22
+ /**
23
+ * Bidirectional converter for adjacency-list format (`Record<string, string[]>`).
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * import { adjacencyListConverter, createGraph } from '@statelyai/graph';
28
+ *
29
+ * const graph = createGraph({
30
+ * nodes: { a: {}, b: {} },
31
+ * edges: [{ source: 'a', target: 'b' }],
32
+ * });
33
+ *
34
+ * const adj = adjacencyListConverter.to(graph);
35
+ * // { a: ['b'], b: [] }
36
+ *
37
+ * const roundTripped = adjacencyListConverter.from(adj);
38
+ * ```
39
+ */
40
+ declare const adjacencyListConverter: GraphFormatConverter<Record<string, string[]>>;
41
+ /**
42
+ * Bidirectional converter for edge-list format (`[source, target][]`).
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * import { edgeListConverter, createGraph } from '@statelyai/graph';
47
+ *
48
+ * const graph = createGraph({
49
+ * nodes: { a: {}, b: {} },
50
+ * edges: [{ source: 'a', target: 'b' }],
51
+ * });
52
+ *
53
+ * const edges = edgeListConverter.to(graph);
54
+ * // [['a', 'b']]
55
+ *
56
+ * const roundTripped = edgeListConverter.from(edges);
57
+ * ```
58
+ */
59
+ declare const edgeListConverter: GraphFormatConverter<[string, string][]>;
60
+ //#endregion
61
+ export { adjacencyListConverter, createFormatConverter, edgeListConverter };
@@ -0,0 +1,3 @@
1
+ import { n as createFormatConverter, r as edgeListConverter, t as adjacencyListConverter } from "../../converter-CchokMDg.mjs";
2
+
3
+ export { adjacencyListConverter, createFormatConverter, edgeListConverter };
@@ -0,0 +1,83 @@
1
+ import { c as Graph, f as GraphFormatConverter } from "../../types-BDXC1O5b.mjs";
2
+
3
+ //#region src/formats/cytoscape/index.d.ts
4
+ interface CytoscapeNode {
5
+ data: {
6
+ id: string;
7
+ parent?: string;
8
+ [key: string]: any;
9
+ };
10
+ position?: {
11
+ x: number;
12
+ y: number;
13
+ };
14
+ }
15
+ interface CytoscapeEdge {
16
+ data: {
17
+ id: string;
18
+ source: string;
19
+ target: string;
20
+ [key: string]: any;
21
+ };
22
+ }
23
+ interface CytoscapeJSON {
24
+ data?: Record<string, any>;
25
+ elements: {
26
+ nodes: CytoscapeNode[];
27
+ edges: CytoscapeEdge[];
28
+ };
29
+ }
30
+ /**
31
+ * Converts a graph to Cytoscape.js JSON format.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * import { createGraph } from '@statelyai/graph';
36
+ * import { toCytoscapeJSON } from '@statelyai/graph/formats/cytoscape';
37
+ *
38
+ * const graph = createGraph({
39
+ * nodes: [{ id: 'a' }, { id: 'b' }],
40
+ * edges: [{ id: 'e0', sourceId: 'a', targetId: 'b' }],
41
+ * });
42
+ *
43
+ * const cyto = toCytoscapeJSON(graph);
44
+ * // { elements: { nodes: [...], edges: [...] } }
45
+ * ```
46
+ */
47
+ declare function toCytoscapeJSON(graph: Graph): CytoscapeJSON;
48
+ /**
49
+ * Parses a Cytoscape.js JSON object into a graph.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * import { fromCytoscapeJSON } from '@statelyai/graph/formats/cytoscape';
54
+ *
55
+ * const graph = fromCytoscapeJSON({
56
+ * elements: {
57
+ * nodes: [{ data: { id: 'a' } }, { data: { id: 'b' } }],
58
+ * edges: [{ data: { id: 'e0', source: 'a', target: 'b' } }],
59
+ * },
60
+ * });
61
+ * ```
62
+ */
63
+ declare function fromCytoscapeJSON(cyto: CytoscapeJSON): Graph;
64
+ /**
65
+ * Bidirectional converter for Cytoscape.js JSON format.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * import { createGraph } from '@statelyai/graph';
70
+ * import { cytoscapeConverter } from '@statelyai/graph/formats/cytoscape';
71
+ *
72
+ * const graph = createGraph({
73
+ * nodes: [{ id: 'a' }, { id: 'b' }],
74
+ * edges: [{ id: 'e0', sourceId: 'a', targetId: 'b' }],
75
+ * });
76
+ *
77
+ * const cyto = cytoscapeConverter.to(graph);
78
+ * const roundTripped = cytoscapeConverter.from(cyto);
79
+ * ```
80
+ */
81
+ declare const cytoscapeConverter: GraphFormatConverter<CytoscapeJSON>;
82
+ //#endregion
83
+ export { CytoscapeEdge, CytoscapeJSON, CytoscapeNode, cytoscapeConverter, fromCytoscapeJSON, toCytoscapeJSON };
@@ -0,0 +1,135 @@
1
+ import { n as createFormatConverter } from "../../converter-CchokMDg.mjs";
2
+
3
+ //#region src/formats/cytoscape/index.ts
4
+ /**
5
+ * Converts a graph to Cytoscape.js JSON format.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { createGraph } from '@statelyai/graph';
10
+ * import { toCytoscapeJSON } from '@statelyai/graph/formats/cytoscape';
11
+ *
12
+ * const graph = createGraph({
13
+ * nodes: [{ id: 'a' }, { id: 'b' }],
14
+ * edges: [{ id: 'e0', sourceId: 'a', targetId: 'b' }],
15
+ * });
16
+ *
17
+ * const cyto = toCytoscapeJSON(graph);
18
+ * // { elements: { nodes: [...], edges: [...] } }
19
+ * ```
20
+ */
21
+ function toCytoscapeJSON(graph) {
22
+ const graphData = {};
23
+ if (graph.id) graphData.id = graph.id;
24
+ graphData.type = graph.type;
25
+ if (graph.initialNodeId !== null) graphData.initialNodeId = graph.initialNodeId;
26
+ if (graph.data !== void 0) graphData.graphData = graph.data;
27
+ if (graph.direction) graphData.direction = graph.direction;
28
+ return {
29
+ ...Object.keys(graphData).length > 0 && { data: graphData },
30
+ elements: {
31
+ nodes: graph.nodes.map((n) => {
32
+ const data = { id: n.id };
33
+ if (n.parentId !== null) data.parent = n.parentId;
34
+ if (n.label) data.label = n.label;
35
+ if (n.initialNodeId !== null) data.initialNodeId = n.initialNodeId;
36
+ if (n.data !== void 0) data.nodeData = n.data;
37
+ if (n.width !== void 0) data.width = n.width;
38
+ if (n.height !== void 0) data.height = n.height;
39
+ if (n.shape) data.shape = n.shape;
40
+ if (n.color) data.color = n.color;
41
+ const node = { data };
42
+ if (n.x !== void 0 && n.y !== void 0) node.position = {
43
+ x: n.x,
44
+ y: n.y
45
+ };
46
+ return node;
47
+ }),
48
+ edges: graph.edges.map((e) => {
49
+ const data = {
50
+ id: e.id,
51
+ source: e.sourceId,
52
+ target: e.targetId
53
+ };
54
+ if (e.label) data.label = e.label;
55
+ if (e.data !== void 0) data.edgeData = e.data;
56
+ if (e.color) data.color = e.color;
57
+ return { data };
58
+ })
59
+ }
60
+ };
61
+ }
62
+ /**
63
+ * Parses a Cytoscape.js JSON object into a graph.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * import { fromCytoscapeJSON } from '@statelyai/graph/formats/cytoscape';
68
+ *
69
+ * const graph = fromCytoscapeJSON({
70
+ * elements: {
71
+ * nodes: [{ data: { id: 'a' } }, { data: { id: 'b' } }],
72
+ * edges: [{ data: { id: 'e0', source: 'a', target: 'b' } }],
73
+ * },
74
+ * });
75
+ * ```
76
+ */
77
+ function fromCytoscapeJSON(cyto) {
78
+ if (!cyto || typeof cyto !== "object") throw new Error("Cytoscape: expected an object");
79
+ if (!cyto.elements || typeof cyto.elements !== "object") throw new Error("Cytoscape: missing \"elements\" property");
80
+ if (!Array.isArray(cyto.elements.nodes)) throw new Error("Cytoscape: \"elements.nodes\" must be an array");
81
+ if (!Array.isArray(cyto.elements.edges)) throw new Error("Cytoscape: \"elements.edges\" must be an array");
82
+ return {
83
+ id: cyto.data?.id ?? "",
84
+ type: cyto.data?.type === "undirected" ? "undirected" : "directed",
85
+ initialNodeId: cyto.data?.initialNodeId ?? null,
86
+ data: cyto.data?.graphData,
87
+ ...cyto.data?.direction && { direction: cyto.data.direction },
88
+ nodes: cyto.elements.nodes.map((n) => ({
89
+ type: "node",
90
+ id: n.data.id,
91
+ parentId: n.data.parent ?? null,
92
+ initialNodeId: n.data.initialNodeId ?? null,
93
+ label: n.data.label ?? "",
94
+ data: n.data.nodeData,
95
+ ...n.position && {
96
+ x: n.position.x,
97
+ y: n.position.y
98
+ },
99
+ ...n.data.width !== void 0 && { width: n.data.width },
100
+ ...n.data.height !== void 0 && { height: n.data.height },
101
+ ...n.data.shape && { shape: n.data.shape },
102
+ ...n.data.color && { color: n.data.color }
103
+ })),
104
+ edges: cyto.elements.edges.map((e, i) => ({
105
+ type: "edge",
106
+ id: e.data.id ?? `e${i}`,
107
+ sourceId: e.data.source,
108
+ targetId: e.data.target,
109
+ label: e.data.label ?? "",
110
+ data: e.data.edgeData,
111
+ ...e.data.color && { color: e.data.color }
112
+ }))
113
+ };
114
+ }
115
+ /**
116
+ * Bidirectional converter for Cytoscape.js JSON format.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * import { createGraph } from '@statelyai/graph';
121
+ * import { cytoscapeConverter } from '@statelyai/graph/formats/cytoscape';
122
+ *
123
+ * const graph = createGraph({
124
+ * nodes: [{ id: 'a' }, { id: 'b' }],
125
+ * edges: [{ id: 'e0', sourceId: 'a', targetId: 'b' }],
126
+ * });
127
+ *
128
+ * const cyto = cytoscapeConverter.to(graph);
129
+ * const roundTripped = cytoscapeConverter.from(cyto);
130
+ * ```
131
+ */
132
+ const cytoscapeConverter = createFormatConverter(toCytoscapeJSON, fromCytoscapeJSON);
133
+
134
+ //#endregion
135
+ export { cytoscapeConverter, fromCytoscapeJSON, toCytoscapeJSON };
@@ -0,0 +1,68 @@
1
+ import { c as Graph, f as GraphFormatConverter } from "../../types-BDXC1O5b.mjs";
2
+
3
+ //#region src/formats/d3/index.d.ts
4
+ interface D3Node {
5
+ id: string;
6
+ [key: string]: any;
7
+ }
8
+ interface D3Link {
9
+ source: string;
10
+ target: string;
11
+ [key: string]: any;
12
+ }
13
+ interface D3Graph {
14
+ nodes: D3Node[];
15
+ links: D3Link[];
16
+ }
17
+ /**
18
+ * Converts a graph to D3.js force-directed format.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * import { createGraph } from '@statelyai/graph';
23
+ * import { toD3Graph } from '@statelyai/graph/formats/d3';
24
+ *
25
+ * const graph = createGraph({
26
+ * nodes: [{ id: 'a' }, { id: 'b' }],
27
+ * edges: [{ id: 'e0', sourceId: 'a', targetId: 'b' }],
28
+ * });
29
+ *
30
+ * const d3 = toD3Graph(graph);
31
+ * // { nodes: [{ id: 'a' }, { id: 'b' }], links: [{ source: 'a', target: 'b' }] }
32
+ * ```
33
+ */
34
+ declare function toD3Graph(graph: Graph): D3Graph;
35
+ /**
36
+ * Parses a D3.js force-directed JSON object into a graph.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * import { fromD3Graph } from '@statelyai/graph/formats/d3';
41
+ *
42
+ * const graph = fromD3Graph({
43
+ * nodes: [{ id: 'a' }, { id: 'b' }],
44
+ * links: [{ source: 'a', target: 'b' }],
45
+ * });
46
+ * ```
47
+ */
48
+ declare function fromD3Graph(d3: D3Graph): Graph;
49
+ /**
50
+ * Bidirectional converter for D3.js force-directed JSON format.
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * import { createGraph } from '@statelyai/graph';
55
+ * import { d3Converter } from '@statelyai/graph/formats/d3';
56
+ *
57
+ * const graph = createGraph({
58
+ * nodes: [{ id: 'a' }, { id: 'b' }],
59
+ * edges: [{ id: 'e0', sourceId: 'a', targetId: 'b' }],
60
+ * });
61
+ *
62
+ * const d3 = d3Converter.to(graph);
63
+ * const roundTripped = d3Converter.from(d3);
64
+ * ```
65
+ */
66
+ declare const d3Converter: GraphFormatConverter<D3Graph>;
67
+ //#endregion
68
+ export { D3Graph, D3Link, D3Node, d3Converter, fromD3Graph, toD3Graph };
@@ -0,0 +1,111 @@
1
+ import { n as createFormatConverter } from "../../converter-CchokMDg.mjs";
2
+
3
+ //#region src/formats/d3/index.ts
4
+ /**
5
+ * Converts a graph to D3.js force-directed format.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { createGraph } from '@statelyai/graph';
10
+ * import { toD3Graph } from '@statelyai/graph/formats/d3';
11
+ *
12
+ * const graph = createGraph({
13
+ * nodes: [{ id: 'a' }, { id: 'b' }],
14
+ * edges: [{ id: 'e0', sourceId: 'a', targetId: 'b' }],
15
+ * });
16
+ *
17
+ * const d3 = toD3Graph(graph);
18
+ * // { nodes: [{ id: 'a' }, { id: 'b' }], links: [{ source: 'a', target: 'b' }] }
19
+ * ```
20
+ */
21
+ function toD3Graph(graph) {
22
+ return {
23
+ nodes: graph.nodes.map((n) => {
24
+ const node = { id: n.id };
25
+ if (n.label) node.label = n.label;
26
+ if (n.data !== void 0) node.data = n.data;
27
+ if (n.x !== void 0) node.x = n.x;
28
+ if (n.y !== void 0) node.y = n.y;
29
+ if (n.color) node.color = n.color;
30
+ if (n.shape) node.shape = n.shape;
31
+ return node;
32
+ }),
33
+ links: graph.edges.map((e) => {
34
+ const link = {
35
+ source: e.sourceId,
36
+ target: e.targetId
37
+ };
38
+ if (e.id) link.id = e.id;
39
+ if (e.label) link.label = e.label;
40
+ if (e.data !== void 0) link.data = e.data;
41
+ if (e.color) link.color = e.color;
42
+ return link;
43
+ })
44
+ };
45
+ }
46
+ /**
47
+ * Parses a D3.js force-directed JSON object into a graph.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * import { fromD3Graph } from '@statelyai/graph/formats/d3';
52
+ *
53
+ * const graph = fromD3Graph({
54
+ * nodes: [{ id: 'a' }, { id: 'b' }],
55
+ * links: [{ source: 'a', target: 'b' }],
56
+ * });
57
+ * ```
58
+ */
59
+ function fromD3Graph(d3) {
60
+ if (!d3 || typeof d3 !== "object") throw new Error("D3: expected an object");
61
+ if (!Array.isArray(d3.nodes)) throw new Error("D3: \"nodes\" must be an array");
62
+ if (!Array.isArray(d3.links)) throw new Error("D3: \"links\" must be an array");
63
+ return {
64
+ id: "",
65
+ type: "directed",
66
+ initialNodeId: null,
67
+ data: void 0,
68
+ nodes: d3.nodes.map((n) => ({
69
+ type: "node",
70
+ id: n.id,
71
+ parentId: null,
72
+ initialNodeId: null,
73
+ label: n.label ?? "",
74
+ data: n.data,
75
+ ...n.x !== void 0 && { x: n.x },
76
+ ...n.y !== void 0 && { y: n.y },
77
+ ...n.color && { color: n.color },
78
+ ...n.shape && { shape: n.shape }
79
+ })),
80
+ edges: d3.links.map((l, i) => ({
81
+ type: "edge",
82
+ id: l.id ?? `e${i}`,
83
+ sourceId: typeof l.source === "string" ? l.source : l.source.id,
84
+ targetId: typeof l.target === "string" ? l.target : l.target.id,
85
+ label: l.label ?? "",
86
+ data: l.data,
87
+ ...l.color && { color: l.color }
88
+ }))
89
+ };
90
+ }
91
+ /**
92
+ * Bidirectional converter for D3.js force-directed JSON format.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * import { createGraph } from '@statelyai/graph';
97
+ * import { d3Converter } from '@statelyai/graph/formats/d3';
98
+ *
99
+ * const graph = createGraph({
100
+ * nodes: [{ id: 'a' }, { id: 'b' }],
101
+ * edges: [{ id: 'e0', sourceId: 'a', targetId: 'b' }],
102
+ * });
103
+ *
104
+ * const d3 = d3Converter.to(graph);
105
+ * const roundTripped = d3Converter.from(d3);
106
+ * ```
107
+ */
108
+ const d3Converter = createFormatConverter(toD3Graph, fromD3Graph);
109
+
110
+ //#endregion
111
+ export { d3Converter, fromD3Graph, toD3Graph };
@@ -0,0 +1,63 @@
1
+ import { c as Graph, f as GraphFormatConverter } from "../../types-BDXC1O5b.mjs";
2
+
3
+ //#region src/formats/dot/index.d.ts
4
+
5
+ /**
6
+ * Converts a graph to a DOT (Graphviz) format string.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { createGraph, toDOT } from '@statelyai/graph';
11
+ *
12
+ * const graph = createGraph({
13
+ * nodes: { a: {}, b: {} },
14
+ * edges: [{ source: 'a', target: 'b' }],
15
+ * });
16
+ *
17
+ * const dot = toDOT(graph);
18
+ * // digraph "" {
19
+ * // a;
20
+ * // b;
21
+ * // a -> b;
22
+ * // }
23
+ * ```
24
+ */
25
+ declare function toDOT(graph: Graph): string;
26
+ /**
27
+ * Parses a DOT (Graphviz) format string into a graph.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import { fromDOT } from '@statelyai/graph';
32
+ *
33
+ * const graph = fromDOT(`
34
+ * digraph {
35
+ * a -> b;
36
+ * b -> c;
37
+ * }
38
+ * `);
39
+ *
40
+ * graph.nodes; // [{id: 'a', ...}, {id: 'b', ...}, {id: 'c', ...}]
41
+ * graph.edges; // [{sourceId: 'a', targetId: 'b', ...}, ...]
42
+ * ```
43
+ */
44
+ declare function fromDOT(dot: string): Graph;
45
+ /**
46
+ * Bidirectional converter for DOT (Graphviz) format.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * import { dotConverter, createGraph } from '@statelyai/graph';
51
+ *
52
+ * const graph = createGraph({
53
+ * nodes: { a: {}, b: {} },
54
+ * edges: [{ source: 'a', target: 'b' }],
55
+ * });
56
+ *
57
+ * const dot = dotConverter.to(graph);
58
+ * const roundTripped = dotConverter.from(dot);
59
+ * ```
60
+ */
61
+ declare const dotConverter: GraphFormatConverter<string>;
62
+ //#endregion
63
+ export { dotConverter, fromDOT, toDOT };