-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.js
144 lines (119 loc) · 3.6 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
export const cssProperties = ['float'].concat(Object.keys(
typeof document === 'undefined'
? {}
: findWidth(document.documentElement.style)
).filter(p => p.indexOf('-') === -1 && p !== 'length'))
function findWidth(obj) {
return obj
? obj.hasOwnProperty('width')
? obj
: findWidth(Object.getPrototypeOf(obj))
: {}
}
export const isProp = /^-?-?[a-z][a-z-_0-9]*$/i
export const memoize = (fn, cache = {}) => item =>
item in cache
? cache[item]
: cache[item] = fn(item)
export function add(style, prop, values) {
if (prop in style) // Recursively increase specificity
add(style, '!' + prop, values)
else
style[prop] = formatValues(prop, values)
}
export const vendorMap = Object.create(null, {})
export const vendorValuePrefix = Object.create(null, {})
export const vendorRegex = /^(o|O|ms|MS|Ms|moz|Moz|webkit|Webkit|WebKit)([A-Z])/
const appendPx = memoize(prop => {
const el = document.createElement('div')
try {
el.style[prop] = '1px'
el.style.setProperty(prop, '1px')
return el.style[prop].slice(-3) === '1px' ? 'px' : ''
} catch (err) {
return ''
}
}, {
flex: '',
boxShadow: 'px',
border: 'px',
borderTop: 'px',
borderRight: 'px',
borderBottom: 'px',
borderLeft: 'px'
})
export function lowercaseFirst(string) {
return string.charAt(0).toLowerCase() + string.slice(1)
}
export function assign(obj, obj2) {
for (const key in obj2) {
if (obj2.hasOwnProperty(key))
obj[key] = obj2[key]
}
return obj
}
export function hyphenToCamelCase(hyphen) {
return hyphen.slice(hyphen.charAt(0) === '-' ? 1 : 0).replace(/-([a-z])/g, function(match) {
return match[1].toUpperCase()
})
}
export function camelCaseToHyphen(camelCase) {
return camelCase.replace(/(\B[A-Z])/g, '-$1').toLowerCase()
}
export function initials(camelCase) {
return camelCase.charAt(0) + (camelCase.match(/([A-Z])/g) || []).join('').toLowerCase()
}
export function objectToRules(style, selector, suffix = '', single) {
const base = {}
let rules = []
Object.keys(style).forEach(prop => {
if (prop.charAt(0) === '@')
rules.push(prop + '{' + objectToRules(style[prop], selector, suffix, single) + '}')
else if (typeof style[prop] === 'object')
rules = rules.concat(objectToRules(style[prop], selector, suffix + prop, single))
else
base[prop] = style[prop]
})
if (Object.keys(base).length) {
rules.unshift(
((single || (suffix.charAt(0) === ' ') ? '' : '&') + '&' + suffix).replace(/&/g, selector) +
'{' + stylesToCss(base) + '}'
)
}
return rules
}
export const selectorSplit = /,(?=(?:(?:[^"]*"){2})*[^"]*$)/
export function stylesToCss(style) {
return Object.keys(style).reduce((acc, prop) =>
acc + propToString(prop.replace(/!/g, ''), style[prop])
, '')
}
export function readClasses(sheet) {
throw new Error('not implemented')
}
function propToString(prop, value) {
prop = prop in vendorMap ? vendorMap[prop] : prop
return (vendorRegex.test(prop) ? '-' : '')
+ (cssVar(prop)
? prop
: camelCaseToHyphen(prop)
)
+ ':'
+ value
+ ';'
}
function formatValues(prop, value) {
return Array.isArray(value)
? value.map(v => formatValue(prop, v)).join(' ')
: typeof value === 'string'
? formatValues(prop, value.split(' '))
: formatValue(prop, value)
}
function formatValue(prop, value) {
return value in vendorValuePrefix
? vendorValuePrefix[value]
: value + (isNaN(value) || value === null || typeof value === 'boolean' || cssVar(prop) ? '' : appendPx(prop))
}
function cssVar(prop) {
return prop.charAt(0) === '-' && prop.charAt(1) === '-'
}