Add in place versions of compact, uniq, flatten to Array.prototype
Reported by matthuhiggins | April 29th, 2008 @ 09:26 PM
Given that prototype is modeled after Ruby, it seems like the in place '!' versions of these functions are missing. Since Array.prototype.reverse already takes an optional 'inline' parameter, it seems inconsistent that these other functions do not support it.
Let me try to hand type a rough version in this little text box:
_compact: function() {
var index = 0, last = this.length - 1;
while(index <= last) {
if (this[last] == null) {
last--;
} else if (this[index] == null) {
this[index++] = this[last--];
} else {
index++;
}
}
this.length = last + 1;
return this;
},
compact: function(inline) {
return (inline !== false ? this : this.toArray())._compact();
}
...
(Apologies if I am bringing up an old subject that has already been shot down.)
Comments and changes to this ticket
-

-

matthuhiggins April 29th, 2008 @ 09:41 PM
_compact: function() { var index = 0, last = this.length - 1; while(index <= last) { if (this[last] == null) last-- else if (this[index] == null) this[index++] = this[last--] else index++ } this.length = last + 1; return this; }, compact: function(inline) { return (inline !== false ? this : this.toArray())._compact(); } -
Tobie Langel April 29th, 2008 @ 11:51 PM
- → State changed from new to enhancement
-
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
The Prototype JavaScript library.
