# jashkenas/underscore

Revert "_.partition only accepts arrays."

`This reverts commit e4785d3.`
1 parent 6456f11 commit b3e20f92b8713e2269d95c93e34d8779240b5a66
Showing with 33 additions and 33 deletions.
1. +12 −12 index.html
2. +0 −17 test/arrays.js
3. +18 −0 test/collections.js
4. +3 −4 underscore.js
 @@ -221,6 +221,7 @@
• - sample
• - toArray
• - size
• +
• - partition
• @@ -234,7 +235,6 @@
• - compact
• - flatten
• - without
• -
• - partition
• - union
• - intersection
• - difference
• @@ -773,6 +773,17 @@

Collection Functions (Arrays or Objects)

_.size({one: 1, two: 2, three: 3});
=> 3
+
+ +

+ partition_.partition(array, predicate) +
+ Split array into two arrays: one whose elements all satisfy + predicate and one whose elements all do not satisfy predicate. +

+
+_.partition([0, 1, 2, 3, 4, 5], isOdd);
+=> [[1, 3, 5], [0, 2, 4]]

Array Functions

@@ -866,17 +877,6 @@

Array Functions

_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
=> [2, 3, 4]
-
- -

- partition_.partition(array, predicate) -
- Split array into two arrays: one whose elements all satisfy - predicate and one whose elements all do not satisfy predicate. -

-
-_.partition([0, 1, 2, 3, 4, 5], isOdd);
-=> [[1, 3, 5], [0, 2, 4]]

 @@ -226,21 +226,4 @@ equal(_.range(0, -10, -1).join(' '), '0 -1 -2 -3 -4 -5 -6 -7 -8 -9', 'final example in the Python docs'); }); - test('partition', function() { - var list = [0, 1, 2, 3, 4, 5]; - deepEqual(_.partition(list, function(x) { return x < 4; }), [[0,1,2,3],[4,5]], 'handles bool return values'); - deepEqual(_.partition(list, function(x) { return x & 1; }), [[1,3,5],[0,2,4]], 'handles 0 and 1 return values'); - deepEqual(_.partition(list, function(x) { return x - 3; }), [[0,1,2,4,5],[3]], 'handles other numeric return values'); - deepEqual(_.partition(list, function(x) { return x > 1 ? null : true; }), [[0,1],[2,3,4,5]], 'handles null return values'); - deepEqual(_.partition(list, function(x) { if(x < 2) return true; }), [[0,1],[2,3,4,5]], 'handles undefined return values'); - - // Default iterator - deepEqual(_.partition([1, false, true, '']), [[1, true], [false, '']], 'Default iterator'); - deepEqual(_.partition([{x: 1}, {x: 0}, {x: 1}], 'x'), [[{x: 1}, {x: 1}], [{x: 0}]], 'Takes a string'); - - // Context - var predicate = function(x){ return x === this.x }; - deepEqual(_.partition([1, 2, 3], predicate, {x: 2}), [[2], [1, 3]], 'partition takes a context argument'); - }); - })();
 @@ -511,4 +511,22 @@ equal(_.size(null), 0, 'handles nulls'); }); + test('partition', function() { + var list = [0, 1, 2, 3, 4, 5]; + deepEqual(_.partition(list, function(x) { return x < 4; }), [[0,1,2,3],[4,5]], 'handles bool return values'); + deepEqual(_.partition(list, function(x) { return x & 1; }), [[1,3,5],[0,2,4]], 'handles 0 and 1 return values'); + deepEqual(_.partition(list, function(x) { return x - 3; }), [[0,1,2,4,5],[3]], 'handles other numeric return values'); + deepEqual(_.partition(list, function(x) { return x > 1 ? null : true; }), [[0,1],[2,3,4,5]], 'handles null return values'); + deepEqual(_.partition(list, function(x) { if(x < 2) return true; }), [[0,1],[2,3,4,5]], 'handles undefined return values'); + deepEqual(_.partition({a: 1, b: 2, c: 3}, function(x) { return x > 1; }), [[2, 3], [1]], 'handles objects'); + + // Default iterator + deepEqual(_.partition([1, false, true, '']), [[1, true], [false, '']], 'Default iterator'); + deepEqual(_.partition([{x: 1}, {x: 0}, {x: 1}], 'x'), [[{x: 1}, {x: 1}], [{x: 0}]], 'Takes a string'); + + // Context + var predicate = function(x){ return x === this.x }; + deepEqual(_.partition([1, 2, 3], predicate, {x: 2}), [[2], [1, 3]], 'partition takes a context argument'); + }); + })();
 @@ -465,13 +465,12 @@ // Split an array into two arrays: one whose elements all satisfy the given // predicate, and one whose elements all do not satisfy the predicate. - _.partition = function(array, predicate, context) { + _.partition = function(obj, predicate, context) { predicate = lookupIterator(predicate); var pass = [], fail = []; - for (var i = 0, length = array.length; i < length; i++) { - var elem = array[i]; + each(obj, function(elem) { (predicate.call(context, elem) ? pass : fail).push(elem); - } + }); return [pass, fail]; };