Skip to content

Instantly share code, notes, and snippets.

@awinogradov
Created May 7, 2017 00:36
Show Gist options
  • Save awinogradov/8ca52fd3364486fe46791a7387fd550b to your computer and use it in GitHub Desktop.
Save awinogradov/8ca52fd3364486fe46791a7387fd550b to your computer and use it in GitHub Desktop.
easy-breezy API for @bem/naming
const naming = require('./pretty-naming');
const b1 = p('block').m({ m1: 1, m2: 2 });
const b2 = p('block42').m({ m3: 3 });
b1.mix(b2);
console.log(b1.stringify());
// 'block_m1_1 block_m2_2 block42_m3_3'
b1.e('elem');
console.log(b1.stringify());
// 'block__elem_m1_1 block__elem_m2_2 block42_m3_3'
const bemNaming = require('@bem/naming')();
module.exports = function pretty(name) {
const _entity = { block : name };
let _mods = {};
let _mixes = [];
const api = {
e(name) {
_entity.elem = name;
return api;
},
m(mods) {
_mods = Object.assign({}, _mods, mods);
return api;
},
mix() {
_mixes = Array.prototype.slice.call(arguments, 0);
return api;
},
_entities() {
let _all = Object.keys(_mods).reduce((prev, curr) => {
prev.push(Object.assign({}, _entity, {
mod : { name : curr, val : _mods[curr] }
}));
return prev;
}, []);
_mixes.forEach(mix => (_all = _all.concat(mix._entities())));
return _all;
},
stringify() {
return api._entities().map(item => bemNaming.stringify(item)).join(' ');
}
};
return api;
};
@Yeti-or
Copy link

Yeti-or commented May 7, 2017

toString() { return this.stringify(); },
valueOf() { return this.stringify(); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment