-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmap.d.ts
246 lines (246 loc) · 6.98 KB
/
map.d.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
export = map;
/**
* @name map
*
* @synopsis
* ```coffeescript [specscript]
* type Mappable = Array|Object|Set|Map|Iterator|AsyncIterator
*
* type Mapper = (
* value any,
* indexOrKey number|string,
* collection Mappable
* )=>(mappedItem Promise|any)
*
* map(value Mappable, mapper Mapper) -> result Promise|Mappable
* map(mapper Mapper)(value Mappable) -> result Promise|Mappable
* ```
*
* @description
* Applies a synchronous or asynchronous mapper function concurrently to each item of a collection, returning the results in a new collection of the same type. If order is implied by the collection, it is maintained in the result. `map` accepts the following collections:
*
* * `Array`
* * `Object`
* * `Set`
* * `Map`
* * `Iterator`/`Generator`
* * `AsyncIterator`/`AsyncGenerator`
*
* With arrays (type `Array`), `map` applies the mapper function to each item of the array, returning the transformed results in a new array ordered the same as the original array.
*
* ```javascript [playground]
* const square = number => number ** 2
*
* const array = [1, 2, 3, 4, 5]
*
* console.log(
* map(array, square)
* ) // [1, 4, 9, 16, 25]
*
* console.log(
* map(square)(array)
* ) // [1, 4, 9, 16, 25]
* ```
*
* With objects (type `Object`), `map` applies the mapper function to each value of the object, returning the transformed results as values in a new object ordered by the keys of the original object
*
* ```javascript [playground]
* const square = number => number ** 2
*
* const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }
*
* console.log(
* map(square)(obj)
* ) // { a: 1, b: 4, c: 9, d: 16, e: 25 }
*
* console.log(
* map(obj, square)
* ) // { a: 1, b: 4, c: 9, d: 16, e: 25 }
* ```
*
* With sets (type `Set`), `map` applies the mapper function to each value of the set, returning the transformed results unordered in a new set.
*
* ```javascript [playground]
* const square = number => number ** 2
*
* const set = new Set([1, 2, 3, 4, 5])
*
* console.log(
* map(set, square)
* ) // [1, 4, 9, 16, 25]
*
* console.log(
* map(square)(set)
* ) // [1, 4, 9, 16, 25]
* ```
*
* With maps (type `Map`), `map` applies the mapper function to each value of the map, returning the results at the same keys in a new map. The entries of the resulting map are in the same order as those of the original map
*
* ```javascript [playground]
* const square = number => number ** 2
*
* const m = new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]])
*
* console.log(
* map(square)(m)
* ) // Map { 'a' => 1, 'b' => 4, 'c' => 9, 'd' => 16, 'e' => 25 }
*
* console.log(
* map(m, square)
* ) // Map { 'a' => 1, 'b' => 4, 'c' => 9, 'd' => 16, 'e' => 25 }
* ```
*
* With iterators (type `Iterator`) or generators (type `Generator`), `map` applies the mapper function lazily to each value of the iterator/generator, creating a new iterator with transformed iterations.
*
* ```javascript [playground]
* const capitalize = string => string.toUpperCase()
*
* const abcGeneratorFunc = function* () {
* yield 'a'; yield 'b'; yield 'c'
* }
*
* const abcGenerator = abcGeneratorFunc()
* const ABCGenerator = map(abcGeneratorFunc(), capitalize)
* const ABCGenerator2 = map(capitalize)(abcGeneratorFunc())
*
* console.log([...abcGenerator]) // ['a', 'b', 'c']
*
* console.log([...ABCGenerator]) // ['A', 'B', 'C']
*
* console.log([...ABCGenerator2]) // ['A', 'B', 'C']
* ```
*
* With asyncIterators (type `AsyncIterator`, or `AsyncGenerator`), `map` applies the mapper function lazily to each value of the asyncIterator, creating a new asyncIterator with transformed iterations
*
* ```javascript [playground]
* const capitalize = string => string.toUpperCase()
*
* const abcAsyncGeneratorFunc = async function* () {
* yield 'a'; yield 'b'; yield 'c'
* }
*
* const abcAsyncGenerator = abcAsyncGeneratorFunc()
* const ABCGenerator = map(abcAsyncGeneratorFunc(), capitalize)
* const ABCGenerator2 = map(capitalize)(abcAsyncGeneratorFunc())
*
* ;(async function () {
* for await (const letter of abcAsyncGenerator) {
* console.log(letter)
* // a
* // b
* // c
* }
*
* for await (const letter of ABCGenerator) {
* console.log(letter)
* // A
* // B
* // C
* }
*
* for await (const letter of ABCGenerator2) {
* console.log(letter)
* // A
* // B
* // C
* }
* })()
* ```
*
* @execution concurrent
*
* @TODO streamMap
*/
declare function map(...args: any[]): any;
declare namespace map {
/**
* @name map.entries
*
* @synopsis
* ```coffeescript [specscript]
* map.entries(
* mapper ([key any, value any])=>Promise|[any, any],
* )(value Map|Object) -> Promise|Map|Object
* ```
*
* @description
* `map` over the entries rather than the values of a collection. Accepts collections of type `Map` or `Object`.
*
* ```javascript [playground]
* const upperCaseKeysAndSquareValues =
* map.entries(([key, value]) => [key.toUpperCase(), value ** 2])
*
* console.log(upperCaseKeysAndSquareValues({ a: 1, b: 2, c: 3 }))
* // { A: 1, B: 4, C: 9 }
*
* console.log(upperCaseKeysAndSquareValues(new Map([['a', 1], ['b', 2], ['c', 3]])))
* // Map(3) { 'A' => 1, 'B' => 4, 'C' => 9 }
* ```
*
* @since v1.7.0
*/
function entries(mapper: any): (value: any) => any;
/**
* @name map.series
*
* @synopsis
* ```coffeescript [specscript]
* map.series(
* mapperFunc (value any, index number)=>Promise|any,
* )(array Array) -> Promise|Array
* ```
*
* @description
* `map` with serial execution.
*
* ```javascript [playground]
* const delayedLog = number => new Promise(function (resolve) {
* setTimeout(function () {
* console.log(number)
* resolve()
* }, 1000)
* })
*
* console.log('start')
* map.series(delayedLog)([1, 2, 3, 4, 5])
* ```
*
* @execution series
*/
function series(mapper: any, index: number): (value: any) => any;
/**
* @name map.pool
*
* @synopsis
* ```coffeescript [specscript]
* map.pool(
* maxConcurrency number,
* mapper (value any)=>Promise|any,
* )(array Array) -> result Promise|Array
* ```
*
* @description
* `map` that specifies the maximum concurrency (number of ongoing promises at any time) of the execution. Only works for arrays.
*
* ```javascript [playground]
* const ids = [1, 2, 3, 4, 5]
*
* const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
*
* const delayedIdentity = async value => {
* await sleep(1000)
* return value
* }
*
* map.pool(2, pipe([
* delayedIdentity,
* console.log,
* ]))(ids)
* ```
*
* @TODO objectMapPool
*
* @execution concurrent
*/
function pool(concurrencyLimit: any, mapper: any): (value: any) => any[] | Promise<any>;
}