rebem-classname
Version:
Helpers for composing and parsing BEM classNames
77 lines (60 loc) • 2 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.stringify = stringify;
var _validate = require('./validate.js');
Object.defineProperty(exports, 'validate', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_validate).default;
}
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var modDelim = process.env.REBEM_MOD_DELIM || '_';
var elemDelim = process.env.REBEM_ELEM_DELIM || '__';
function stringify(props) {
if (!props) {
return '';
}
var out = '';
// block
if (typeof props.block !== 'undefined') {
(function () {
out += (out ? ' ' : '') + props.block;
// elem
if (typeof props.elem !== 'undefined') {
out += elemDelim + props.elem;
}
var entity = out;
if (typeof props.mods !== 'undefined') {
Object.keys(props.mods).forEach(function (modName) {
var modValue = props.mods[modName];
var modValueString = '';
if (modValue !== false) {
// 'short' boolean mods
if (modValue !== true) {
modValueString += modDelim + modValue;
}
out += ' ' + entity + modDelim + modName + modValueString;
}
});
}
})();
}
if (typeof props.mix !== 'undefined') {
// convert object or array into array
var mixes = [].concat(props.mix);
mixes
// filter holes in array
.filter(function (mix) {
return mix;
}).forEach(function (mix) {
out += (out ? ' ' : '') + stringify(mix);
});
}
if (typeof props.className !== 'undefined') {
out += (out ? ' ' : '') + props.className;
}
return out;
}
;