Created
July 8, 2022 15:01
-
-
Save ErickWendel/5980104058402374f68fc8d360fdb637 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @erickwendel_ | |
import { Readable } from 'node:stream' | |
import { pipeline } from 'node:stream/promises' | |
const take = (limit) => async function* (source) { | |
let count = 0; | |
for await (const item of source) { | |
if (count++ >= limit) break | |
yield item | |
} | |
} | |
const filter = (fn) => async function* (source) { | |
for await (const item of source) | |
if (fn(item)) yield item | |
} | |
const map = (fn) => async function* (source) { | |
for await (const item of source) yield fn(item) | |
} | |
const log = _ => async function* (source) { | |
for await (const item of source) console.log(item) | |
} | |
await pipeline( | |
Readable.from(['π€―', 'π¬', 'π', 'π³', 'π³']), | |
take(3), | |
filter(item => item !== 'π¬'), | |
map(item => 'emoji: '.concat(item)), | |
log(), | |
) | |
/* | |
emoji: π€― | |
emoji: π | |
*/ | |
console.log() | |
const readable = | |
Readable.from(['π€―', 'π¬', 'π', 'π³', 'π³']) | |
.take(3) | |
.filter(item => item !== 'π¬') | |
.map(item => 'emoji: '.concat(item)) | |
await pipeline( | |
readable, | |
log() | |
) | |
/* | |
emoji: π€― | |
emoji: π | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment