@@ -30,15 +30,32 @@ export type ConcurOptional<Value> = ConcurIterable<Value>
30
30
* Otherwise, returns the result of invoking `fn`.
31
31
*
32
32
* @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
+ * )
35
42
* //=> sloth
36
43
*
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!
39
51
*
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!
42
59
* ```
43
60
*
44
61
* @category Optionals
@@ -55,20 +72,30 @@ export const or: {
55
72
* the awaited result of invoking `fn`.
56
73
*
57
74
* @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`))
60
92
* //=> 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???
72
99
* ```
73
100
*
74
101
* @category Optionals
@@ -90,20 +117,32 @@ export const orAsync: {
90
117
* the awaited result of invoking `fn`.
91
118
*
92
119
* @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
95
138
* //=> 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???
107
146
* ```
108
147
*
109
148
* @category Optionals
@@ -124,7 +163,9 @@ export const orConcur: {
124
163
* Otherwise, throws an error.
125
164
*
126
165
* @example
127
- * ```js
166
+ * ```js playground
167
+ * import { get } from 'lfi'
168
+ *
128
169
* console.log(get([`sloth`]))
129
170
* //=> sloth
130
171
*
@@ -136,7 +177,7 @@ export const orConcur: {
136
177
* //=> Oh no! It was empty...
137
178
*
138
179
* try {
139
- * console.log(get([1 , `sloth `, 3 ]))
180
+ * console.log(get([`sloth` , `lazy `, `sleep` ]))
140
181
* } catch {
141
182
* console.log(`Oh no! It had more than one value...`)
142
183
* }
@@ -153,23 +194,34 @@ export const get: <Value>(iterable: Iterable<Value>) => Value
153
194
* contains exactly one value. Otherwise, returns a promise that rejects.
154
195
*
155
196
* @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`))
158
214
* //=> sloth
159
- *
215
+ * console.log(await findWordWithPartOfSpeech(`verb`))
216
+ * //=> sloth
217
+ * console.log(await findWordWithPartOfSpeech(`adjective`))
218
+ * //=> lazy
160
219
* try {
161
- * console.log(await getAsync(emptyAsync ))
220
+ * console.log(await findWordWithPartOfSpeech(`adverb` ))
162
221
* } catch {
163
222
* console.log(`Oh no! It was empty...`)
164
223
* }
165
224
* //=> 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...
173
225
* ```
174
226
*
175
227
* @category Optionals
@@ -184,23 +236,36 @@ export const getAsync: <Value>(
184
236
* contains exactly one value. Otherwise, returns a promise that rejects.
185
237
*
186
238
* @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
189
257
* //=> 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
191
263
* try {
192
- * console.log(await getConcur(emptyConcur ))
264
+ * console.log(await findWordWithPartOfSpeech(`adverb` ))
193
265
* } catch {
194
266
* console.log(`Oh no! It was empty...`)
195
267
* }
196
268
* //=> 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...
204
269
* ```
205
270
*
206
271
* @category Optionals
@@ -217,18 +282,18 @@ export const getConcur: <Value>(
217
282
* values of `iterable`. The second iterable can only be iterated once.
218
283
*
219
284
* @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`])
223
289
*
224
290
* console.log(get(first))
225
- * //=> sleeping
291
+ * //=> sloth
226
292
*
227
293
* console.log([...rest])
228
- * //=> [ 'yawning ', 'eating ' ]
294
+ * //=> [ 'lazy ', 'sleep ' ]
229
295
*
230
- * const badThingsAboutSloths = []
231
- * const [first2, rest2] = next(badThingsAboutSloths)
296
+ * const [first2, rest2] = next([])
232
297
*
233
298
* console.log(count(first2))
234
299
* //=> 0
@@ -252,18 +317,32 @@ export const next: <Value>(
252
317
* of `asyncIterable`. The second async iterable can only be iterated once.
253
318
*
254
319
* @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
+ * )
258
333
*
259
334
* console.log(await getAsync(first))
260
- * //=> sleeping
335
+ * //=> /slɑθ/
261
336
*
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/' ]
264
344
*
265
- * const badThingsAboutSloths = emptyAsync
266
- * const [first2, rest2] = await nextAsync(badThingsAboutSloths)
345
+ * const [first2, rest2] = await nextAsync(emptyAsync)
267
346
*
268
347
* console.log(await countAsync(first2))
269
348
* //=> 0
0 commit comments