Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

DRY camelcase #10337

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"uppercase": false,
"manualLowercase": false,
"manualUppercase": false,
"camelcase": false,
"isArrayLike": false,
"forEach": false,
"sortedKeys": false,
Expand Down
21 changes: 21 additions & 0 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
uppercase: true,
manualLowercase: true,
manualUppercase: true,
camelcase: true,
nodeName_: true,
isArrayLike: true,
forEach: true,
Expand Down Expand Up @@ -108,6 +109,8 @@
*/

var REGEX_STRING_REGEXP = /^\/(.+)\/([a-z]*)$/;
var SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g;
var MOZ_HACK_REGEXP = /^moz([A-Z])/;

// The name of a form control's ValidityState property.
// This is used so that it's possible for internal tests to create mock ValidityStates.
Expand Down Expand Up @@ -161,6 +164,24 @@ if ('i' !== 'I'.toLowerCase()) {
uppercase = manualUppercase;
}

/**
* @ngdoc function
* @name angular.camelcase
* @module ng
* @kind function
*
* @description Converts the specified string dash-separated to camelcase.
* @param {string} string String to be converted to camelcase.
* @returns {string} Camelcased string.
*/
var camelcase = function(name) {
return name.
replace(SPECIAL_CHARS_REGEXP, function(_, separator, letter, offset) {
return offset ? letter.toUpperCase() : letter;
}).
replace(MOZ_HACK_REGEXP, 'Moz$1');
};


