-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
collect.ts
43 lines (39 loc) · 1.5 KB
/
collect.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
import type { MaybePromise } from './internal.ts';
/**
* A function used in `collect` methods to collect values from a collection. This is basically a single-pass map and filter.
* @param value - the input value
* @param index - the index of the input value
* @param skip - a token that can be returned to skip the value
* @param halt - a function that, when called, ensures no more values are passed
* @returns either a new value to collect, or the `skip` token indicating to skip the value
*/
export type CollectFun<T, R> = (
value: T,
index: number,
skip: CollectFun.Skip,
halt: () => void
) => R | CollectFun.Skip;
export namespace CollectFun {
/**
* Indicates, when returned from a collect function, to skip the vale.
*/
export const Skip = Symbol('Skip');
/**
* Indicates, when returned from a collect function, to skip the vale.
*/
export type Skip = typeof Skip;
}
/**
* A potentially asynchronous function used in `collect` methods to collect values from a collection. This is basically a single-pass map and filter.
* @param value - the input value
* @param index - the index of the input value
* @param skip - a token that can be returned to skip the value
* @param halt - a function that, when called, ensures no more values are passed
* @returns either a new value to collect, or the `skip` token indicating to skip the value
*/
export type AsyncCollectFun<T, R> = (
value: T,
index: number,
skip: CollectFun.Skip,
halt: () => void
) => MaybePromise<R | CollectFun.Skip>;