Is there any utility function in AngularJS that converts a dash-case string to a camelCase string? e.g.: "min-max" becomes "minMax"
That's quite simple to implement my own function:
function toCamelCase(name) {
return name.replace(/-(\w)/g, function(match) {
return match[1].toUpperCase();
});
}
But, I know that Angular already does this (e.g. directive name), so I wonder if it's possible to use the mechanism that Angular has?
Camel case is the practice of writing phrases such that each word or abbreviation in the middle of the phrase begins with a capital letter, with no intervening spaces or punctuation. For example, Concurrent hash maps in camel case would be written as − ConcurrentHashMaps.
You can access to angular's camelCase function inside of a directive:
link: function (scope, element, attrs) {
attrs.$normalize('ng-model'); // ngModel
attrs.$normalize('ng:click'); // ngClick
attrs.$normalize('ng_show'); // ngShow
}
Best!
In angular this is part of jqlite, the minimalistic jquery implementation that they're using internally. I don't think there's a way of accessing that because I believe it's private. This is the function though:
function camelCase(name) {
return name.
replace(SPECIAL_CHARS_REGEXP, function(_, separator, letter, offset) {
return offset ? letter.toUpperCase() : letter;
}).
replace(MOZ_HACK_REGEXP, 'Moz$1');
}
Where
var SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g;
var MOZ_HACK_REGEXP = /^moz([A-Z])/;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With