This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.mjs
203 lines (176 loc) · 5.29 KB
/
utils.mjs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// export const resolveFigmaValues = (
// obj = {},
// originalObj,
// res = {},
// extraKey = '',
// ) => {
// Object.keys(obj).forEach(key => {
// if (typeof obj[key] !== 'object') {
// if (key === 'value') {
// if (/{.*}/.test(obj[key])) {
// const parsedKey = obj[key].slice(1, -1);
// const parts = parsedKey.split('.');
// let completeKey = originalObj.global;
// parts.forEach(part => (completeKey = completeKey[part]));
// res[extraKey.slice(0, -1)] = `${completeKey.value}`;
// } else res[extraKey.slice(0, -1)] = obj[key];
// }
// } else {
// resolveFigmaValues(obj[key], originalObj, res, `${extraKey}${key}.`);
// }
// });
// return res;
// };
const merge = (arg1, arg2) => {
// bias to second, to mimic Object spread behavior
if (typeof arg1 !== 'object' || typeof arg2 !== 'object') return arg2;
const result = {};
Object.keys(arg1).forEach(k => {
if (arg2[k] !== undefined) {
result[k] = merge(arg1[k], arg2[k]);
} else result[k] = arg1[k];
});
Object.keys(arg2).forEach(k => {
if (arg1[k] === undefined) result[k] = arg2[k];
});
return result;
};
export const stretchAndResolveTokens = tokens => {
const result = {};
// first, build up object structure
// 'anchor.label = text' becomes { anchor: { label: { v: text } } }
Object.keys(tokens).forEach(key => {
const parts = key.split('.');
let node = result;
while (parts.length) {
const part = parts.shift();
if (part) {
if (!node[part]) node[part] = {};
node = node[part];
}
}
node.v = tokens[key];
});
// second, resolve value references
const find = path => {
const parts = path.split('.');
let node = result;
while (node && parts.length) node = node[parts.shift()];
return node;
};
const resolve = node => {
Object.keys(node).forEach(key => {
const value = node[key];
if (typeof value === 'object') return resolve(value);
if (typeof value === 'string') node[key] = find(value) ?? value;
});
};
resolve(result);
// third, remove 'v' and merge objects
const prune = node => {
Object.keys(node).forEach(key => {
while (node[key].v !== undefined) {
const v = node[key].v;
if (Object.keys(node[key]).length === 1) node[key] = v;
else {
node[key] = merge(v, node[key]);
delete node[key].v;
}
}
if (typeof node[key] === 'object') prune(node[key]);
});
};
prune(result);
return result;
};
export const splitTheme = tokens => {
const light = {};
const dark = {};
const set = (root, path, value) => {
const setPath = [...path];
let node = root;
while (setPath.length > 1) {
const key = setPath.shift();
if (!node[key]) node[key] = {};
node = node[key];
}
node[setPath[0]] = value;
};
const split = (node, path = []) => {
Object.keys(node)
.filter(
key =>
typeof node[key] !== 'string' || (key !== 'light' && key !== 'dark'),
)
.forEach(key => {
const keyPath = [...path, key];
const value = node[key];
if (typeof value === 'object') {
if (typeof value.light === 'string' && value.light && value.dark) {
if (Object.keys(value).length > 2) {
// need to inject 'default' so we can keep other keys
const defaultKeyPath = [...keyPath, 'default'];
set(light, defaultKeyPath, value.light);
set(dark, defaultKeyPath, value.dark);
} else {
set(light, keyPath, value.light);
set(dark, keyPath, value.dark);
}
}
split(value, keyPath);
} else set(light, keyPath, value);
});
};
split(tokens);
return [light, dark];
};
export const flattenTokens = tokens => {
const result = {};
const descend = (node, path = []) => {
Object.keys(node).forEach(key => {
const value = node[key];
const keyPath = [...path, key];
if (typeof value === 'object') return descend(value, keyPath);
result[keyPath.join('.')] = value;
});
};
descend(tokens);
return result;
};
// vanilla-extract cannot handle 'number' values, it seems to require 'string'.
// We convert here so the typescript types work from the get-go, without
// having to do some type shifting within grommet-exp-theme.
export const stringifyTokens = tokens => {
const result = JSON.parse(JSON.stringify(tokens));
const convert = node => {
Object.keys(node).forEach(key => {
const value = node[key];
if (typeof value === 'object') return convert(value);
else if (typeof value === 'number') node[key] = `${value}`;
});
};
convert(result);
return result;
};
export const generateCssVars = tokens => [
`:root {
${Object.keys(tokens)
.filter(key => !key.endsWith('dark'))
.map(key => {
const parts = key.split('.');
if (parts[parts.length - 1] === 'light') parts.splice(-1);
return ` --${parts.join('-')}: ${tokens[key]};`;
})
.join('\n')}
}`,
`:root {
${Object.keys(tokens)
.filter(key => key.endsWith('dark'))
.map(key => {
const parts = key.split('.');
parts.splice(-1);
return ` --${parts.join('-')}: ${tokens[key]};`;
})
.join('\n')}
}`,
];