Permalink
Browse files

Fix bug when calling method on second item in collection

  • Loading branch information...
1 parent 942ef73 commit d3a1020d9d61b99d5dbe182ab240010dd6370375 @captbaritone captbaritone committed Nov 3, 2016
Showing with 12 additions and 4 deletions.
  1. +8 −1 test/collections.js
  2. +4 −3 underscore.js
View
@@ -451,7 +451,7 @@
QUnit.test('invoke', function(assert) {
- assert.expect(12);
+ assert.expect(13);
var list = [[5, 1, 7], [3, 2, 1]];
var result = _.invoke(list, 'sort');
assert.deepEqual(result[0], [1, 5, 7], 'first array sorted');
@@ -493,6 +493,13 @@
assert.deepEqual(_.invoke(arr, ['e']), ['foo'], 'handles path arrays of length one');
assert.deepEqual(_.invoke(arr, ['f']), [item], 'correct uses parent context with shallow array syntax');
assert.deepEqual(_.invoke(arr, ['g', 'h']), [void 0], 'does not execute intermediate functions');
+
+ arr = [{
+ a: function() { return 'foo'; }
+ }, {
+ a: function() { return 'bar'; }
+ }];
+ assert.deepEqual(_.invoke(arr, 'a'), ['foo', 'bar'], 'can handle different methods on subsequent objects');
});
QUnit.test('invoke w/ function reference', function(assert) {
View
@@ -300,12 +300,13 @@
path = path[path.length - 1];
}
return _.map(obj, function(context) {
- if (!func) {
+ var method = func;
+ if (!method) {
if (contextPath.length) context = deepGet(context, contextPath);
if (context == null) return void 0;
- func = context[path];
+ method = context[path];
}
- return func == null ? func : func.apply(context, args);
+ return method == null ? method : method.apply(context, args);
});
});

0 comments on commit d3a1020

Please sign in to comment.