-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli-utils.js
42 lines (32 loc) · 989 Bytes
/
cli-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const kebabCase = require('lodash.kebabcase')
function flattenNestedStyles(styles, className) {
const stylesArray = []
const keys = Object.keys(styles)
keys.forEach(function(key) {
if (typeof styles[key] === 'object') {
const nestedClassName = getFlatClassName(key, className)
stylesArray.push({ selector: nestedClassName, styles: styles[key] })
delete styles[key]
}
})
stylesArray.unshift({ selector: className, styles })
return stylesArray
}
function getFlatClassName(selector, className) {
return selector.replace('&', className)
}
function objectToCss(styles) {
const propertyNames = Object.keys(styles)
const cssArray = propertyNames.map(function(key) {
const property = kebabCase(key)
let value = styles[key]
if (value && typeof value === 'number') value += 'px'
return `${property}: ${value};`
})
return cssArray.join('\n ')
}
module.exports = {
flattenNestedStyles,
getFlatClassName,
objectToCss
}