Skip to content

Instantly share code, notes, and snippets.

@ratiw
Last active February 2, 2018 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ratiw/c8d576b550f3f3b37dca to your computer and use it in GitHub Desktop.
Save ratiw/c8d576b550f3f3b37dca to your computer and use it in GitHub Desktop.
Javascript Utility Functions
// http://jamesroberts.name/blog/2010/02/22/string-functions-for-javascript-trim-to-camel-case-to-dashed-and-to-underscore/
// Trim
String.prototype.trim = function(){
return this.replace(/^\s+|\s+$/g, "");
};
// Camel Case
String.prototype.toCamel = function(){
return this.replace(/(\-[a-z])/g, function($1){return $1.toUpperCase().replace('-','');});
};
// Dash
String.prototype.toDash = function(){
return this.replace(/([A-Z])/g, function($1){return "-"+$1.toLowerCase();});
};
// Underscore
String.prototype.toUnderscore = function(){
return this.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment