-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
94 lines (89 loc) · 2.51 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import type { AssignableTo } from '../../types'
/**
* Applies a `filter` function to the elements of the given array, and then
* calls a `forEach` function with each of the filtered elements.
*
* @param array The array to iterate over.
* @param filter A function that determines whether an element should be included.
* @param forEach A function to call for each included element.
*
* @example
* #### Basic Usage
* ```ts
* filteredForEach(
* [1, 2, 3, 4, 5],
* (value) => value > 3,
* (value) => console.log(value ** 2)
* ) // Prints: 16, 25
* ```
*
* #### Type narrowing
* ```ts
* import { isString } from 'uft'
*
* filteredForEach(
* ['foo', { a: 1 }, [], 'bar'],
* isString,
* (value) => {
* value // type: string
* console.log(value.toUpperCase())
* }
* ) // Prints: FOO, BAR
* ```
* @category Array
*/
// Requires that the type predicate of `filter` extends
// one of the elements of the array.
export function filteredForEach<
T extends readonly unknown[],
S extends T[number],
>(
array: T,
filter: (element: T[number], index: number) => element is S,
forEach: (element: S, index: number) => void
): void
// Requires that an element of the array extends the
// type predicate of `filter`.
// This allows using a type guard that accepts an unknown
// value (a common use case) and returns a looser predicate
// which can be intersected with the element type.
// For example:
// ```ts
// const isObject = (value: unknown): value is object => {
// return typeof value === 'object' && value !== null
// }
// filteredForEach(
// [{ a: 1 }, { a: "hi" }, 99],
// isObject,
// (value) => {
// value // type: { a: number} | { a: string }
// }
// )
// ```
export function filteredForEach<
T extends readonly unknown[],
S,
>(
array: T,
filter: (element: T[number], index: number) => element is S,
forEach: (element: AssignableTo<T[number], S>, index: number) => void
): void
// Does not require the use of a type predicate.
export function filteredForEach<
T extends readonly unknown[],
>(
array: T,
filter: (element: T[number], index: number) => boolean,
forEach: (element: T[number], index: number) => void
): void
export function filteredForEach(
array: unknown[],
filter: (element: unknown, index: number) => boolean,
forEach: (element: unknown, index: number) => void
) {
for (let index = 0, rIndex = 0; index < array.length; index++) {
if (filter(array[index], index)) {
forEach(array[index], rIndex++)
}
}
}