-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbem-react-helper.js
More file actions
40 lines (31 loc) · 805 Bytes
/
bem-react-helper.js
File metadata and controls
40 lines (31 loc) · 805 Bytes
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
'use strict';
module.exports = b;
export default b;
function b(name, props = {}, defaultMods = {}) {
let result = [name];
const { mix = [], mods = {} } = props;
const addMod = (key, value) => {
const mod = [name, camelToKebab(key)];
if (typeof value !== 'boolean') {
mod.push(camelToKebab(value));
}
result.push(mod.join('_'));
};
Object.keys(defaultMods).forEach(key => {
if (!mods.hasOwnProperty(key) && defaultMods[key]) {
addMod(key, defaultMods[key]);
}
});
Object.keys(mods).forEach(key => {
if (mods[key]) {
addMod(key, mods[key]);
}
});
if (mix) {
result = result.concat(mix);
}
return result.join(' ');
}
function camelToKebab(str) {
return str.toString().replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}