Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Compose functions together

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 bin
Octocat-spinner-32 examples
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 LICENCE
Octocat-spinner-32 Makefile
Octocat-spinner-32 README.md
Octocat-spinner-32 index.js
Octocat-spinner-32 package.json
Octocat-spinner-32 testem.json
README.md

Composite build status

Compose functions together

Function composition is a mathematical concept where a function is applied to the results of another (inner) function. In algebra this is often expressed as "g of f" when describing two functions "f" and "g", where g is applied to the results of f. The notation is typically: (g ° f)(x) or alternately g(f(x)).

Example

var compose = require("composite"),
    composeAsync = compose.async

// note the last argument gets called first just like the mathematical 
// composition operator
var composed = compose(function (b) {
    return b * 2
}, function (a) {
    return a * 3
})

composed(3) // 18

var composedAsync = composeAsync(function second(err, b, outer) {
    outer(null, b * 2)
}, function first(a, second) {
    second(null, a * 3)
})

composedAsync(3, function outer(err, result) {
    // error === null, result === 18
})

Example abusing this

var composed = composeAsync(parseFile, stringifyFile, fs.readFile)

composed.call({}, "foo.js", function (err, data) {
    // data = someJSONObject
})

// If you specify a this value, then that value will be used as this for
// all the functionsan empty object is given. This is useful to store state
function stringifyFile(err, data, cb) {
    if (err) {
        return cb(err)
    }

    this.data = data.toString()
    cb(null)
}

function parseFile(err, cb) {
    cb(err, this.data && JSON.parse(this.data))
}

Example Working with errors

composeAsync(function (err, data, cb) {
    // note how the callback is still the third parameter.
    // This is because asyncCompose ensures cb is always the
    // last parameter
    if (err) {
        return cb(err)
    }
}, function (cb) {
    cb(new Error("oops"))
})(function (err) {
    // we got the oops err
})

Installation

npm install composite

Tests

make test

Contributors

  • Raynos

MIT Licenced

Something went wrong with that request. Please try again.