UNPKG

underarm

Version:

Transducers for JavaScript with Underscore API and Async extensions

91 lines (78 loc) 2.04 kB
'use strict' var util = require('./util-internal'), merge = util.objectMerge, isArray = util.isArray, isFunction = util.isFunction var _r = function(obj, transform) { if (_r.as(obj)){ if(transform === void 0){ return obj } var wrappedFns = obj._wrappedFns.slice() wrappedFns.push(transform) var copy = new _r(obj._wrapped, wrappedFns) copy._opts = merge({}, obj._opts) return copy } if (!(_r.as(this))) return new _r(obj, transform) if(_r.as(transform)){ this._opts = merge({}, transform._opts) transform = transform._wrappedFns } else { this._opts = {} } if(isFunction(transform)){ this._wrappedFns = [transform] } else if(isArray(transform)){ this._wrappedFns = transform } else { this._wrappedFns = [] } this._wrapped = _r.wrap.call(this, obj) } _r.VERSION = '0.8.0' // Export for browser or Common-JS // Save the previous value of the `_r` variable. var previous_r, root if(typeof window !== 'undefined'){ /*global window*/ var root = window previous_r = root._r root._r = _r _r._ = root._ } else { root = {} } module.exports = _r // Returns the value if it is a chained transformation, else null _r.as = function(value){ return value instanceof _r ? value : null } // Run Underscore.js in *noConflict* mode, returning the `_` variable to its // previous owner. Returns a reference to the Underscore object. _r.noConflict = function() { root._r = previous_r return this } // Returns a new chained instance using current transformation, but // wrapping the given source _r.prototype.withSource = function(obj){ return _r(obj, this) } // Add your own custom transducers to the Underscore.transducer object. _r.mixin = function(obj) { var name, fn for(name in obj){ fn = obj[name] if(typeof fn === 'function'){ _r[name] = fn _r.prototype[name] = _method(fn) } } } function _method(func){ return function() { var method = func.apply(this, arguments) return _r(this, method) } }