-
Notifications
You must be signed in to change notification settings - Fork 0
/
bracketExpansion.ts
executable file
·39 lines (30 loc) · 1023 Bytes
/
bracketExpansion.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
import {
range,
} from "https://raw.githubusercontent.com/vaclavbenes/range/master/range.ts";
export type Range = number | string;
const removeCurly = (value: string) => {
return value.replace("{", "").replace("}", "");
};
const isNumeric = (num: any) => {
return isNaN(num);
};
const bracketExpression = (value: string): string[] | Range[] => {
// use basic {1,2} nebo {1..3} , dont use in nested {1{}}}
if (value.includes(",")) {
value = removeCurly(value);
return value.split(",");
} else if (value.includes("..")) {
value = removeCurly(value);
const numbers = value.split("..");
const min = isNumeric(numbers[0]) ? numbers[0] : parseInt(numbers[0]);
const max = isNumeric(numbers[numbers.length - 1])
? numbers[numbers.length - 1]
: parseInt(numbers[numbers.length - 1]);
const rangeOfValues = range(min, max);
if (rangeOfValues) return rangeOfValues;
}
console.log("data");
// its normal string
return [];
};
export default bracketExpression;