• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

marcomontalbano / figma-export / 8040637088

25 Feb 2024 08:41PM CUT coverage: 96.473%. Remained the same
8040637088

push

github

web-flow
Merge pull request #162 from marcomontalbano/add-overrides-for-axios

Add `overrides` for axios

236 of 258 branches covered (91.47%)

Branch coverage included in aggregate %.

612 of 621 relevant lines covered (98.55%)

17.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.2
/packages/core/src/lib/figmaStyles/textStyle.ts
1
import * as Figma from 'figma-js';
2
import * as FigmaExport from '@figma-export/types';
3

4
const translateTextTransform = (figmaTextCase?: string): FigmaExport.TextTransform => {
1✔
5
    const map: { [key: string]: FigmaExport.TextTransform } = {
14✔
6
        undefined: 'none',
7
        UPPER: 'uppercase',
8
        LOWER: 'lowercase',
9
        TITLE: 'capitalize',
10
    };
11

12
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
13
    return map[figmaTextCase!] || map.undefined;
14✔
14
};
15

16
const translateFontVariant = (figmaTextCase?: string): FigmaExport.FontVariant => {
1✔
17
    const map: { [key: string]: FigmaExport.FontVariant } = {
14✔
18
        undefined: 'normal',
19
        SMALL_CAPS: 'small-caps',
20
        SMALL_CAPS_FORCED: 'all-small-caps',
21
    };
22

23
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
24
    return map[figmaTextCase!] || map.undefined;
14✔
25
};
26

27
const translateTextDecoration = (figmaTextDecoration?: string): FigmaExport.TextDecoration => {
1✔
28
    const map: { [key: string]: FigmaExport.TextDecoration } = {
14✔
29
        undefined: 'none',
30
        STRIKETHROUGH: 'line-through',
31
        UNDERLINE: 'underline',
32
    };
33

34
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
35
    return map[figmaTextDecoration!] || map.undefined;
14!
36
};
37

38
const translateTextAlign = (figmaTextAlignHorizontal: string): FigmaExport.TextAlign => {
1✔
39
    const map: { [key: string]: FigmaExport.TextAlign } = {
14✔
40
        LEFT: 'left',
41
        RIGHT: 'right',
42
        CENTER: 'center',
43
        JUSTIFIED: 'justify',
44
    };
45

46
    return map[figmaTextAlignHorizontal];
14✔
47
};
48

49
const translateVerticalAlign = (figmaTextAlignVertical: string): FigmaExport.VerticalAlign => {
1✔
50
    const map: { [key: string]: FigmaExport.VerticalAlign } = {
14✔
51
        TOP: 'top',
52
        CENTER: 'middle',
53
        BOTTOM: 'bottom',
54
    };
55

56
    return map[figmaTextAlignVertical];
14✔
57
};
58

59
const translateLineHeight = ({
1✔
60
    lineHeightPx,
61
    lineHeightPercentFontSize,
62
    lineHeightUnit,
63
}: Figma.TypeStyle): string => {
64
    if (lineHeightUnit === 'FONT_SIZE_%') {
14✔
65
        // Figma API doesn't return `lineHeightPercentFontSize`
66
        // if lineHeightPercent is 100
67
        return lineHeightPercentFontSize
4!
68
            ? Math.fround(lineHeightPercentFontSize / 100).toString()
69
            : '1';
70
    }
71

72
    // this unit is deprecated and will be
73
    // removed in future version of Figma API
74
    // https://www.figma.com/developers/api#files-types
75
    if (lineHeightUnit === 'INTRINSIC_%') {
10✔
76
        // this looks wrong at first
77
        // but Pixels in this if-branch are intentional
78
        // https://github.com/marcomontalbano/figma-export/pull/156#issuecomment-1931415352
79
        return `${lineHeightPx}px`;
6✔
80
    }
81

82
    if (lineHeightUnit === 'PIXELS') {
4!
83
        return `${lineHeightPx}px`;
4✔
84
    }
85

86
    throw new Error(`Unknown lineHeightUnit: ${lineHeightUnit}`);
×
87
};
88

89
const createTextStyle = (textNode: Figma.Style & Figma.Text): FigmaExport.TextStyle => {
1✔
90
    const {
91
        fontFamily, fontWeight, fontSize, letterSpacing,
92
        italic, textCase, textDecoration, textAlignHorizontal, textAlignVertical,
93
    } = textNode.style;
14✔
94

95
    return {
14✔
96
        fontFamily,
97
        fontWeight,
98
        fontSize,
99
        letterSpacing,
100
        lineHeight: translateLineHeight(textNode.style),
101
        fontStyle: italic ? 'italic' : 'normal',
14!
102
        fontVariant: translateFontVariant(textCase),
103
        textTransform: translateTextTransform(textCase),
104
        textDecoration: translateTextDecoration(textDecoration),
105
        textAlign: translateTextAlign(textAlignHorizontal),
106
        verticalAlign: translateVerticalAlign(textAlignVertical),
107
    };
108
};
109

110
const parse = (node: FigmaExport.StyleNode): FigmaExport.StyleTypeText | undefined => {
1✔
111
    if (node.styleType === 'TEXT' && node.type === 'TEXT') {
17✔
112
        return {
14✔
113
            styleType: 'TEXT',
114
            style: createTextStyle(node),
115
        };
116
    }
117

118
    return undefined;
3✔
119
};
120

121
export {
122
    parse,
1✔
123
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc