@jacebenson/jsn 5.3.0 → 5.4.0-dev.141.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.
@@ -0,0 +1,67 @@
1
+ // Number only already-loaded rows; this module performs no instance reads.
2
+ const compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
3
+ const byNameId = (a, b) => compare(a.name, b.name) || compare(a.sys_id, b.sys_id);
4
+ const unconditional = edge => edge.condition_status === 'none'
5
+ || (edge.condition_status === 'ok' && /^always$/i.test(edge.condition_name.trim()));
6
+ const exception = edge => edge.condition_status === 'ok'
7
+ && /^(rejected|reject|failure|failed|error|skipped|cancelled|canceled|timeout|expired)$/i.test(edge.condition_name.trim());
8
+ const preference = edge => unconditional(edge) ? 0
9
+ : edge.condition_status === 'ok'
10
+ && /^(approved|approve|success|ok|true|yes|completed)$/i.test(edge.condition_name.trim()) ? 1
11
+ : exception(edge) ? 3 : 2;
12
+ const byEdge = (a, b) => preference(a) - preference(b)
13
+ || compare(a.to_name, b.to_name) || compare(a.to_id, b.to_id)
14
+ || compare(a.condition_name, b.condition_name) || compare(a.condition_id, b.condition_id)
15
+ || compare(a.sys_id, b.sys_id);
16
+
17
+ /** Deterministic iterative DFS preorder, starting only at the version's start. */
18
+ export function numberWorkflowSteps(activities, transitions, startId, components) {
19
+ const stable = [...activities].sort(byNameId);
20
+ const activityById = new Map(stable.map(activity => [activity.sys_id, activity]));
21
+ const outgoing = new Map(stable.map(activity => [activity.sys_id, []]));
22
+ for (const edge of transitions) outgoing.get(edge.from_id)?.push(edge);
23
+ for (const edges of outgoing.values()) edges.sort(byEdge);
24
+ let fallback_reason = null;
25
+ if (!startId) fallback_reason = 'no start activity';
26
+ else if (!activityById.has(startId)) fallback_reason = 'start activity not in the readable activities';
27
+ else if (components.activities !== 'ok' || !['ok', 'not_applicable'].includes(components.transitions)) {
28
+ fallback_reason = 'activities or transitions not readable';
29
+ }
30
+ const steps = [];
31
+ const visited = new Set();
32
+ const add = (activity, reached_from_start) => {
33
+ visited.add(activity.sys_id);
34
+ steps.push({ number: steps.length + 1, sys_id: activity.sys_id, name: activity.name, reached_from_start });
35
+ };
36
+ if (!fallback_reason) {
37
+ const stack = [startId];
38
+ while (stack.length) {
39
+ const id = stack.pop();
40
+ if (visited.has(id)) continue;
41
+ add(activityById.get(id), true);
42
+ // Reverse push preserves preferred-edge order without recursive stack limits.
43
+ for (const edge of [...outgoing.get(id)].reverse()) {
44
+ if (activityById.has(edge.to_id) && !visited.has(edge.to_id)) stack.push(edge.to_id);
45
+ }
46
+ }
47
+ }
48
+ for (const activity of stable) if (!visited.has(activity.sys_id)) add(activity, false);
49
+ // Stable partition after traversal; fallback retains its disclosed name/ID order.
50
+ if (!fallback_reason) {
51
+ steps.sort((a, b) => Number(outgoing.get(a.sys_id).length === 0)
52
+ - Number(outgoing.get(b.sys_id).length === 0));
53
+ steps.forEach((step, index) => { step.number = index + 1; });
54
+ }
55
+ const byId = new Map(steps.map(step => [step.sys_id, step]));
56
+ // Exactly one preferred edge may be implicit. Parallel conditions remain visible.
57
+ for (const edge of transitions) edge.is_sequential = false;
58
+ for (const step of steps) {
59
+ if (!step.reached_from_start) continue;
60
+ const edges = outgoing.get(step.sys_id);
61
+ const next = edges[0];
62
+ const target = byId.get(next?.to_id);
63
+ if (next && (!exception(next) || edges.length === 1)
64
+ && target?.reached_from_start && target.number === step.number + 1) next.is_sequential = true;
65
+ }
66
+ return { steps, step_order: { method: fallback_reason ? 'stable_fallback' : 'start_traversal', fallback_reason } };
67
+ }