Mirror of https://github.com/roostorg/coop github.com/roostorg/coop
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 53 lines 1.4 kB view raw
1import lodash from 'lodash'; 2 3const { isPlainObject: _isPlainObject, unzip, zip } = lodash; 4 5/** 6 * A type-safe wrapper around lodash unzip, that only works for arrays of 7 * 2-tuples, but also handles inverting a `zip([], []) => []`, which callers 8 * rely on and which _.unzip can't do, because it doesn't know how many source 9 * arrays there would've been. 10 */ 11export function unzip2<T, U>(it: readonly (readonly [T, U])[]) { 12 return (it.length ? (unzip(it) as unknown) : [[], []]) as [T[], U[]]; 13} 14 15/** 16 * A wrapper around lodash.zip that checks that the two arrays are of equal 17 * length first, and thereby allows the return type to exclude undefined (which 18 * is used to pad the zipped pairs only when the arrays are of unequal lengths). 19 */ 20export function equalLengthZip<T, U>( 21 a: readonly T[], 22 b: readonly U[], 23): [T, U][] { 24 if (a.length !== b.length) { 25 throw new Error("Can't zip arrays of different lengths"); 26 } 27 28 return zip(a, b) as [T, U][]; 29} 30 31export async function someAsync<T>( 32 arr: T[], 33 predicate: (v: T) => Promise<boolean>, 34) { 35 for (const e of arr) { 36 if (await predicate(e)) { 37 return true; 38 } 39 } 40 return false; 41} 42 43export async function everyAsync<T>( 44 arr: T[], 45 predicate: (v: T) => Promise<boolean>, 46) { 47 for (const e of arr) { 48 if (!(await predicate(e))) { 49 return false; 50 } 51 } 52 return true; 53}