@@ -14,15 +14,17 @@ import type {
14
14
* Returns a {@link Reducer} that collects values to an `Array`.
15
15
*
16
16
* @example
17
- * ```js
17
+ * ```js playground
18
+ * import { cycle, pipe, reduce, take, toArray } from 'lfi'
19
+ *
18
20
* console.log(
19
21
* pipe(
20
- * cycle([`sloth`, `more sloth `]),
22
+ * cycle([`sloth`, `lazy`, `sleep `]),
21
23
* take(4),
22
24
* reduce(toArray()),
23
25
* ),
24
26
* )
25
- * //=> [ 'sloth', 'more sloth ', 'sloth ', 'more sloth' ]
27
+ * //=> [ 'sloth', 'lazy ', 'sleep ', 'sloth' ]
26
28
* ```
27
29
*
28
30
* @category Collections
@@ -34,15 +36,21 @@ export const toArray: <Value>() => Reducer<Value, Value[]>
34
36
* Returns a {@link Reducer} that collects values to a `Set`.
35
37
*
36
38
* @example
37
- * ```js
39
+ * ```js playground
40
+ * import { cycle, pipe, reduce, take, toSet } from 'lfi'
41
+ *
38
42
* console.log(
39
43
* pipe(
40
- * cycle([`sloth`, `more sloth `]),
44
+ * cycle([`sloth`, `lazy`, `sleep `]),
41
45
* take(4),
42
- * reduce(toArray ()),
46
+ * reduce(toSet ()),
43
47
* ),
44
48
* )
45
- * //=> Set(2) { 'sloth', 'more sloth' }
49
+ * //=> Set(3) {
50
+ * //=> 'sloth',
51
+ * //=> 'lazy',
52
+ * //=> 'sleep'
53
+ * //=> }
46
54
* ```
47
55
*
48
56
* @category Collections
@@ -54,10 +62,12 @@ export const toSet: <Value>() => Reducer<Value, Set<Value>>
54
62
* Returns a {@link Reducer} that collects objects to a `WeakSet`.
55
63
*
56
64
* @example
57
- * ```js
65
+ * ```js playground
66
+ * import { cycle, map, pipe, reduce, take, toWeakSet } from 'lfi'
67
+ *
58
68
* console.log(
59
69
* pipe(
60
- * cycle([`sloth`, `more sloth `]),
70
+ * cycle([`sloth`, `lazy`, `sleep `]),
61
71
* take(4),
62
72
* map(string => ({ sloth: string })),
63
73
* reduce(toWeakSet()),
@@ -80,15 +90,21 @@ export const toWeakSet: <Value extends object>() => Reducer<
80
90
* In the case of pairs with duplicate keys, the value of the last one wins.
81
91
*
82
92
* @example
83
- * ```js
93
+ * ```js playground
94
+ * import { map, pipe, reduce, toObject } from 'lfi'
95
+ *
84
96
* console.log(
85
97
* pipe(
86
- * [`sloth`, `more sloth `, `even more sloth `],
98
+ * [`sloth`, `lazy `, `sleep `],
87
99
* map(string => [string, string.length]),
88
100
* reduce(toObject()),
89
101
* ),
90
102
* )
91
- * //=> { sloth: 5, 'more sloth': 10, 'even more sloth': 15 }
103
+ * //=> {
104
+ * //=> sloth: 5,
105
+ * //=> lazy: 4,
106
+ * //=> sleep: 5
107
+ * //=> }
92
108
* ```
93
109
*
94
110
* @category Collections
@@ -106,15 +122,21 @@ export const toObject: <Key extends keyof never, Value>() => RawKeyedReducer<
106
122
* In the case of pairs with duplicate keys, the value of the last one wins.
107
123
*
108
124
* @example
109
- * ```js
125
+ * ```js playground
126
+ * import { map, pipe, reduce, toMap } from 'lfi'
127
+ *
110
128
* console.log(
111
129
* pipe(
112
- * [`sloth`, `more sloth `, `even more sloth `],
130
+ * [`sloth`, `lazy `, `sleep `],
113
131
* map(string => [string, string.length]),
114
132
* reduce(toMap()),
115
133
* ),
116
134
* )
117
- * //=> Map(3) { 'sloth' => 5, 'more sloth' => 10, 'even more sloth' => 15 }
135
+ * //=> Map(3) {
136
+ * //=> 'sloth' => 5,
137
+ * //=> 'lazy' => 4,
138
+ * //=> 'sleep' => 5
139
+ * //=> }
118
140
* ```
119
141
*
120
142
* @category Collections
@@ -132,10 +154,12 @@ export const toMap: <Key, Value>() => RawKeyedReducer<
132
154
* In the case of pairs with duplicate keys, the value of the last one wins.
133
155
*
134
156
* @example
135
- * ```js
157
+ * ```js playground
158
+ * import { map, pipe, reduce, toWeakMap } from 'lfi'
159
+ *
136
160
* console.log(
137
161
* pipe(
138
- * [`sloth`, `more sloth `, `even more sloth `],
162
+ * [`sloth`, `lazy `, `sleep `],
139
163
* map(string => [{ sloth: string }, string.length]),
140
164
* reduce(toWeakMap()),
141
165
* ),
@@ -157,18 +181,19 @@ export const toWeakMap: <Key extends object, Value>() => RawKeyedReducer<
157
181
* and reduces values with the same key using `innerReducer`.
158
182
*
159
183
* @example
160
- * ```js
184
+ * ```js playground
185
+ * import { map, pipe, reduce, toArray, toGrouped, toMap } from 'lfi'
186
+ *
161
187
* console.log(
162
188
* pipe(
163
- * [`sloth`, `some sloth `, `sleep`, `more sloth`, `even more sloth `],
189
+ * [`sloth`, `lazy `, `sleep`],
164
190
* map(string => [string.length, string]),
165
191
* reduce(toGrouped(toArray(), toMap())),
166
192
* ),
167
193
* )
168
- * //=> Map(3 ) {
194
+ * //=> Map(2 ) {
169
195
* //=> 5 => [ 'sloth', 'sleep' ],
170
- * //=> 10 => [ 'some sloth', 'more sloth' ],
171
- * //=> 15 => [ 'even more sloth' ]
196
+ * //=> 4 => [ 'lazy' ]
172
197
* //=> }
173
198
* ```
174
199
*
@@ -252,19 +277,25 @@ export const toGrouped: {
252
277
* an {@link OptionalReducer}. Otherwise, returns a {@link Reducer}.
253
278
*
254
279
* @example
255
- * ```js
280
+ * ```js playground
281
+ * import { map, pipe, reduce, toCount, toJoin, toMultiple, toSet } from 'lfi'
282
+ *
256
283
* console.log(
257
284
* pipe(
258
- * [`sloth`, `some sloth `, `sleep`, `more sloth`, `even more sloth `],
285
+ * [`sloth`, `lazy `, `sleep`],
259
286
* map(string => string.length),
260
287
* reduce(toMultiple([toSet(), toCount(), toJoin(`,`)])),
261
288
* ),
262
289
* )
263
- * //=> [ Set(3) { 5, 10, 15 }, 5, '5,10,5,10,15' ]
290
+ * //=> [
291
+ * //=> Set(2) { 5, 4 },
292
+ * //=> 3,
293
+ * //=> '5,4,5'
294
+ * //=> ]
264
295
*
265
296
* console.log(
266
297
* pipe(
267
- * [`sloth`, `some sloth `, `sleep`, `more sloth`, `even more sloth `],
298
+ * [`sloth`, `lazy `, `sleep`],
268
299
* map(string => string.length),
269
300
* reduce(
270
301
* toMultiple({
@@ -275,7 +306,11 @@ export const toGrouped: {
275
306
* ),
276
307
* ),
277
308
* )
278
- * //=> { set: Set(3) { 5, 10, 15 }, count: 5, string: '5,10,5,10,15' }
309
+ * //=> {
310
+ * //=> set: Set(2) { 5, 4 },
311
+ * //=> count: 3,
312
+ * //=> string: '5,4,5'
313
+ * //=> }
279
314
* ```
280
315
*
281
316
* @category Collections
@@ -366,15 +401,20 @@ export const toMultiple: {
366
401
* {@link joinConcur} for direct use on iterables.
367
402
*
368
403
* @example
369
- * ```js
404
+ * ```js playground
405
+ * import { map, pipe, reduce, toGrouped, toJoin, toMap } from 'lfi'
406
+ *
370
407
* console.log(
371
408
* pipe(
372
- * [`sloth`, `more sloth `, `sleep`, `some sloth `],
409
+ * [`sloth`, `lazy `, `sleep`],
373
410
* map(string => [string.length, string]),
374
411
* reduce(toGrouped(toJoin(`,`), toMap())),
375
412
* ),
376
413
* )
377
- * //=> Map(2) { 5 => 'sloth,sleep', 10 => 'more sloth,some sloth' }
414
+ * //=> Map(2) {
415
+ * //=> 5 => 'sloth,sleep',
416
+ * //=> 4 => 'lazy'
417
+ * //=> }
378
418
* ```
379
419
*
380
420
* @category Collections
@@ -383,21 +423,24 @@ export const toMultiple: {
383
423
export const toJoin : ( separator : string ) => Reducer < unknown , unknown , string >
384
424
385
425
/**
386
- * Returns the result of concatenating the values of `iterable` to a string
387
- * where values are separated by `separator`.
426
+ * Returns the string concatenation of the values of `iterable`, separated by
427
+ * `separator`.
388
428
*
389
- * Like `Array.prototype.join`, but for iterables, but does not treat `null`,
390
- * `undefined`, or `[]` specially.
429
+ * Like `Array.prototype.join`, but for iterables, except it does not treat
430
+ * `null`, ` undefined`, or `[]` specially.
391
431
*
392
432
* @example
393
- * ```js
433
+ * ```js playground
434
+ * import { join, map, pipe } from 'lfi'
435
+ *
394
436
* console.log(
395
437
* pipe(
396
- * [`sloth`, `more sloth`, `even more sloth`],
438
+ * [`sloth`, `lazy`, `sleep`],
439
+ * map(string => string.toUpperCase()),
397
440
* join(`, `),
398
441
* ),
399
442
* )
400
- * //=> sloth, more sloth, even more sloth
443
+ * //=> SLOTH, LAZY, SLEEP
401
444
* ```
402
445
*
403
446
* @category Collections
@@ -409,21 +452,29 @@ export const join: {
409
452
}
410
453
411
454
/**
412
- * Returns a promise that resolves to the result of concatenating the values of
413
- * `asyncIterable` to a string where values are separated by `separator`.
455
+ * Returns a promise that resolves to the string concatenation of the values of
456
+ * `asyncIterable`, separated by `separator`.
414
457
*
415
- * Like `Array.prototype.join`, but for async iterables, but does not treat
416
- * `null`, `undefined`, or `[]` specially.
458
+ * Like `Array.prototype.join`, but for async iterables, except it does not
459
+ * treat `null`, `undefined`, or `[]` specially.
417
460
*
418
461
* @example
419
- * ```js
462
+ * ```js playground
463
+ * import { asAsync, joinAsync, mapAsync, pipe } from 'lfi'
464
+ *
465
+ * const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
466
+ *
420
467
* console.log(
421
468
* await pipe(
422
- * asAsync([`sloth`, `more sloth`, `even more sloth`]),
469
+ * asAsync([`sloth`, `lazy`, `sleep`]),
470
+ * mapAsync(async word => {
471
+ * const response = await fetch(`${API_URL}/${word}`)
472
+ * return (await response.json())[0].phonetic
473
+ * }),
423
474
* joinAsync(`, `),
424
475
* ),
425
476
* )
426
- * //=> sloth, more sloth, even more sloth
477
+ * //=> /slɑθ/, /ˈleɪzi/, /sliːp/
427
478
* ```
428
479
*
429
480
* @category Collections
@@ -437,21 +488,33 @@ export const joinAsync: {
437
488
}
438
489
439
490
/**
440
- * Returns a promise that resolves to the result of concatenating the values of
441
- * `concurIterable` to a string where values are separated by `separator`.
491
+ * Returns a promise that resolves to the string concatenation of the values of
492
+ * `concurIterable`, separated by `separator`.
442
493
*
443
- * Like `Array.prototype.join`, but for concur iterables, but does not treat
444
- * `null`, `undefined`, or `[]` specially.
494
+ * Like `Array.prototype.join`, but for concur iterables, except it does not
495
+ * treat `null`, `undefined`, or `[]` specially.
496
+ *
497
+ * WARNING: The iteration order of concur iterables is not deterministic, so the
498
+ * values will be concatenated in an arbitrary order.
445
499
*
446
500
* @example
447
- * ```js
501
+ * ```js playground
502
+ * import { asConcur, joinConcur, mapConcur, pipe } from 'lfi'
503
+ *
504
+ * const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
505
+ *
448
506
* console.log(
449
507
* await pipe(
450
- * asConcur([`sloth`, `more sloth`, `even more sloth`]),
508
+ * asConcur([`sloth`, `lazy`, `sleep`]),
509
+ * mapConcur(async word => {
510
+ * const response = await fetch(`${API_URL}/${word}`)
511
+ * return (await response.json())[0].phonetic
512
+ * }),
451
513
* joinConcur(`, `),
452
514
* ),
453
515
* )
454
- * //=> sloth, more sloth, even more sloth
516
+ * // NOTE: This order may change between runs
517
+ * //=> /slɑθ/, /ˈleɪzi/, /sliːp/
455
518
* ```
456
519
*
457
520
* @category Collections
0 commit comments