Files
eewing cf73cd3b4c INIT
2026-02-17 14:10:16 -06:00

24 lines
946 B
JavaScript

function createParser(matcher, replacer) {
const regex = RegExp(matcher, "g");
return (str) => {
// throw an error if not a string
if (typeof str !== "string") {
throw new TypeError(`expected an argument of type string, but got ${typeof str}`);
}
// if no match between string and matcher
if (!str.match(regex))
return str;
// executes the replacer function for each match
return str.replace(regex, replacer);
};
}
const camelToKebab = createParser(/[A-Z]/, (match) => `-${match.toLowerCase()}`);
export function styleToCSS(styleObj) {
if (!styleObj || typeof styleObj !== "object" || Array.isArray(styleObj)) {
throw new TypeError(`expected an argument of type object, but got ${typeof styleObj}`);
}
return Object.keys(styleObj)
.map((property) => `${camelToKebab(property)}: ${styleObj[property]};`)
.join("\n");
}