aliascss
Version:
AliasCSS is a CSS post processor.
108 lines (107 loc) • 4.62 kB
JavaScript
export default (key, compiler) => {
var _a;
const dynamicCompilerObj = {};
let staticCompilerObj = {};
const property = compiler.property || key;
const alias = compiler.alias || false;
// Generate Static ClassNames
if ((_a = compiler.values) === null || _a === void 0 ? void 0 : _a.length) {
staticCompilerObj = Object.assign(Object.assign({}, staticValueCompiler(property, alias, compiler.values)), staticCompilerObj);
}
// Make Dynamic Class Compiler
if (compiler.compiler && typeof compiler.compiler === 'function') {
if (property)
dynamicCompilerObj[key] = { property, compiler: compiler.compiler };
if (alias)
dynamicCompilerObj[alias] = { property, compiler: compiler.compiler };
}
return [dynamicCompilerObj, staticCompilerObj];
};
export function staticValueCompiler(property, alias = false, values) {
const result = {};
if (values.length) {
values.forEach((value) => {
const val = value.split(':')[0];
value.split(':').forEach((each) => {
result[`${property}-${each}`] = `${property}:${val}`;
if (alias)
result[`${alias}-${each}`] = `${property}:${val}`;
});
});
}
return result;
}
export function extractProperty(className, data) {
// we just need parts before '--' or __ as they can't be in property is any;
const splittedClassName = className.split('--')[0];
const extractPossiblePropertyPortion = splittedClassName.match(/^[A-Z]?[a-z-]+/);
// Not a valid Aliascss class name
if (!extractPossiblePropertyPortion)
return [null, null];
let propertyPortion = extractPossiblePropertyPortion[0]
.replace(/-$/, '') // remove last "-"
.trim();
let propertyHolder = null;
// try to match whole portion with property alias
// From higher to lower i.e border-right-color.......border-right.....border
if (data.hasOwnProperty(propertyPortion)) {
propertyHolder = data[propertyPortion];
// console.log(className,0);
}
else {
// remove last -[a-z]+ and try again un till ^[a-z]+ means we are at begin ing
while (/[-][a-z]+$/.test(propertyPortion)) {
propertyPortion = propertyPortion.replace(/[-][a-z]+$/, '');
if (data.hasOwnProperty(propertyPortion)) {
propertyHolder = data[propertyPortion];
break;
}
}
}
return [propertyHolder, propertyPortion];
}
;
export function generateStaticClassNames(data, global = []) {
let staticClassNames = {};
Object.keys(data).map((key) => {
const [property, alias, values] = [(data[key].property || key), (data[key].property ? key : (data[key].alias || false)), [...(data[key].values || []), ...global]
];
staticClassNames = Object.assign(Object.assign({}, staticValueCompiler(property, alias, values)), staticClassNames);
});
return staticClassNames;
}
;
// takes compiler as arguments and return compiler obj with alias and staticClassNames with Alias
export function createCompilerObj(obj, globalValue = []) {
const [staticClassNames, compilerObj] = [{}, {}];
Object.keys(obj).map((key) => {
var _a, _b;
// check compiler for type=group
if (obj[key].type === 'group') {
compilerObj[key] = obj[key];
// if(obj[key].alias?.trim()) {
// const alias=obj[key].alias?.trim()||null;
// if(alias){
// compilerObj[alias]=obj[key];
// }
return;
}
// createStaticClassNames from Obj
const [property, alias, values] = [(obj[key].property || key), (obj[key].property ? key : (obj[key].alias || false)), [...(obj[key].values || []), ...globalValue]
];
for (const [key, value] of Object.entries(staticValueCompiler(property, alias, values))) {
staticClassNames[key] = value;
}
// staticClassNames={...staticValueCompiler(property,alias,values),...staticClassNames}
// Dynamic compiler generator for css-Props and Alias
compilerObj[key] = obj[key];
if ((_a = obj[key].alias) === null || _a === void 0 ? void 0 : _a.trim()) {
const alias = ((_b = obj[key].alias) === null || _b === void 0 ? void 0 : _b.trim()) || null;
if (alias) {
compilerObj[alias] = obj[key];
compilerObj[alias].property = key;
}
}
});
return [staticClassNames, compilerObj];
}