/
index.ts
31 lines (24 loc) · 877 Bytes
/
index.ts
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
export default function getStyle(...cssArr: Array<string>) {
let css: string = cssArr.join("")
const tokens = css.split(";");
let json = "{\n";
tokens.forEach(token => {
const value = getValue(token).trim();
const property = getProperty(token).trim();
if (!(value.length > 0) || !(property.length > 0)) return;
json += `"${camelCase(property)}" : ${!isNaN(value as unknown as number) ? value : "\"" + value + "\""},\n`;
});
json = json.slice(0, -2) + "\n}";
return JSON.parse(json);
}
function getValue(token: string) {
return token.split(":").pop() || "";
}
function getProperty(token: string) {
let res = token.split(":");
res.pop();
return res.join(":");
}
function camelCase(str: string) {
return str.split("-").map((s, i) => !(i > 0) ? s : s[0].toUpperCase() + s.slice(1)).join("");
}