chainsaw

WebJar for chainsaw

License

License

MIT
Categories

Categories

Github Development Tools Version Controls
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

github-com-substack-node-chainsaw
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

chainsaw
WebJar for chainsaw
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/substack/node-chainsaw

Download github-com-substack-node-chainsaw

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/github-com-substack-node-chainsaw/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>github-com-substack-node-chainsaw</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/github-com-substack-node-chainsaw/
implementation 'org.webjars.npm:github-com-substack-node-chainsaw:0.1.0'
// https://jarcasting.com/artifacts/org.webjars.npm/github-com-substack-node-chainsaw/
implementation ("org.webjars.npm:github-com-substack-node-chainsaw:0.1.0")
'org.webjars.npm:github-com-substack-node-chainsaw:jar:0.1.0'
<dependency org="org.webjars.npm" name="github-com-substack-node-chainsaw" rev="0.1.0">
  <artifact name="github-com-substack-node-chainsaw" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='github-com-substack-node-chainsaw', version='0.1.0')
)
libraryDependencies += "org.webjars.npm" % "github-com-substack-node-chainsaw" % "0.1.0"
[org.webjars.npm/github-com-substack-node-chainsaw "0.1.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.webjars.npm : traverse jar [0.3.0,0.4)

Project Modules

There are no modules declared in this project.

chainsaw

Build chainable fluent interfaces the easy way in node.js.

build status

With this meta-module you can write modules with chainable interfaces. Chainsaw takes care of all of the boring details and makes nested flow control super simple too.

Just call chainsaw with a constructor function like in the examples below. In your methods, just do saw.next() to move along to the next event and saw.nest() to create a nested chain.

chainsaw

Examples

add_do.js

This silly example adds values with a chainsaw.

var chainsaw = require('chainsaw');

function AddDo (sum) {
    return chainsaw(function (saw) {
        this.add = function (n) {
            sum += n;
            saw.next();
        };
         
        this.do = function (cb) {
            saw.nest(cb, sum);
        };
    });
}

AddDo(0)
    .add(5)
    .add(10)
    .do(function (sum) {
        if (sum > 12) this.add(-10);
    })
    .do(function (sum) {
        console.log('Sum: ' + sum);
    })
;

Output: Sum: 5

prompt.js

This example provides a wrapper on top of stdin with the help of node-lazy for line-processing.

var chainsaw = require('chainsaw');
var lazy = require('lazy');

module.exports = Prompt;
function Prompt (stream) {
    var waiting = [];
    var lines = [];
    var lazy = lazy(stream).lines.map(String)
        .forEach(function (line) {
            if (waiting.length) {
                var w = waiting.shift();
                w(line);
            }
            else lines.push(line);
        })
    ;
    
    var vars = {};
    return chainsaw(function (saw) {
        this.getline = function (f) {
            var g = function (line) {
                saw.nest(f, line, vars);
            };
            
            if (lines.length) g(lines.shift());
            else waiting.push(g);
        };
        
        this.do = function (cb) {
            saw.nest(cb, vars);
        };
    });
}

And now for the new Prompt() module in action:

var util = require('util');
var stdin = process.openStdin();
 
Prompt(stdin)
    .do(function () {
        util.print('x = ');
    })
    .getline(function (line, vars) {
        vars.x = parseInt(line, 10);
    })
    .do(function () {
        util.print('y = ');
    })
    .getline(function (line, vars) {
        vars.y = parseInt(line, 10);
    })
    .do(function (vars) {
        if (vars.x + vars.y < 10) {
            util.print('z = ');
            this.getline(function (line) {
                vars.z = parseInt(line, 10);
            })
        }
        else {
            vars.z = 0;
        }
    })
    .do(function (vars) {
        console.log('x + y + z = ' + (vars.x + vars.y + vars.z));
        process.exit();
    })
;

Installation

With npm do:

npm install chainsaw

Light Mode vs Full Mode

node-chainsaw supports two different modes. In full mode, every action is recorded, which allows you to replay actions using the jump(), trap() and down() methods.

However, if your chainsaws are long-lived, recording every action can consume a tremendous amount of memory, so we also offer a "light" mode where actions are not recorded and the aforementioned methods are disabled.

To enable light mode simply use chainsaw.light() to construct your saw, instead of chainsaw().

Versions

Version
0.1.0