@stdlib/utils-async-group-by 0.0.8 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +0 -304
- package/NOTICE +1 -1
- package/README.md +38 -23
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +357 -41
- package/lib/factory.js +4 -4
- package/lib/index.js +3 -3
- package/lib/limit.js +0 -1
- package/lib/{group_by.js → main.js} +0 -1
- package/lib/validate.js +5 -4
- package/package.json +21 -18
- package/docs/repl.txt +0 -232
- package/docs/types/test.ts +0 -201
package/docs/types/index.d.ts
CHANGED
|
@@ -16,129 +16,224 @@
|
|
|
16
16
|
* limitations under the License.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
// TypeScript Version:
|
|
19
|
+
// TypeScript Version: 4.1
|
|
20
20
|
|
|
21
21
|
/// <reference types="@stdlib/types"/>
|
|
22
22
|
|
|
23
|
-
import { Collection } from '@stdlib/types/
|
|
23
|
+
import { Collection } from '@stdlib/types/array';
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Interface defining function options.
|
|
27
27
|
*/
|
|
28
|
-
interface
|
|
28
|
+
interface BaseOptions<T, V> {
|
|
29
|
+
/**
|
|
30
|
+
* Execution context.
|
|
31
|
+
*/
|
|
32
|
+
thisArg?: ThisParameterType<Indicator<T, V>>;
|
|
33
|
+
|
|
29
34
|
/**
|
|
30
35
|
* The maximum number of pending invocations at any one time.
|
|
31
36
|
*/
|
|
32
37
|
limit?: number;
|
|
33
38
|
|
|
34
39
|
/**
|
|
35
|
-
* Boolean indicating whether to
|
|
40
|
+
* Boolean indicating whether to sequentially invoke the `indicator` function for each `collection` element. If `true`, the function sets `options.limit=1`.
|
|
36
41
|
*/
|
|
37
42
|
series?: boolean;
|
|
43
|
+
}
|
|
38
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Interface defining function options.
|
|
47
|
+
*/
|
|
48
|
+
interface ValuesOptions<T, V> extends BaseOptions<T, V> {
|
|
39
49
|
/**
|
|
40
|
-
*
|
|
50
|
+
* Indicates that the function should return collection values when grouping.
|
|
51
|
+
*/
|
|
52
|
+
returns: 'values';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Interface defining function options.
|
|
57
|
+
*/
|
|
58
|
+
interface IndicesOptions<T, V> extends BaseOptions<T, V> {
|
|
59
|
+
/**
|
|
60
|
+
* Indicates that the function should return collection indices when grouping.
|
|
61
|
+
*/
|
|
62
|
+
returns: 'indices';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Interface defining function options.
|
|
67
|
+
*/
|
|
68
|
+
interface IndicesAndValuesOptions<T, V> extends BaseOptions<T, V> {
|
|
69
|
+
/**
|
|
70
|
+
* Indicates to return both indices and values when grouping collection elements.
|
|
71
|
+
*/
|
|
72
|
+
returns: '*';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Interface describing group results when returning values.
|
|
77
|
+
*/
|
|
78
|
+
interface ValuesResults<T> {
|
|
79
|
+
/**
|
|
80
|
+
* Results for an individual group.
|
|
41
81
|
*/
|
|
42
|
-
|
|
82
|
+
[ key: string ]: Array<T>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Interface describing group results when returning indices.
|
|
87
|
+
*/
|
|
88
|
+
interface IndicesResults<T> {
|
|
89
|
+
/**
|
|
90
|
+
* Results for an individual group.
|
|
91
|
+
*/
|
|
92
|
+
[ key: string ]: Array<number>;
|
|
93
|
+
}
|
|
43
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Interface describing group results when returning indices and values.
|
|
97
|
+
*/
|
|
98
|
+
interface IndicesAndValuesResults<T> {
|
|
44
99
|
/**
|
|
45
|
-
*
|
|
100
|
+
* Results for an individual group.
|
|
46
101
|
*/
|
|
47
|
-
|
|
102
|
+
[ key: string ]: Array<[ number, T ]>;
|
|
48
103
|
}
|
|
49
104
|
|
|
50
105
|
/**
|
|
51
106
|
* Callback invoked either upon processing all collection elements or upon encountering an error.
|
|
52
107
|
*/
|
|
53
|
-
type
|
|
108
|
+
type Nullary = () => void;
|
|
54
109
|
|
|
55
110
|
/**
|
|
56
111
|
* Callback invoked either upon processing all collection elements or upon encountering an error.
|
|
57
112
|
*
|
|
58
113
|
* @param error - encountered error or null
|
|
59
114
|
*/
|
|
60
|
-
type
|
|
115
|
+
type Unary = ( error: Error | null ) => void;
|
|
61
116
|
|
|
62
117
|
/**
|
|
63
118
|
* Callback invoked either upon processing all collection elements or upon encountering an error.
|
|
64
119
|
*
|
|
65
120
|
* @param error - encountered error or null
|
|
66
|
-
* @param result - group
|
|
121
|
+
* @param result - group results
|
|
67
122
|
*/
|
|
68
|
-
type
|
|
123
|
+
type ValuesBinary<T> = ( error: Error | null, result: ValuesResults<T> ) => void;
|
|
69
124
|
|
|
70
125
|
/**
|
|
71
126
|
* Callback invoked either upon processing all collection elements or upon encountering an error.
|
|
72
127
|
*
|
|
73
128
|
* @param error - encountered error or null
|
|
74
|
-
* @param result - group
|
|
129
|
+
* @param result - group results
|
|
75
130
|
*/
|
|
76
|
-
type
|
|
131
|
+
type IndicesBinary<T> = ( error: Error | null, result: IndicesResults<T> ) => void;
|
|
77
132
|
|
|
78
133
|
/**
|
|
79
|
-
* Callback
|
|
134
|
+
* Callback invoked either upon processing all collection elements or upon encountering an error.
|
|
135
|
+
*
|
|
136
|
+
* @param error - encountered error or null
|
|
137
|
+
* @param result - group results
|
|
80
138
|
*/
|
|
81
|
-
type
|
|
139
|
+
type IndicesAndValuesBinary<T> = ( error: Error | null, result: IndicesAndValuesResults<T> ) => void;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Callback invoked either upon processing all collection elements or upon encountering an error.
|
|
143
|
+
*
|
|
144
|
+
* @param error - encountered error or null
|
|
145
|
+
* @param result - group results
|
|
146
|
+
*/
|
|
147
|
+
type ValuesCallback<T> = Nullary | Unary | ValuesBinary<T>;
|
|
82
148
|
|
|
83
149
|
/**
|
|
84
|
-
* Callback
|
|
150
|
+
* Callback invoked either upon processing all collection elements or upon encountering an error.
|
|
85
151
|
*
|
|
86
152
|
* @param error - encountered error or null
|
|
153
|
+
* @param result - group results
|
|
87
154
|
*/
|
|
88
|
-
type
|
|
155
|
+
type IndicesCallback<T> = Nullary | Unary | IndicesBinary<T>;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Callback invoked either upon processing all collection elements or upon encountering an error.
|
|
159
|
+
*
|
|
160
|
+
* @param error - encountered error or null
|
|
161
|
+
* @param result - group results
|
|
162
|
+
*/
|
|
163
|
+
type IndicesAndValuesCallback<T> = Nullary | Unary | IndicesAndValuesBinary<T>;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Callback function to invoke once the indicator function has finished processing a collection value.
|
|
167
|
+
*/
|
|
168
|
+
type NullaryNext = () => void;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Callback function to invoke once the indicator function has finished processing a collection value.
|
|
172
|
+
*
|
|
173
|
+
* @param error - encountered error or null
|
|
174
|
+
*/
|
|
175
|
+
type UnaryNext = ( error: Error | null ) => void;
|
|
89
176
|
|
|
90
177
|
/**
|
|
91
|
-
* Callback function.
|
|
178
|
+
* Callback function to invoke once the indicator function has finished processing a collection value.
|
|
92
179
|
*
|
|
93
180
|
* @param error - encountered error or null
|
|
94
181
|
* @param group - value group
|
|
95
182
|
*/
|
|
96
|
-
type
|
|
183
|
+
type BinaryNext = ( error: Error | null, group: string ) => void;
|
|
97
184
|
|
|
98
185
|
/**
|
|
99
|
-
* Callback function.
|
|
186
|
+
* Callback function to invoke once the indicator function has finished processing a collection value.
|
|
100
187
|
*
|
|
101
188
|
* @param error - encountered error or null
|
|
102
189
|
* @param group - value group
|
|
103
190
|
*/
|
|
104
|
-
type
|
|
191
|
+
type Next = NullaryNext | UnaryNext | BinaryNext;
|
|
105
192
|
|
|
106
193
|
/**
|
|
107
|
-
*
|
|
194
|
+
* Returns the group to which a collection element belongs.
|
|
108
195
|
*
|
|
109
196
|
* @param value - collection value
|
|
110
197
|
* @param next - a callback to be invoked after processing a collection `value`
|
|
111
198
|
*/
|
|
112
|
-
type BinaryIndicator = ( value:
|
|
199
|
+
type BinaryIndicator<T, V> = ( this: V, value: T, next: Next ) => void;
|
|
113
200
|
|
|
114
201
|
/**
|
|
115
|
-
*
|
|
202
|
+
* Returns the group to which a collection element belongs.
|
|
116
203
|
*
|
|
117
204
|
* @param value - collection value
|
|
118
205
|
* @param index - collection index
|
|
119
206
|
* @param next - a callback to be invoked after processing a collection `value`
|
|
120
207
|
*/
|
|
121
|
-
type TernaryIndicator = ( value:
|
|
208
|
+
type TernaryIndicator<T, V> = ( this: V, value: T, index: number, next: Next ) => void;
|
|
122
209
|
|
|
123
210
|
/**
|
|
124
|
-
*
|
|
211
|
+
* Returns the group to which a collection element belongs.
|
|
125
212
|
*
|
|
126
213
|
* @param value - collection value
|
|
127
214
|
* @param index - collection index
|
|
128
215
|
* @param collection - input collection
|
|
129
216
|
* @param next - a callback to be invoked after processing a collection `value`
|
|
130
217
|
*/
|
|
131
|
-
type QuaternaryIndicator = ( value:
|
|
218
|
+
type QuaternaryIndicator<T, V> = ( this: V, value: T, index: number, collection: Collection<T>, next: Next ) => void;
|
|
132
219
|
|
|
133
220
|
/**
|
|
134
|
-
*
|
|
221
|
+
* Returns the group to which a collection element belongs.
|
|
135
222
|
*
|
|
136
223
|
* @param value - collection value
|
|
137
224
|
* @param index - collection index
|
|
138
225
|
* @param collection - input collection
|
|
139
226
|
* @param next - a callback to be invoked after processing a collection `value`
|
|
140
227
|
*/
|
|
141
|
-
type Indicator =
|
|
228
|
+
type Indicator<T, V> = BinaryIndicator<T, V> | TernaryIndicator<T, V> | QuaternaryIndicator<T, V>;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Invokes an indicator function for each element in a collection.
|
|
232
|
+
*
|
|
233
|
+
* @param collection - input collection
|
|
234
|
+
* @param done - function to invoke upon completion
|
|
235
|
+
*/
|
|
236
|
+
type ValuesFactoryFunction<T> = ( collection: Collection<T>, done: ValuesCallback<T> ) => void;
|
|
142
237
|
|
|
143
238
|
/**
|
|
144
239
|
* Invokes an indicator function for each element in a collection.
|
|
@@ -146,7 +241,15 @@ type Indicator = Unary | BinaryIndicator | TernaryIndicator | QuaternaryIndicato
|
|
|
146
241
|
* @param collection - input collection
|
|
147
242
|
* @param done - function to invoke upon completion
|
|
148
243
|
*/
|
|
149
|
-
type
|
|
244
|
+
type IndicesFactoryFunction<T> = ( collection: Collection<T>, done: IndicesCallback<T> ) => void;
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Invokes an indicator function for each element in a collection.
|
|
248
|
+
*
|
|
249
|
+
* @param collection - input collection
|
|
250
|
+
* @param done - function to invoke upon completion
|
|
251
|
+
*/
|
|
252
|
+
type IndicesAndValuesFactoryFunction<T> = ( collection: Collection<T>, done: IndicesAndValuesCallback<T> ) => void;
|
|
150
253
|
|
|
151
254
|
/**
|
|
152
255
|
* Interface for `groupByAsync`.
|
|
@@ -159,6 +262,55 @@ interface GroupByAsync {
|
|
|
159
262
|
*
|
|
160
263
|
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
|
|
161
264
|
*
|
|
265
|
+
* @param collection - input collection
|
|
266
|
+
* @param options - function options
|
|
267
|
+
* @param options.thisArg - execution context
|
|
268
|
+
* @param options.limit - maximum number of pending invocations at any one time
|
|
269
|
+
* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: false)
|
|
270
|
+
* @param options.returns - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned (default: 'values')
|
|
271
|
+
* @param indicator - indicator function specifying which group an element in the input collection belongs to
|
|
272
|
+
* @param done - function to invoke upon completion
|
|
273
|
+
* @throws must provide valid options
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* var readFile = require( '@stdlib/fs-read-file' );
|
|
277
|
+
*
|
|
278
|
+
* function done( error, result ) {
|
|
279
|
+
* if ( error ) {
|
|
280
|
+
* throw error;
|
|
281
|
+
* }
|
|
282
|
+
* console.log( result );
|
|
283
|
+
* }
|
|
284
|
+
*
|
|
285
|
+
* function indicator( file, next ) {
|
|
286
|
+
* var opts = {
|
|
287
|
+
* 'encoding': 'utf8'
|
|
288
|
+
* };
|
|
289
|
+
* readFile( file, opts, onFile );
|
|
290
|
+
*
|
|
291
|
+
* function onFile( error ) {
|
|
292
|
+
* if ( error ) {
|
|
293
|
+
* return next( null, 'nonreadable' );
|
|
294
|
+
* }
|
|
295
|
+
* next( null, 'readable' );
|
|
296
|
+
* }
|
|
297
|
+
* }
|
|
298
|
+
*
|
|
299
|
+
* var files = [
|
|
300
|
+
* './beep.js',
|
|
301
|
+
* './boop.js'
|
|
302
|
+
* ];
|
|
303
|
+
*
|
|
304
|
+
* groupByAsync( files, {}, indicator, done );
|
|
305
|
+
*/
|
|
306
|
+
<T = unknown, V = unknown>( collection: Collection<T>, options: IndicesOptions<T, V>, indicator: Indicator<T, V>, done: IndicesCallback<T> ): void;
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Groups values according to an indicator function.
|
|
310
|
+
*
|
|
311
|
+
* ## Notes
|
|
312
|
+
*
|
|
313
|
+
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
|
|
162
314
|
*
|
|
163
315
|
* @param collection - input collection
|
|
164
316
|
* @param options - function options
|
|
@@ -171,7 +323,7 @@ interface GroupByAsync {
|
|
|
171
323
|
* @throws must provide valid options
|
|
172
324
|
*
|
|
173
325
|
* @example
|
|
174
|
-
* var readFile = require(
|
|
326
|
+
* var readFile = require( '@stdlib/fs-read-file' );
|
|
175
327
|
*
|
|
176
328
|
* function done( error, result ) {
|
|
177
329
|
* if ( error ) {
|
|
@@ -201,7 +353,7 @@ interface GroupByAsync {
|
|
|
201
353
|
*
|
|
202
354
|
* groupByAsync( files, {}, indicator, done );
|
|
203
355
|
*/
|
|
204
|
-
( collection: Collection
|
|
356
|
+
<T = unknown, V = unknown>( collection: Collection<T>, options: IndicesAndValuesOptions<T, V>, indicator: Indicator<T, V>, done: IndicesAndValuesCallback<T> ): void;
|
|
205
357
|
|
|
206
358
|
/**
|
|
207
359
|
* Groups values according to an indicator function.
|
|
@@ -210,13 +362,62 @@ interface GroupByAsync {
|
|
|
210
362
|
*
|
|
211
363
|
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
|
|
212
364
|
*
|
|
365
|
+
* @param collection - input collection
|
|
366
|
+
* @param options - function options
|
|
367
|
+
* @param options.thisArg - execution context
|
|
368
|
+
* @param options.limit - maximum number of pending invocations at any one time
|
|
369
|
+
* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: false)
|
|
370
|
+
* @param options.returns - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned (default: 'values')
|
|
371
|
+
* @param indicator - indicator function specifying which group an element in the input collection belongs to
|
|
372
|
+
* @param done - function to invoke upon completion
|
|
373
|
+
* @throws must provide valid options
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* var readFile = require( '@stdlib/fs-read-file' );
|
|
377
|
+
*
|
|
378
|
+
* function done( error, result ) {
|
|
379
|
+
* if ( error ) {
|
|
380
|
+
* throw error;
|
|
381
|
+
* }
|
|
382
|
+
* console.log( result );
|
|
383
|
+
* }
|
|
384
|
+
*
|
|
385
|
+
* function indicator( file, next ) {
|
|
386
|
+
* var opts = {
|
|
387
|
+
* 'encoding': 'utf8'
|
|
388
|
+
* };
|
|
389
|
+
* readFile( file, opts, onFile );
|
|
390
|
+
*
|
|
391
|
+
* function onFile( error ) {
|
|
392
|
+
* if ( error ) {
|
|
393
|
+
* return next( null, 'nonreadable' );
|
|
394
|
+
* }
|
|
395
|
+
* next( null, 'readable' );
|
|
396
|
+
* }
|
|
397
|
+
* }
|
|
398
|
+
*
|
|
399
|
+
* var files = [
|
|
400
|
+
* './beep.js',
|
|
401
|
+
* './boop.js'
|
|
402
|
+
* ];
|
|
403
|
+
*
|
|
404
|
+
* groupByAsync( files, {}, indicator, done );
|
|
405
|
+
*/
|
|
406
|
+
<T = unknown, V = unknown>( collection: Collection<T>, options: ValuesOptions<T, V> | BaseOptions<T, V>, indicator: Indicator<T, V>, done: ValuesCallback<T> ): void;
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Groups values according to an indicator function.
|
|
410
|
+
*
|
|
411
|
+
* ## Notes
|
|
412
|
+
*
|
|
413
|
+
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
|
|
213
414
|
*
|
|
214
415
|
* @param collection - input collection
|
|
215
416
|
* @param indicator - indicator function specifying which group an element in the input collection belongs to
|
|
216
417
|
* @param done - function to invoke upon completion
|
|
217
418
|
*
|
|
218
419
|
* @example
|
|
219
|
-
* var readFile = require(
|
|
420
|
+
* var readFile = require( '@stdlib/fs-read-file' );
|
|
220
421
|
*
|
|
221
422
|
* function done( error, result ) {
|
|
222
423
|
* if ( error ) {
|
|
@@ -246,7 +447,7 @@ interface GroupByAsync {
|
|
|
246
447
|
*
|
|
247
448
|
* groupByAsync( files, indicator, done );
|
|
248
449
|
*/
|
|
249
|
-
( collection: Collection
|
|
450
|
+
<T = unknown, V = unknown>( collection: Collection<T>, indicator: Indicator<T, V>, done: ValuesCallback<T> ): void;
|
|
250
451
|
|
|
251
452
|
/**
|
|
252
453
|
* Returns a function for grouping values according to an indicator function.
|
|
@@ -255,6 +456,64 @@ interface GroupByAsync {
|
|
|
255
456
|
*
|
|
256
457
|
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
|
|
257
458
|
*
|
|
459
|
+
* @param options - function options
|
|
460
|
+
* @param options.thisArg - execution context
|
|
461
|
+
* @param options.limit - maximum number of pending invocations at any one time
|
|
462
|
+
* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: false)
|
|
463
|
+
* @param options.returns - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned (default: 'values')
|
|
464
|
+
* @param indicator - indicator function specifying which group an element in the input collection belongs to
|
|
465
|
+
* @throws must provide valid options
|
|
466
|
+
* @returns function which invokes the indicator function once for each element in a collection
|
|
467
|
+
*
|
|
468
|
+
* @example
|
|
469
|
+
* var readFile = require( '@stdlib/fs-read-file' );
|
|
470
|
+
*
|
|
471
|
+
* function indicator( file, next ) {
|
|
472
|
+
* var opts = {
|
|
473
|
+
* 'encoding': 'utf8'
|
|
474
|
+
* };
|
|
475
|
+
* readFile( file, opts, onFile );
|
|
476
|
+
*
|
|
477
|
+
* function onFile( error ) {
|
|
478
|
+
* if ( error ) {
|
|
479
|
+
* return next( null, 'nonreadable' );
|
|
480
|
+
* }
|
|
481
|
+
* next( null, 'readable' );
|
|
482
|
+
* }
|
|
483
|
+
* }
|
|
484
|
+
*
|
|
485
|
+
* var opts = {
|
|
486
|
+
* 'series': true
|
|
487
|
+
* };
|
|
488
|
+
*
|
|
489
|
+
* // Create a `groupByAsync` function which invokes the indicator function for each collection element sequentially:
|
|
490
|
+
* var groupByAsync = factory( opts, indicator );
|
|
491
|
+
*
|
|
492
|
+
* // Create a collection over which to iterate:
|
|
493
|
+
* var files = [
|
|
494
|
+
* './beep.js',
|
|
495
|
+
* './boop.js'
|
|
496
|
+
* ];
|
|
497
|
+
*
|
|
498
|
+
* // Define a callback which handles results:
|
|
499
|
+
* function done( error, result ) {
|
|
500
|
+
* if ( error ) {
|
|
501
|
+
* throw error;
|
|
502
|
+
* }
|
|
503
|
+
* console.log( result );
|
|
504
|
+
* }
|
|
505
|
+
*
|
|
506
|
+
* // Try to read each element in `files`:
|
|
507
|
+
* groupByAsync( files, done );
|
|
508
|
+
*/
|
|
509
|
+
factory<T = unknown, V = unknown>( options: IndicesOptions<T, V>, indicator: Indicator<T, V> ): IndicesFactoryFunction<T>;
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Returns a function for grouping values according to an indicator function.
|
|
513
|
+
*
|
|
514
|
+
* ## Notes
|
|
515
|
+
*
|
|
516
|
+
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
|
|
258
517
|
*
|
|
259
518
|
* @param options - function options
|
|
260
519
|
* @param options.thisArg - execution context
|
|
@@ -266,7 +525,7 @@ interface GroupByAsync {
|
|
|
266
525
|
* @returns function which invokes the indicator function once for each element in a collection
|
|
267
526
|
*
|
|
268
527
|
* @example
|
|
269
|
-
* var readFile = require(
|
|
528
|
+
* var readFile = require( '@stdlib/fs-read-file' );
|
|
270
529
|
*
|
|
271
530
|
* function indicator( file, next ) {
|
|
272
531
|
* var opts = {
|
|
@@ -306,7 +565,7 @@ interface GroupByAsync {
|
|
|
306
565
|
* // Try to read each element in `files`:
|
|
307
566
|
* groupByAsync( files, done );
|
|
308
567
|
*/
|
|
309
|
-
factory( options:
|
|
568
|
+
factory<T = unknown, V = unknown>( options: IndicesAndValuesOptions<T, V>, indicator: Indicator<T, V> ): IndicesAndValuesFactoryFunction<T>;
|
|
310
569
|
|
|
311
570
|
/**
|
|
312
571
|
* Returns a function for grouping values according to an indicator function.
|
|
@@ -315,12 +574,70 @@ interface GroupByAsync {
|
|
|
315
574
|
*
|
|
316
575
|
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
|
|
317
576
|
*
|
|
577
|
+
* @param options - function options
|
|
578
|
+
* @param options.thisArg - execution context
|
|
579
|
+
* @param options.limit - maximum number of pending invocations at any one time
|
|
580
|
+
* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: false)
|
|
581
|
+
* @param options.returns - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned (default: 'values')
|
|
582
|
+
* @param indicator - indicator function specifying which group an element in the input collection belongs to
|
|
583
|
+
* @throws must provide valid options
|
|
584
|
+
* @returns function which invokes the indicator function once for each element in a collection
|
|
585
|
+
*
|
|
586
|
+
* @example
|
|
587
|
+
* var readFile = require( '@stdlib/fs-read-file' );
|
|
588
|
+
*
|
|
589
|
+
* function indicator( file, next ) {
|
|
590
|
+
* var opts = {
|
|
591
|
+
* 'encoding': 'utf8'
|
|
592
|
+
* };
|
|
593
|
+
* readFile( file, opts, onFile );
|
|
594
|
+
*
|
|
595
|
+
* function onFile( error ) {
|
|
596
|
+
* if ( error ) {
|
|
597
|
+
* return next( null, 'nonreadable' );
|
|
598
|
+
* }
|
|
599
|
+
* next( null, 'readable' );
|
|
600
|
+
* }
|
|
601
|
+
* }
|
|
602
|
+
*
|
|
603
|
+
* var opts = {
|
|
604
|
+
* 'series': true
|
|
605
|
+
* };
|
|
606
|
+
*
|
|
607
|
+
* // Create a `groupByAsync` function which invokes the indicator function for each collection element sequentially:
|
|
608
|
+
* var groupByAsync = factory( opts, indicator );
|
|
609
|
+
*
|
|
610
|
+
* // Create a collection over which to iterate:
|
|
611
|
+
* var files = [
|
|
612
|
+
* './beep.js',
|
|
613
|
+
* './boop.js'
|
|
614
|
+
* ];
|
|
615
|
+
*
|
|
616
|
+
* // Define a callback which handles results:
|
|
617
|
+
* function done( error, result ) {
|
|
618
|
+
* if ( error ) {
|
|
619
|
+
* throw error;
|
|
620
|
+
* }
|
|
621
|
+
* console.log( result );
|
|
622
|
+
* }
|
|
623
|
+
*
|
|
624
|
+
* // Try to read each element in `files`:
|
|
625
|
+
* groupByAsync( files, done );
|
|
626
|
+
*/
|
|
627
|
+
factory<T = unknown, V = unknown>( options: ValuesOptions<T, V> | BaseOptions<T, V>, indicator: Indicator<T, V> ): ValuesFactoryFunction<T>;
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* Returns a function for grouping values according to an indicator function.
|
|
631
|
+
*
|
|
632
|
+
* ## Notes
|
|
633
|
+
*
|
|
634
|
+
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
|
|
318
635
|
*
|
|
319
636
|
* @param indicator - indicator function specifying which group an element in the input collection belongs to
|
|
320
637
|
* @returns function which invokes the indicator function once for each element in a collection
|
|
321
638
|
*
|
|
322
639
|
* @example
|
|
323
|
-
* var readFile = require(
|
|
640
|
+
* var readFile = require( '@stdlib/fs-read-file' );
|
|
324
641
|
*
|
|
325
642
|
* function indicator( file, next ) {
|
|
326
643
|
* var opts = {
|
|
@@ -356,7 +673,7 @@ interface GroupByAsync {
|
|
|
356
673
|
* // Try to read each element in `files`:
|
|
357
674
|
* groupByAsync( files, done );
|
|
358
675
|
*/
|
|
359
|
-
factory( indicator: Indicator ):
|
|
676
|
+
factory<T = unknown, V = unknown>( indicator: Indicator<T, V> ): ValuesFactoryFunction<T>;
|
|
360
677
|
}
|
|
361
678
|
|
|
362
679
|
/**
|
|
@@ -366,7 +683,6 @@ interface GroupByAsync {
|
|
|
366
683
|
*
|
|
367
684
|
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
|
|
368
685
|
*
|
|
369
|
-
*
|
|
370
686
|
* @param collection - input collection
|
|
371
687
|
* @param options - function options
|
|
372
688
|
* @param options.thisArg - execution context
|
|
@@ -378,7 +694,7 @@ interface GroupByAsync {
|
|
|
378
694
|
* @throws must provide valid options
|
|
379
695
|
*
|
|
380
696
|
* @example
|
|
381
|
-
* var readFile = require(
|
|
697
|
+
* var readFile = require( '@stdlib/fs-read-file' );
|
|
382
698
|
*
|
|
383
699
|
* function done( error, result ) {
|
|
384
700
|
* if ( error ) {
|
package/lib/factory.js
CHANGED
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
|
|
23
23
|
var isFunction = require( '@stdlib/assert-is-function' );
|
|
24
24
|
var isCollection = require( '@stdlib/assert-is-collection' );
|
|
25
|
+
var format = require( '@stdlib/string-format' );
|
|
25
26
|
var PINF = require( '@stdlib/constants-float64-pinf' );
|
|
26
27
|
var validate = require( './validate.js' );
|
|
27
28
|
var limit = require( './limit.js' );
|
|
@@ -36,7 +37,6 @@ var limit = require( './limit.js' );
|
|
|
36
37
|
*
|
|
37
38
|
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
|
|
38
39
|
*
|
|
39
|
-
*
|
|
40
40
|
* @param {Options} [options] - function options
|
|
41
41
|
* @param {*} [options.thisArg] - execution context
|
|
42
42
|
* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time
|
|
@@ -105,7 +105,7 @@ function factory( options, indicator ) {
|
|
|
105
105
|
f = options;
|
|
106
106
|
}
|
|
107
107
|
if ( !isFunction( f ) ) {
|
|
108
|
-
throw new TypeError( 'invalid argument. Last argument must be a function. Value:
|
|
108
|
+
throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );
|
|
109
109
|
}
|
|
110
110
|
if ( opts.series ) {
|
|
111
111
|
opts.limit = 1;
|
|
@@ -126,10 +126,10 @@ function factory( options, indicator ) {
|
|
|
126
126
|
*/
|
|
127
127
|
function groupByAsync( collection, done ) {
|
|
128
128
|
if ( !isCollection( collection ) ) {
|
|
129
|
-
throw new TypeError( 'invalid argument. First argument must be a collection. Value:
|
|
129
|
+
throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );
|
|
130
130
|
}
|
|
131
131
|
if ( !isFunction( done ) ) {
|
|
132
|
-
throw new TypeError( 'invalid argument. Last argument must be a function. Value:
|
|
132
|
+
throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );
|
|
133
133
|
}
|
|
134
134
|
return limit( collection, opts, f, clbk );
|
|
135
135
|
|
package/lib/index.js
CHANGED
|
@@ -59,15 +59,15 @@
|
|
|
59
59
|
// MODULES //
|
|
60
60
|
|
|
61
61
|
var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
|
|
62
|
-
var
|
|
62
|
+
var main = require( './main.js' );
|
|
63
63
|
var factory = require( './factory.js' );
|
|
64
64
|
|
|
65
65
|
|
|
66
66
|
// MAIN //
|
|
67
67
|
|
|
68
|
-
setReadOnly(
|
|
68
|
+
setReadOnly( main, 'factory', factory );
|
|
69
69
|
|
|
70
70
|
|
|
71
71
|
// EXPORTS //
|
|
72
72
|
|
|
73
|
-
module.exports =
|
|
73
|
+
module.exports = main;
|
package/lib/limit.js
CHANGED
|
@@ -39,7 +39,6 @@ var debug = logger( 'group-by-async:limit' );
|
|
|
39
39
|
* - We need to cache the collection value to prevent the edge case where, during the invocation of the indicator function, the element at index `i` is swapped for some other value. For some, that might be a feature; here, we take the stance that one should be less clever.
|
|
40
40
|
* - Checking for an "own" property is necessary to guard against the edge case where an indicator function returns a group identifier which matches a method or property on the `Object` prototype.
|
|
41
41
|
*
|
|
42
|
-
*
|
|
43
42
|
* @private
|
|
44
43
|
* @param {Collection} collection - input collection
|
|
45
44
|
* @param {Options} opts - function options
|
|
@@ -32,7 +32,6 @@ var factory = require( './factory.js' );
|
|
|
32
32
|
*
|
|
33
33
|
* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
|
|
34
34
|
*
|
|
35
|
-
*
|
|
36
35
|
* @param {Collection} collection - input collection
|
|
37
36
|
* @param {Options} [options] - function options
|
|
38
37
|
* @param {*} [options.thisArg] - execution context
|