-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cjs
99 lines (79 loc) · 2.45 KB
/
index.cjs
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
const split = (
classes = "",
{ separator = ",", opBracket = "", clBracket = "" } = {},
) => {
const arrClasses = [];
let isBracket = false;
let startIndex = -1;
for (let i = 0; i < classes.length; i++) {
const c = classes[i];
if (c === opBracket) isBracket = true;
if (c === clBracket) isBracket = false;
if (c !== separator) {
if (startIndex === -1) startIndex = i;
}
if (c === separator) {
if (!isBracket) {
const str = classes.slice(startIndex, i);
arrClasses.push(str);
startIndex = -1;
}
}
}
if (startIndex !== -1) {
arrClasses.push(classes.slice(startIndex));
}
return arrClasses;
};
const createTransform = ({
separator = ",",
opBracket = "",
clBracket = "",
} = {}) => {
if (separator && separator.trim() === "") {
throw new Error("Separator should not be empty");
}
const isVoidOpBracket = opBracket.trim() === "";
const isVoidClBracket = clBracket.trim() === "";
if (
!(
(isVoidOpBracket && isVoidClBracket) ||
(!isVoidOpBracket && !isVoidClBracket)
)
) {
throw new Error("The brackets must either be both, or they should not be");
}
const opBracketRegExp = opBracket ? `\\${opBracket}` : "";
const clBracketRegExp = clBracket ? `\\${clBracket}` : "";
const separatorRegExp = `\\${separator}`;
const regExp = new RegExp(
`(?<left>[^\\s${separatorRegExp}<>\"\'\`]+):((${opBracketRegExp}(?<rightOne>([^\\s${separatorRegExp}<>\"\'\`]+${separatorRegExp}?)+)${clBracketRegExp})|(?<rightTwo>([^\\s${separatorRegExp}<>\"\'\`]+${separatorRegExp}?)+))`,
"g",
);
//? FIXME: symbol ";" in CSS (perhaps not only that)
// FIXME: ,p={key:!0,ref:!0,__self:!0,__source:!0};
// FIXME: add \s (and separator?) in before <left> and after <right>?
const findPseudo = (content = "") => {
return content.replaceAll(
regExp,
(match, p1, p2, p3, p4, p5, p6, p7, p8, p9, groups, p10) => {
const classes = groups.rightOne ?? groups.rightTwo;
const arrClasses1 = split(classes, {
separator,
opBracket,
clBracket,
});
const arrClasses2 = arrClasses1.map((item) => findPseudo(item));
const arrClasses3 = arrClasses2.map((item) => item.split(" "));
const arrClasses = arrClasses3.flat(Number.POSITIVE_INFINITY);
const transformClasses = arrClasses.map(
(item) => `${groups.left}:${item}`,
);
const strClasses = transformClasses.join(" ");
return strClasses;
},
);
};
return (content = "") => findPseudo(content);
};
module.exports = createTransform;