@gtechdoodler/bem-it
Version:
A BEM util to help with consistent element class naming in components.
128 lines • 4.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addClass = exports.Output = void 0;
var Output = (function () {
function Output(lastBemIt) {
this._lastBemIt = lastBemIt;
}
Object.defineProperty(Output.prototype, "and", {
get: function () {
return new BemIt(this._lastBemIt);
},
enumerable: false,
configurable: true
});
Output.prototype.out = function (option) {
return this._lastBemIt.out(option);
};
return Output;
}());
exports.Output = Output;
var BemIt = (function () {
function BemIt(block) {
this._block = '';
this._lastBlockObj = null;
this._fullBem = '';
if (typeof block === 'string') {
this._block = block;
this._fullBem = block;
}
else {
this._lastBlockObj = block;
this._fullBem = block.blockString;
}
}
Object.defineProperty(BemIt.prototype, "blockString", {
get: function () {
var _a;
if (this._block) {
return this._block;
}
return ((_a = this._lastBlockObj) === null || _a === void 0 ? void 0 : _a.blockString) || '';
},
enumerable: false,
configurable: true
});
Object.defineProperty(BemIt.prototype, "peek", {
get: function () {
return this._fullBem;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BemIt.prototype, "and", {
get: function () {
return new BemIt(this);
},
enumerable: false,
configurable: true
});
BemIt.prototype.el = function (element) {
this._fullBem = element
? this._fullBem + "__" + element.trim()
: this._fullBem;
return this;
};
BemIt.prototype.mod = function (modifier) {
var _this = this;
var modStr = function (val) {
val = val ? val.trim() : '';
return val !== '' ? "" + _this._fullBem + ("--" + val.trim()) : undefined;
};
var modMap = function (val) {
return val.filter(function (m) { return m; }).map(function (m) { return modStr(m); }).join(' ');
};
if (typeof modifier === 'string') {
modifier = modStr(modifier);
}
else if (Array.isArray(modifier)) {
modifier = modMap(modifier);
}
else if (modifier) {
var keyArr_1 = [];
Object.entries(modifier).forEach(function (_a) {
var key = _a[0], value = _a[1];
if (value) {
keyArr_1.push(key);
}
});
modifier = modMap(keyArr_1);
}
if (modifier) {
this._fullBem = this._fullBem + " " + modifier;
}
return new Output(this);
};
BemIt.prototype.out = function (option) {
var _a;
if (option === void 0) { option = "default"; }
var ret = this._block
? this._fullBem
: ((_a = this._lastBlockObj) === null || _a === void 0 ? void 0 : _a.out()) + " " + this._fullBem;
this._fullBem = this.blockString;
this._lastBlockObj = null;
if (option === "last") {
var splitRet = ret.split(" ");
var splitRetLength = splitRet.length;
if (splitRetLength) {
return splitRet[splitRetLength - 1];
}
}
return ret;
};
return BemIt;
}());
exports.default = BemIt;
function addClass(name) {
name = name ? name.trim() : undefined;
return {
before: function (bem) {
return "" + (name ? name + " " : '') + bem.out();
},
after: function (bem) {
return "" + bem.out() + (name ? " " + name : '');
}
};
}
exports.addClass = addClass;
//# sourceMappingURL=bem-it.js.map