From a8f0445192aeac118b3f347a3abfc7bf5af51fd3 Mon Sep 17 00:00:00 2001 From: Malcolm Locke Date: Wed, 31 Aug 2011 22:39:05 +1200 Subject: [PATCH 1/2] Add an optional index argument to _.last() This makes _.last() behave the same as _.first(). Passing an optional second argument n will return the last n elements of the array. --- test/arrays.js | 2 ++ underscore.js | 7 ++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/test/arrays.js b/test/arrays.js index 78cf098a0..bbf74e6da 100644 --- a/test/arrays.js +++ b/test/arrays.js @@ -26,6 +26,8 @@ $(document).ready(function() { test("arrays: last", function() { equals(_.last([1,2,3]), 3, 'can pull out the last element of an array'); + equals(_.last([1,2,3], 0).join(', '), "", 'can pass an index to last'); + 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'); }); diff --git a/underscore.js b/underscore.js index 0d587b41c..7963394cb 100644 --- a/underscore.js +++ b/underscore.js @@ -306,9 +306,10 @@ return slice.call(array, (index == null) || guard ? 1 : index); }; - // Get the last element of an array. - _.last = function(array) { - return array[array.length - 1]; + // 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]; }; // Trim out all falsy values from an array. From e449b00a26d0bfc29c0be42f1ea98c7d8f5493b9 Mon Sep 17 00:00:00 2001 From: Malcolm Locke Date: Thu, 1 Sep 2011 01:10:10 +1200 Subject: [PATCH 2/2] Add guard check to _.last() Allows _.last() to work as expected with _.map(). --- test/arrays.js | 2 ++ underscore.js | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/test/arrays.js b/test/arrays.js index bbf74e6da..950e8417f 100644 --- a/test/arrays.js +++ b/test/arrays.js @@ -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() { diff --git a/underscore.js b/underscore.js index 7963394cb..239d7dd34 100644 --- a/underscore.js +++ b/underscore.js @@ -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.