-
Notifications
You must be signed in to change notification settings - Fork 0
/
range.ts
executable file
·41 lines (34 loc) · 1.03 KB
/
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
export type Range = number | string;
interface _Iterable extends Iterable<{}> {
length: number;
}
const FIRST_LETTER = 0;
const LAST_LETTER = 123;
const firstChar = (value: string) => value.charCodeAt(FIRST_LETTER);
class _Array<T> extends Array<T> {
static range(
from: Range,
to: Range,
step: number = 1,
): Range[] | undefined {
if (typeof from === "number" && typeof to === "number") {
return Array.from(
(<_Iterable>{ length: Math.floor((to - from) / step) + 1 }),
(v, k) => from + k * step,
);
} else if (typeof to === "string" && typeof from === "string") {
const [charStart, charTo] = [firstChar(from), firstChar(to) + 1];
const lenCharTo = charTo - charStart;
if (lenCharTo < FIRST_LETTER) return;
if (lenCharTo > LAST_LETTER) return;
const _arr = [];
for (let i = charStart; i < charTo; i += step) {
_arr.push(String.fromCharCode(i));
}
return _arr;
} else {
return;
}
}
}
export const range = _Array.range;