Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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.

underscore.string looks like it might suit your needs. Here's how they describe it:

Underscore.string is JavaScript library for comfortable manipulation with strings, extension for Underscore.js inspired by Prototype.js, Right.js, Underscore and beautiful Ruby language.

Underscore.string provides you several useful functions: capitalize, clean, includes, count, escapeHTML, unescapeHTML, insert, splice, startsWith, endsWith, titleize, trim, truncate and so on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top