@canmingir/link 1.2.10 → 1.2.11

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.
@@ -1,111 +0,0 @@
1
- export function assertLinkedGraph(data) {
2
- if (!data || typeof data !== "object") {
3
- throw new Error(
4
- "FlowChart expected a linked graph object: { nodes, roots? }."
5
- );
6
- }
7
- const { nodes, roots } = data;
8
- if (!nodes || typeof nodes !== "object") {
9
- throw new Error("FlowChart expected data.nodes to be an object.");
10
- }
11
-
12
- let useRoots = Array.isArray(roots) ? [...roots] : null;
13
- if (!useRoots || useRoots.length === 0) {
14
- useRoots = Object.keys(nodes).filter((id) => !nodes[id]?.previous);
15
- }
16
- if (useRoots.length === 0) {
17
- const first = Object.keys(nodes)[0];
18
- if (first) useRoots = [first];
19
- }
20
-
21
- for (const r of useRoots) {
22
- if (!nodes[r]) throw new Error(`Root id "${r}" not found in data.nodes.`);
23
- }
24
-
25
- return { nodesById: nodes, roots: useRoots };
26
- }
27
-
28
- export function buildTreeFromLinked(rootId, nodesById) {
29
- if (!rootId || !nodesById?.[rootId]) return null;
30
- const seen = new Set();
31
-
32
- const cloneNode = (node) => {
33
- if (!node) return null;
34
- const { next, previous, children, ...rest } = node;
35
- return { ...rest, id: node.id, previous, next, children: [] };
36
- };
37
-
38
- const dfs = (id) => {
39
- if (!id || seen.has(id) || !nodesById[id]) return null;
40
- seen.add(id);
41
-
42
- const node = nodesById[id];
43
- const out = cloneNode(node);
44
-
45
- const nextArr = Array.isArray(node.next)
46
- ? node.next
47
- : node.next != null
48
- ? [node.next]
49
- : [];
50
-
51
- for (const nxt of nextArr) {
52
- const nextId = typeof nxt === "string" ? nxt : nxt?.id;
53
- if (!nextId || !nodesById[nextId]) continue;
54
-
55
- const target = nodesById[nextId];
56
- if (target.previous == null || target.previous === id) {
57
- const built = dfs(nextId);
58
- if (built) out.children.push(built);
59
- }
60
- }
61
- return out;
62
- };
63
-
64
- return dfs(rootId);
65
- }
66
-
67
- export const getContentParts = (n) => {
68
- const entries = Object.entries(n).filter(
69
- ([key]) =>
70
- key !== "children" && key !== "id" && key !== "previous" && key !== "next"
71
- );
72
-
73
- if (entries.length === 0) {
74
- return {
75
- title: "(empty)",
76
- subtitle: null,
77
- metaEntries: [],
78
- };
79
- }
80
-
81
- const preferredTitleKeys = ["label", "title", "name"];
82
- const titleEntry =
83
- entries.find(([key]) => preferredTitleKeys.includes(key)) || entries[0];
84
- const [titleKey, rawTitle] = titleEntry;
85
- const title = String(rawTitle);
86
- let remaining = entries.filter(([key]) => key !== titleKey);
87
-
88
- const preferredSubtitleKeys = ["description", "role", "type", "status"];
89
- const subtitleEntry =
90
- remaining.find(([key]) => preferredSubtitleKeys.includes(key)) || null;
91
-
92
- let subtitle = null;
93
- if (subtitleEntry) {
94
- const [subtitleKey, raw] = subtitleEntry;
95
- subtitle = String(raw);
96
- remaining = remaining.filter(([key]) => key !== subtitleKey);
97
- }
98
-
99
- const metaEntries = remaining
100
- .filter(([, value]) => {
101
- const t = typeof value;
102
- return (
103
- (t === "string" || t === "number" || t === "boolean") &&
104
- value !== "" &&
105
- value !== null
106
- );
107
- })
108
- .map(([k, v]) => [k, v]);
109
-
110
- return { title, subtitle, metaEntries };
111
- };