Skip to content

Instantly share code, notes, and snippets.

@Lexty
Created February 4, 2016 11:53
Show Gist options
  • Save Lexty/d6fa22a9a7fa89d34071 to your computer and use it in GitHub Desktop.
Save Lexty/d6fa22a9a7fa89d34071 to your computer and use it in GitHub Desktop.
Some JavaScript extensions
if (!String.prototype.ucFirst) {
(function() {
String.prototype.ucFirst = function() {
var str = this;
if (str.length) {
str = str.charAt(0).toUpperCase() + str.slice(1);
}
return str;
};
})();
}
if (!String.prototype.lcFirst) {
(function() {
String.prototype.lcFirst = function() {
var str = this;
if (str.length) {
str = str.charAt(0).toLowerCase() + str.slice(1);
}
return str;
};
})();
}
if (!String.prototype.ucWords) {
(function() {
String.prototype.ucWords = function() {
var str = this.toLowerCase();
return str.replace(/(^([a-zA-Z\p{M}]))|([ -][a-zA-Z\p{M}])/g,
function(s){
return s.toUpperCase();
});
};
})();
}
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
if (!String.prototype.superTrim) {
(function() {
String.prototype.superTrim = function(characters) {
var str = this;
var charArray = characters.split('');
var result = '';
for (var i = 0; i < charArray.length; i++)
result += '\\' + charArray[i];
return str.replace(new RegExp('^[' + result + ']+|['+ result +']+$', 'g'), '');
};
})();
}
if (!String.prototype.isNumeric) {
(function() {
String.prototype.isNumeric = function() {
return !isNaN(parseInt(this, 10));
};
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment