Last active
January 19, 2017 10:14
-
-
Save patrickkunka/75770aa387a9f520f4e36374b6ae5c17 to your computer and use it in GitHub Desktop.
Common string manipulations for URI segments
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
// dash or snake to camel | |
String.prototype.toCamel = function() { | |
return this.replace(/([_-][a-z0-9])/g, function($1) { | |
return $1.toUpperCase().replace(/[_-]/, ''); | |
}); | |
}; | |
// camel or pascal to snake | |
String.prototype.toSnake = function() { | |
return this.replace(/([A-Z])/g, '_$1').replace(/^_/, '').toLowerCase(); | |
}; | |
// camel or pascal to dash | |
String.prototype.toDash = function() { | |
return this.replace(/([A-Z])/g, '-$1').replace(/^-/, '').toLowerCase(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment