Skip to content

Commit 5531b0b

Browse files
author
DoubleU23
committed
✨ injectSubClasses() run now recursive
1 parent 0fe5c45 commit 5531b0b

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,11 @@ let style = require('cssobjects-loader!sass-loader!./your.sass');
8787
### issues
8888
* for objectformat and enhanced usage go to [nativeCss](https://github.com/raphamorim/native-css)
8989
* loader related issues or PR's are welcome
90-
* __known issues__:
91-
* style's subclasses recognized only 1 lvl deep
92-
(others are defined as `{parentClass__subClass1__subClass2: {}}`)
90+
* __known issues__:
9391
* native-css not found (issue #18)
9492

95-
### to be done
96-
* inject style's subclasses recursively
97-
* use [humps](https://www.npmjs.com/package/humps) for objectKeys
98-
* write testscripts, DocBlocks, Comments
99-
* ES6 (/dist + buildScript)
93+
### v1 roadmap
94+
* [x] inject style's subclasses recursively
95+
* [ ] use [humps](https://www.npmjs.com/package/humps) for objectKeys
96+
* [ ] write testscripts, DocBlocks, Comments
97+
* [ ] ES6 (/dist + buildScript)
Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,68 @@
1-
'use strict';
1+
'use strict'
22

33
Object.defineProperty(exports, "__esModule", {
44
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
88
var transformPropertiesToCamelCase = function transformPropertiesToCamelCase (object) {
9-
var output = {};
9+
var output = {}
1010

1111
for (var _key in object) {
1212
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]
1515
if (~key.indexOf('-')) {
16-
var splittedKeys = key.split('-');
16+
var splittedKeys = key.split('-')
1717
splittedKeys = splittedKeys.map(function (v, i) {
1818
return i > 0
1919
// 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('')
2323
}
24-
output[key] = transformPropertiesToCamelCase(value);
24+
output[key] = transformPropertiesToCamelCase(value)
2525
}
26-
return output;
27-
};
26+
return output
27+
}
2828

2929
// only for one level down
3030
// tbd: make it recursive
3131
// transforms
3232
// {className__subClassName: {property: value}}
3333
// to {className: {subClassName: {property: value}}}
3434
var injectSubClasses = function injectSubClasses (_object) {
35-
var object = _object;
35+
var object = _object
3636
for (var key in object) {
3737
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+
4142
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]
4451
}
4552
}
46-
return object;
47-
};
53+
return object
54+
}
4855

4956
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
5768
}

0 commit comments

Comments
 (0)