forked from razwan/json-to-sass-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (55 loc) · 1.44 KB
/
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
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
//--------------------------------------------------------
//-- JSON to SCSS
//--------------------------------------------------------
'use strict';
const forEach = require('lodash.foreach');
const mapKeys = require('lodash.mapkeys');
let indentStyle;
const getSCSS = (chunk, level = 0) => {
const indent = indentStyle.repeat(level);
let scss = '';
if (typeof chunk === 'object' && !Array.isArray(chunk)) {
mapKeys(chunk, (value, key) => {
if (level === 0) {
scss += `$${key}: `;
} else {
scss += `${indent}'${key}': `;
}
if (typeof value === 'object') {
if (Array.isArray(value)) {
const indent2 = indentStyle.repeat(level + 1);
scss += '(\n';
forEach(value, (val1) => {
if (Array.isArray(val1)) {
scss += `${indent2}`;
forEach(val1, (val2) => {
scss += `${val2} `;
});
scss = `${scss.slice(0, -1)},\n`;
} else {
scss += `${indent2}${val1},\n`;
}
});
scss = scss.slice(0, -2);
scss += `\n${indent})`;
} else {
scss += `(\n${getSCSS(value, level + 1)}\n${indent})`;
}
} else {
scss += getSCSS(value, level + 1);
}
scss += `${level === 0 ? ';\n' : ','}\n`;
});
scss = scss.slice(0, -2);
} else {
scss += chunk;
}
return scss;
};
class JsonToScss {
convert(data, indent = `\t`) {
indentStyle = indent;
return data ? getSCSS(JSON.parse(data)) : false;
}
}
module.exports = new JsonToScss();