route-graphics 1.12.3 → 1.14.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,61 @@
1
+ export const parseStateSelection = (value, stateCount) => {
2
+ if (!Number.isInteger(stateCount) || stateCount < 0) {
3
+ throw new Error("State count must be a non-negative integer.");
4
+ }
5
+
6
+ if (value === undefined || value === null || value === "") {
7
+ return Array.from({ length: stateCount }, (_entry, index) => index);
8
+ }
9
+
10
+ const selected = [];
11
+ const seen = new Set();
12
+
13
+ const addIndex = (index) => {
14
+ if (index < 0 || index >= stateCount) {
15
+ throw new Error(
16
+ `State index ${index} is out of range for ${stateCount} state(s).`,
17
+ );
18
+ }
19
+
20
+ if (!seen.has(index)) {
21
+ seen.add(index);
22
+ selected.push(index);
23
+ }
24
+ };
25
+
26
+ for (const rawPart of String(value).split(",")) {
27
+ const part = rawPart.trim();
28
+ if (!part) {
29
+ throw new Error("State selection contains an empty segment.");
30
+ }
31
+
32
+ const rangeMatch = /^(\d+)-(\d+)$/.exec(part);
33
+ if (rangeMatch) {
34
+ const start = Number.parseInt(rangeMatch[1], 10);
35
+ const end = Number.parseInt(rangeMatch[2], 10);
36
+
37
+ if (start > end) {
38
+ throw new Error(`State range "${part}" must be ascending.`);
39
+ }
40
+
41
+ for (let index = start; index <= end; index += 1) {
42
+ addIndex(index);
43
+ }
44
+ continue;
45
+ }
46
+
47
+ if (!/^\d+$/.test(part)) {
48
+ throw new Error(
49
+ `Invalid state selection "${part}". Use indexes or ranges like 0,2-4.`,
50
+ );
51
+ }
52
+
53
+ addIndex(Number.parseInt(part, 10));
54
+ }
55
+
56
+ if (selected.length === 0) {
57
+ throw new Error("State selection did not include any states.");
58
+ }
59
+
60
+ return selected;
61
+ };