Extends the String native object to include methods useful in managing various kinds of strings (query strings, urls, html, etc).
(function(){
var special = {
'a': '[àáâãäåăą]',
'A': '[ÀÁÂÃÄÅĂĄ]',
'c': '[ćčç]',
'C': '[ĆČÇ]',
'd': '[ďđ]',
'D': '[ĎÐ]',
'e': '[èéêëěę]',
'E': '[ÈÉÊËĚĘ]',
'g': '[ğ]',
'G': '[Ğ]',
'i': '[ìíîï]',
'I': '[ÌÍÎÏ]',
'l': '[ĺľł]',
'L': '[ĹĽŁ]',
'n': '[ñňń]',
'N': '[ÑŇŃ]',
'o': '[òóôõöøő]',
'O': '[ÒÓÔÕÖØ]',
'r': '[řŕ]',
'R': '[ŘŔ]',
's': '[ššş]',
'S': '[ŠŞŚ]',
't': '[ťţ]',
'T': '[ŤŢ]',
'ue': '[ü]',
'UE': '[Ü]',
'u': '[ùúûůµ]',
'U': '[ÙÚÛŮ]',
'y': '[ÿý]',
'Y': '[ŸÝ]',
'z': '[žźż]',
'Z': '[ŽŹŻ]',
'th': '[þ]',
'TH': '[Þ]',
'dh': '[ð]',
'DH': '[Ð]',
'ss': '[ß]',
'oe': '[œ]',
'OE': '[Œ]',
'ae': '[æ]',
'AE': '[Æ]'
},
tidy = {
' ': '[\xa0\u2002\u2003\u2009]',
'*': '[\xb7]',
'\'': '[\u2018\u2019]',
'"': '[\u201c\u201d]',
'...': '[\u2026]',
'-': '[\u2013]',
'--': '[\u2014]',
'»': '[\uFFFD]'
};
function walk(string, replacements){
var result = string;
for (key in replacements){
result = result.replace(new RegExp(replacements[key], 'g'), key);
}
return result;
}
function getRegexForTag(tag, contents){
tag = tag || '';
var regstr = contents ? "<" + tag + "[^>]*>([\\s\\S]*?)<\/" + tag + ">" : "<\/?" + tag + "([^>]+)?>";
reg = new RegExp(regstr, "gi");
return reg;
};
String.implement({
standardize: function(){
return walk(this, special);
},
repeat: function(times){
return new Array(times + 1).join(this);
},
pad: function(length, str, direction){
if (this.length >= length) return this;
var pad = (str == null ? ' ' : '' + str)
.repeat(length - this.length)
.substr(0, length - this.length);
if (!direction || direction == 'right') return this + pad;
if (direction == 'left') return pad + this;
return pad.substr(0, (pad.length / 2).floor()) + this + pad.substr(0, (pad.length / 2).ceil());
},
getTags: function(tag, contents){
return this.match(getRegexForTag(tag, contents)) || [];
},
stripTags: function(tag, contents){
return this.replace(getRegexForTag(tag, contents), '');
},
tidy: function(){
return walk(this, tidy);
}
});
var UID = Math.floor(Math.random() * 10e12);
String.uniqueID = function(){
return (UID++).toString(36);
};
})();