Skip to content

Commit a66bd43

Browse files
Create utility helper for camel casing text with hyphens
This helper will be used when converting CSS styles to JS objects. Then the `style` object will be consistent in the React props.
1 parent 97ce98a commit a66bd43

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

lib/utilities.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
/**
4+
* Convert a string to camel case.
5+
*
6+
* @param {String} string - The string.
7+
* @return {String}
8+
*/
9+
function camelCase(string) {
10+
if (typeof string !== 'string') {
11+
throw new Error('`camelCase`: first argument must be a string.');
12+
}
13+
14+
// hyphen found after first character
15+
if (string.indexOf('-') > 0) {
16+
var strings = string.toLowerCase().split('-');
17+
18+
// capitalize starting from the second string item
19+
for (var i = 1, len = strings.length; i < len; i++) {
20+
strings[i] = strings[i].charAt(0).toUpperCase() + strings[i].slice(1);
21+
}
22+
23+
return strings.join('');
24+
}
25+
26+
return string;
27+
}
28+
29+
/**
30+
* Export utilties.
31+
*/
32+
module.exports = {
33+
camelCase: camelCase
34+
};

0 commit comments

Comments
 (0)