Skip to content

Commit

Permalink
feat(website): filters tsdoc playground examples (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach authored Dec 1, 2024
1 parent 6af727e commit e1644e5
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 95 deletions.
160 changes: 95 additions & 65 deletions src/operations/filters.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ export const filter: {
* filterAsync(async word => {
* const response = await fetch(`${API_URL}/${word}`)
* const [{ meanings }] = await response.json()
* return meanings[0].partOfSpeech === `verb`
* return meanings.some(meaning => meaning.partOfSpeech === `adjective`)
* }),
* reduceAsync(toArray()),
* ),
* )
* //=> [ 'sleep' ]
* //=> [ 'lazy' ]
* ```
*
* @category Filters
Expand Down Expand Up @@ -110,12 +110,12 @@ export const filterAsync: {
* filterConcur(async word => {
* const response = await fetch(`${API_URL}/${word}`)
* const [{ meanings }] = await response.json()
* return meanings[0].partOfSpeech === `verb`
* return meanings.some(meaning => meaning.partOfSpeech === `adjective`)
* }),
* reduceConcur(toArray()),
* ),
* )
* //=> [ 'sleep' ]
* //=> [ 'lazy' ]
* ```
*
* @category Filters
Expand Down Expand Up @@ -651,26 +651,26 @@ type FindConcur = {
* Like `Array.prototype.find`, but for iterables.
*
* @example
* ```js
* const iterable = [1, 2, `sloth`, 4, `other string`]
* ```js playground
* import { find, or, pipe } from 'lfi'
*
* console.log(
* pipe(
* iterable,
* find(value => typeof value === `string`),
* or(() => `yawn!`),
* )
* [`sloth`, `lazy`, `sleep`],
* find(word => word.includes(`s`)),
* or(() => `not found!`),
* ),
* )
* //=> sloth
*
* console.log(
* pipe(
* iterable,
* find(value => Array.isArray(value)),
* or(() => `yawn!`),
* )
* [`sloth`, `lazy`, `sleep`],
* find(word => word.includes(`x`)),
* or(() => `not found!`),
* ),
* )
* //=> yawn!
* //=> not found!
* ```
*
* @category Filters
Expand All @@ -686,26 +686,33 @@ export const find: Find
* Like `Array.prototype.find`, but for async iterables.
*
* @example
* ```js
* const asyncIterable = asAsync([1, 2, `sloth`, 4, `other string`])
* ```js playground
* import { asAsync, findAsync, orAsync, pipe } from 'lfi'
*
* const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
* const getPartsOfSpeech = async word => {
* const response = await fetch(`${API_URL}/${word}`)
* const [{ meanings }] = await response.json()
* return meanings.map(meaning => meaning.partOfSpeech)
* }
*
* console.log(
* await pipe(
* asyncIterable,
* findAsync(value => typeof value === `string`),
* orAsync(() => `yawn!`),
* )
* asAsync([`sloth`, `lazy`, `sleep`]),
* findAsync(async word => (await getPartsOfSpeech(word)).includes(`verb`)),
* orAsync(() => `not found!`),
* ),
* )
* //=> sloth
*
* console.log(
* await pipe(
* asyncIterable,
* findAsync(value => Array.isArray(value)),
* orAsync(() => `yawn!`),
* )
* asAsync([`sloth`, `lazy`, `sleep`]),
* findAsync(async word => (await getPartsOfSpeech(word)).includes(`adverb`)),
* orAsync(() => `not found!`),
* ),
* )
* //=> yawn!
* //=> not found!
* ```
*
* @category Filters
Expand All @@ -721,26 +728,34 @@ export const findAsync: FindAsync
* Like `Array.prototype.find`, but for concur iterables.
*
* @example
* ```js
* const concurIterable = asConcur([1, 2, `sloth`, 4, `other string`])
* ```js playground
* import { asConcur, findConcur, orConcur, pipe } from 'lfi'
*
* const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
* const getPartsOfSpeech = async word => {
* const response = await fetch(`${API_URL}/${word}`)
* const [{ meanings }] = await response.json()
* return meanings.map(meaning => meaning.partOfSpeech)
* }
*
* console.log(
* await pipe(
* concurIterable,
* findConcur(value => typeof value === `string`),
* orConcur(() => `yawn`),
* asConcur([`sloth`, `lazy`, `sleep`]),
* findConcur(async word => (await getPartsOfSpeech(word)).includes(`verb`)),
* orConcur(() => `not found!`),
* ),
* )
* // NOTE: This word may change between runs
* //=> sloth
*
* console.log(
* await pipe(
* concurIterable,
* findConcur(value => Array.isArray(value)),
* orConcur(() => `yawn`),
* asConcur([`sloth`, `lazy`, `sleep`]),
* findConcur(async word => (await getPartsOfSpeech(word)).includes(`adverb`)),
* orConcur(() => `not found!`),
* ),
* )
* //=> yawn!
* //=> not found!
* ```
*
* @category Filters
Expand All @@ -753,26 +768,26 @@ export const findConcur: FindConcur
* returns a truthy value. Otherwise, returns an empty iterable.
*
* @example
* ```js
* const iterable = [1, 2, `sloth`, 4, `other string`]
* ```js playground
* import { findLast, or, pipe } from 'lfi'
*
* console.log(
* pipe(
* iterable,
* findLast(value => typeof value === `string`),
* or(() => `yawn!`),
* [`sloth`, `lazy`, `sleep`],
* findLast(word => word.includes(`s`)),
* or(() => `not found!`),
* ),
* )
* //=> other string
* //=> sleep
*
* console.log(
* pipe(
* iterable,
* findLast(value => Array.isArray(value)),
* or(() => `yawn!`),
* [`sloth`, `lazy`, `sleep`],
* findLast(word => word.includes(`x`)),
* or(() => `not found!`),
* ),
* )
* //=> yawn!
* //=> not found!
* ```
*
* @category Filters
Expand All @@ -786,26 +801,33 @@ export const findLast: Find
* empty async iterable.
*
* @example
* ```js
* const asyncIterable = asAsync([1, 2, `sloth`, 4, `other string`])
* ```js playground
* import { asAsync, findLastAsync, orAsync, pipe } from 'lfi'
*
* const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
* const getPartsOfSpeech = async word => {
* const response = await fetch(`${API_URL}/${word}`)
* const [{ meanings }] = await response.json()
* return meanings.map(meaning => meaning.partOfSpeech)
* }
*
* console.log(
* await pipe(
* asyncIterable,
* findLastAsync(value => typeof value === `string`),
* orAsync(() => `yawn!`),
* asAsync([`sloth`, `lazy`, `sleep`]),
* findLastAsync(async word => (await getPartsOfSpeech(word)).includes(`verb`)),
* orAsync(() => `not found!`),
* ),
* )
* //=> other string
* //=> sleep
*
* console.log(
* await pipe(
* asyncIterable,
* findLastAsync(value => Array.isArray(value)),
* orAsync(() => `yawn!`),
* asAsync([`sloth`, `lazy`, `sleep`]),
* findLastAsync(async word => (await getPartsOfSpeech(word)).includes(`adverb`)),
* orAsync(() => `not found!`),
* ),
* )
* //=> yawn!
* //=> not found!
* ```
*
* @category Filters
Expand All @@ -819,26 +841,34 @@ export const findLastAsync: FindAsync
* empty concur iterable.
*
* @example
* ```js
* const concurIterable = asConcur([1, 2, `sloth`, 4, `other string`])
* ```js playground
* import { asConcur, findLastConcur, orConcur, pipe } from 'lfi'
*
* const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
* const getPartsOfSpeech = async word => {
* const response = await fetch(`${API_URL}/${word}`)
* const [{ meanings }] = await response.json()
* return meanings.map(meaning => meaning.partOfSpeech)
* }
*
* console.log(
* await pipe(
* concurIterable,
* findLastConcur(value => typeof value === `string`),
* orConcur(() => `yawn!`),
* asConcur([`sloth`, `lazy`, `sleep`]),
* findLastConcur(async word => (await getPartsOfSpeech(word)).includes(`verb`)),
* orConcur(() => `not found!`),
* ),
* )
* //=> other string
* // NOTE: This word may change between runs
* //=> sleep
*
* console.log(
* await pipe(
* concurIterable,
* findLastConcur(value => Array.isArray(value)),
* orConcur(() => `yawn!`),
* asConcur([`sloth`, `lazy`, `sleep`]),
* findLastConcur(async word => (await getPartsOfSpeech(word)).includes(`adverb`)),
* orConcur(() => `not found!`),
* ),
* )
* //=> yawn!
* //=> not found!
* ```
*
* @category Filters
Expand Down
42 changes: 21 additions & 21 deletions website/docs/concepts/reducer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ console.log(
```js playground
import {
asAsync,
mapAsync,
flatMapAsync,
pipe,
reduceAsync,
toCount,
Expand All @@ -920,29 +920,29 @@ import {
} from 'lfi'

const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
const getPartOfSpeech = async word => {
const getPartsOfSpeech = async word => {
const response = await fetch(`${API_URL}/${word}`)
const [{ meanings }] = await response.json()
return meanings[0].partOfSpeech
return meanings.map(meaning => meaning.partOfSpeech)
}

console.log(
await pipe(
asAsync([`sloth`, `lazy`, `sleep`]),
mapAsync(getPartOfSpeech),
flatMapAsync(getPartsOfSpeech),
reduceAsync(toMultiple([toSet(), toCount(), toJoin(`,`)])),
),
)
//=> [
//=> Set(2) { 'noun', 'verb' },
//=> 3,
//=> 'noun,noun,verb'
//=> Set(3) { 'noun', 'verb', 'adjective' },
//=> 6,
//=> 'noun,verb,noun,verb,adjective,verb'
//=> ]

console.log(
await pipe(
asAsync([`sloth`, `lazy`, `sleep`]),
mapAsync(getPartOfSpeech),
flatMapAsync(getPartsOfSpeech),
reduceAsync(
toMultiple({
set: toSet(),
Expand All @@ -953,9 +953,9 @@ console.log(
),
)
//=> {
//=> set: Set(2) { 'noun', 'verb' },
//=> count: 3,
//=> string: 'noun,noun,verb'
//=> set: Set(3) { 'noun', 'verb', 'adjective' },
//=> count: 6,
//=> string: 'noun,verb,noun,verb,adjective,verb'
//=> }
```

Expand All @@ -975,30 +975,30 @@ import {
} from 'lfi'

const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
const getPartOfSpeech = async word => {
const getPartsOfSpeech = async word => {
const response = await fetch(`${API_URL}/${word}`)
const [{ meanings }] = await response.json()
return meanings[0].partOfSpeech
return meanings.map(meaning => meaning.partOfSpeech)
}

console.log(
await pipe(
asConcur([`sloth`, `lazy`, `sleep`]),
mapConcur(getPartOfSpeech),
mapConcur(getPartsOfSpeech),
reduceConcur(toMultiple([toSet(), toCount(), toJoin(`,`)])),
),
)
// NOTE: This order may change between runs
//=> [
//=> Set(2) { 'noun', 'verb' },
//=> 3,
//=> 'noun,noun,verb'
//=> Set(3) { 'noun', 'verb', 'adjective' },
//=> 6,
//=> 'noun,verb,noun,verb,adjective,verb'
//=> ]

console.log(
await pipe(
asConcur([`sloth`, `lazy`, `sleep`]),
mapConcur(getPartOfSpeech),
mapConcur(getPartsOfSpeech),
reduceConcur(
toMultiple({
set: toSet(),
Expand All @@ -1010,9 +1010,9 @@ console.log(
)
// NOTE: This order may change between runs
//=> {
//=> set: Set(2) { 'noun', 'verb' },
//=> count: 3,
//=> string: 'noun,noun,verb'
//=> set: Set(3) { 'noun', 'verb', 'adjective' },
//=> count: 6,
//=> string: 'noun,verb,noun,verb,adjective,verb'
//=> }
```

Expand Down
Loading

0 comments on commit e1644e5

Please sign in to comment.