Skip to content


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.


Streaming string formatter

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


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.


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

var MyObjStream = function(options) {
    var options = options || {};
    options.objectMode = true;, options);

util.inherits(MyObjStream, stream.Transform);

MyObjStream.prototype._transform = function(data, encoding, callback) {

var instance = new MyObjStream();

    format: "Hello, %s! I hear that you're %d years old.",
    variables: [
})).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.