-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathdata-json-formatter.js
80 lines (61 loc) · 1.67 KB
/
data-json-formatter.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
73
74
75
76
77
78
79
80
import { format as JSONSetsFormat } from "./json-sets-formatter.js";
export const format = ({ dictionary, platform, file, options }) => {
const prefix = platform.prefix ? platform.prefix : false;
let result = {};
const jsonSets = JSON.parse(
JSONSetsFormat({ dictionary, platform, file, options })
);
const convertRef = (ref) => {
return ref?.toString()?.replace(/\{(.*?)\}/g, `var(--${prefix}-$1)`);
};
const deconstructSets = (obj, scope = undefined) => {
let ret = obj;
Object.entries(obj.sets ?? {}).forEach(([context, data]) => {
if (context === "wireframe") return;
if (typeof scope !== "undefined" && scope !== context) return;
delete data.uuid;
if (data.ref) {
data.ref = convertRef(data.ref);
if (data.ref?.toString() === data.value?.toString()) {
delete data.ref;
}
}
if (data.sets) {
data = deconstructSets(data, context);
ret = {
...ret,
...data
};
}
else {
ret[context] = data;
}
delete ret.sets;
});
delete ret.uuid;
if (ret.ref) ret.ref = convertRef(ret.ref);
if (ret.ref === ret.value) {
delete ret.ref;
}
return ret;
};
Object.keys(jsonSets).forEach((tokenName) => {
const tokenValue = jsonSets[tokenName];
// Add the property to the results object
result[tokenName] = {
prop: (prefix)? `--${prefix}-${tokenName}` : `--${tokenName}`,
...(deconstructSets(tokenValue) ?? {}),
};
});
// Sort the result alphabetically by keys
result = Object.entries(result).sort().reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {});
return JSON.stringify(result, null, 2);
};
format.nested = true;
export default {
name: "json/sets",
format,
};