Fix bug with _.last(a, n) incorrectly handling n > a.length

This commit is contained in:
David Hirtle
2011-11-08 01:13:08 -08:00
parent 0535545038
commit 05c107576b
2 changed files with 3 additions and 1 deletions

View File

@@ -335,7 +335,7 @@
// Get the last element of an array. Passing **n** will return the last N
// 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];
return (n != null) && !guard ? slice.call(array, Math.max(array.length - n, 0)) : array[array.length - 1];
};
// Returns everything but the first entry of the array. Aliased as `tail`.