Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 1x 10x 10x 1x 14x 14x 14x 1x 2x | import type {
Config,
Filter,
PlatformConfig,
TransformedToken,
ValueTransform,
} from 'style-dictionary/types';
import type { StyleDictionaryHost } from '../../style-dictionary-host.js';
import {
isValueUnitObject,
valueUnitToString,
getRawValueUnitValue,
} from '../../utils/value-unit-helpers.js';
/**
* Filter to detect tokens with value+unit object values
*
* @param {Object} token
* @returns boolean
*/
export const valueUnitObjectFilter = ((token: TransformedToken, _options: Config) => {
const value = getRawValueUnitValue(token);
return isValueUnitObject(value);
}) satisfies Filter['filter'];
/**
* Transform that converts value+unit objects to string format
* Converts {value: 0.25, unit: "rem"} to "0.25rem"
* Per DTCG spec, validates value+unit objects strictly
*
* @param {Object} token
* @returns {string}
* @throws {Error} If the value is not a valid value+unit object
*/
export const valueUnitToStringTransformer = ((
token: TransformedToken,
_platform: PlatformConfig,
_options: Config,
) => {
const value = getRawValueUnitValue(token);
const tokenPath = token.path.join('.');
// Let valueUnitToString handle all validation
return valueUnitToString(value, tokenPath);
}) satisfies ValueTransform['transform'];
/**
* Style Dictionary registration for the value-unit-to-string transform
*
* @param {StyleDictionary} StyleDictionary
*/
export const valueUnitStringTransform = (StyleDictionary: StyleDictionaryHost) => {
StyleDictionary.registerTransform({
name: 'value/value-unit-to-string',
type: 'value',
transitive: true,
filter: valueUnitObjectFilter,
transform: valueUnitToStringTransformer,
} satisfies ValueTransform);
};
|