Extending Prototype - squeeze me!

written by Tobie on October 3rd, 2006 @ 04:22 AM

I have been cleaning up some of my stuff lately and posted a bunch of enhancement patches to Prototype which I had been working on for some of my current projects.

Until they make it to the trunk, they have also been added to xPrototype, my small enhancement lib to Prototype, which thus moves into version 0.1rc3.

The ones which will make it to Prototype's trunk will be removed from future releases of xPrototype.

These patches provide the following enhancements:

  • a clone() method for Array (previously explained here),
  • isOdd() and isEven() methods for Number (base on this code),
  • a capitalize() method for String (ruthlessly stolen from Andrew Dupont),
  • a stripLineBreaks() String method, which simply replaces line breaks by a space,
  • an addLeadingZero() method (which you'll learn more about in an upcoming post) and
  • an emulation of Ruby's squeeze() method.

Most of these enhancements are trivial, and some have partially been covered in my previous posts, so I will only cover the latter.

squeeeeeeeeeeeeeze!

Just like it's Ruby counterpart, squeeze() allows easy removal of duplicate characters from a given string.

For instance, it's great for removing extra whitespace:

'hello       world'.squeeze(' ');
// -> 'hello world'

Cleaning-up (excessive) punctuation marks:

'hello world!!!!!!!!!!'.squeeze('!');
// -> 'hello world!'

or even better, doing both at the same time!

'hello        world!!!!!!!!!!???????'.squeeze(' ?!');
// -> 'hello world!?'

As you can see, squeeze() really behaves like it's rubyish cousin; each character passed in the argument is treated separately and its duplicates removed accordingly.

Note that the following behavior is emulated too:

'hello world'.squeeze('');

leaves the string untouched... and

'hello world'.squeeze();
// -> 'helo world'

removes every duplicate characters... which can lead to some unexpected results.

how it differs

The proposed patch does not however offer the possibility of passing a range of characters as argument like ruby's squeeze() can accept. Obtaining the same effect is still possible, only a little more verbose:

'aaa sttring'.squeeze('t-z');// ruby style 'aaa sttring'.squeeze('tuvwxyz'); // current JavaScript equivalent

Any comments and suggestions on how to improve these methods are of course always welcomed.

UPDATE: Thanks to Martin's suggestion, squeeze() now accepts character ranges just like ruby's, so the following:

'hello wwwwworrrrrrrld'.squeeze('q-z');

will now correctly output:

// -> 'hello world'

The method should be a tiny bit faster too.

Comments

  • Great work! It would be nice if you could use a regex for the chars like: ‘aaa sttring’.squeeze(/[t-z]/)

    Tue, October 03 at 08:42 AM

  • Nice work some useful methods.

    Wed, October 04 at 00:36 AM

  • I’m afraid it is true. I will not be using prototype in future projects. With that said though I still have a great appreciation for some of the code in prototype. Prototype has taught me a lot.

    jQuery is my library of choice now.

    I was trying to update a couple of my patches for prototype the other day but kept getting an error while trying to register with trac… so oh well …

    Not sure if you want any of it… but you are more than welcome to any of the patches I’ve made to prototype to include in your prototype extension library.

    Wed, October 04 at 17:24 PM

Comments are closed