Skip to content

Commit db54573

Browse files
committed
refactor: assertLocaleData returns a value
1 parent cac6278 commit db54573

3 files changed

Lines changed: 41 additions & 34 deletions

File tree

src/internal/assert-locale-data.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { FakerError } from '../errors/faker-error';
2+
3+
/**
4+
* Checks that the value is not null or undefined and throws an error if it is.
5+
*
6+
* @param value The value to check.
7+
* @param path The path to the locale data.
8+
*
9+
* @returns The value if it's not null or undefined.
10+
*
11+
* @throws {FakerError} If the value is null or undefined.
12+
*/
13+
export function assertLocaleData<T>(
14+
value: T,
15+
...path: string[]
16+
): NonNullable<T> {
17+
if (value === null) {
18+
throw new FakerError(
19+
`The locale data for '${path.join('.')}' aren't applicable to this locale.
20+
If you think this is a bug, please report it at: https://github.com/faker-js/faker`
21+
);
22+
} else if (value === undefined) {
23+
throw new FakerError(
24+
`The locale data for '${path.join('.')}' are missing in this locale.
25+
If this is a custom Faker instance, please make sure all required locales are used e.g. '[de_AT, de, en, base]'.
26+
Please contribute the missing data to the project or use a locale/Faker instance that has these data.
27+
For more information see https://fakerjs.dev/guide/localization.html`
28+
);
29+
}
30+
31+
return value;
32+
}

src/internal/locale-proxy.ts

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { LocaleDefinition } from '../definitions';
22
import { FakerError } from '../errors/faker-error';
3+
import { assertLocaleData } from './assert-locale-data';
34

45
/**
56
* A proxy for LocaleDefinition that marks all properties as required and throws an error when an entry is accessed that is not defined.
@@ -54,31 +55,6 @@ export function createLocaleProxy(locale: LocaleDefinition): LocaleProxy {
5455
}) as LocaleProxy;
5556
}
5657

57-
/**
58-
* Checks that the value is not null or undefined and throws an error if it is.
59-
*
60-
* @param value The value to check.
61-
* @param path The path to the locale data.
62-
*/
63-
export function assertLocaleData<T>(
64-
value: T,
65-
...path: string[]
66-
): asserts value is NonNullable<T> {
67-
if (value === null) {
68-
throw new FakerError(
69-
`The locale data for '${path.join('.')}' aren't applicable to this locale.
70-
If you think this is a bug, please report it at: https://github.com/faker-js/faker`
71-
);
72-
} else if (value === undefined) {
73-
throw new FakerError(
74-
`The locale data for '${path.join('.')}' are missing in this locale.
75-
If this is a custom Faker instance, please make sure all required locales are used e.g. '[de_AT, de, en, base]'.
76-
Please contribute the missing data to the project or use a locale/Faker instance that has these data.
77-
For more information see https://fakerjs.dev/guide/localization.html`
78-
);
79-
}
80-
}
81-
8258
/**
8359
* Creates a proxy for a category that throws an error when accessing an undefined property.
8460
*
@@ -106,8 +82,7 @@ function createCategoryProxy<
10682
return value;
10783
}
10884

109-
assertLocaleData(value, categoryName, entryName.toString());
110-
return value;
85+
return assertLocaleData(value, categoryName, entryName.toString());
11186
},
11287

11388
set: throwReadOnlyError,

src/modules/date/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { DateEntryDefinition } from '../../definitions';
22
import { FakerError } from '../../errors/faker-error';
33
import type { Faker } from '../../faker';
4+
import { assertLocaleData } from '../../internal/assert-locale-data';
45
import { toDate } from '../../internal/date';
5-
import { assertLocaleData } from '../../internal/locale-proxy';
66
import { SimpleModuleBase } from '../../internal/module-base';
77

88
/**
@@ -657,9 +657,9 @@ export class DateModule extends SimpleDateModule {
657657
type = useContext ? 'wide_context' : 'wide';
658658
}
659659

660-
const values = source[type];
661-
assertLocaleData(values, 'date.month', type);
662-
return this.faker.helpers.arrayElement(values);
660+
return this.faker.helpers.arrayElement(
661+
assertLocaleData(source[type], 'date.month', type)
662+
);
663663
}
664664

665665
/**
@@ -709,9 +709,9 @@ export class DateModule extends SimpleDateModule {
709709
type = useContext ? 'wide_context' : 'wide';
710710
}
711711

712-
const values = source[type];
713-
assertLocaleData(values, 'date.weekday', type);
714-
return this.faker.helpers.arrayElement(values);
712+
return this.faker.helpers.arrayElement(
713+
assertLocaleData(source[type], 'date.weekday', type)
714+
);
715715
}
716716

717717
/**

0 commit comments

Comments
 (0)