diff --git a/CHANGELOG b/CHANGELOG index 30d8787..b54b0c0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Array.prototype.uniq optimization. Closes #7417. [Christophe Porteneuve] + * String.prototype.endsWith should not fail on multiple occurrences. Closes #7416. [Christophe Porteneuve] * Add Form.Methods.request as a convenience method for serializing and submitting a form via Ajax.Request to the URL in the form's action attribute. [sam] diff --git a/src/array.js b/src/array.js index a594f97..dbf036d 100644 --- a/src/array.js +++ b/src/array.js @@ -68,9 +68,11 @@ Object.extend(Array.prototype, { return this.length > 1 ? this : this[0]; }, - uniq: function() { - return this.inject([], function(array, value) { - return array.include(value) ? array : array.concat([value]); + uniq: function(sorted) { + return this.inject([], function(array, value, index) { + if (0 == index || (sorted ? array.last() != value : !array.include(value))) + array.push(value); + return array; }); }, diff --git a/test/unit/array.html b/test/unit/array.html index 6c0cc55..f8518ff 100644 --- a/test/unit/array.html +++ b/test/unit/array.html @@ -127,6 +127,7 @@ assertEnumEqual([1], [1].uniq()); assertEnumEqual([], [].uniq()); assertEnumEqual([0, 1, 2, 3], [0, 1, 2, 2, 3, 0, 2].uniq()); + assertEnumEqual([0, 1, 2, 3], [0, 0, 1, 1, 2, 3, 3, 3].uniq(true)); }}, testWithout: function(){ with(this) {