views:

128

answers:

4

I find that many high level functions are missing in most well-known javascript libraries such as jquery, YUI...etc. Taking string manipulation as an example, startsWith, endsWith, contains, lTrim, rTrim, trim, isNullOrEmpty...etc. These function are actually very common ones.

I would like to know if there exists a javascript library/ plugin of a javascript library that fills these gaps (including but not limited to string manipulation)?

It would be great if the library does not override the prototype.

+6  A: 

Take a look at underscore.js (sadly, no string manipulation, but lots of other good stuff).

itsadok
+3  A: 

Most of those string functions are available using other methods associated with the string object eg

var myString = 'hello world';

myString.indexOf('hello') == 0; //same as startsWith('hello');

You could wrap these functions up into other functions if you wish. I think adding prototypes to the string object would be the way to go there and any libraries you find will probably go down that route anyway.

James Westgate
Yes, they are available using other methods associated with string object but this would not be high level enough.
bobo
+2  A: 

The ms ajax core library contains all of those string methods as well as date methods etc. basically a valiant attempt at bringing .net to js.

You don't need to load the entire MS Ajax js stack, just the core file.

Sky Sanders
+1  A: 

All of this is easily implemented with wrappers if you don't want to extend the prototype

var StringWrapper = (function(){
    var wrapper = {
        string: null,
        trim: function(){
            return this.string.replace(/^\s+|\s+$/g, "");
        },
        lTrim: function(){

        }
    };

    return function(string){
        wrapper.string = string;
        return wrapper;
    };
})();

StringWrapper("   aaaa bbbb    ").trim(); /// "aaaa bbbb"

The functions are only being created once, so its quite efficient. But using a wrapper over a helper object does incur one extra function call.

Sean Kinsey