style-dictionary-utils
Version:
Utilities to use in your style dictionary config
24 lines (23 loc) • 687 B
JavaScript
/**
* jsonToNestedValue
* @description creates a nested json three where every final value is the `.value` prop
* @param token StyleDictionary.DesignToken
* @returns nested json three
*/
export const jsonToNestedValue = (token) => {
// is non-object value
if (!token || typeof token !== 'object')
return token;
// is design token
if ('value' in token)
return token.value;
if ('$value' in token)
return token.$value;
// is obj
const nextObj = {};
for (const [prop, value] of Object.entries(token)) {
// @ts-expect-error: can't predict type
nextObj[prop] = jsonToNestedValue(value);
}
return nextObj;
};