Nimble

Download: Development | Production (837 bytes, minified and gzipped) Fork me on GitHub

Functional flow-control for JavaScript, that is lightweight and portable. Runs on Node.js and cross-browser, providing a small set of powerful tools, supporting sync and async JavaScript. Combines the most useful features of the underscore and async libraries with a simple API and a vastly reduced filesize.

Nimble on GitHub | Me on Twitter

Map

_.map([1, 2, 3], function (val) {
    return val * 2;
});

Map creates a new array by iterating over a list and applying a transformation to each member.

Run code

Result

Clear result

Map objects

_.map({a:1, b:2, c:3}, function (val, key) {
    return key + '=' + val;
});

Nimble's map function can also iterate over the properties of an object.

Run code

Result

Clear result

Async map

_.map([1, 2, 3], function (val, callback) {
    callback(null, val * 2);
},
function (err, result) {
    console.log(result);
});

By adding a callback, you can perform asynchronous operations, like AJAX requests or Node.js methods.

Like in Node.js, the first argument of the callback is an optional error.

Run code

Result

Clear result

Filter

_.filter([1, 2, 3], function (val) {
    return val % 2;
});

Filter creates a new list containing only the values for which the function returns true. Here, we return the remainder of each value when divided by two.

Like map, filter can also accept objects and use callbacks for async operations.

Run code

Result

Clear result

Reduce

_.reduce([1, 2, 3], function (memo, val) {
    return memo + val;
}, 0);

Reduce returns a single value by calling the function on each item, passing in the previous result and the current item's value.

Reduce can also accept objects and use callbacks for async operations. A start value is passed in as the 3rd argument.

Run code

Result

Clear result

Each

_.each([1, 2, 3], function (val) {
    console.log(val);
});

If you just want to iterate over a list or object, you can use each. Again, each can use callbacks for performing async operations.

Run code

Result

Clear result

Parallel

_.parallel([
    function (callback) {
        setTimeout(function () {
            console.log('one');
            callback();
        }, 25);
    },
    function (callback) {
        setTimeout(function () {
            console.log('two');
            callback();
        }, 0);
    }
]);

Nimble also contains some useful flow control functions. Parallel will run a list of async functions all at the same time.

You can also add a final callback to use once all functions have completed as the second argument to _.parallel.

Run code

Result

Clear result

Series

_.series([
    function (callback) {
        setTimeout(function () {
            console.log('one');
            callback();
        }, 25);
    },
    function (callback) {
        setTimeout(function () {
            console.log('two');
            callback();
        }, 0);
    }
]);

Series works similarly to parallel, only it runs each function only once the previous has finished.

Again, you can add a final callback to use once all functions have completed.

Run code

Result

Clear result

Nimble is small

By adding only the most frequently used features, and designing the code with minification and gzip compression in mind, Nimble is able to fit into a tiny 837 bytes. Previously, I was including both underscore.js and async.js, which adds up to over 5kb.