Skip to content

Instantly share code, notes, and snippets.

@bjarnef
Last active August 29, 2015 14:25
Show Gist options
  • Save bjarnef/2fbc4254ac37890d6b34 to your computer and use it in GitHub Desktop.
Save bjarnef/2fbc4254ac37890d6b34 to your computer and use it in GitHub Desktop.
camelCase function
angular.module('umbraco.filters').filter('camelCase', function () {
var camelCaseFilter = function (input) {
// remove all characters that should not be in a variable name
// as well underscores an numbers from the beginning of the string
var s = (input||'').replace(/([^a-zA-Z0-9_\- ])|([_\- :,.+]+$)|^[_\-0-9]+/g, "").trim().toLowerCase();
// uppercase letters preceeded by a hyphen, underscore or a space
s = s.replace(/([ -_]+)([a-zA-Z0-9])/g, function (a, b, c) {
return c.toUpperCase();
});
// uppercase letters following numbers
s = s.replace(/([0-9]+)([a-zA-Z])/g, function (a, b, c) {
return b + c.toUpperCase();
});
return s;
};
return camelCaseFilter;
});
// simple function to convert string to camelCase
String.prototype.toCamelCase = function () {
// remove all characters that should not be in a variable name
// as well underscores an numbers from the beginning of the string
var s = (this||'').replace(/([^a-zA-Z0-9_\- ])|([_\- :,.+]+$)|^[_\-0-9]+/g, "").trim().toLowerCase();
// uppercase letters preceeded by a hyphen, underscore or a space
s = s.replace(/([ -_]+)([a-zA-Z0-9])/g, function (a, b, c) {
return c.toUpperCase();
});
// uppercase letters following numbers
s = s.replace(/([0-9]+)([a-zA-Z])/g, function (a, b, c) {
return b + c.toUpperCase();
});
return s;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment