UNPKG

style-dictionary-utils

Version:
93 lines (92 loc) 4.28 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; /** * Recursively traverses an object and changes any child object * with a direct "$type" property matching the specified value, * replacing it with the provided replacement object. * * @param obj - The object to traverse * @param targetType - The $type value to match * @param replacement - The replacement object or function * If a function, receives (currentObj) and returns new object * @returns A new object with replacements applied */ /** * Recursively traverses an object and changes any child object * with a direct "$type" property matching the specified value, * replacing it with the provided replacement object. * * @param obj - The object to traverse * @param targetType - The $type value to match * @param replacement - The replacement object or function * If a function, receives (currentObj, parentObj) and returns new object * @returns A new object with replacements applied */ /** * Recursively traverses an object and changes any child object * with a direct "$type" property matching the specified value, * allowing a replacement function to return a MODIFIED PARENT object * (rather than the matched child) in-place in the parent context. * * @param obj - The object to traverse * @param targetType - The $type value to match * @param replacement - The replacement function which receives (child, parent, childKey) * and returns the new parent object, or undefined to skip replacement. * @returns A new object with replacements applied */ export function changeTypeInObject(obj, targetType, replacement) { function recursive(current) { if (Array.isArray(current) || typeof current !== 'object' || current === null) return current; // Track if any substitution happens let changed = false; const result = Object.assign({}, current); for (const key in current) { if (Object.prototype.hasOwnProperty.call(current, key)) { const child = current[key]; // Only direct children match if (typeof child === 'object' && child !== null && Object.prototype.hasOwnProperty.call(child, '$type') && child.$type === targetType) { // Replacement function returns new parent object if wants to change the parent const replacedParent = replacement(child, current, key); if (typeof replacedParent !== 'undefined') { // If replacement returns a new parent, stop traversing children, and return it return replacedParent; } } // Otherwise recursively process child result[key] = recursive(child); if (result[key] !== current[key]) changed = true; } } return changed ? result : current; } return recursive(obj); } export const extractLetterSpacingPreprocessor = { name: 'extract-letterSpacing-preprocessor', preprocessor: (tokens, _options) => { const newTokens = changeTypeInObject(tokens, 'typography', (child, parent, childKey) => { const _a = child.$value, { letterSpacing } = _a, rest = __rest(_a, ["letterSpacing"]); return Object.assign(Object.assign({}, parent), { [`${childKey}`]: Object.assign(Object.assign({}, child), { $value: rest }), [`${childKey}-letterSpacing`]: { $value: letterSpacing, $type: 'dimension', isSource: child.isSource, filePath: child.filePath, } }); }); return newTokens; }, };