Skip to content

Commit

Permalink
Fix bug when calling method on second item in collection
Browse files Browse the repository at this point in the history
  • Loading branch information
captbaritone committed Nov 3, 2016
1 parent 942ef73 commit d3a1020
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 8 additions & 1 deletion test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down

0 comments on commit d3a1020

Please sign in to comment.