prototype: Array.prototype.uniq optimization. Closes #7417.

This commit is contained in:
Sam Stephenson 2007-02-05 05:09:41 +00:00
parent 74596ac289
commit 6eca3f0eea
3 changed files with 8 additions and 3 deletions

View File

@ -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]

View File

@ -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;
});
},

View File

@ -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) {