Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x | /**
* Async filter permits to filter an array using an async predicate function
* @param arr The array of data
* @param predicate The predicate function to run over arr items
* @returns A filtered array
*/
export const asyncFilter = async <T>(
arr: T[] | null | undefined,
predicate: (entity: T) => Promise<boolean>,
): Promise<T[]> => {
const predicates = await Promise.all((arr ?? []).map(predicate));
return (arr ?? []).filter((_, idx) => predicates[idx]);
};
/**
* Given an array and a predicate, ensure that elements that matches the predicate
* are put at the top of the array.
*
* @param array
* @param predicate
*/
export const putOnTop = <T>(array: T[], predicate: (elt: T) => boolean) =>
[...array].sort((a) => (predicate(a) ? -1 : 1));
export const sortByIndex = <T extends { index: number | undefined }>(array: T[]): T[] =>
[...array].sort((a, b) => (a.index || 0) - (b.index || 0));
/**
* Immutable replace in an array.
*/
export const replaceAt = <T>(array: T[], index: number, value: T) => {
// Creating shallow copy.
const ret = array.slice(0);
// Replacing the element.
ret[index] = value;
// Return the new array.
return ret;
};
export const formatStringArrayWithLimit = (arr: string[], limit: number): string => {
const extract = arr.slice(0, limit).join(', ');
return arr.length <= limit ? extract : `${extract} (+${arr.length - limit})`;
};
|