@primer/primitives
Version:
Typography, spacing, and color primitives for Primer design system
24 lines (23 loc) • 743 B
JavaScript
/**
* @description converts tokens to type and returns array with used type
* @param tree object
* @param callback function to be run on valid items
* @param validItem function to test if a node is a validItem to be transformed
* @returns object
*/
export const treeWalker = (tree, callback, isValidItem) => {
// is a valid item -> pass through callback
if (isValidItem(tree)) {
return callback(tree);
}
// is non-object value -> ignore
if (typeof tree !== 'object' || tree === null) {
return;
}
// is obj -> continue
const nextObj = {};
for (const [prop, value] of Object.entries(tree)) {
nextObj[prop] = treeWalker(value, callback, isValidItem);
}
return nextObj;
};