From d7acbca2ce84d76549b7a23d510238ea1812d75d Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Wed, 24 Feb 2010 12:53:35 -0500 Subject: [PATCH] reverted _.buildLookup, restoring _.without to it's previous implementation, adding a test for object identity --- test/arrays.js | 4 ++++ test/collections.js | 7 ------- test/objects.js | 2 +- underscore.js | 22 ++-------------------- 4 files changed, 7 insertions(+), 28 deletions(-) diff --git a/test/arrays.js b/test/arrays.js index 9eb8dac54..4b74ecade 100644 --- a/test/arrays.js +++ b/test/arrays.js @@ -46,6 +46,10 @@ $(document).ready(function() { equals(_.without(list, 0, 1).join(', '), '2, 3, 4', 'can remove all instances of an object'); var result = (function(){ return _.without(arguments, 0, 1); })(1, 2, 1, 0, 3, 1, 4); equals(result.join(', '), '2, 3, 4', 'works on an arguments object'); + + var list = [{one : 1}, {two : 2}]; + ok(_.without(list, {one : 1}).length == 2, 'uses real object identity for comparisons.'); + ok(_.without(list, list[0]).length == 1, 'ditto.'); }); test("arrays: uniq", function() { diff --git a/test/collections.js b/test/collections.js index 516dbf334..3759a6495 100644 --- a/test/collections.js +++ b/test/collections.js @@ -164,12 +164,5 @@ $(document).ready(function() { test('collections: size', function() { equals(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object'); }); - - test('collections: buildLookup', function() { - same(_.buildLookup([1,'hi']), {1:true, 'hi':true}, 'defaults values to true'); - same(_.buildLookup([1,'hi'], 1), {1:1, 'hi':1}, 'can override value'); - same(_.buildLookup({5:'five'}), {five: true}, 'making a map from an object uses its values'); - }); - }); diff --git a/test/objects.js b/test/objects.js index cf1c7e68c..ac56c495f 100644 --- a/test/objects.js +++ b/test/objects.js @@ -11,7 +11,7 @@ $(document).ready(function() { }); test("objects: functions", function() { - var expected = ["all", "any", "bind", "bindAll", "breakLoop", "buildLookup", "clone", "compact", + 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", "inject", "intersect", "invoke", "isArguments", "isArray", "isDate", "isElement", "isEmpty", "isEqual", diff --git a/underscore.js b/underscore.js index 1ddd106aa..75d4fdcb9 100644 --- a/underscore.js +++ b/underscore.js @@ -252,24 +252,6 @@ return _.toArray(obj).length; }; - // Build a lookup map from a collection. - // Pay a little memory upfront to make searching a collection for a - // value faster later - // e.g: - // - // var lookup = _.buildLookup([1,2,8]) // {1: true, 2: true, 8: true} - // lookup[key] // instead of: _.include(array, key) - // - // See example usage in #without - // By default sets the value to true, can pass in a value to use instead - _.buildLookup = function (obj, useValue) { - useValue = (useValue === undefined)? true : useValue; - return _.reduce(obj, {}, function (memo, value) { - memo[value] = useValue; - return memo; - }); - }; - // -------------------------- Array Functions: ------------------------------ // Get the first element of an array. Passing "n" will return the first N @@ -308,8 +290,8 @@ // Return a version of the array that does not contain the specified value(s). _.without = function(array) { - var lookup = _.buildLookup(_.rest(arguments)); - return _.filter(array, function(value){ return !lookup[value]; }); + var values = _.rest(arguments); + return _.select(array, function(value){ return !_.include(values, value); }); }; // Produce a duplicate-free version of the array. If the array has already