Saturday, December 6, 2008

Rich Array jQuery plugin

Programmers often deal with arrays. This data type is present in almost all programming languages. In this case the difference between languages is in the set of functions of standard library for working with arrays. JavaScript is not an exception, there is a built-in object called Array in this language, with methods for array slicing, getting the index of the element with a specified value etc. In most cases this set of methods is enough (it is excess after all), but sometimes we have a desire to have more rich set of functions, because we have to perform the same operations on arrays several times in our application, and these operations aren't present in the standard library. As a result, you have to write boring, trivial functions, consisting of the only for loop.

Programmers shouldn't do this. Instead of this, they should create a good software that people will want to use, that will help people to solve their problems. Trivial problems kill time and creative energy of the developer, that's why we should try to automate as much of them as possible. As a result we will have more cheap and quality software, and both users and developers will be happy.

jQuery helped us to forget about lots of routine problems that were a part of the life of JavaScript-programmer in the past. Now you aren't forced to set an ID of each element you want to access - this can be done simplier and nicier now. You don't need to worry about browser bugs - the developers of the library deal with them. You should only solve your problem, not being distracted by noise.

The Prototype library extends a standard set of methods of Array object, giving a freedom from solving trivial problems to the developer. jQuery doesn't do it, and it doesn't need to. If the developers tried to make jQuery "for everything", it would lose its main advantage - simplicity. Instead of this, the developers of jQuery have made it pluggable, and it let us to extend the functionality of the library. And I wrote Rich Array - jQuery plugin for working with arrays.

I tried to do all my best to simplify developer's life in this sphere. I included functions for solving the most widespread problems related to arrays to the plugin. Have you ever had to remove null values from the array? To find the intersection of two arrays or difference between them? Filter the array according to some criteria? Sum the values of the array? If yes, than I advice you to use the plugin. It's extrmely lightweight, and your users won't suffer because of long time of page loading. But Rich Array will probably save you some time and energy that you spend as you want - spend more time with your family, have a rest or concentrate on more interesting and important parts of work.

I won't put the documentation for plugin here - the source code is understandable and well-commented. If somebody will want to write the documentation, I will be glad. You may leave here all suggestions on improving or bugs reporting or write me at Kadalashvili dot Vladimir dot gmail dot com.

Good luck!

Resouces:

6 comments:

Joe said...

Very nice. Thank you for commenting the code as well, many people overlook this.

Ben said...

Very nice! If only it weren't licensed under the GPL :-/ BSD, Apache, MPL, LGPL, any of those would be great.

Bryan Buchs said...

The "in" method is causing errors in IE6. Renaming it to "contains" (and changing any references to it) solved my problems.

Ben Atkin said...

What do you think about including random functions? I'd like three functions for arrays: one to return a random element; one to return a set of n random elements; and one to shuffle an array.

Matthew said...

Nice.
"in" is a reserved word, so you might want to rename that one.
Cheers

driedger said...

I too had to rename the 'in' function to something else, and change all references to it before it would not error out in Internet Explorer. This was with IE8 standards mode, as well as compatibility modes.