Skip to content

Instantly share code, notes, and snippets.

@patrickkunka
Last active January 19, 2017 10:14
Show Gist options
  • Save patrickkunka/75770aa387a9f520f4e36374b6ae5c17 to your computer and use it in GitHub Desktop.
Save patrickkunka/75770aa387a9f520f4e36374b6ae5c17 to your computer and use it in GitHub Desktop.
Common string manipulations for URI segments
// 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