howsmydriving-nyc
Version:
NYC region plug-in for @HowsMyDrivingWA.
39 lines (38 loc) • 1.09 kB
JavaScript
/**
* Add lpad(string, number) to String prototype
*
* Params:
* pad: character to prepend to string to make it length.
* l: length of string returned
* Returns:
* string of length >= l where pad character is prepended to string
* enough times to satisfy length requirement. If string.length >= l,
* then the string is returned.
*
* Exceptions:
* If pad is not a single character, an Error is thrown.
*
* See: string-ext.d.ts
**/
String.prototype.title = function () {
return this.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};
String.prototype.lpad = function (pad, l) {
var str = this;
if (!pad || pad.length != 1) {
throw new Error("Argument pad must be a single character.");
}
while (str.length < l) {
str = pad + str;
}
return str;
};
// not sure why I can't make this extended function work...
String.prototype.trunc = function (n) {
if (this.length > n) {
return this.slice(0, n - 3) + '...';
}
return this;
};