@@ -10,16 +10,16 @@ export function ofIterable<T>(source: Iterable<T>): T[] {
10
10
* Creates a new array whose elements are the results of applying the specified mapping to each of the elements of the source collection.
11
11
* @param mapping A function to transform items from the input collection.
12
12
*/
13
- export function map < T , U > ( mapping : ( item : T ) => U ) : ( source : T [ ] ) => U [ ]
13
+ export function map < T , U > ( mapping : ( item : T , index : number ) => U ) : ( source : T [ ] ) => U [ ]
14
14
/**
15
15
* Creates a new array whose elements are the results of applying the specified mapping to each of the elements of the source collection.
16
16
* @param source The input collection.
17
17
* @param mapping A function to transform items from the input collection.
18
18
*/
19
- export function map < T , U > ( source : T [ ] , mapping : ( item : T ) => U ) : U [ ]
19
+ export function map < T , U > ( source : T [ ] , mapping : ( item : T , index : number ) => U ) : U [ ]
20
20
export function map < T , U > ( a : any , b ?: any ) : any {
21
21
const partial = typeof a === 'function'
22
- const mapping : ( item : T ) => U = partial ? a : b
22
+ const mapping : ( item : T , index : number ) => U = partial ? a : b
23
23
function exec ( source : T [ ] ) {
24
24
return source . map ( mapping )
25
25
}
@@ -30,16 +30,16 @@ export function map<T, U>(a: any, b?: any): any {
30
30
* Returns a new array containing only the elements of the collection for which the given predicate returns true.
31
31
* @param predicate A function to test whether each item in the input collection should be included in the output.
32
32
*/
33
- export function filter < T > ( predicate : ( item : T ) => boolean ) : ( source : T [ ] ) => T [ ]
33
+ export function filter < T > ( predicate : ( item : T , index : number ) => boolean ) : ( source : T [ ] ) => T [ ]
34
34
/**
35
35
* Returns a new array containing only the elements of the collection for which the given predicate returns true.
36
36
* @param source The input collection.
37
37
* @param predicate A function to test whether each item in the input collection should be included in the output.
38
38
*/
39
- export function filter < T > ( source : T [ ] , predicate : ( item : T ) => boolean ) : T [ ]
39
+ export function filter < T > ( source : T [ ] , predicate : ( item : T , index : number ) => boolean ) : T [ ]
40
40
export function filter < T , U > ( a : any , b ?: any ) : any {
41
41
const partial = typeof a === 'function'
42
- const predicate : ( item : T ) => boolean = partial ? a : b
42
+ const predicate : ( item : T , index : number ) => boolean = partial ? a : b
43
43
function exec ( source : T [ ] ) {
44
44
return source . filter ( predicate )
45
45
}
@@ -50,23 +50,27 @@ export function filter<T, U>(a: any, b?: any): any {
50
50
* Applies the given function to each element of the array and returns a new array comprised of the results for each element where the function returns a value.
51
51
* @param chooser A function to transform items from the input collection to a new value to be included, or undefined to be excluded.
52
52
*/
53
- export function choose < T , U > ( chooser : ( item : T ) => U | undefined ) : ( source : T [ ] ) => U [ ]
53
+ export function choose < T , U > (
54
+ chooser : ( item : T , index : number ) => U | undefined
55
+ ) : ( source : T [ ] ) => U [ ]
54
56
/**
55
57
* Applies the given function to each element of the array and returns a new array comprised of the results for each element where the function returns a value.
56
58
* @param source The input collection.
57
59
* @param chooser A function to transform items from the input collection to a new value to be included, or undefined to be excluded.
58
60
*/
59
- export function choose < T , U > ( source : T [ ] , chooser : ( item : T ) => U | undefined ) : U [ ]
61
+ export function choose < T , U > ( source : T [ ] , chooser : ( item : T , index : number ) => U | undefined ) : U [ ]
60
62
export function choose < T , U > ( a : any , b ?: any ) : any {
61
63
const partial = typeof a === 'function'
62
- const chooser : ( item : T ) => U | undefined = partial ? a : b
64
+ const chooser : ( item : T , index : number ) => U | undefined = partial ? a : b
63
65
function exec ( source : T [ ] ) {
64
66
const target = [ ]
67
+ let index = 0
65
68
for ( const item of source ) {
66
- const chosen = chooser ( item )
69
+ const chosen = chooser ( item , index )
67
70
if ( chosen !== undefined ) {
68
71
target . push ( chosen )
69
72
}
73
+ index ++
70
74
}
71
75
return target
72
76
}
@@ -77,23 +81,27 @@ export function choose<T, U>(a: any, b?: any): any {
77
81
* Applies the given function to each element of the source array and concatenates all the results.
78
82
* @param mapping A function to transform elements of the input collection into collections that are concatenated.
79
83
*/
80
- export function collect < T , U > ( mapping : ( item : T ) => Iterable < U > ) : ( source : T [ ] ) => U [ ]
84
+ export function collect < T , U > (
85
+ mapping : ( item : T , index : number ) => Iterable < U >
86
+ ) : ( source : T [ ] ) => U [ ]
81
87
/**
82
88
* Applies the given function to each element of the source array and concatenates all the results.
83
89
* @param source The input collection.
84
90
* @param mapping A function to transform elements of the input collection into collections that are concatenated.
85
91
*/
86
- export function collect < T , U > ( source : T [ ] , mapping : ( item : T ) => Iterable < U > ) : U [ ]
92
+ export function collect < T , U > ( source : T [ ] , mapping : ( item : T , index : number ) => Iterable < U > ) : U [ ]
87
93
export function collect < T , U > ( a : any , b ?: any ) : any {
88
94
const partial = typeof a === 'function'
89
- const mapping : ( item : T ) => Iterable < U > = partial ? a : b
95
+ const mapping : ( item : T , index : number ) => Iterable < U > = partial ? a : b
90
96
function exec ( source : T [ ] ) {
91
97
const target = [ ]
98
+ let index = 0
92
99
for ( const item of source ) {
93
- const children = mapping ( item )
100
+ const children = mapping ( item , index )
94
101
for ( const child of children ) {
95
102
target . push ( child )
96
103
}
104
+ index ++
97
105
}
98
106
return target
99
107
}
@@ -142,25 +150,27 @@ export function concat<T>(sources: Iterable<T[]>): T[] {
142
150
* @param selector A function that transforms the array items into comparable keys.
143
151
* @param source The input collection.
144
152
*/
145
- export function distinctBy < T , Key > ( selector : ( item : T ) => Key ) : ( source : T [ ] ) => T [ ]
153
+ export function distinctBy < T , Key > ( selector : ( item : T , index : number ) => Key ) : ( source : T [ ] ) => T [ ]
146
154
/**
147
155
* Returns an array that contains no duplicate entries according to the equality comparisons on
148
156
* the keys returned by the given key-generating function. If an element occurs multiple times in
149
157
* the sequence then the later occurrences are discarded.
150
158
* @param source The input collection.
151
159
* @param selector A function that transforms the array items into comparable keys.
152
160
*/
153
- export function distinctBy < T , Key > ( source : T [ ] , selector : ( item : T ) => Key ) : T [ ]
161
+ export function distinctBy < T , Key > ( source : T [ ] , selector : ( item : T , index : number ) => Key ) : T [ ]
154
162
export function distinctBy < T , Key > ( a : any , b ?: any ) : any {
155
163
const partial = typeof a === 'function'
156
- const selector : ( item : T ) => Key = partial ? a : b
164
+ const selector : ( item : T , index : number ) => Key = partial ? a : b
157
165
function exec ( source : T [ ] ) : T [ ] {
158
166
const seen = new Map < Key , T > ( )
167
+ let index = 0
159
168
for ( const item of source ) {
160
- const key = selector ( item )
169
+ const key = selector ( item , index )
161
170
if ( ! seen . has ( key ) ) {
162
171
seen . set ( key , item )
163
172
}
173
+ index ++
164
174
}
165
175
return Array . from ( seen . values ( ) )
166
176
}
@@ -172,16 +182,16 @@ export function distinctBy<T, Key>(a: any, b?: any): any {
172
182
* @param predicate A function to test each item of the input collection.
173
183
* @param source The input collection.
174
184
*/
175
- export function exists < T > ( predicate : ( item : T ) => boolean ) : ( source : T [ ] ) => boolean
185
+ export function exists < T > ( predicate : ( item : T , index : number ) => boolean ) : ( source : T [ ] ) => boolean
176
186
/**
177
187
* Tests if any element of the array satisfies the given predicate.
178
188
* @param source The input collection.
179
189
* @param predicate A function to test each item of the input collection.
180
190
*/
181
- export function exists < T > ( source : T [ ] , predicate : ( item : T ) => boolean ) : boolean
191
+ export function exists < T > ( source : T [ ] , predicate : ( item : T , index : number ) => boolean ) : boolean
182
192
export function exists < T > ( a : any , b ?: any ) : any {
183
193
const partial = typeof a === 'function'
184
- const predicate : ( item : T ) => boolean = partial ? a : b
194
+ const predicate : ( item : T , index : number ) => boolean = partial ? a : b
185
195
function exec ( source : T [ ] ) : boolean {
186
196
return source . some ( predicate )
187
197
}
@@ -194,22 +204,24 @@ export function exists<T>(a: any, b?: any): any {
194
204
* @param source The input collection.
195
205
* @throws If no item is found matching the criteria of the predicate.
196
206
*/
197
- export function get < T > ( predicate : ( item : T ) => boolean ) : ( source : T [ ] ) => T
207
+ export function get < T > ( predicate : ( item : T , index : number ) => boolean ) : ( source : T [ ] ) => T
198
208
/**
199
209
* Returns the first element for which the given function returns true.
200
210
* @param source The input collection.
201
211
* @param predicate A function to test whether an item in the collection should be returned.
202
212
* @throws If no item is found matching the criteria of the predicate.
203
213
*/
204
- export function get < T > ( source : T [ ] , predicate : ( item : T ) => boolean ) : T
214
+ export function get < T > ( source : T [ ] , predicate : ( item : T , index : number ) => boolean ) : T
205
215
export function get < T > ( a : any , b ?: any ) : any {
206
216
const partial = typeof a === 'function'
207
- const predicate : ( item : T ) => boolean = partial ? a : b
217
+ const predicate : ( item : T , index : number ) => boolean = partial ? a : b
208
218
function exec ( source : T [ ] ) : T | undefined {
219
+ let index = 0
209
220
for ( const item of source ) {
210
- if ( predicate ( item ) ) {
221
+ if ( predicate ( item , index ) ) {
211
222
return item
212
223
}
224
+ index ++
213
225
}
214
226
throw new Error ( 'Element not found matching criteria' )
215
227
}
@@ -221,42 +233,53 @@ export function get<T>(a: any, b?: any): any {
221
233
* @param predicate A function to test whether an item in the collection should be returned.
222
234
* @param source The input collection.
223
235
*/
224
- export function find < T > ( predicate : ( item : T ) => boolean ) : ( source : T [ ] ) => T | undefined
236
+ export function find < T > (
237
+ predicate : ( item : T , index : number ) => boolean
238
+ ) : ( source : T [ ] ) => T | undefined
225
239
/**
226
240
* Returns the first element for which the given function returns true, otherwise undefined.
227
241
* @param source The input collection.
228
242
* @param predicate A function to test whether an item in the collection should be returned.
229
243
*/
230
- export function find < T > ( source : T [ ] , predicate : ( item : T ) => boolean ) : T | undefined
244
+ export function find < T > ( source : T [ ] , predicate : ( item : T , index : number ) => boolean ) : T | undefined
231
245
export function find < T > ( a : any , b ?: any ) : any {
232
246
const partial = typeof a === 'function'
233
- const predicate : ( item : T ) => boolean = partial ? a : b
247
+ const predicate : ( item : T , index : number ) => boolean = partial ? a : b
234
248
function exec ( source : T [ ] ) : T | undefined {
249
+ let index = 0
235
250
for ( const item of source ) {
236
- if ( predicate ( item ) ) {
251
+ if ( predicate ( item , index ) ) {
237
252
return item
238
253
}
254
+ index ++
239
255
}
240
256
return undefined
241
257
}
242
258
return partial ? exec : exec ( a )
243
259
}
244
260
245
- export function groupBy < T , Key > ( selector : ( item : T ) => Key ) : ( source : T [ ] ) => [ Key , T [ ] ] [ ]
246
- export function groupBy < T , Key > ( source : T [ ] , selector : ( item : T ) => Key ) : [ Key , T [ ] ] [ ]
261
+ export function groupBy < T , Key > (
262
+ selector : ( item : T , index : number ) => Key
263
+ ) : ( source : T [ ] ) => [ Key , T [ ] ] [ ]
264
+ export function groupBy < T , Key > (
265
+ source : T [ ] ,
266
+ selector : ( item : T , index : number ) => Key
267
+ ) : [ Key , T [ ] ] [ ]
247
268
export function groupBy < T , Key > ( a : any , b ?: any ) : any {
248
269
const partial = typeof a === 'function'
249
- const selector : ( item : T ) => Key = partial ? a : b
270
+ const selector : ( item : T , index : number ) => Key = partial ? a : b
250
271
function exec ( source : T [ ] ) : [ Key , T [ ] ] [ ] {
251
272
const groups = new Map < Key , T [ ] > ( )
273
+ let index = 0
252
274
for ( const item of source ) {
253
- const key = selector ( item )
275
+ const key = selector ( item , index )
254
276
const group = groups . get ( key )
255
277
if ( group === undefined ) {
256
278
groups . set ( key , [ item ] )
257
279
} else {
258
280
group . push ( item )
259
281
}
282
+ index ++
260
283
}
261
284
return Array . from ( groups . entries ( ) )
262
285
}
0 commit comments