through1

1.0.1 • Public • Published

1️⃣♒️1️⃣ through1 v1.0.1

CircleCI codecov

A simple utility for creating 1 to 1 transform stream.

💿 Install

Via npm:

npm i through1

Motivation

In the the most use cases, transforms are 1 to 1 transform, which means that it generates one output for each input. The official transform constructor is too complex for this usage. It has the following signature:

new Transform({
  transform (chunk, enc, cb) {
    ...
  }
})

This supports one to N transform, where N is 0, 1, 2, or any arbitrary positive integer. This is too much for 1 to 1 transform.

through1 provides simpler interface for creating 1 to 1 transform.

📝 Usage

const through1 = require('through1')

If you transform the input synchronously, then return the transformed output:

// byte stream
// synchronous transform
through1((chunk, enc) => myTransformFunc(chunk, enc))

If you work on the object mode stream (for example, like gulp's stream), then use .obj shorthand:

// object stream
// synchronous transform
through1.obj(file => myTransformFunc(func))

Asynchronous use case

If your transformation is asynchronous, then return the promise:

// byte stream
// asynchronous transform
through1((chunk, enc) => myTransformPromise(chunk, enc))

If you work on object mode:

// object stream
// asynchronous transform
through1.obj(file => myTransformPromise(file))

Comparison

through1 map-stream gulp-map
object mode support
non object mode support
promise support
stream version stream 2 stream 1 stream 2
back-pressure support
dependencies 0 0 9

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i through1

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

9.62 kB

Total Files

9

Last publish

Collaborators

  • kt3k