Skip to content

Commit 457e5a8

Browse files
feat(website): tsdoc playground examples for optionals (#61)
1 parent 14f2425 commit 457e5a8

File tree

2 files changed

+152
-73
lines changed

2 files changed

+152
-73
lines changed

src/operations/optionals.d.ts

+150-71
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,32 @@ export type ConcurOptional<Value> = ConcurIterable<Value>
3030
* Otherwise, returns the result of invoking `fn`.
3131
*
3232
* @example
33-
* ```js
34-
* console.log(pipe([`sloth`], or(() => `Never called`)))
33+
* ```js playground
34+
* import { or, pipe } from 'lfi'
35+
*
36+
* console.log(
37+
* pipe(
38+
* [`sloth`],
39+
* or(() => `never called`),
40+
* ),
41+
* )
3542
* //=> sloth
3643
*
37-
* console.log(pipe([], or(() => `I get called!`)))
38-
* //=> I get called!
44+
* console.log(
45+
* pipe(
46+
* [],
47+
* or(() => `called!`),
48+
* ),
49+
* )
50+
* //=> called!
3951
*
40-
* console.log(pipe([1, `sloth`, 3], or(() => `I also get called!`)))
41-
* //=> I also get called!
52+
* console.log(
53+
* pipe(
54+
* [`sloth`, `lazy`, `sleep`],
55+
* or(() => `called!`),
56+
* ),
57+
* )
58+
* //=> called!
4259
* ```
4360
*
4461
* @category Optionals
@@ -55,20 +72,30 @@ export const or: {
5572
* the awaited result of invoking `fn`.
5673
*
5774
* @example
58-
* ```js
59-
* console.log(await pipe(asAsync([`sloth`]), orAsync(() => `Never called`)))
75+
* ```js playground
76+
* import { asAsync, findAsync, orAsync, pipe } from 'lfi'
77+
*
78+
* const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
79+
*
80+
* const findWordWithPartOfSpeech = partOfSpeech =>
81+
* pipe(
82+
* asAsync([`sloth`, `lazy`, `sleep`]),
83+
* findAsync(async word => {
84+
* const response = await fetch(`${API_URL}/${word}`)
85+
* const [{ meanings }] = await response.json()
86+
* return meanings.some(meaning => meaning.partOfSpeech === partOfSpeech)
87+
* }),
88+
* orAsync(() => `no ${partOfSpeech}???`),
89+
* )
90+
*
91+
* console.log(await findWordWithPartOfSpeech(`noun`))
6092
* //=> sloth
61-
*
62-
* console.log(await pipe(emptyAsync, orAsync(() => `I get called!`)))
63-
* //=> I get called!
64-
*
65-
* console.log(
66-
* await pipe(
67-
* asAsync([1, `sloth`, 3]),
68-
* orAsync(() => `I also get called!`),
69-
* ),
70-
* )
71-
* //=> I also get called!
93+
* console.log(await findWordWithPartOfSpeech(`verb`))
94+
* //=> sloth
95+
* console.log(await findWordWithPartOfSpeech(`adjective`))
96+
* //=> lazy
97+
* console.log(await findWordWithPartOfSpeech(`adverb`))
98+
* //=> no adverb???
7299
* ```
73100
*
74101
* @category Optionals
@@ -90,20 +117,32 @@ export const orAsync: {
90117
* the awaited result of invoking `fn`.
91118
*
92119
* @example
93-
* ```js
94-
* console.log(await pipe(asConcur([`sloth`]), orConcur(() => `Never called`)))
120+
* ```js playground
121+
* import { asConcur, findConcur, orConcur, pipe } from 'lfi'
122+
*
123+
* const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
124+
*
125+
* const findWordWithPartOfSpeech = partOfSpeech =>
126+
* pipe(
127+
* asConcur([`sloth`, `lazy`, `sleep`]),
128+
* findConcur(async word => {
129+
* const response = await fetch(`${API_URL}/${word}`)
130+
* const [{ meanings }] = await response.json()
131+
* return meanings.some(meaning => meaning.partOfSpeech === partOfSpeech)
132+
* }),
133+
* orConcur(() => `no ${partOfSpeech}???`),
134+
* )
135+
*
136+
* console.log(await findWordWithPartOfSpeech(`noun`))
137+
* // NOTE: This word may change between runs
95138
* //=> sloth
96-
*
97-
* console.log(await pipe(emptyConcur, orConcur(() => `I get called!`)))
98-
* //=> I get called!
99-
*
100-
* console.log(
101-
* await pipe(
102-
* asConcur([1, `sloth`, 3]),
103-
* orConcur(() => `I also get called!`),
104-
* ),
105-
* )
106-
* //=> I also get called!
139+
* console.log(await findWordWithPartOfSpeech(`verb`))
140+
* // NOTE: This word may change between runs
141+
* //=> sloth
142+
* console.log(await findWordWithPartOfSpeech(`adjective`))
143+
* //=> lazy
144+
* console.log(await findWordWithPartOfSpeech(`adverb`))
145+
* //=> no adverb???
107146
* ```
108147
*
109148
* @category Optionals
@@ -124,7 +163,9 @@ export const orConcur: {
124163
* Otherwise, throws an error.
125164
*
126165
* @example
127-
* ```js
166+
* ```js playground
167+
* import { get } from 'lfi'
168+
*
128169
* console.log(get([`sloth`]))
129170
* //=> sloth
130171
*
@@ -136,7 +177,7 @@ export const orConcur: {
136177
* //=> Oh no! It was empty...
137178
*
138179
* try {
139-
* console.log(get([1, `sloth`, 3]))
180+
* console.log(get([`sloth`, `lazy`, `sleep`]))
140181
* } catch {
141182
* console.log(`Oh no! It had more than one value...`)
142183
* }
@@ -153,23 +194,34 @@ export const get: <Value>(iterable: Iterable<Value>) => Value
153194
* contains exactly one value. Otherwise, returns a promise that rejects.
154195
*
155196
* @example
156-
* ```js
157-
* console.log(await getAsync(asAsync([`sloth`])))
197+
* ```js playground
198+
* import { asAsync, findAsync, getAsync, pipe } from 'lfi'
199+
*
200+
* const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
201+
*
202+
* const findWordWithPartOfSpeech = partOfSpeech =>
203+
* pipe(
204+
* asAsync([`sloth`, `lazy`, `sleep`]),
205+
* findAsync(async word => {
206+
* const response = await fetch(`${API_URL}/${word}`)
207+
* const [{ meanings }] = await response.json()
208+
* return meanings.some(meaning => meaning.partOfSpeech === partOfSpeech)
209+
* }),
210+
* getAsync,
211+
* )
212+
*
213+
* console.log(await findWordWithPartOfSpeech(`noun`))
158214
* //=> sloth
159-
*
215+
* console.log(await findWordWithPartOfSpeech(`verb`))
216+
* //=> sloth
217+
* console.log(await findWordWithPartOfSpeech(`adjective`))
218+
* //=> lazy
160219
* try {
161-
* console.log(await getAsync(emptyAsync))
220+
* console.log(await findWordWithPartOfSpeech(`adverb`))
162221
* } catch {
163222
* console.log(`Oh no! It was empty...`)
164223
* }
165224
* //=> Oh no! It was empty...
166-
*
167-
* try {
168-
* console.log(await getAsync(asAsync([1, `sloth`, 3])))
169-
* } catch {
170-
* console.log(`Oh no! It had more than one value...`)
171-
* }
172-
* //=> Oh no! It had more than one value...
173225
* ```
174226
*
175227
* @category Optionals
@@ -184,23 +236,36 @@ export const getAsync: <Value>(
184236
* contains exactly one value. Otherwise, returns a promise that rejects.
185237
*
186238
* @example
187-
* ```js
188-
* console.log(await getConcur(asConcur([`sloth`])))
239+
* ```js playground
240+
* import { asConcur, findConcur, getConcur, pipe } from 'lfi'
241+
*
242+
* const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
243+
*
244+
* const findWordWithPartOfSpeech = partOfSpeech =>
245+
* pipe(
246+
* asConcur([`sloth`, `lazy`, `sleep`]),
247+
* findConcur(async word => {
248+
* const response = await fetch(`${API_URL}/${word}`)
249+
* const [{ meanings }] = await response.json()
250+
* return meanings.some(meaning => meaning.partOfSpeech === partOfSpeech)
251+
* }),
252+
* getConcur,
253+
* )
254+
*
255+
* console.log(await findWordWithPartOfSpeech(`noun`))
256+
* // NOTE: This word may change between runs
189257
* //=> sloth
190-
*
258+
* console.log(await findWordWithPartOfSpeech(`verb`))
259+
* // NOTE: This word may change between runs
260+
* //=> sloth
261+
* console.log(await findWordWithPartOfSpeech(`adjective`))
262+
* //=> lazy
191263
* try {
192-
* console.log(await getConcur(emptyConcur))
264+
* console.log(await findWordWithPartOfSpeech(`adverb`))
193265
* } catch {
194266
* console.log(`Oh no! It was empty...`)
195267
* }
196268
* //=> Oh no! It was empty...
197-
*
198-
* try {
199-
* console.log(await getConcur(asConcur([1, `sloth`, 3])))
200-
* } catch {
201-
* console.log(`Oh no! It had more than one value...`)
202-
* }
203-
* //=> Oh no! It had more than one value...
204269
* ```
205270
*
206271
* @category Optionals
@@ -217,18 +282,18 @@ export const getConcur: <Value>(
217282
* values of `iterable`. The second iterable can only be iterated once.
218283
*
219284
* @example
220-
* ```js
221-
* const slothActivities = [`sleeping`, `yawning`, `eating`]
222-
* const [first, rest] = next(slothActivities)
285+
* ```js playground
286+
* import { count, get, next } from 'lfi'
287+
*
288+
* const [first, rest] = next([`sloth`, `lazy`, `sleep`])
223289
*
224290
* console.log(get(first))
225-
* //=> sleeping
291+
* //=> sloth
226292
*
227293
* console.log([...rest])
228-
* //=> [ 'yawning', 'eating' ]
294+
* //=> [ 'lazy', 'sleep' ]
229295
*
230-
* const badThingsAboutSloths = []
231-
* const [first2, rest2] = next(badThingsAboutSloths)
296+
* const [first2, rest2] = next([])
232297
*
233298
* console.log(count(first2))
234299
* //=> 0
@@ -252,18 +317,32 @@ export const next: <Value>(
252317
* of `asyncIterable`. The second async iterable can only be iterated once.
253318
*
254319
* @example
255-
* ```js
256-
* const slothActivities = asAsync([`sleeping`, `yawning`, `eating`])
257-
* const [first, rest] = await nextAsync(slothActivities)
320+
* ```js playground
321+
* import { asAsync, countAsync, emptyAsync, getAsync, mapAsync, nextAsync, pipe, reduceAsync, toArray } from 'lfi'
322+
*
323+
* const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
324+
*
325+
* const [first, rest] = await pipe(
326+
* asAsync([`sloth`, `lazy`, `sleep`]),
327+
* mapAsync(async word => {
328+
* const response = await fetch(`${API_URL}/${word}`)
329+
* return (await response.json())[0].phonetic
330+
* }),
331+
* nextAsync,
332+
* )
258333
*
259334
* console.log(await getAsync(first))
260-
* //=> sleeping
335+
* //=> /slɑθ/
261336
*
262-
* console.log(await reduceAsync(toArray(), rest))
263-
* //=> [ 'yawning', 'eating' ]
337+
* console.log(
338+
* await pipe(
339+
* rest,
340+
* reduceAsync(toArray()),
341+
* ),
342+
* )
343+
* //=> [ '/ˈleɪzi/', '/sliːp/' ]
264344
*
265-
* const badThingsAboutSloths = emptyAsync
266-
* const [first2, rest2] = await nextAsync(badThingsAboutSloths)
345+
* const [first2, rest2] = await nextAsync(emptyAsync)
267346
*
268347
* console.log(await countAsync(first2))
269348
* //=> 0

website/docs/concepts/reducer.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ console.log(await meanSquares([]))
514514
//=> NaN
515515
```
516516

517-
If the console logs in the playground are suprising to you, then
517+
If the console logs in the playground are surprising to you, then
518518
[read about the order async reducers values combine values](#in-what-order-are-values-combined)!
519519

520520
</TabItem>
@@ -1185,7 +1185,7 @@ Instead of alternating between awaiting `next()` and `add(...)` sequentially,
11851185
can be combined with an async iterable value or another accumulator, but two
11861186
async iterable values cannot be combined
11871187

1188-
This continues until one accumulator remains, at which point it is tranformed
1188+
This continues until one accumulator remains, at which point it is transformed
11891189
with `finish` and returned.
11901190

11911191
`reduceConcur` works the same way, but the observed behavior is less surprising

0 commit comments

Comments
 (0)