var
msie, // holds major version number for IE, or NaN if UA is not IE.
Expand Down
1 change: 1 addition & 0 deletions src/AngularPublic.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ function publishExternalAPI(angular) {
'isDate': isDate,
'lowercase': lowercase,
'uppercase': uppercase,
'camelcase': camelcase,
'callbacks': {counter: 0},
'getTestability': getTestability,
'$$minErr': minErr,
Expand Down
17 changes: 1 addition & 16 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,9 @@ JQLite._data = function(node) {
function jqNextId() { return ++jqId; }


var SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g;
var MOZ_HACK_REGEXP = /^moz([A-Z])/;
var MOUSE_EVENT_MAP= { mouseleave: "mouseout", mouseenter: "mouseover"};
var jqLiteMinErr = minErr('jqLite');

/**
* Converts snake_case to camelCase.
* Also there is special case for Moz prefix starting with upper case letter.
* @param name Name to normalize
*/
function camelCase(name) {
return name.
replace(SPECIAL_CHARS_REGEXP, function(_, separator, letter, offset) {
return offset ? letter.toUpperCase() : letter;
}).
replace(MOZ_HACK_REGEXP, 'Moz$1');
}

var SINGLE_TAG_REGEXP = /^<(\w+)\s*\/?>(?:<\/\1>|)$/;
var HTML_REGEXP = /<|&#?\w+;/;
var TAG_NAME_REGEXP = /<([\w:]+)/;
Expand Down Expand Up @@ -577,7 +562,7 @@ forEach({
hasClass: jqLiteHasClass,

css: function(element, name, value) {
name = camelCase(name);
name = camelcase(name);

if (isDefined(value)) {
element.style[name] = value;
Expand Down
2 changes: 1 addition & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2495,7 +2495,7 @@ var PREFIX_REGEXP = /^((?:x|data)[\:\-_])/i;
* @param name Name to normalize
*/
function directiveNormalize(name) {
return camelCase(name.replace(PREFIX_REGEXP, ''));
return camelcase(name.replace(PREFIX_REGEXP, ''));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/ng/sce.js
Original file line number Diff line number Diff line change
Expand Up @@ -1033,13 +1033,13 @@ function $SceProvider() {

forEach(SCE_CONTEXTS, function(enumValue, name) {
var lName = lowercase(name);
sce[camelCase("parse_as_" + lName)] = function(expr) {
sce[camelcase("parse_as_" + lName)] = function(expr) {
return parse(enumValue, expr);
};
sce[camelCase("get_trusted_" + lName)] = function(value) {
sce[camelcase("get_trusted_" + lName)] = function(value) {
return getTrusted(enumValue, value);
};
sce[camelCase("trust_as_" + lName)] = function(value) {
sce[camelcase("trust_as_" + lName)] = function(value) {
return trustAs(enumValue, value);
};
});
Expand Down
11 changes: 2 additions & 9 deletions src/ngAria/aria.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,8 @@ function $AriaProvider() {
config = angular.extend(config, newConfig);
};

function camelCase(input) {
return input.replace(/-./g, function(letter, pos) {
return letter[1].toUpperCase();
});
}


function watchExpr(attrName, ariaAttr, negate) {
var ariaCamelName = camelCase(ariaAttr);
var ariaCamelName = angular.camelcase(ariaAttr);
return function(scope, elem, attr) {
if (config[ariaCamelName] && !attr[ariaCamelName]) {
scope.$watch(attr[attrName], function(boolVal) {
Expand Down Expand Up @@ -178,7 +171,7 @@ function $AriaProvider() {
this.$get = function() {
return {
config: function(key) {
return config[camelCase(key)];
return config[angular.camelcase(key)];
},
$$watchExpr: watchExpr
};
Expand Down
1 change: 1 addition & 0 deletions test/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"uppercase": false,
"manualLowercase": false,
"manualUppercase": false,
"camelcase": false,
"isArrayLike": false,
"forEach": false,
"sortedKeys": false,
Expand Down
34 changes: 33 additions & 1 deletion test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,47 @@ describe('angular', function() {
dealoc(element);
});

describe('case', function() {
describe('lowercase', function() {
it('should change case', function() {
expect(lowercase('ABC90')).toEqual('abc90');
expect(manualLowercase('ABC90')).toEqual('abc90');
});
});

describe('uppercase', function() {
it('should change case', function() {
expect(uppercase('abc90')).toEqual('ABC90');
expect(manualUppercase('abc90')).toEqual('ABC90');
});
});

describe('camelcase', function() {
it('should change case', function() {
expect(camelcase('foo-bar')).toEqual('fooBar');
expect(camelcase('foo-Bar')).toEqual('fooBar');
});

it('should leave non-dashed strings alone', function() {
expect(camelcase('foo')).toBe('foo');
expect(camelcase('')).toBe('');
expect(camelcase('fooBar')).toBe('fooBar');
});


it('should covert dash-separated strings to camelCase', function() {
expect(camelcase('foo-bar')).toBe('fooBar');
expect(camelcase('foo-bar-baz')).toBe('fooBarBaz');
expect(camelcase('foo:bar_baz')).toBe('fooBarBaz');
});


it('should covert browser specific css properties', function() {
expect(camelcase('-moz-foo-bar')).toBe('MozFooBar');
expect(camelcase('-webkit-foo-bar')).toBe('webkitFooBar');
expect(camelcase('-webkit-foo-bar')).toBe('webkitFooBar');
});
});

describe("copy", function() {
it("should return same object", function() {
var obj = {};
Expand Down
24 changes: 0 additions & 24 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1965,30 +1965,6 @@ describe('jqLite', function() {
});
});


describe('camelCase', function() {
it('should leave non-dashed strings alone', function() {
expect(camelCase('foo')).toBe('foo');
expect(camelCase('')).toBe('');
expect(camelCase('fooBar')).toBe('fooBar');
});


it('should covert dash-separated strings to camelCase', function() {
expect(camelCase('foo-bar')).toBe('fooBar');
expect(camelCase('foo-bar-baz')).toBe('fooBarBaz');
expect(camelCase('foo:bar_baz')).toBe('fooBarBaz');
});


it('should covert browser specific css properties', function() {
expect(camelCase('-moz-foo-bar')).toBe('MozFooBar');
expect(camelCase('-webkit-foo-bar')).toBe('webkitFooBar');
expect(camelCase('-webkit-foo-bar')).toBe('webkitFooBar');
});
});


describe('jqLiteDocumentLoaded', function() {

function createMockWindow(readyState) {
Expand Down