Skip to content

Instantly share code, notes, and snippets.

@bitsprint
Created May 22, 2013 15:31
Show Gist options
  • Select an option

  • Save bitsprint/5628516 to your computer and use it in GitHub Desktop.

Select an option

Save bitsprint/5628516 to your computer and use it in GitHub Desktop.
String functions
/*
var s = "Replace {foo}";
var o = { foo: "this"};
var result = s.interpolate(o);
console.log(result);
// Replace this
*/
String.prototype.interpolate = function (o) {
return this.replace(/{([^{}]*)}/g, function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
});
};
String.prototype.stripCommas = function() {
var value = this;
while (value.indexOf(",") != -1) {
value = value.replace(",", "");
}
return value;
};
String.prototype.ellipsise = function (position) {
if (position == null || this == null || this.length <= position)
return String(this);
return this.substring(0, position) + '...';
};
String.prototype.replace(value, newvalue)
{
while (value.indexOf(this, 0) != -1)
{
value = value.replace(this, newvalue);
}
return value;
};
String.prototype.replaceBreaks()
{
return this.replace("<br />", "\n");
}
String.prototype.replaceLineBreaks(value)
{
return this.replace("\n", "<br />");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment