-
Notifications
You must be signed in to change notification settings - Fork 567
/
css-variables.js
72 lines (60 loc) · 2.05 KB
/
css-variables.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
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
function formatter(dictionary, config) {
const header = config.header || '';
const body = dictionary.allProperties.map(mapProp.bind(this, config));
return `${header}:root {\n${body.join("\n")}\n}\n`;
}
function mapProp(config, prop) {
const comment = (prop.comment) ? ` /* ${prop.comment} */` : '';
const varName = makePropCSSVar(prop.name);
const value = setValue(prop, config);
let token = ` ${varName}: ${value};${comment}`;
if (config.dark && prop.attributes && prop.attributes.dark) {
const darkProp = generateDarkToken(prop);
const darkValue = setValue(darkProp, config);
token += `;\n ${varName}-dark: ${darkValue};`;
}
return token;
}
function setValue(prop, config) {
if (typeof prop.value === "object") {
throwObjectError(prop);
}
const nVal = prop.value;
const oVal = prop.original && prop.original.value;
if (config.deep && nVal !== oVal && isAlias(oVal)) {
return useReferenceValue(oVal, config.prefix);
}
return typeIsString(prop) ? `"${nVal}"` : nVal;
}
function generateDarkToken(prop) {
let returnProp = prop;
if (prop.attributes && prop.attributes.dark) {
returnProp = {
name: prop.name+'-dark',
value: prop.attributes.dark.value,
original: { value: prop.attributes.dark.original },
};
}
return returnProp;
}
function throwObjectError(prop) {
let message = `"${prop.name}" has an original value of "${prop.original.value}". \n`;
message += "This points to an object. ";
message += "Reference the object's \"value\" key if it's an alias \n";
throw new Error(message);
}
function isAlias(str) {
// is string and matches "{text.value}"
return !!(typeof str === "string" && str.match(/(^{.*\.value}$)/gm));
}
function typeIsString(prop) {
return !!(prop.attributes && prop.attributes["data-type"] === "string");
}
function makePropCSSVar(name) {
return `--${name}`;
}
function useReferenceValue(value, prefix) {
const head = prefix ? `var(--${prefix}-` : "var(--";
return `${head}${value.replace(/\./g, "-").replace(/({|-value})/g, "")})`;
}
module.exports = formatter;