Last active
September 19, 2022 20:02
-
-
Save blizzrdof77/b57cd71168d4999da5837a1001337bc7 to your computer and use it in GitHub Desktop.
Javascript String Prototype Helper Methods
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
/** | |
* Check if string contains a provided substring | |
* | |
* @param String search | |
* @return Bool | |
*/ | |
String.prototype.contains = function(search) { | |
return (this.indexOf(search) !== -1); | |
}; | |
String.prototype.toTitleCase = function() { | |
return this.replace(/\w\S*/g, function(txt) { | |
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); | |
}); | |
}; | |
String.prototype.camelCaseToDashed = function() { | |
return this.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); | |
}; | |
String.prototype.toProperCase = function() { | |
return this.replace(/\w\S*/g, function(txt) { | |
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); | |
}); | |
}; | |
String.prototype.toCamelCase = function() { | |
return this.replace(/(?:^\w|\-[A-Z]|\b\w)/g, function(letter, index) { | |
return index == 0 ? letter.toLowerCase() : letter.toUpperCase(); | |
}).replace(/\s+/g, '').replace('-', ''); | |
}; | |
String.prototype.toDashedCase = function() { | |
return this.toCamelCase().camelCaseToDashed(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://cdn.simpledigital.net/common/js/extensions/String.prototypeHelpers.js