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

Commit 5cf03e7

Browse files
committed
feat(Angular): add camelcase as a public method
convert dash-separated strings to camelCase
1 parent f2e7f87 commit 5cf03e7

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

src/.jshintrc

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"uppercase": false,
2727
"manualLowercase": false,
2828
"manualUppercase": false,
29+
"camelcase": false,
2930
"isArrayLike": false,
3031
"forEach": false,
3132
"sortedKeys": false,

src/Angular.js

+19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
uppercase: true,
2020
manualLowercase: true,
2121
manualUppercase: true,
22+
camelcase: true,
2223
nodeName_: true,
2324
isArrayLike: true,
2425
forEach: true,
@@ -161,6 +162,24 @@ if ('i' !== 'I'.toLowerCase()) {
161162
uppercase = manualUppercase;
162163
}
163164

165+
/**
166+
* @ngdoc function
167+
* @name angular.camelcase
168+
* @module ng
169+
* @kind function
170+
*
171+
* @description Converts the specified string dash-separated to camelcase.
172+
* @param {string} string String to be converted to camelcase.
173+
* @returns {string} Camelcased string.
174+
*/
175+
var camelcase = function(name) {
176+
return name.
177+
replace(SPECIAL_CHARS_REGEXP, function(_, separator, letter, offset) {
178+
return offset ? letter.toUpperCase() : letter;
179+
}).
180+
replace(MOZ_HACK_REGEXP, 'Moz$1');
181+
};
182+
164183

165184
var
166185
msie, // holds major version number for IE, or NaN if UA is not IE.

src/AngularPublic.js

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ function publishExternalAPI(angular) {
137137
'isDate': isDate,
138138
'lowercase': lowercase,
139139
'uppercase': uppercase,
140+
'camelcase': camelcase,
140141
'callbacks': {counter: 0},
141142
'getTestability': getTestability,
142143
'$$minErr': minErr,

test/.jshintrc

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"uppercase": false,
2525
"manualLowercase": false,
2626
"manualUppercase": false,
27+
"camelcase": false,
2728
"isArrayLike": false,
2829
"forEach": false,
2930
"sortedKeys": false,

test/AngularSpec.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,47 @@ describe('angular', function() {
77
dealoc(element);
88
});
99

10-
describe('case', function() {
10+
describe('lowercase', function() {
1111
it('should change case', function() {
1212
expect(lowercase('ABC90')).toEqual('abc90');
1313
expect(manualLowercase('ABC90')).toEqual('abc90');
14+
});
15+
});
16+
17+
describe('uppercase', function() {
18+
it('should change case', function() {
1419
expect(uppercase('abc90')).toEqual('ABC90');
1520
expect(manualUppercase('abc90')).toEqual('ABC90');
1621
});
1722
});
1823

24+
describe('camelcase', function() {
25+
it('should change case', function() {
26+
expect(camelcase('foo-bar')).toEqual('fooBar');
27+
expect(camelcase('foo-Bar')).toEqual('fooBar');
28+
});
29+
30+
it('should leave non-dashed strings alone', function() {
31+
expect(camelcase('foo')).toBe('foo');
32+
expect(camelcase('')).toBe('');
33+
expect(camelcase('fooBar')).toBe('fooBar');
34+
});
35+
36+
37+
it('should covert dash-separated strings to camelCase', function() {
38+
expect(camelcase('foo-bar')).toBe('fooBar');
39+
expect(camelcase('foo-bar-baz')).toBe('fooBarBaz');
40+
expect(camelcase('foo:bar_baz')).toBe('fooBarBaz');
41+
});
42+
43+
44+
it('should covert browser specific css properties', function() {
45+
expect(camelcase('-moz-foo-bar')).toBe('MozFooBar');
46+
expect(camelcase('-webkit-foo-bar')).toBe('webkitFooBar');
47+
expect(camelcase('-webkit-foo-bar')).toBe('webkitFooBar');
48+
});
49+
});
50+
1951
describe("copy", function() {
2052
it("should return same object", function() {
2153
var obj = {};

0 commit comments

Comments
 (0)