Skip to content
read from one stream for a while, then read from another stream, then the first stream again.
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
README.md
index.js
package.json
test.js

README.md

Format-Stream

A streaming simple formatter.

API

var format = new Format(token, stream)

format is a transform stream that checks if each chunk of data matches the token. If it does not, then the chunk is passed through. Otherwise, format emits data from the second argument, stream until it is consumed.

Example

Suppose you have a file called base-template.html:

<html>
    <head>
        <title>"Your app!"</title>
    </head>
    <body>
        {{welcome}}
    </body>
</html>

And one called welcome-template.html.

<p>"Hey you party people!"</p>

In index.js you write:

var Sentinal = require('sentinal-stream')
  , Format = require('format-stream')
  , token = '{{welcome}}'

var inside_stream = fs.createReadStream('welcome-template.html')
  , outside_stream = fs.createReadStream('base-template.html')

// Make sure any instance of {{welcome}} is emitted in its
// own data event
var tokenizer = new Sentinal(new Buffer(token)) 
  , format = new Format(token, inside_stream)

outside_stream
    .pipe(tokenizer)
    .pipe(format)
    .pipe(process.stdout)

Then node index.js will print:

<html>
    <head>
        <title>"Your app!"</title>
    </head>
    <body>
        <p>"Hey you party people!"</p>
    </body>
</html>
Something went wrong with that request. Please try again.