-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.parse_range.ts
68 lines (56 loc) · 2.04 KB
/
cli.parse_range.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function parseCommandLineRange(rangeParams: string) {
let result;
try {
result = flattenCommandLineRangeInput(parseCommandLineRangeObject(expand2(expand(removeWhitespace(rangeParams)))));
} catch (e) {
throw new Error("Invalid parameters.");
}
return result;
}
function removeWhitespace(toBeRemoved: string) {
return toBeRemoved.replace(/\s/g, "");
}
// {1-4} => {1,2,3,4}
function expand(orig: string) {
if (!orig.includes("-")) return orig;
const pattern = /(\d*-\d*)/gm;
return orig.replace(pattern, (match) => {
const [start, end] = match.split("-").map(Number);
return Array(end - start + 1).fill(0).map((_, i) => i + start).join(",");
});
}
// {1:{2},3} => {"1": {"2": null}, "3": null}
function expand2(orig: string) {
const matcher1 = /(\d+),/gm; // {1, => {"1":null,
const matcher2 = /(\d+)}/gm; // 3} => "3":null}
const matcher3 = /(\d+):{/gm; // 1:{ => "1":{
return orig.replace(matcher1, `"$1":null,`).replace(matcher2, `"$1":null}`).replace(matcher3, `"$1":{`);
}
type ParsedCommandLineObject = {
[key: number]: ParsedCommandLineObject | null;
}
function isParsed(obj: any): obj is ParsedCommandLineObject {
return typeof obj === "object" && obj !== null && Object.keys(obj).every(k => !isNaN(Number(k)));
}
function parseCommandLineRangeObject(orig: string) {
return JSON.parse(orig) as ParsedCommandLineObject;
}
// {1: {2: null}, 3: null} => [[1, 2], [3]]
function flattenCommandLineRangeInput(parsed: ParsedCommandLineObject | null): number[][] {
let result: number[][] = [];
if (parsed === null) return result;
const keys = Object.keys(parsed).map(Number);
for (const key of keys) {
const value = parsed[key];
if (isParsed(value)) {
const subResult = flattenCommandLineRangeInput(value);
for (const sub of subResult) {
result.push([key, ...sub]);
}
} else {
result.push([key]);
}
}
return result;
}
export { parseCommandLineRange };