Skip to content

Commit

Permalink
Filter implemented with a transform stream
Browse files Browse the repository at this point in the history
  • Loading branch information
marnusw committed Dec 1, 2021
1 parent 645ce77 commit 2f70ec5
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/filter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Node } from '.'
import { createDuplex } from './utils'
import { Transform } from 'stream'

export const filter = (callback: (node: Node) => boolean) =>
createDuplex({
write(chunk, _encoding, next) {
if (callback(chunk)) {
this.push(chunk)
}
next()
new Transform({
objectMode: true,
autoDestroy: false,
transform: function transform(chunk, _encoding, next) {
callback(chunk) ? next(null, chunk) : next()
}
})

0 comments on commit 2f70ec5

Please sign in to comment.