This project is archived and is in readonly mode.

#62 ✓resolved
matthuhiggins

Add in place versions of compact, uniq, flatten to Array.prototype

Reported by matthuhiggins | April 29th, 2008 @ 07:13 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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages