-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
71 lines (62 loc) · 1.43 KB
/
utils.ts
File metadata and controls
71 lines (62 loc) · 1.43 KB
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
69
70
71
import { SortingComparator } from "./custom-types/sorting-comparator";
export class Utils {
public static range(start: number, end: number, step: number = 1): number[] {
let i = start;
let output: number[] = [];
while (i < end) {
output.push(i);
i += step;
}
return output;
}
public static swapValues<T>(array: T[], from: number, to: number): void {
const temp = array[from];
array[from] = array[to];
array[to] = temp;
}
public static modernSwapValues<T>(
array: T[],
from: number,
to: number
): void {
[array[from], array[to]] = [array[to], array[from]];
}
public static lt<T>(a: T, b: T): boolean {
return a < b;
}
public static gt<T>(a: T, b: T): boolean {
return a > b;
}
public static eq<T>(a: T, b: T): boolean {
return a === b;
}
public static minIndex<T>(
array: T[],
from: number = 0,
to: number = array.length,
comparator: SortingComparator<T> = Utils.gt
) {
const arraySize: number = to;
let lowestIndex: number = from;
for (let i = from; i < arraySize; i++) {
if (comparator(array[lowestIndex], array[i])) {
lowestIndex = i;
}
}
return lowestIndex;
}
public static findIndexBy<T>(
array: T[],
comparator: (el: T, index?: number, array?: T[]) => boolean
) {
let lowestIndex: number = -1;
array.some((entry, index) => {
if (comparator(entry, index, array)) {
lowestIndex = index;
return true;
}
return false;
});
return lowestIndex;
}
}