-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (46 loc) · 1.23 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
const autoprefixer = require('autoprefixer-core')
const decamelize = require('decamelize')
const fixCase = require('./fix-case')
function isRule({ type }) {
return type === 'rule'
}
function isDeclaration({ type }) {
return type === 'decl'
}
function parseDeclaration({ prop, value }) {
return {
key: fixCase(prop),
value: /^[0-9]+$/.test(value) ? parseInt(value, 10) : value
}
}
function parseRule({nodes}) {
return nodes.filter(isDeclaration).map(parseDeclaration)
}
function toCssKey(k) {
let ret = decamelize(k, '-')
if (/^(Webkit|Moz|ms)/.test(k)) {
ret = `-${ret}`
}
return ret
}
function objectToStyle(object) {
return Object.keys(object).map(k => `${toCssKey(k)}: ${object[k]};`).join('')
}
function cssRule(style) {
return `* {${style}}`
}
module.exports = function autoprefix(object) {
const css = cssRule(objectToStyle(object))
return autoprefixer.process(css).root.nodes
.filter(isRule)
.map(parseRule)
.reduce((p, c) => p.concat(c), [])
.reduce((p, c) => {
if (p.hasOwnProperty(c.key)) {
Array.isArray(p[c.key]) ? p[c.key].push(c.value) : p[c.key] = [p[c.key], c.value]
} else {
p = Object.assign({}, p, {[c.key]: c.value})
}
return p
}, {})
}