|
1 |
| -'use strict'; |
| 1 | +'use strict' |
2 | 2 |
|
3 | 3 | Object.defineProperty(exports, "__esModule", {
|
4 | 4 | value: true
|
5 |
| -}); |
6 |
| -exports.default = transformToNestedDomStyleObjects; |
7 |
| -// f.e. font-family => fontFamily; border-radius => borderRadius |
| 5 | +}) |
| 6 | +exports.default = transformToNestedDomStyleObjects |
| 7 | +// f.e. font-family => fontFamily border-radius => borderRadius |
8 | 8 | var transformPropertiesToCamelCase = function transformPropertiesToCamelCase (object) {
|
9 |
| - var output = {}; |
| 9 | + var output = {} |
10 | 10 |
|
11 | 11 | for (var _key in object) {
|
12 | 12 | if (typeof object === 'string') { return object.replace(' !important', '') }
|
13 |
| - var key = _key; |
14 |
| - var value = object[_key]; |
| 13 | + var key = _key |
| 14 | + var value = object[_key] |
15 | 15 | if (~key.indexOf('-')) {
|
16 |
| - var splittedKeys = key.split('-'); |
| 16 | + var splittedKeys = key.split('-') |
17 | 17 | splittedKeys = splittedKeys.map(function (v, i) {
|
18 | 18 | return i > 0
|
19 | 19 | // UCFIRST if ! first element
|
20 |
| - ? v.charAt(0).toUpperCase() + v.substr(1) : v; |
21 |
| - }); |
22 |
| - key = splittedKeys.join(''); |
| 20 | + ? v.charAt(0).toUpperCase() + v.substr(1) : v |
| 21 | + }) |
| 22 | + key = splittedKeys.join('') |
23 | 23 | }
|
24 |
| - output[key] = transformPropertiesToCamelCase(value); |
| 24 | + output[key] = transformPropertiesToCamelCase(value) |
25 | 25 | }
|
26 |
| - return output; |
27 |
| -}; |
| 26 | + return output |
| 27 | +} |
28 | 28 |
|
29 | 29 | // only for one level down
|
30 | 30 | // tbd: make it recursive
|
31 | 31 | // transforms
|
32 | 32 | // {className__subClassName: {property: value}}
|
33 | 33 | // to {className: {subClassName: {property: value}}}
|
34 | 34 | var injectSubClasses = function injectSubClasses (_object) {
|
35 |
| - var object = _object; |
| 35 | + var object = _object |
36 | 36 | for (var key in object) {
|
37 | 37 | if (~key.indexOf('__')) {
|
38 |
| - var keySplit = key.split('__'); |
39 |
| - var parentkey = keySplit[0]; |
40 |
| - var newKey = keySplit.slice(1).join('__'); |
| 38 | + var keySplit = key.split('__') |
| 39 | + var parentkey = keySplit[0] |
| 40 | + var newKey = keySplit.slice(1).join('__') |
| 41 | + |
41 | 42 | object[parentkey] = object[parentkey] || {}
|
42 |
| - object[parentkey][newKey] = object[key]; |
43 |
| - delete object[key]; |
| 43 | + object[parentkey][newKey] = object[key] |
| 44 | + |
| 45 | + if (~newKey.indexOf('__')) { |
| 46 | + // do it recursively |
| 47 | + object[parentkey] = injectSubClasses(object[parentkey]) |
| 48 | + } |
| 49 | + |
| 50 | + delete object[key] |
44 | 51 | }
|
45 | 52 | }
|
46 |
| - return object; |
47 |
| -}; |
| 53 | + return object |
| 54 | +} |
48 | 55 |
|
49 | 56 | function transformToNestedDomStyleObjects (cssObject) {
|
50 |
| - var transformTo = arguments.length <= 1 || arguments[1] === undefined ? 'js' : arguments[1]; |
51 |
| - if (transformTo !== 'js') { |
52 |
| - throw Error('[transformToNestedDomStyleObjects] transforming js to css is not supported yet'); |
53 |
| - } |
54 |
| - var output = {}; |
55 |
| - var jsStyle = injectSubClasses(transformPropertiesToCamelCase(cssObject)); |
56 |
| - return jsStyle; |
| 57 | + // TBD: outputStyle option |
| 58 | + // var transformTo = arguments.length <= 1 || arguments[1] === undefined ? 'js' : arguments[1] |
| 59 | + // if (transformTo !== 'js') { |
| 60 | + // throw Error('[transformToNestedDomStyleObjects] transforming js to css is not supported yet') |
| 61 | + // } |
| 62 | + |
| 63 | + var output = {} |
| 64 | + , cssObjectCamelCase = transformPropertiesToCamelCase(cssObject) |
| 65 | + , splittedCssObject = injectSubClasses(cssObjectCamelCase) |
| 66 | + |
| 67 | + return splittedCssObject |
57 | 68 | }
|
0 commit comments