Skip to content

Commit

Permalink
curried typeEquals
Browse files Browse the repository at this point in the history
  • Loading branch information
mimarz committed Jun 20, 2024
1 parent 35a5734 commit b59f827
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
13 changes: 7 additions & 6 deletions packages/cli/src/tokens/formats/css-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ const bemify = R.pipe(
);

const classSelector = R.pipe(R.prop('path'), bemify);
const isTypography = R.curry(typeEquals)('typography');
const sortTypographyLast = R.sortWith<TransformedToken>([R.ascend((token) => (isTypography(token) ? 1 : 0))]);
const sortTypographyLast = R.sortWith<TransformedToken>([
R.ascend((token) => (typeEquals('typography')(token) ? 1 : 0)),
]);

/**
* Creates CSS classes from typography tokens
Expand Down Expand Up @@ -91,10 +92,10 @@ export const cssClassesTypography: Format = {

if (typeEquals('typography', token)) {
const references = getReferences(getValue<Typgraphy>(token.original), dictionary.tokens);
const fontweight = R.find<TransformedToken>(R.curry(typeEquals)(['fontweights']))(references);
const lineheight = R.find<TransformedToken>(R.curry(typeEquals)(['lineheights']))(references);
const fontsize = R.find<TransformedToken>(R.curry(typeEquals)(['fontsizes']))(references);
const letterSpacing = R.find<TransformedToken>(R.curry(typeEquals)(['letterSpacing']))(references);
const fontweight = R.find<TransformedToken>(typeEquals(['fontweights']))(references);
const lineheight = R.find<TransformedToken>(typeEquals(['lineheights']))(references);
const fontsize = R.find<TransformedToken>(typeEquals(['fontsizes']))(references);
const letterSpacing = R.find<TransformedToken>(typeEquals(['letterSpacing']))(references);

const fontSizeVar = fontsize ? getVariableName(format(fontsize)) : null;
const fontWeightVar = fontweight ? getVariableName(format(fontweight)) : null;
Expand Down
11 changes: 9 additions & 2 deletions packages/cli/src/tokens/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ export const getType = (token: TransformedToken) => ((token.$type ?? token.type)
*/
export const getValue = <T>(token: TransformedToken | DesignToken): T => (token.$value ?? token.value) as T;

export const typeEquals = (types: string[] | string, token: TransformedToken) => {
/**
* Check if token type matches provided type
* This function is curried
* @param types Type or array of types to check against
* @param token Transformed token
* @returns boolean
*/
export const typeEquals = R.curry((types: string[] | string, token: TransformedToken) => {
if (R.isNil(token)) {
return false;
}

return R.includes(R.toLower(getType(token)), R.map(R.toLower, Array.isArray(types) ? types : [types]));
};
});

0 comments on commit b59f827

Please sign in to comment.