All files / packages/design-tokens/src/style-dictionary/transforms/common value-unit-to-string.js

100% Statements 11/11
100% Branches 0/0
100% Functions 5/5
100% Lines 11/11

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                        2x 7082x 7082x                       2x 562x 562x     562x               2x 3x       7074x 549x      
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) => {
  const value = getRawValueUnitValue(token);
  return isValueUnitObject(value);
};
 
/**
 * 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) => {
  const value = getRawValueUnitValue(token);
  const tokenPath = token.path.join('.');
 
  // Let valueUnitToString handle all validation
  return valueUnitToString(value, tokenPath);
};
 
/**
 * Style Dictionary registration for the value-unit-to-string transform
 *
 * @param {StyleDictionary} StyleDictionary
 */
export const valueUnitStringTransform = (StyleDictionary) => {
  StyleDictionary.registerTransform({
    name: 'value/value-unit-to-string',
    type: 'value',
    transitive: true,
    filter: (token) => valueUnitObjectFilter(token),
    transform: (token) => valueUnitToStringTransformer(token),
  });
};