Created
May 22, 2013 15:31
-
-
Save bitsprint/5628516 to your computer and use it in GitHub Desktop.
String functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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