forked from noya-app/noya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariant.ts
118 lines (111 loc) · 2.48 KB
/
variant.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { FontWeight, FontSlant } from 'noya-fonts';
import {
GoogleFontVariant,
GoogleItalicFontVariant,
GoogleRegularFontVariant,
} from './types';
export function isItalicVariant(
variant: GoogleFontVariant,
): variant is GoogleItalicFontVariant {
return variant.includes('italic');
}
export function isRegularVariant(
variant: GoogleFontVariant,
): variant is GoogleRegularFontVariant {
return !variant.includes('italic');
}
export function getGoogleFontVariantWeight(variant: GoogleFontVariant) {
switch (variant) {
case '100':
case '100italic':
return 'ultralight';
case '200':
case '200italic':
return 'thin';
case '300':
case '300italic':
return 'light';
case 'regular':
case 'italic':
return 'regular';
case '500':
case '500italic':
return 'medium';
case '600':
case '600italic':
return 'semibold';
case '700':
case '700italic':
return 'bold';
case '800':
case '800italic':
return 'heavy';
case '900':
case '900italic':
return 'black';
}
}
export function decodeGoogleFontVariant(
variant: GoogleFontVariant,
): {
fontWeight: FontWeight;
fontSlant: FontSlant;
} {
return {
fontWeight: getGoogleFontVariantWeight(variant),
fontSlant: isItalicVariant(variant) ? 'italic' : 'upright',
};
}
export function encodeGoogleFontVariant(
fontSlant: FontSlant,
fontWeight: FontWeight,
): GoogleFontVariant {
const suffix = fontSlant === 'upright' ? '' : 'italic';
switch (fontWeight) {
case 'ultralight':
return `100${suffix}`;
case 'thin':
return `200${suffix}`;
case 'light':
return `300${suffix}`;
case 'regular':
return fontSlant === 'upright' ? 'regular' : 'italic';
case 'medium':
return `500${suffix}`;
case 'semibold':
return `600${suffix}`;
case 'bold':
return `700${suffix}`;
case 'heavy':
return `800${suffix}`;
case 'black':
return `900${suffix}`;
}
}
export function isValidFontVariant(
string: string,
): string is GoogleFontVariant {
switch (string) {
case '100':
case '100italic':
case '200':
case '200italic':
case '300':
case '300italic':
case 'regular':
case 'italic':
case '500':
case '500italic':
case '600':
case '600italic':
case '700':
case '700italic':
case '800':
case '800italic':
case '900':
case '900italic':
return true;
default:
return false;
}
}