UNPKG

underarm

Version:

Transducers for JavaScript with Underscore API and Async extensions

87 lines (76 loc) 2.2 kB
'use strict' var array = require('transduce/array') var forEach = array.forEach module.exports = function(_r){ // Array Functions // --------------- _r.mixin({ forEach: forEach, each: forEach, find: find, detect: find, every: every, all: every, some: some, any: some, contains: contains, include: contains, findWhere: findWhere, push: array.push, unshift: array.unshift, at: at, slice: array.slice, initial: array.initial, last: last }) var iteratee = _r.iteratee, resolveSingleValue = _r.resolveSingleValue, _ = _r._ // Return the first value which passes a truth test. Aliased as `detect`. function find(predicate) { /*jshint validthis:true*/ resolveSingleValue(this) return array.find(iteratee(predicate)) } // Determine whether all of the elements match a truth test. // Aliased as `all`. function every(predicate) { /*jshint validthis:true*/ resolveSingleValue(this) return array.every(iteratee(predicate)) } // Determine if at least one element in the object matches a truth test. // Aliased as `any`. function some(predicate) { /*jshint validthis:true*/ resolveSingleValue(this) return array.some(iteratee(predicate)) } // Determine if contains a given value (using `===`). // Aliased as `include`. function contains(target) { /*jshint validthis:true*/ return some.call(this, function(x){ return x === target }) } // Convenience version of a common use case of `find`: getting the first object // containing specific `key:value` pairs. function findWhere(attrs) { /*jshint validthis:true*/ return find.call(this, _.matches(attrs)) } // Retrieves the value at the given index. Resolves as single value. function at(idx){ /*jshint validthis:true*/ resolveSingleValue(this) return array.slice(idx, idx+1) } // Get the last element. Passing **n** will return the last N values. // Note that no items will be sent until completion. function last(n) { if(n === void 0){ /*jshint validthis:true*/ resolveSingleValue(this) } return array.last(n) } }