judocss
Version:
The functional CSS toolkit designed for minimal effort and maximum efficiency.
81 lines (60 loc) • 2.24 kB
JavaScript
;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
(function () {
var queryModifierRegex = /--(small|medium|large|small-only|medium-only)$/;
var findKeyCache = {};
function getValue(className, entry) {
var type = _typeof(entry.value);
var res = "";
if (type === "string") {
res = entry.value;
}
if (type === "function") {
var args = entry.regex.exec(className).splice(1);
res = entry.value.apply(undefined, args);
}
var childSelector = /(.+)(?:\*)$/.exec(className);
if (childSelector) {
className = childSelector[1] + "\\* > *";
}
return ['.', className.replace(/[%#.]/g, "\\$&"), ' ', res].join('');
}
function queryKey(className) {
var res = queryModifierRegex.exec(className);
return res ? res[1] : "all";
}
function _findKey(regMap, className) {
className = className.replace(/--(small|medium|large|small-only|medium-only)\*?$/, "");
var parts = className.split("-");
if (parts.length <= 1) {
return className;
}
var i = parts.length;
var key = void 0;
while (i--) {
parts.pop();
key = parts.join("-") + "-";
if (regMap[key]) {
return key;
}
}
}
function findKey(regMap, className) {
findKeyCache[className] = findKeyCache[className] || _findKey(regMap, className);
return findKeyCache[className];
}
function ruleset(regMap, className, callback) {
var key = findKey(regMap, className);
var entries = regMap[key];
if (entries) {
var i = entries.length;
while (i--) {
var entry = entries[i];
if (entry.regex.test(className)) {
callback(getValue(className, entry), queryKey(className));
}
}
}
}
module.exports = ruleset;
})();