Last active
August 29, 2015 14:25
-
-
Save bjarnef/2fbc4254ac37890d6b34 to your computer and use it in GitHub Desktop.
camelCase function
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
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; | |
}); |
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
// 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