Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Formatted strings for object streams
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
.gitignore
README.md
index.js
package.json

README.md

forma

Streaming string formatter

Forma is a Transform stream that converts data from any object stream to a formatted string, using util.format and Confidence.

API

Pipe an object stream to a forma object. The forma object takes one argument: an object of options.

Possible options:

  • format - The format string, passed as the first argument to util.format.
  • variables - An array of keys for Confidence to match against when an object is processed. An object such as {some: {data: "being"}, piped: true} would be mapped as follows:

    "/some" - {data: "being"}

    "/some/data" - "being"

    "/piped" - true

    The values will be passed sequentially to util.format.

Example

var stream = require("stream"),
    forma = require("./"),
    util = require("util");

var MyObjStream = function(options) {
    var options = options || {};
    options.objectMode = true;
    stream.Transform.call(this, options);
}

util.inherits(MyObjStream, stream.Transform);

MyObjStream.prototype._transform = function(data, encoding, callback) {
    this.push(data);
    callback();
}

var instance = new MyObjStream();

instance.pipe(forma({
    format: "Hello, %s! I hear that you're %d years old.",
    variables: [
        "/name",
        "/age"
    ]
})).on("data", console.log);

instance.write({name: "Tim", age: 45});
instance.write({name: "Jonah", age: 19});
instance.write({name: "Elsa", age: 22});

This will output the following to stdout:

Hello, Tim! I hear that you're 45 years old.
Hello, Jonah! I hear that you're 19 years old.
Hello, Elsa! I hear that you're 22 years old.
Something went wrong with that request. Please try again.