-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
makeFontWeightTokens.ts
40 lines (33 loc) · 1.35 KB
/
makeFontWeightTokens.ts
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
import { FRAME as Frame } from '../../../contracts/Figma';
import { FontWeightTokens } from '../../../contracts/Tokens';
import { sanitizeString } from '../../../frameworks/string/sanitizeString';
import {
ErrorMakeFontWeightTokensNoFrame,
ErrorMakeFontWeightTokensNoChildren,
ErrorMakeFontWeightTokensMissingProps,
ErrorMakeFontWeightTokensMissingWeight
} from '../../../frameworks/errors/errors';
/**
* @description Places all Figma font weights into a clean object
*/
export function makeFontWeightTokens(
fontWeightFrame: Frame,
camelizeTokenNames?: boolean
): FontWeightTokens {
if (!fontWeightFrame) throw Error(ErrorMakeFontWeightTokensNoFrame);
if (!fontWeightFrame.children) throw Error(ErrorMakeFontWeightTokensNoChildren);
const fontWeights: Record<string, unknown> = {};
const TOKENS = fontWeightFrame.children.reverse();
TOKENS.forEach((item: Frame) => makeFontWeightToken(item, fontWeights, camelizeTokenNames));
return fontWeights;
}
function makeFontWeightToken(
item: Frame,
fontWeights: Record<string, unknown>,
camelizeTokenNames?: boolean
) {
if (!item.name || !item.style) throw Error(ErrorMakeFontWeightTokensMissingProps);
if (!item.style.fontWeight) throw Error(ErrorMakeFontWeightTokensMissingWeight);
const NAME = sanitizeString(item.name, camelizeTokenNames);
fontWeights[NAME] = item.style.fontWeight;
}