/
index.js
34 lines (29 loc) · 871 Bytes
/
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
import { shorthands } from "./shorthands";
const whitelist = [
"padding",
"margin",
"borderWidth",
"borderRadius",
"borderColor",
"borderStyle",
"borderWidth"
];
const cammelcase = /([A-Z][a-z0-9]+)+/;
export function expandShorthand(key, val) {
const values = val.split(" ");
const keys = Object.keys(shorthands[key]);
const keyValues = Object.values(shorthands[key]);
if (whitelist.includes(key)) {
const [prefix, suffix] = key.split(cammelcase);
return {
[`${prefix}Top${suffix || ""}`]: values[0],
[`${prefix}Right${suffix || ""}`]: values[1] || values[0],
[`${prefix}Bottom${suffix || ""}`]: values[2] || values[0],
[`${prefix}Left${suffix || ""}`]: values[3] || values[1] || values[0]
};
}
return keys.reduce((obj, key, i) => {
obj[key] = values[i] || keyValues[i];
return obj;
}, {});
}