@primer/primitives
Version:
Typography, spacing, and color primitives for Primer design system
45 lines (44 loc) • 1.75 kB
JavaScript
import { isFontFamily } from '../filters/index.js';
import { hasSpaceInString } from './utilities/hasSpaceInString.js';
/**
* takes a value and returns it if its a string or concats strings in an array quoting strings with spaces
* @param value
* @returns string
*/
export const parseFontFamily = (token, fontFamilies = {}, options) => {
const valueProp = options.usesDtcg ? '$value' : 'value';
// return value from fontFamilies
if (token.name in fontFamilies) {
return fontFamilies[token.name];
}
// return string
if (typeof token[valueProp] === 'string') {
return token[valueProp];
}
// return stringified array
if (Array.isArray(token[valueProp])) {
return token[valueProp]
.map((string) => {
if (typeof string !== 'string') {
throw new Error(`Invalid value in array ${string}, must be a string`);
}
return hasSpaceInString(string) ? `'${string}'` : string;
})
.join(', ');
}
// invalid value
throw new Error(`Invalid value ${token[valueProp]}, should be a string or array of strings`);
};
/**
* @description converts fontFamily tokens value to string
* @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
* @matcher matches all tokens of $type `fontFamily`
* @transformer returns a string
*/
export const fontFamilyToFigma = {
name: 'fontFamily/figma',
type: 'value',
transitive: true,
filter: isFontFamily,
transform: (token, platform, options) => { var _a; return parseFontFamily(token, (_a = platform.options) === null || _a === void 0 ? void 0 : _a.fontFamilies, options); },
};