-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.ts
52 lines (46 loc) · 1.33 KB
/
helper.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
export function toTitleCase(str: string): string {
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
export function randomChoice<T>(arr: T[]) {
return arr[Math.floor(Math.random() * arr.length)];
}
export function randomSample<T>(arr: T[], n: number): T[] {
const shuffled = arr.sort(() => 0.5 - Math.random());
return shuffled.slice(0, n);
}
export function randomShuffle<T>(arr: T[]): T[] {
const shuffled = arr.sort(() => 0.5 - Math.random());
return shuffled;
}
// deno-lint-ignore no-explicit-any
export function counter(arr: any[]) {
// deno-lint-ignore no-explicit-any
const obj: { [key: string]: any } = {};
arr.forEach(function (v) {
if (v in obj) ++obj[v];
else obj[v] = 1;
});
return obj;
}
export function mostCommon(obj: { [key: string]: number }, n: number) {
let sortedList = [];
sortedList = Object.entries(obj).sort((a, b) => {
if (b[1] > a[1]) return 1;
else if (b[1] < a[1]) return -1;
else {
if (a[0] > b[0]) return 1;
else if (a[0] < b[0]) return -1;
else return 0;
}
});
return sortedList.map((el) => el[0]).slice(0, n);
}
export function ltrim(x: string, characters: string) {
let start = 0;
while (characters.indexOf(x[start]) >= 0) {
start += 1;
}
return x.substr(start);
}