This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
/
utils.ts
91 lines (72 loc) · 2.43 KB
/
utils.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { err, ok, Result } from 'neverthrow/dist';
function findByPredicate<T>(arr: T[], filter: (t: T) => boolean): { index: number; value: T } | undefined {
for (let [index, value] of arr.entries()) {
if (filter(value)) {
return { index, value };
}
}
return undefined;
}
export function findAndPushToEnd<T>(arr: T[], filter: (t: T) => boolean): T | undefined {
let result = findByPredicate(arr, filter);
if (result == null) {
return undefined;
}
let { index, value } = result;
arr.splice(index, 1);
arr.push(value);
return value;
}
export function removeInPlace<T>(arr: T[], element: T): void {
let index;
while ((index = arr.indexOf(element)) != -1) {
arr.splice(index, 1);
}
}
export function setDefaults<T>(input: Partial<T> = {}, defaults: T): T {
return Object.assign({}, defaults, input);
}
export function applyMixins(derivedCtor: any, baseCtors: any[]): void {
baseCtors.forEach(baseCtor => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
if (name !== 'constructor') {
derivedCtor.prototype[name] = baseCtor.prototype[name];
}
});
});
}
export function splitIntoChunks(bits: string[], separator: string = ' ', limit: number = 500): string[][] {
let chunks: string[][] = [];
let currentChunk: string[] = [];
let currentChunkJoinedLength = 0;
let tryAppend = (bit, recursive = false): void => {
let addedLength;
if (currentChunk.length <= 0) {
addedLength = bit.length;
} else {
addedLength = separator.length + bit.length;
}
if (currentChunkJoinedLength + addedLength <= limit) {
currentChunk.push(bit);
currentChunkJoinedLength += addedLength;
} else {
chunks.push(currentChunk);
currentChunk = [];
currentChunkJoinedLength = 0;
if (recursive) {
throw new Error('Found a piece that can never fit the target length limit');
}
tryAppend(bit, true);
}
};
for (let bit of bits) {
tryAppend(bit);
}
if (currentChunk.length > 0) {
chunks.push(currentChunk);
}
return chunks;
}
export function promiseToResult<T>(p: Promise<T>): Promise<Result<T, Error>> {
return (p.then(r => ok(r), (e) => err(e))) as Promise<Result<T, Error>>;
}