Skip to content

Commit 14ff5dd

Browse files
authored
Merge pull request #1254 from basics/feature/type-generation-font-options
refactor(types): use font option for type generation
2 parents 437abff + f738788 commit 14ff5dd

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed

src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ async function addBuildTemplates(nuxt: Nuxt, options: ModuleOptions) {
169169

170170
addTypeTemplate({
171171
filename: 'types/booster.d.ts',
172-
getContents: () => getTypesContent(fontConfig),
172+
getContents: () => getTypesContent(options.fonts || []),
173173
write: true
174174
});
175175
}

src/utils/types.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type FontConfig from '../classes/FontConfig';
2-
import type { FontOptionVariance } from '../types';
1+
import type { FontOption, FontOptionVariance } from '../types';
32

43
/* eslint-disable security/detect-object-injection */
54
interface TextProperties {
@@ -10,14 +9,14 @@ interface TextProperties {
109
}
1110

1211
function getFromVariances<T>(
13-
fontConfig: FontConfig,
12+
fonts: FontOption[],
1413
key: keyof FontOptionVariance
1514
): T[] {
1615
const uniqueStyles = new Set<T>();
17-
const fontCount = fontConfig.fonts.length;
16+
const fontCount = fonts.length;
1817

1918
for (let i = 0; i < fontCount; i++) {
20-
const values = fontConfig.fonts[i].variances;
19+
const values = fonts[i].variances;
2120
for (let j = 0, valueCount = values.length; j < valueCount; j++) {
2221
uniqueStyles.add(values[j][key] as T);
2322
}
@@ -50,19 +49,22 @@ function generateAliases(textProperties: TextProperties): string {
5049
// Merge all values into a union (|) type value.
5150
const values: string[] = new Array(valueCount);
5251
for (let j = 0; j < valueCount; j++) {
53-
values[j] = `'${propValues[j]}'`;
52+
if (/\d+/.test(String(propValues[j]))) {
53+
values[j] = String(propValues[j]);
54+
} else {
55+
values[j] = `'${propValues[j]}'`;
56+
}
5457
}
5558
output[i] = `${line}${values.join(' | ')};`;
5659
}
5760
}
5861
return output.join('\n');
5962
}
6063

61-
export function getTypesContent(fontConfig: FontConfig): string {
64+
export function getTypesContent(fonts: FontOption[]): string {
6265
return generateAliases({
63-
family: unique(fontConfig.fonts.map(font => font.family)),
64-
weight: getFromVariances(fontConfig, 'weight'),
65-
style: getFromVariances(fontConfig, 'style')
66+
family: unique(fonts.map(font => font.family)),
67+
weight: getFromVariances(fonts, 'weight'),
68+
style: getFromVariances(fonts, 'style')
6669
});
6770
}
68-
/* eslint-enable security/detect-object-injection */

test/tests/utils/getTypesContent.test.ts

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,46 @@ import { getTypesContent } from '../../../src/utils/types';
44
describe('getTypesContent', () => {
55
it('generates generic types on empty font config', () => {
66
expect(
7-
getTypesContent({
8-
fonts: [
9-
{
10-
family: '',
11-
variances: [
12-
{
13-
style: '',
14-
weight: 0,
15-
sources: []
16-
}
17-
]
18-
}
19-
]
20-
})
7+
getTypesContent([
8+
{
9+
family: '',
10+
fallback: [],
11+
variances: [
12+
{
13+
style: '',
14+
weight: 0,
15+
sources: []
16+
}
17+
]
18+
}
19+
])
2120
).toBe(
2221
"export type FontFamily = 'string';\nexport type FontWeight = 'string';\nexport type FontStyle = 'string';"
2322
);
2423
});
2524

2625
it('generates proper types on valid font config', () => {
2726
expect(
28-
getTypesContent({
29-
fonts: [
30-
{
31-
family: 'Roboto',
32-
variances: [
33-
{
34-
style: 'italic',
35-
weight: 400,
36-
sources: []
37-
},
38-
{
39-
style: 'normal',
40-
weight: 'bold',
41-
sources: []
42-
}
43-
]
44-
}
45-
]
46-
})
27+
getTypesContent([
28+
{
29+
family: 'Roboto',
30+
fallback: [],
31+
variances: [
32+
{
33+
style: 'italic',
34+
weight: 400,
35+
sources: []
36+
},
37+
{
38+
style: 'normal',
39+
weight: 'bold',
40+
sources: []
41+
}
42+
]
43+
}
44+
])
4745
).toBe(
48-
"export type FontFamily = 'Roboto';\nexport type FontWeight = '400' | 'bold';\nexport type FontStyle = 'italic' | 'normal';"
46+
"export type FontFamily = 'Roboto';\nexport type FontWeight = 400 | 'bold';\nexport type FontStyle = 'italic' | 'normal';"
4947
);
5048
});
5149
});

0 commit comments

Comments
 (0)