UNPKG

@styn/plugin-tokenizer

Version:

Create tokens to use in your styn styles

39 lines (30 loc) 851 B
function flatten(ob) { let toReturn = {}; for (const i in ob) { if (!ob.hasOwnProperty(i)) continue; if (typeof ob[i] == "object" && ob[i] !== null) { const flatObject = flatten(ob[i]); for (const x in flatObject) { if (!flatObject.hasOwnProperty(x)) continue; toReturn[i + "." + x] = flatObject[x]; } } else { toReturn[i] = ob[i]; } } return toReturn; } const tokenizer = tokens => (tree, walk) => { return walk(tree, rule => { const tokensFlat = flatten(tokens); if (rule.declarations) { for (const property in rule.declarations) { const value = rule.declarations[property]; if (typeof value !== "object" && value in tokensFlat) { rule.declarations[property] = tokensFlat[value]; } } } }); }; export { tokenizer };