( iterable: List, predicate: (value: Element) => Promisable<boolean>, )
| 23 | } |
| 24 | |
| 25 | export function pSomeFunction< |
| 26 | List extends Iterable<unknown>, |
| 27 | Element extends IterableElement<List>, |
| 28 | >( |
| 29 | iterable: List, |
| 30 | predicate: (value: Element) => Promisable<boolean>, |
| 31 | ): Promisable<boolean> { |
| 32 | const promises: Array<PromiseLike<boolean>> = []; |
| 33 | // Prioritize sync functions and early returns |
| 34 | for (const item of iterable) { |
| 35 | const result = predicate(item as Element); |
| 36 | if (typeof result === 'boolean') { |
| 37 | if (result) { |
| 38 | // Early sync return on the first truthy value |
| 39 | return true; |
| 40 | } |
| 41 | } else { |
| 42 | promises.push(result); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if (promises.length === 0) { |
| 47 | // Matches `[].some(Boolean)` |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | return pSome(promises); |
| 52 | } |
| 53 | |
| 54 | export function pEveryFunction< |
| 55 | List extends Iterable<unknown>, |
no test coverage detected