-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
29 lines (26 loc) · 886 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
var polyfill = require("@riim/object-entries-polyfill");
const escapeSpecialChars = (string) =>
string.replace(/(?<!\\)[!"#$%&'()*+,./:;<=>?@\[\]^{}|~]/g, "\\$&");
function flattenObjSass(
obj,
prefix = "$",
transform = (_derivedKey, val) => val
) {
return Object.entries(obj).reduce((accumulated, [key, value]) => {
const derivedKey = `${prefix}${escapeSpecialChars(key)}`;
if (typeof value === "object" && !Array.isArray(value) && value) {
const processedObject = flattenObjSass(
value,
`${derivedKey}-`,
transform
);
return accumulated + processedObject;
} else {
const processedValue = Array.isArray(value)
? `(${transform(derivedKey, value)})`
: transform(derivedKey, value);
return accumulated + `${derivedKey}: ${processedValue}; `;
}
}, "");
}
module.exports = flattenObjSass;