diff --git a/CHANGELOG b/CHANGELOG index dfe1afe..de46059 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Remove Array#reduce which currently overrides native `reduce` in clients implementing JS1.8, e.g. Firefox 3+ (Tobie Langel, Andrew Dupont, kangax) + * Make sure try/catch/finally is used instead of try/finally for clients without support for the latter one (e.g. Blackberry, IE) (Ville Koskinen, kangax) * Use `in` operator when accessing property of a nodelist to prevent Safari <=2.0.4 from crashing (kangax) diff --git a/src/lang/array.js b/src/lang/array.js index 62f5f4d..c71c1a2 100644 --- a/src/lang/array.js +++ b/src/lang/array.js @@ -206,15 +206,6 @@ Array.from = $A; return (inline !== false ? this : this.toArray())._reverse(); } - /** - * Array#reduce() -> Array - * Reduces arrays: one-element arrays are turned into their unique item, - * while multiple-element arrays are returned untouched. - **/ - function reduce() { - return this.length > 1 ? this : this[0]; - } - /** * Array#uniq([sorted = false]) -> Array * - sorted (Boolean): Whether the array has already been sorted. If `true`, @@ -349,7 +340,6 @@ Array.from = $A; flatten: flatten, without: without, reverse: reverse, - reduce: reduce, uniq: uniq, intersect: intersect, clone: clone, diff --git a/test/unit/array_test.js b/test/unit/array_test.js index 88d8fad..d090504 100644 --- a/test/unit/array_test.js +++ b/test/unit/array_test.js @@ -137,14 +137,6 @@ new Test.Unit.Runner({ this.assertEqual('[\"a\", 1]', ['a', 1].toJSON()); this.assertEqual('[\"a\", {\"b\": null}]', ['a', {'b': null}].toJSON()); }, - - testReduce: function(){ - this.assertUndefined([].reduce()); - this.assertNull([null].reduce()); - this.assertEqual(1, [1].reduce()); - this.assertEnumEqual([1,2,3], [1,2,3].reduce()); - this.assertEnumEqual([1,null,3], [1,null,3].reduce()); - }, testReverse: function(){ this.assertEnumEqual([], [].reverse()); diff --git a/test/unit/selector_test.js b/test/unit/selector_test.js index 5501eed..9ec3c34 100644 --- a/test/unit/selector_test.js +++ b/test/unit/selector_test.js @@ -1,5 +1,9 @@ var $RunBenchmarks = false; +function reduce(arr) { + return arr.length > 1 ? arr : arr[0]; +} + new Test.Unit.Runner({ testSelectorWithTagName: function() { @@ -193,12 +197,12 @@ new Test.Unit.Runner({ testSelectorWithAdjacence: function() { this.assertEnumEqual([$('uncle')], $$('div.brothers + div.brothers')); this.assertEnumEqual([$('uncle')], $$('div.brothers + div')); - this.assertEqual($('level2_2'), $$('#level2_1+span').reduce()); - this.assertEqual($('level2_2'), $$('#level2_1 + span').reduce()); - this.assertEqual($('level2_2'), $$('#level2_1 + *').reduce()); + this.assertEqual($('level2_2'), reduce($$('#level2_1+span'))); + this.assertEqual($('level2_2'), reduce($$('#level2_1 + span'))); + this.assertEqual($('level2_2'), reduce($$('#level2_1 + *'))); this.assertEnumEqual([], $$('#level2_2 + span')); - this.assertEqual($('level3_2'), $$('#level3_1 + span').reduce()); - this.assertEqual($('level3_2'), $$('#level3_1 + *').reduce()); + this.assertEqual($('level3_2'), reduce($$('#level3_1 + span'))); + this.assertEqual($('level3_2'), reduce($$('#level3_1 + *'))); this.assertEnumEqual([], $$('#level3_2 + *')); this.assertEnumEqual([], $$('#level3_1 + em')); $RunBenchmarks && this.wait(500, function() { @@ -208,8 +212,8 @@ new Test.Unit.Runner({ testSelectorWithLaterSibling: function() { this.assertEnumEqual([$('list')], $$('h1 ~ ul')); - this.assertEqual($('level2_2'), $$('#level2_1 ~ span').reduce()); - this.assertEnumEqual($('level2_2', 'level2_3'), $$('#level2_1 ~ *').reduce()); + this.assertEqual($('level2_2'), reduce($$('#level2_1 ~ span'))); + this.assertEnumEqual($('level2_2', 'level2_3'), reduce($$('#level2_1 ~ *'))); this.assertEnumEqual([], $$('#level2_2 ~ span')); this.assertEnumEqual([], $$('#level3_2 ~ *')); this.assertEnumEqual([], $$('#level3_1 ~ em'));