mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
Merge branch 'fix_reduceRight_initial_value' of http://github.com/mcmire/underscore
This commit is contained in:
@@ -71,8 +71,14 @@ $(document).ready(function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('collections: reduceRight', function() {
|
test('collections: reduceRight', function() {
|
||||||
var list = _.foldr([1, 2, 3], function(memo, num){ return memo + num; }, '');
|
var list = _.reduceRight(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
|
||||||
equals(list, '321', 'can perform right folds');
|
equals(list, 'bazbarfoo', 'can perform right folds');
|
||||||
|
|
||||||
|
var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
|
||||||
|
equals(list, 'bazbarfoo', 'aliased as "foldr"');
|
||||||
|
|
||||||
|
var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; });
|
||||||
|
equals(list, 'bazbarfoo', 'default initial value');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('collections: detect', function() {
|
test('collections: detect', function() {
|
||||||
|
|||||||
@@ -112,7 +112,9 @@
|
|||||||
_.reduceRight = _.foldr = function(obj, iterator, memo, context) {
|
_.reduceRight = _.foldr = function(obj, iterator, memo, context) {
|
||||||
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
|
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
|
||||||
if (context) iterator = _.bind(iterator, context);
|
if (context) iterator = _.bind(iterator, context);
|
||||||
return obj.reduceRight(iterator, memo);
|
var args = [iterator];
|
||||||
|
if (memo !== undefined) args.push(memo);
|
||||||
|
return obj.reduceRight.apply(obj, args);
|
||||||
}
|
}
|
||||||
var reversed = (_.isArray(obj) ? obj.slice() : _.toArray(obj)).reverse();
|
var reversed = (_.isArray(obj) ? obj.slice() : _.toArray(obj)).reverse();
|
||||||
return _.reduce(reversed, iterator, memo, context);
|
return _.reduce(reversed, iterator, memo, context);
|
||||||
|
|||||||
Reference in New Issue
Block a user