-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCssObjectTransfer.js
More file actions
104 lines (103 loc) · 3 KB
/
Copy pathCssObjectTransfer.js
File metadata and controls
104 lines (103 loc) · 3 KB
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
const camel2Kebab = (str) => {
const hyphenateRE = /([a-z])([A-Z])/g;
return str.replace(hyphenateRE, "$1-$2").toLowerCase();
};
const kebab2Camel = (str) => {
const hyphenateRE = /-(\w)/g;
return str.replace(hyphenateRE, (_, c) => (c ? c.toUpperCase() : ""));
};
const isNumber = (value) => {
return Object.prototype.toString.call(value) === "[object Number]";
};
const isObject = (value) => {
return Object.prototype.toString.call(value) === "[object Object]";
};
class CssObjectTransfer {
static unit = "px";
static isPxPropsKey(key) {
const pxPropsKey = [
"width",
"height",
"font-size",
"top",
"left",
"right",
"bottom",
];
return pxPropsKey.some((v) => v === camel2Kebab(key));
}
static hasPxPropsKeyword(key) {
const pxPropsKeyword = ["padding", "margin", "radius"];
return pxPropsKeyword.some((v) => camel2Kebab(key).includes(v));
}
static objectFormatCssText(obj) {
const resultObj = {};
for (let key in obj) {
if (Object.hasOwnProperty.call(obj, key)) {
const value = obj[key];
if (
isNumber(value) &&
(CssObjectTransfer.isPxPropsKey(key) ||
CssObjectTransfer.hasPxPropsKeyword(key))
) {
resultObj[camel2Kebab(key)] = value + CssObjectTransfer.unit;
continue;
}
resultObj[camel2Kebab(key)] = isObject(value)
? CssObjectTransfer.objectFormatCssText(value)
: value;
}
}
return resultObj
}
static object2CssText(obj) {
let resultStr = JSON.stringify(CssObjectTransfer.objectFormatCssText(obj), null, 4)
.replace(/(")|(\}$)|(^\{)/g, "")
.replaceAll(": {", " {")
.replaceAll(",", "").replace('\n','')
.replaceAll("\n", ";\n")
.replaceAll("{;", "{")
.replaceAll("};", "}")
return resultStr
}
static cssText2Object(text, camel = false) {
let result =
'{"' +
text
.replaceAll("{", '":{"')
.replaceAll("}", "}")
.replace(/:(.+);/g, '":"$1","')
.replace(/\s/g, "")
.replaceAll('","}', '"}') +
"}";
const resultObj = CssObjectTransfer.formatObjectKeyValue(
JSON.parse(result),
camel
);
return JSON.stringify(resultObj, null, 4);
}
static formatObjectKeyValue(obj, camel) {
const resultObj = {};
for (let key in obj) {
if (Object.hasOwnProperty.call(obj, key)) {
const value = obj[key];
key =
camel && /^[A-Za-z]/.test(key) ? kebab2Camel(key) : camel2Kebab(key);
if (isObject(value)) {
resultObj[key] = CssObjectTransfer.formatObjectKeyValue(value, camel);
continue;
}
if (
/^\d+px/.test(value) &&
(CssObjectTransfer.isPxPropsKey(key) ||
CssObjectTransfer.hasPxPropsKeyword(key))
) {
resultObj[key] = Number(value.slice(0, -2));
continue;
}
resultObj[key] = /^\d+$/.test(value) ? Number(value) : value;
}
}
return resultObj;
}
}