mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
Add guard check to _.last()
Allows _.last() to work as expected with _.map().
This commit is contained in:
@@ -30,6 +30,8 @@ $(document).ready(function() {
|
||||
equals(_.last([1,2,3], 2).join(', '), '2, 3', 'can pass an index to last');
|
||||
var result = (function(){ return _(arguments).last(); })(1, 2, 3, 4);
|
||||
equals(result, 4, 'works on an arguments object');
|
||||
result = _.map([[1,2,3],[1,2,3]], _.last);
|
||||
equals(result.join(','), '3,3', 'works well with _.map');
|
||||
});
|
||||
|
||||
test("arrays: compact", function() {
|
||||
|
||||
@@ -307,9 +307,9 @@
|
||||
};
|
||||
|
||||
// Get the last element of an array. Passing **n** will return the last N
|
||||
// values in the array.
|
||||
_.last = function(array, n) {
|
||||
return (n != null) ? slice.call(array, array.length - n) : array[array.length - 1];
|
||||
// values in the array. The **guard** check allows it to work with `_.map`.
|
||||
_.last = function(array, n, guard) {
|
||||
return (n != null) && !guard ? slice.call(array, array.length - n) : array[array.length - 1];
|
||||
};
|
||||
|
||||
// Trim out all falsy values from an array.
|
||||
|
||||
Reference in New Issue
Block a user