JavaScript String Library - Hitting a Minor Roadblock

Posted by OneNerd on Stack Overflow See other posts from Stack Overflow or by OneNerd
Published on 2010-05-10T15:50:55Z Indexed on 2010/05/10 15:54 UTC
Read the original article Hit count: 236

Filed under:
|

Ok - am trying to create a string library that contains a handful of useful things missing from JavaScript. Here is what I have so far:

;function $__STRING__$(in_string) {
    /* internal functions */
    this.s              = in_string;
    this.toString       = function(){return this.s;};
    /******* these functions CAN be chained (they return the $__STRING__$ object) ******/   
    this.uppercase      = function(){this.s = this.s.toUpperCase(); return this;};
    this.lowercase      = function(){this.s = this.s.toLowerCase(); return this;};
    this.trim           = function(){this.s = this.s.replace(/^\s+|\s+$/g,""); return this;};
    this.ltrim          = function(){this.s = this.s.replace(/^\s+/,""); return this;};
    this.rtrim          = function(){this.s = this.s.replace(/\s+$/,""); return this;};
    this.striptags      = function(){this.s = this.s.replace(/<\/?[^>]+(>|$)/g, ""); return this;};
    this.escapetags     = function(){this.s = this.s.replace(/</g,"<").replace(/>/g,">"); return this;};
    this.unescapetags   = function(){this.s = this.s.replace(/</g,"<").replace(/>/g,">"); return this;};
    this.underscorize   = function(){this.s = this.s.replace(/ /g,"_"); return this;};
    this.dasherize      = function(){this.s = this.s.replace(/ /g,"-"); return this;};
    this.spacify        = function(){this.s = this.s.replace(/_/g," "); return this;};
    this.left           = function(length){this.s = this.s.substring(length,0); return this;};
    this.right          = function(length){this.s = this.s.substring(this.s.length,this.s.length-length); return this;};
    this.shorten        = function(length){if(this.s.length<=length){return this.s;}else{this.left(this.s,length)+"..."; return this;}};
    this.mid            = function(start,length){return this.s.substring(start,(length+start));};
    this._down          = function(){return this.s;};   // breaks chain, but lets you run core js string functions
    /******* these functions CANNOT be chained (they do not return the $__STRING__$ object) ******/ 
    this.contains       = function(needle){if(this.s.indexOf(needle)!==-1){return true;}else{return false;}};
    this.startswith     = function(needle){if(this.left(this.s,needle.length)==needle){return true;}else{return false;}};
    this.endswith       = function(needle){if(this.right(this.s,needle.length)==needle){return true;}else{return false;};};
}
function $E(in_string){return new $__STRING__$(in_string);}
String.prototype._enhance   = function(){return new $__STRING__$(this);};
String.prototype._up        = function(){return new $__STRING__$(this);};

It works fairly well, and I can chain commands etc.

I set it up so I can cast a string as an enhanced string these 2 ways:

$E('some string');
'some string'._enhance();

However, each time I want to use a built-in string method, I need to convert it back to a string first. So for now, I put in _down() and _up() methods like so:

alert( $E("hello man").uppercase()._down().replace("N", "Y")._up().dasherize() ); 
alert( "hello man"._enhance().uppercase()._down().replace("N", "Y")._up().dasherize() );

It works fine, but what I really want to do it be able to use all of the built-in functions a string can use. I realize I can just replicate each function inside my object, but I was hoping there was a simpler way.

So question is, is there an easy way to do that?

Thanks -

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about oop