-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (60 loc) · 1.85 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
function classMapFromValues(classPrefix, propName, values, prepend) {
let mapValueToClassName = {};
for (let i = 0; i < values.length; ++i) {
let v = values[i];
if (!v) continue;
if (prepend) {
let valueUpperCase = v.charAt(0).toUpperCase() + v.slice(1);
mapValueToClassName[v] = classPrefix + propName + valueUpperCase;
} else {
mapValueToClassName[v] = classPrefix + v;
}
}
return mapValueToClassName;
}
function classMapFromBoolean(classPrefix, propName, value) {
let mapValueToClassName = {};
mapValueToClassName[value] = classPrefix + propName;
return mapValueToClassName;
}
function createFromProps(blockName, config) {
let classPrefix = blockName + '--';
let mapPropToValueClassNameMap = {};
for (let propName in config) {
let value = config[propName];
if (value === null || value === undefined) continue;
let valueType = typeof value;
let classMap;
if (valueType === 'boolean') {
classMap = classMapFromBoolean(classPrefix, propName, value);
} else if (Array.isArray(value) && value.length) {
classMap = classMapFromValues(classPrefix, propName, value);
} else if (valueType === 'object') {
classMap = classMapFromValues(
classPrefix,
propName,
value.values,
value.prepend
);
} else {
continue;
}
mapPropToValueClassNameMap[propName] = classMap;
}
let fromProps = function(props) {
let classes = [];
classes.push(blockName);
for (let propName in mapPropToValueClassNameMap) {
let propValue = props[propName];
let className = mapPropToValueClassNameMap[propName][propValue];
if (className) {
classes.push(mapPropToValueClassNameMap[propName][propValue]);
}
}
return classes;
};
return fromProps;
}
module.exports = {
createFromProps: createFromProps,
};