From 8cac2d5bd75c606fa987a97a97213e48974ed7f0 Mon Sep 17 00:00:00 2001 From: Luke Sutton Date: Thu, 19 Nov 2009 11:07:14 +1030 Subject: [PATCH 1/2] Add init(), tail() and reverse() Array functions. Alias first() to head(); --- test/arrays.js | 15 +++++++++++++++ underscore.js | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/test/arrays.js b/test/arrays.js index d069d9d62..a9dcb4e90 100644 --- a/test/arrays.js +++ b/test/arrays.js @@ -58,4 +58,19 @@ $(document).ready(function() { equals(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element'); }); + test("arrays: tail", function() { + var numbers = [1, 2, 3, 4]; + equals(_.tail(numbers).join(", "), "2, 3, 4"); + }); + + test("arrays: init", function() { + var numbers = [1, 2, 3, 4]; + equals(_.init(numbers).join(", "), "1, 2, 3"); + }); + + test("arrays: reverse", function() { + var numbers = [1, 2, 4, 6]; + equals(_.reverse(numbers).join(", "), "6, 4, 2, 1"); + }); + }); diff --git a/underscore.js b/underscore.js index ac1b8659d..ad6dbecac 100644 --- a/underscore.js +++ b/underscore.js @@ -305,6 +305,29 @@ while (i--) if (array[i] === item) return i; return -1; }; + + // Returns everything but the first entry of the array. Conceptually the + // same as calling shift(), but doesn't mutate the array passed in. + _.tail = function(array) { + var tail = _.clone(array); + tail.shift(); + return tail; + }; + + // Returns everything but the last entry of the array. Conceptually the + // same as calling pop(), but doesn't mutate the array passed in. + _.init = function(array) { + var init = _.clone(array); + init.pop(); + return init; + }; + + // Returns a new array, with the entries or the passed-in array in reverse + // order. + _.reverse = function(array) { + var reverse = _.clone(array); + return reverse.reverse(); + }; /* ----------------------- Function Functions: -----------------------------*/ @@ -501,6 +524,7 @@ /*------------------------------- Aliases ----------------------------------*/ + _.head = _.first; _.forEach = _.each; _.foldl = _.inject = _.reduce; _.foldr = _.reduceRight; From 4ed79d5f778e8e8eb5fa0232545ac719cf4c9d42 Mon Sep 17 00:00:00 2001 From: Luke Sutton Date: Thu, 19 Nov 2009 11:35:21 +1030 Subject: [PATCH 2/2] Correct the test for functions() to account for the new functions and aliases. --- test/utility.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/utility.js b/test/utility.js index ed6ee120b..0acc6a16d 100644 --- a/test/utility.js +++ b/test/utility.js @@ -31,13 +31,13 @@ $(document).ready(function() { }); test("utility: functions", function() { - var expected = ["all", "any", "bind", "bindAll", "breakLoop", "clone", "compact", "compose", - "defer", "delay", "detect", "each", "every", "extend", "filter", "first", - "flatten", "foldl", "foldr", "forEach", "functions", "identity", "include", - "indexOf", "inject", "intersect", "invoke", "isArray", "isElement", "isEmpty", "isEqual", + var expected = ["all", "any", "bind", "bindAll", "breakLoop", "clone", "compact", + "compose","defer", "delay", "detect", "each", "every", "extend", "filter", "first", + "flatten", "foldl", "foldr", "forEach", "functions", "head", "identity", "include", + "indexOf", "init", "inject", "intersect", "invoke", "isArray", "isElement", "isEmpty", "isEqual", "isFunction", "isNumber", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max", - "methods", "min", "pluck", "reduce", "reduceRight", "reject", "select", - "size", "some", "sortBy", "sortedIndex", "template", "toArray", "uniq", + "methods", "min", "pluck", "reduce", "reduceRight", "reject", "reverse", "select", + "size", "some", "sortBy", "sortedIndex", "tail", "template", "toArray", "uniq", "uniqueId", "values", "without", "wrap", "zip"]; ok(_(expected).isEqual(_.methods()), 'provides a sorted list of functions'); });