underarm
Version:
Transducers for JavaScript with Underscore API and Async extensions
138 lines (119 loc) • 3.54 kB
JavaScript
var slice = Array.prototype.slice
module.exports = function(_r){
// Base Transducers
// ----------------
_r.mixin({
map: map,
collect: map,
filter: filter,
select: filter,
remove: remove,
reject: remove,
take: take,
first: take,
head: take,
takeWhile: takeWhile,
drop: drop,
rest: drop,
tail: drop,
dropWhile: dropWhile,
cat: cat,
mapcat: mapcat,
partitionAll: partitionAll,
chunkAll: partitionAll,
partitionBy: partitionBy,
compact: compact,
invoke: invoke,
pluck: pluck,
where: where
})
var iteratee = _r.iteratee,
_ = _r._,
util = require('./util-internal'),
transducers = require('transduce/transducers'),
isFunction = util.isFunction,
identity = util.identity
// Return the results of applying the iteratee to each element.
function map(f) {
return transducers.map(iteratee(f))
}
// Return all the elements that pass a truth test.
// Aliased as `select`.
function filter(predicate) {
return transducers.filter(iteratee(predicate))
}
// Return all the elements for which a truth test fails.
function remove(predicate) {
return transducers.remove(iteratee(predicate))
}
// Get the first element of an array. Passing **n** will return the first N
// values in the array. Aliased as `head` and `take`.
function take(n) {
if(n === void 0){
/*jshint validthis:true*/
_r.resolveSingleValue(this)
n = 1
} else {
n = (n > 0) ? n : 0
}
return transducers.take(n)
}
// takes items until predicate returns false
function takeWhile(predicate) {
return transducers.takeWhile(iteratee(predicate))
}
// Returns everything but the first entry. Aliased as `tail` and `drop`.
// Passing an **n** will return the rest N values.
function drop(n) {
n = (n === void 0) ? 1 : (n > 0) ? n : 0
return transducers.drop(n)
}
// Drops items while the predicate returns true
function dropWhile(predicate) {
return transducers.dropWhile(iteratee(predicate))
}
// Concatenating transducer.
// NOTE: unlike libraries, cat should be called as a function to use.
// _r.cat() not _r.cat
function cat(){
return transducers.cat
}
// mapcat.
// Composition of _r.map(f) and _r.cat()
function mapcat(f){
return transducers.mapcat(iteratee(f))
}
// Partitions the source into arrays of size n
// When transformer completes, the array will be stepped with any remaining items.
// Alias chunkAll
function partitionAll(n){
return transducers.partitionAll(n)
}
// Partitions the source into sub arrays while the value of the function
// changes equality.
function partitionBy(f){
return transducers.partitionBy(iteratee(f))
}
// Trim out all falsy values from an array.
function compact() {
return filter(identity)
}
// Invoke a method (with arguments) on every item in a collection.
function invoke(method) {
var args = slice.call(arguments, 2),
isFunc = isFunction(method)
return map(function(value) {
return (isFunc ? method : value[method]).apply(value, args)
})
}
// Convenience version of a common use case of `map`: fetching a property.
function pluck(key) {
return map(_.property(key))
}
// Convenience version of a common use case of `filter`: selecting only objects
// containing specific `key:value` pairs.
function where(attrs) {
return filter(_.matches(attrs))
}
}