Skip to content

Commit cc33c97

Browse files
feat(website): revisions and playground for core and collections tsdoc examples (#50)
1 parent d603675 commit cc33c97

File tree

6 files changed

+269
-107
lines changed

6 files changed

+269
-107
lines changed

pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/operations/collections.d.ts

+114-51
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ import type {
1414
* Returns a {@link Reducer} that collects values to an `Array`.
1515
*
1616
* @example
17-
* ```js
17+
* ```js playground
18+
* import { cycle, pipe, reduce, take, toArray } from 'lfi'
19+
*
1820
* console.log(
1921
* pipe(
20-
* cycle([`sloth`, `more sloth`]),
22+
* cycle([`sloth`, `lazy`, `sleep`]),
2123
* take(4),
2224
* reduce(toArray()),
2325
* ),
2426
* )
25-
* //=> [ 'sloth', 'more sloth', 'sloth', 'more sloth' ]
27+
* //=> [ 'sloth', 'lazy', 'sleep', 'sloth' ]
2628
* ```
2729
*
2830
* @category Collections
@@ -34,15 +36,21 @@ export const toArray: <Value>() => Reducer<Value, Value[]>
3436
* Returns a {@link Reducer} that collects values to a `Set`.
3537
*
3638
* @example
37-
* ```js
39+
* ```js playground
40+
* import { cycle, pipe, reduce, take, toSet } from 'lfi'
41+
*
3842
* console.log(
3943
* pipe(
40-
* cycle([`sloth`, `more sloth`]),
44+
* cycle([`sloth`, `lazy`, `sleep`]),
4145
* take(4),
42-
* reduce(toArray()),
46+
* reduce(toSet()),
4347
* ),
4448
* )
45-
* //=> Set(2) { 'sloth', 'more sloth' }
49+
* //=> Set(3) {
50+
* //=> 'sloth',
51+
* //=> 'lazy',
52+
* //=> 'sleep'
53+
* //=> }
4654
* ```
4755
*
4856
* @category Collections
@@ -54,10 +62,12 @@ export const toSet: <Value>() => Reducer<Value, Set<Value>>
5462
* Returns a {@link Reducer} that collects objects to a `WeakSet`.
5563
*
5664
* @example
57-
* ```js
65+
* ```js playground
66+
* import { cycle, map, pipe, reduce, take, toWeakSet } from 'lfi'
67+
*
5868
* console.log(
5969
* pipe(
60-
* cycle([`sloth`, `more sloth`]),
70+
* cycle([`sloth`, `lazy`, `sleep`]),
6171
* take(4),
6272
* map(string => ({ sloth: string })),
6373
* reduce(toWeakSet()),
@@ -80,15 +90,21 @@ export const toWeakSet: <Value extends object>() => Reducer<
8090
* In the case of pairs with duplicate keys, the value of the last one wins.
8191
*
8292
* @example
83-
* ```js
93+
* ```js playground
94+
* import { map, pipe, reduce, toObject } from 'lfi'
95+
*
8496
* console.log(
8597
* pipe(
86-
* [`sloth`, `more sloth`, `even more sloth`],
98+
* [`sloth`, `lazy`, `sleep`],
8799
* map(string => [string, string.length]),
88100
* reduce(toObject()),
89101
* ),
90102
* )
91-
* //=> { sloth: 5, 'more sloth': 10, 'even more sloth': 15 }
103+
* //=> {
104+
* //=> sloth: 5,
105+
* //=> lazy: 4,
106+
* //=> sleep: 5
107+
* //=> }
92108
* ```
93109
*
94110
* @category Collections
@@ -106,15 +122,21 @@ export const toObject: <Key extends keyof never, Value>() => RawKeyedReducer<
106122
* In the case of pairs with duplicate keys, the value of the last one wins.
107123
*
108124
* @example
109-
* ```js
125+
* ```js playground
126+
* import { map, pipe, reduce, toMap } from 'lfi'
127+
*
110128
* console.log(
111129
* pipe(
112-
* [`sloth`, `more sloth`, `even more sloth`],
130+
* [`sloth`, `lazy`, `sleep`],
113131
* map(string => [string, string.length]),
114132
* reduce(toMap()),
115133
* ),
116134
* )
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+
* //=> }
118140
* ```
119141
*
120142
* @category Collections
@@ -132,10 +154,12 @@ export const toMap: <Key, Value>() => RawKeyedReducer<
132154
* In the case of pairs with duplicate keys, the value of the last one wins.
133155
*
134156
* @example
135-
* ```js
157+
* ```js playground
158+
* import { map, pipe, reduce, toWeakMap } from 'lfi'
159+
*
136160
* console.log(
137161
* pipe(
138-
* [`sloth`, `more sloth`, `even more sloth`],
162+
* [`sloth`, `lazy`, `sleep`],
139163
* map(string => [{ sloth: string }, string.length]),
140164
* reduce(toWeakMap()),
141165
* ),
@@ -157,18 +181,19 @@ export const toWeakMap: <Key extends object, Value>() => RawKeyedReducer<
157181
* and reduces values with the same key using `innerReducer`.
158182
*
159183
* @example
160-
* ```js
184+
* ```js playground
185+
* import { map, pipe, reduce, toArray, toGrouped, toMap } from 'lfi'
186+
*
161187
* console.log(
162188
* pipe(
163-
* [`sloth`, `some sloth`, `sleep`, `more sloth`, `even more sloth`],
189+
* [`sloth`, `lazy`, `sleep`],
164190
* map(string => [string.length, string]),
165191
* reduce(toGrouped(toArray(), toMap())),
166192
* ),
167193
* )
168-
* //=> Map(3) {
194+
* //=> Map(2) {
169195
* //=> 5 => [ 'sloth', 'sleep' ],
170-
* //=> 10 => [ 'some sloth', 'more sloth' ],
171-
* //=> 15 => [ 'even more sloth' ]
196+
* //=> 4 => [ 'lazy' ]
172197
* //=> }
173198
* ```
174199
*
@@ -252,19 +277,25 @@ export const toGrouped: {
252277
* an {@link OptionalReducer}. Otherwise, returns a {@link Reducer}.
253278
*
254279
* @example
255-
* ```js
280+
* ```js playground
281+
* import { map, pipe, reduce, toCount, toJoin, toMultiple, toSet } from 'lfi'
282+
*
256283
* console.log(
257284
* pipe(
258-
* [`sloth`, `some sloth`, `sleep`, `more sloth`, `even more sloth`],
285+
* [`sloth`, `lazy`, `sleep`],
259286
* map(string => string.length),
260287
* reduce(toMultiple([toSet(), toCount(), toJoin(`,`)])),
261288
* ),
262289
* )
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+
* //=> ]
264295
*
265296
* console.log(
266297
* pipe(
267-
* [`sloth`, `some sloth`, `sleep`, `more sloth`, `even more sloth`],
298+
* [`sloth`, `lazy`, `sleep`],
268299
* map(string => string.length),
269300
* reduce(
270301
* toMultiple({
@@ -275,7 +306,11 @@ export const toGrouped: {
275306
* ),
276307
* ),
277308
* )
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+
* //=> }
279314
* ```
280315
*
281316
* @category Collections
@@ -366,15 +401,20 @@ export const toMultiple: {
366401
* {@link joinConcur} for direct use on iterables.
367402
*
368403
* @example
369-
* ```js
404+
* ```js playground
405+
* import { map, pipe, reduce, toGrouped, toJoin, toMap } from 'lfi'
406+
*
370407
* console.log(
371408
* pipe(
372-
* [`sloth`, `more sloth`, `sleep`, `some sloth`],
409+
* [`sloth`, `lazy`, `sleep`],
373410
* map(string => [string.length, string]),
374411
* reduce(toGrouped(toJoin(`,`), toMap())),
375412
* ),
376413
* )
377-
* //=> Map(2) { 5 => 'sloth,sleep', 10 => 'more sloth,some sloth' }
414+
* //=> Map(2) {
415+
* //=> 5 => 'sloth,sleep',
416+
* //=> 4 => 'lazy'
417+
* //=> }
378418
* ```
379419
*
380420
* @category Collections
@@ -383,21 +423,24 @@ export const toMultiple: {
383423
export const toJoin: (separator: string) => Reducer<unknown, unknown, string>
384424

385425
/**
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`.
388428
*
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.
391431
*
392432
* @example
393-
* ```js
433+
* ```js playground
434+
* import { join, map, pipe } from 'lfi'
435+
*
394436
* console.log(
395437
* pipe(
396-
* [`sloth`, `more sloth`, `even more sloth`],
438+
* [`sloth`, `lazy`, `sleep`],
439+
* map(string => string.toUpperCase()),
397440
* join(`, `),
398441
* ),
399442
* )
400-
* //=> sloth, more sloth, even more sloth
443+
* //=> SLOTH, LAZY, SLEEP
401444
* ```
402445
*
403446
* @category Collections
@@ -409,21 +452,29 @@ export const join: {
409452
}
410453

411454
/**
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`.
414457
*
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.
417460
*
418461
* @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+
*
420467
* console.log(
421468
* 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+
* }),
423474
* joinAsync(`, `),
424475
* ),
425476
* )
426-
* //=> sloth, more sloth, even more sloth
477+
* //=> /slɑθ/, /ˈleɪzi/, /sliːp/
427478
* ```
428479
*
429480
* @category Collections
@@ -437,21 +488,33 @@ export const joinAsync: {
437488
}
438489

439490
/**
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`.
442493
*
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.
445499
*
446500
* @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+
*
448506
* console.log(
449507
* 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+
* }),
451513
* joinConcur(`, `),
452514
* ),
453515
* )
454-
* //=> sloth, more sloth, even more sloth
516+
* // NOTE: This order may change between runs
517+
* //=> /slɑθ/, /ˈleɪzi/, /sliːp/
455518
* ```
456519
*
457520
* @category Collections

0 commit comments

Comments
 (0)