Underscore.js: take

Similar to tap() but changes the current object in the chaining. Or seen another way: similar to map() but on a single object.

by Andrew Dunai

HTML

<script src="http://documentcloud.github.com/underscore/underscore.js"></script>

JavaScript

_.mixin({
    take: function(obj, interceptor, context) {
        return interceptor.call(context, obj);
    }
});

function double(value) { return value * 2; }

_([42, 43]).chain()
    .first()            // 42
    .take(double)       // Applies double to 42: 84
    .tap(console.log);