-
Notifications
You must be signed in to change notification settings - Fork 0
/
some.ts
53 lines (46 loc) · 1.8 KB
/
some.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
import type { IterableItem, IterableReturnValue, MaybePromise, UniversalIterable, UniversalIterableItem } from '@/types'
import { IterableTypeException, isAsyncIterable, isIterable } from '@/utils'
import { map } from '../lazy/map'
import { takeUntil } from '../lazy/takeUntil'
import { identity } from './identity'
import { pipe } from './pipe'
import { reduce } from './reduce'
/**
* Returns true if any of the values in Iterable/AsyncIterable pass the `fn` truth test.
*
* @example
* ```typescript
* some((a) => a % 2 !== 0, [2, 4, 6, 8, 10])
* // false
*
* some((a) => a % 2 === 0, [2, 5, 6, 7, 10])
* // true
* ```
*
* {@link https://github.com/niuiic/fx-flow/blob/main/packages/test/src/pipe/strict/some.spec.ts | More examples}
*/
function some<A extends UniversalIterable>(
fn: (args: UniversalIterableItem<A>) => A extends AsyncIterable<unknown> ? MaybePromise<unknown> : unknown
): (iterable: A) => IterableReturnValue<A, boolean>
function some<A extends readonly []>(fn: (args: IterableItem<A>) => boolean, iterable: A): false
function some<A>(fn: (args: A) => unknown, iterable: Iterable<A>): boolean
function some<A>(fn: (args: A) => MaybePromise<unknown>, iterable: AsyncIterable<A>): Promise<boolean>
function some<A extends UniversalIterable>(
fn: (args: UniversalIterableItem<A>) => A extends AsyncIterable<unknown> ? MaybePromise<unknown> : unknown,
iterable?: A
): ((iterable: A) => IterableReturnValue<A, boolean>) | IterableReturnValue<A, boolean> {
if (iterable === undefined) {
return (iterable) => some(fn, iterable as any) as any
}
if (isIterable(iterable) || isAsyncIterable(iterable)) {
return pipe(
iterable,
map(fn),
takeUntil(identity),
reduce((a, b) => a || b),
Boolean
) as any
}
throw new IterableTypeException()
}
export { some }