-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert.js
36 lines (29 loc) · 975 Bytes
/
convert.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
const isCSS = (item) => (item.match(/;/g) || []).length === 1;
const getBeginningWhitespace = (string) =>
string.match(/^\s+/) !== null ? string.match(/^\s+/)[0] : "";
const toCamel = (prop) =>
prop.replace(/-(\w|$)/g, (dash, next) => next.toUpperCase());
const toHyphen = (prop) =>
prop.replace(/([A-Z])/g, (char) => `-${char[0].toLowerCase()}`);
const toJS = (item) => {
const [prop, val] = item.split(":");
return `${getBeginningWhitespace(prop)}${toCamel(
prop.trim()
)}: "${val.trim().replace(";", "")}",`;
};
const toCSS = (item) => {
const [prop, val] = item.split(":");
return `${getBeginningWhitespace(prop)}${toHyphen(
prop.trim()
)}: ${val.trim().replace(/'|"|,/g, "")};`;
};
module.exports = function convert(s) {
const lines = s.split("\n");
const someCss = lines.some((item) => isCSS(item));
return lines
.map((item) => {
if (someCss) return toJS(item);
else return toCSS(item);
})
.join("\n");
};