/
helper.ts
68 lines (61 loc) · 1.65 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
export type ArrayLike = unknown[] | Buffer | string;
/**
* Split array/buffer/string into chunks
* @param array - source array
* @param len - chunk size
*/
function chunkArray<T extends ArrayLike>(array: T, len: number): T[] {
const ret: T[] = [];
const size = Math.ceil(array.length / len);
ret.length = size;
let offset;
for (let i = 0; i < size; i += 1) {
offset = i * len;
ret[i] = array.slice(offset, offset + len) as T;
}
return ret;
}
/**
* Buffer formatting
* @param buffer
*/
export function printBuffer(buffer: Buffer): string {
return chunkArray(chunkArray(buffer.toString('hex'), 2), 16)
.map(chunk => chunk.join('-'))
.join('=');
}
/**
* Create a promise which resolves after the specified milliseconds.
* @param ms
*/
export const delay = (ms: number): Promise<void> =>
new Promise<void>(resolve => {
setTimeout(resolve, ms);
});
/**
* Performs an asynchronous operation sequentially on all elements of an array
* @param array
* @param action
*/
export function series<T, R>(
array: ReadonlyArray<T>,
action: (item: T, index: number, arr: ReadonlyArray<T>, results: ReadonlyArray<R>) => Promise<R>
): Promise<R[]> {
return array.reduce<Promise<R[]>>(
(acc, item, index) =>
acc.then(async items => {
const result = await action(item, index, array, items);
return [...items, result];
}),
Promise.resolve<R[]>([])
);
}
/**
* 'Not Empty' type guard
* @param value
*/
export function notEmpty<TValue>(value: TValue | null | undefined | void): value is TValue {
return (
value !== undefined && value !== null && (typeof value !== 'number' || !Number.isNaN(value))
);
}