diff --git a/src/internal/assert-locale-data.ts b/src/internal/assert-locale-data.ts new file mode 100644 index 00000000000..5bac7d92c65 --- /dev/null +++ b/src/internal/assert-locale-data.ts @@ -0,0 +1,32 @@ +import { FakerError } from '../errors/faker-error'; + +/** + * Checks that the value is not null or undefined and throws an error if it is. + * + * @param value The value to check. + * @param path The path to the locale data. + * + * @returns The value if it's not null or undefined. + * + * @throws {FakerError} If the value is null or undefined. + */ +export function assertLocaleData( + value: T, + ...path: string[] +): NonNullable { + if (value === null) { + throw new FakerError( + `The locale data for '${path.join('.')}' aren't applicable to this locale. + If you think this is a bug, please report it at: https://github.com/faker-js/faker` + ); + } else if (value === undefined) { + throw new FakerError( + `The locale data for '${path.join('.')}' are missing in this locale. + If this is a custom Faker instance, please make sure all required locales are used e.g. '[de_AT, de, en, base]'. + Please contribute the missing data to the project or use a locale/Faker instance that has these data. + For more information see https://fakerjs.dev/guide/localization.html` + ); + } + + return value; +} diff --git a/src/internal/locale-proxy.ts b/src/internal/locale-proxy.ts index 3e77a93a4cd..fd301ab298d 100644 --- a/src/internal/locale-proxy.ts +++ b/src/internal/locale-proxy.ts @@ -1,5 +1,6 @@ import type { LocaleDefinition } from '../definitions'; import { FakerError } from '../errors/faker-error'; +import { assertLocaleData } from './assert-locale-data'; /** * 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 { }) as LocaleProxy; } -/** - * Checks that the value is not null or undefined and throws an error if it is. - * - * @param value The value to check. - * @param path The path to the locale data. - */ -export function assertLocaleData( - value: T, - ...path: string[] -): asserts value is NonNullable { - if (value === null) { - throw new FakerError( - `The locale data for '${path.join('.')}' aren't applicable to this locale. - If you think this is a bug, please report it at: https://github.com/faker-js/faker` - ); - } else if (value === undefined) { - throw new FakerError( - `The locale data for '${path.join('.')}' are missing in this locale. - If this is a custom Faker instance, please make sure all required locales are used e.g. '[de_AT, de, en, base]'. - Please contribute the missing data to the project or use a locale/Faker instance that has these data. - For more information see https://fakerjs.dev/guide/localization.html` - ); - } -} - /** * Creates a proxy for a category that throws an error when accessing an undefined property. * @@ -106,8 +82,7 @@ function createCategoryProxy< return value; } - assertLocaleData(value, categoryName, entryName.toString()); - return value; + return assertLocaleData(value, categoryName, entryName.toString()); }, set: throwReadOnlyError, diff --git a/src/modules/airline/index.ts b/src/modules/airline/index.ts index e8ac47c58c2..258401d1f10 100644 --- a/src/modules/airline/index.ts +++ b/src/modules/airline/index.ts @@ -4,6 +4,7 @@ * responsible for setting standards relating to many aspects of airline * operations. */ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; export enum Aircraft { @@ -88,7 +89,10 @@ export class AirlineModule extends ModuleBase { */ airport(): Airport { return this.faker.helpers.arrayElement( - this.faker.definitions.airline.airport + assertLocaleData( + this.faker.fakerCore.locale.airline?.airport, + 'airline.airport' + ) ); } @@ -102,7 +106,10 @@ export class AirlineModule extends ModuleBase { */ airline(): Airline { return this.faker.helpers.arrayElement( - this.faker.definitions.airline.airline + assertLocaleData( + this.faker.fakerCore.locale.airline?.airline, + 'airline.airline' + ) ); } @@ -116,7 +123,10 @@ export class AirlineModule extends ModuleBase { */ airplane(): Airplane { return this.faker.helpers.arrayElement( - this.faker.definitions.airline.airplane + assertLocaleData( + this.faker.fakerCore.locale.airline?.airplane, + 'airline.airplane' + ) ); } diff --git a/src/modules/animal/index.ts b/src/modules/animal/index.ts index bb200a31c0a..e81950f44b5 100644 --- a/src/modules/animal/index.ts +++ b/src/modules/animal/index.ts @@ -1,3 +1,4 @@ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; /** @@ -21,7 +22,9 @@ export class AnimalModule extends ModuleBase { * @since 5.5.0 */ dog(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.animal.dog); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.animal?.dog, 'animal.dog') + ); } /** @@ -33,7 +36,9 @@ export class AnimalModule extends ModuleBase { * @since 5.5.0 */ cat(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.animal.cat); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.animal?.cat, 'animal.cat') + ); } /** @@ -45,7 +50,12 @@ export class AnimalModule extends ModuleBase { * @since 5.5.0 */ snake(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.animal.snake); + return this.faker.helpers.arrayElement( + assertLocaleData( + this.faker.fakerCore.locale.animal?.snake, + 'animal.snake' + ) + ); } /** @@ -57,7 +67,9 @@ export class AnimalModule extends ModuleBase { * @since 5.5.0 */ bear(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.animal.bear); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.animal?.bear, 'animal.bear') + ); } /** @@ -69,7 +81,9 @@ export class AnimalModule extends ModuleBase { * @since 5.5.0 */ lion(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.animal.lion); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.animal?.lion, 'animal.lion') + ); } /** @@ -82,7 +96,10 @@ export class AnimalModule extends ModuleBase { */ cetacean(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.animal.cetacean + assertLocaleData( + this.faker.fakerCore.locale.animal?.cetacean, + 'animal.cetacean' + ) ); } @@ -95,7 +112,12 @@ export class AnimalModule extends ModuleBase { * @since 5.5.0 */ horse(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.animal.horse); + return this.faker.helpers.arrayElement( + assertLocaleData( + this.faker.fakerCore.locale.animal?.horse, + 'animal.horse' + ) + ); } /** @@ -107,7 +129,9 @@ export class AnimalModule extends ModuleBase { * @since 5.5.0 */ bird(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.animal.bird); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.animal?.bird, 'animal.bird') + ); } /** @@ -119,7 +143,9 @@ export class AnimalModule extends ModuleBase { * @since 5.5.0 */ cow(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.animal.cow); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.animal?.cow, 'animal.cow') + ); } /** @@ -131,7 +157,9 @@ export class AnimalModule extends ModuleBase { * @since 5.5.0 */ fish(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.animal.fish); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.animal?.fish, 'animal.fish') + ); } /** @@ -144,7 +172,10 @@ export class AnimalModule extends ModuleBase { */ crocodilia(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.animal.crocodilia + assertLocaleData( + this.faker.fakerCore.locale.animal?.crocodilia, + 'animal.crocodilia' + ) ); } @@ -158,7 +189,10 @@ export class AnimalModule extends ModuleBase { */ insect(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.animal.insect + assertLocaleData( + this.faker.fakerCore.locale.animal?.insect, + 'animal.insect' + ) ); } @@ -172,7 +206,10 @@ export class AnimalModule extends ModuleBase { */ rabbit(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.animal.rabbit + assertLocaleData( + this.faker.fakerCore.locale.animal?.rabbit, + 'animal.rabbit' + ) ); } @@ -186,7 +223,10 @@ export class AnimalModule extends ModuleBase { */ rodent(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.animal.rodent + assertLocaleData( + this.faker.fakerCore.locale.animal?.rodent, + 'animal.rodent' + ) ); } @@ -199,7 +239,9 @@ export class AnimalModule extends ModuleBase { * @since 5.5.0 */ type(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.animal.type); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.animal?.type, 'animal.type') + ); } /** @@ -212,7 +254,10 @@ export class AnimalModule extends ModuleBase { */ petName(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.animal.pet_name + assertLocaleData( + this.faker.fakerCore.locale.animal?.pet_name, + 'animal.pet_name' + ) ); } } diff --git a/src/modules/book/index.ts b/src/modules/book/index.ts index 86e168d2ad4..1b00b9a4bd0 100644 --- a/src/modules/book/index.ts +++ b/src/modules/book/index.ts @@ -1,3 +1,4 @@ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; /** @@ -26,7 +27,9 @@ export class BookModule extends ModuleBase { * @since 9.1.0 */ author(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.book.author); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.book?.author, 'book.author') + ); } /** @@ -38,7 +41,9 @@ export class BookModule extends ModuleBase { * @since 9.1.0 */ format(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.book.format); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.book?.format, 'book.format') + ); } /** @@ -50,7 +55,9 @@ export class BookModule extends ModuleBase { * @since 9.1.0 */ genre(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.book.genre); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.book?.genre, 'book.genre') + ); } /** @@ -63,7 +70,10 @@ export class BookModule extends ModuleBase { */ publisher(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.book.publisher + assertLocaleData( + this.faker.fakerCore.locale.book?.publisher, + 'book.publisher' + ) ); } @@ -76,7 +86,9 @@ export class BookModule extends ModuleBase { * @since 9.1.0 */ series(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.book.series); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.book?.series, 'book.series') + ); } /** @@ -88,6 +100,8 @@ export class BookModule extends ModuleBase { * @since 9.1.0 */ title(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.book.title); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.book?.title, 'book.title') + ); } } diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 0bfdc8858b7..72c626d2e9e 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -1,3 +1,4 @@ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; /** @@ -217,7 +218,9 @@ export class ColorModule extends ModuleBase { * @since 7.0.0 */ human(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.color.human); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.color?.human, 'color.human') + ); } /** @@ -230,7 +233,9 @@ export class ColorModule extends ModuleBase { * @since 7.0.0 */ space(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.color.space); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.color?.space, 'color.space') + ); } /** diff --git a/src/modules/commerce/index.ts b/src/modules/commerce/index.ts index ef27c161e2b..8304a18758e 100644 --- a/src/modules/commerce/index.ts +++ b/src/modules/commerce/index.ts @@ -1,4 +1,5 @@ import { FakerError } from '../../errors/faker-error'; +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; import { calculateUPCCheckDigit } from './upc-check-digit'; @@ -99,7 +100,10 @@ export class CommerceModule extends ModuleBase { */ department(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.commerce.department + assertLocaleData( + this.faker.fakerCore.locale.commerce?.department, + 'commerce.department' + ) ); } @@ -112,7 +116,10 @@ export class CommerceModule extends ModuleBase { * @since 3.0.0 */ productName(): string { - const patterns = this.faker.definitions.commerce.product_name.pattern; + const patterns = assertLocaleData( + this.faker.fakerCore.locale.commerce?.product_name, + 'commerce.product_name' + ).pattern; return this.faker.helpers.fake(patterns); } @@ -222,7 +229,10 @@ export class CommerceModule extends ModuleBase { */ productAdjective(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.commerce.product_name.adjective + assertLocaleData( + this.faker.fakerCore.locale.commerce?.product_name, + 'commerce.product_name' + ).adjective ); } @@ -236,7 +246,10 @@ export class CommerceModule extends ModuleBase { */ productMaterial(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.commerce.product_name.material + assertLocaleData( + this.faker.fakerCore.locale.commerce?.product_name, + 'commerce.product_name' + ).material ); } @@ -250,7 +263,10 @@ export class CommerceModule extends ModuleBase { */ product(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.commerce.product_name.product + assertLocaleData( + this.faker.fakerCore.locale.commerce?.product_name, + 'commerce.product_name' + ).product ); } @@ -264,7 +280,10 @@ export class CommerceModule extends ModuleBase { */ productDescription(): string { return this.faker.helpers.fake( - this.faker.definitions.commerce.product_description + assertLocaleData( + this.faker.fakerCore.locale.commerce?.product_description, + 'commerce.product_description' + ) ); } diff --git a/src/modules/company/index.ts b/src/modules/company/index.ts index b4e8befd79a..3451910bb4d 100644 --- a/src/modules/company/index.ts +++ b/src/modules/company/index.ts @@ -1,3 +1,4 @@ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; /** @@ -24,7 +25,12 @@ export class CompanyModule extends ModuleBase { * @since 7.4.0 */ name(): string { - return this.faker.helpers.fake(this.faker.definitions.company.name_pattern); + return this.faker.helpers.fake( + assertLocaleData( + this.faker.fakerCore.locale.company?.name_pattern, + 'company.name_pattern' + ) + ); } /** @@ -65,7 +71,10 @@ export class CompanyModule extends ModuleBase { */ catchPhraseAdjective(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.company.adjective + assertLocaleData( + this.faker.fakerCore.locale.company?.adjective, + 'company.adjective' + ) ); } @@ -79,7 +88,10 @@ export class CompanyModule extends ModuleBase { */ catchPhraseDescriptor(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.company.descriptor + assertLocaleData( + this.faker.fakerCore.locale.company?.descriptor, + 'company.descriptor' + ) ); } @@ -92,7 +104,12 @@ export class CompanyModule extends ModuleBase { * @since 2.0.1 */ catchPhraseNoun(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.company.noun); + return this.faker.helpers.arrayElement( + assertLocaleData( + this.faker.fakerCore.locale.company?.noun, + 'company.noun' + ) + ); } /** @@ -105,7 +122,10 @@ export class CompanyModule extends ModuleBase { */ buzzAdjective(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.company.buzz_adjective + assertLocaleData( + this.faker.fakerCore.locale.company?.buzz_adjective, + 'company.buzz_adjective' + ) ); } @@ -119,7 +139,10 @@ export class CompanyModule extends ModuleBase { */ buzzVerb(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.company.buzz_verb + assertLocaleData( + this.faker.fakerCore.locale.company?.buzz_verb, + 'company.buzz_verb' + ) ); } @@ -133,7 +156,10 @@ export class CompanyModule extends ModuleBase { */ buzzNoun(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.company.buzz_noun + assertLocaleData( + this.faker.fakerCore.locale.company?.buzz_noun, + 'company.buzz_noun' + ) ); } } diff --git a/src/modules/database/index.ts b/src/modules/database/index.ts index 6c3595cc4fc..4858522c185 100644 --- a/src/modules/database/index.ts +++ b/src/modules/database/index.ts @@ -1,3 +1,4 @@ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; /** @@ -20,7 +21,10 @@ export class DatabaseModule extends ModuleBase { */ column(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.database.column + assertLocaleData( + this.faker.fakerCore.locale.database?.column, + 'database.column' + ) ); } @@ -34,7 +38,10 @@ export class DatabaseModule extends ModuleBase { */ type(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.database.type + assertLocaleData( + this.faker.fakerCore.locale.database?.type, + 'database.type' + ) ); } @@ -48,7 +55,10 @@ export class DatabaseModule extends ModuleBase { */ collation(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.database.collation + assertLocaleData( + this.faker.fakerCore.locale.database?.collation, + 'database.collation' + ) ); } @@ -62,7 +72,10 @@ export class DatabaseModule extends ModuleBase { */ engine(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.database.engine + assertLocaleData( + this.faker.fakerCore.locale.database?.engine, + 'database.engine' + ) ); } diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index 1bea401cbeb..ed47b7e6dc3 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -1,8 +1,8 @@ import type { DateEntryDefinition } from '../../definitions'; import { FakerError } from '../../errors/faker-error'; import type { Faker } from '../../faker'; +import { assertLocaleData } from '../../internal/assert-locale-data'; import { toDate } from '../../internal/date'; -import { assertLocaleData } from '../../internal/locale-proxy'; import { SimpleModuleBase } from '../../internal/module-base'; /** @@ -647,7 +647,10 @@ export class DateModule extends SimpleDateModule { ): string { const { abbreviated = false, context = false } = options; - const source = this.faker.definitions.date.month; + const source = assertLocaleData( + this.faker.fakerCore.locale.date?.month, + 'date.month' + ); let type: keyof DateEntryDefinition; if (abbreviated) { const useContext = context && source['abbr_context'] != null; @@ -657,9 +660,9 @@ export class DateModule extends SimpleDateModule { type = useContext ? 'wide_context' : 'wide'; } - const values = source[type]; - assertLocaleData(values, 'date.month', type); - return this.faker.helpers.arrayElement(values); + return this.faker.helpers.arrayElement( + assertLocaleData(source[type], 'date.month', type) + ); } /** @@ -699,7 +702,10 @@ export class DateModule extends SimpleDateModule { ): string { const { abbreviated = false, context = false } = options; - const source = this.faker.definitions.date.weekday; + const source = assertLocaleData( + this.faker.fakerCore.locale.date?.weekday, + 'date.weekday' + ); let type: keyof DateEntryDefinition; if (abbreviated) { const useContext = context && source['abbr_context'] != null; @@ -709,9 +715,9 @@ export class DateModule extends SimpleDateModule { type = useContext ? 'wide_context' : 'wide'; } - const values = source[type]; - assertLocaleData(values, 'date.weekday', type); - return this.faker.helpers.arrayElement(values); + return this.faker.helpers.arrayElement( + assertLocaleData(source[type], 'date.weekday', type) + ); } /** @@ -729,7 +735,10 @@ export class DateModule extends SimpleDateModule { */ timeZone(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.date.time_zone + assertLocaleData( + this.faker.fakerCore.locale.date?.time_zone, + 'date.time_zone' + ) ); } } diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index 5139b1f3124..0688a0c2bdb 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -1,4 +1,5 @@ import { FakerError } from '../../errors/faker-error'; +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; import type { BitcoinAddressFamilyType, BitcoinNetworkType } from './bitcoin'; import { @@ -173,7 +174,10 @@ export class FinanceModule extends ModuleBase { accountName(): string { return [ this.faker.helpers.arrayElement( - this.faker.definitions.finance.account_type + assertLocaleData( + this.faker.fakerCore.locale.finance?.account_type, + 'finance.account_type' + ) ), 'Account', ].join(' '); @@ -189,7 +193,10 @@ export class FinanceModule extends ModuleBase { */ routingNumber(): string { const federalReserveRoutingSymbol = this.faker.helpers.arrayElement( - this.faker.definitions.finance.federal_reserve_routing_symbol + assertLocaleData( + this.faker.fakerCore.locale.finance?.federal_reserve_routing_symbol, + 'finance.federal_reserve_routing_symbol' + ) ); const institutionIdentifier = this.faker.string.numeric({ @@ -297,7 +304,10 @@ export class FinanceModule extends ModuleBase { */ transactionType(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.finance.transaction_type + assertLocaleData( + this.faker.fakerCore.locale.finance?.transaction_type, + 'finance.transaction_type' + ) ); } @@ -316,7 +326,10 @@ export class FinanceModule extends ModuleBase { */ currency(): Currency { return this.faker.helpers.arrayElement( - this.faker.definitions.finance.currency + assertLocaleData( + this.faker.fakerCore.locale.finance?.currency, + 'finance.currency' + ) ); } @@ -536,7 +549,10 @@ export class FinanceModule extends ModuleBase { const { issuer = '' } = options; let format: string; - const localeFormat = this.faker.definitions.finance.credit_card; + const localeFormat = assertLocaleData( + this.faker.fakerCore.locale.finance?.credit_card, + 'finance.credit_card' + ); const normalizedIssuer = issuer.toLowerCase(); if (normalizedIssuer in localeFormat) { format = this.faker.helpers.arrayElement(localeFormat[normalizedIssuer]); @@ -576,7 +592,10 @@ export class FinanceModule extends ModuleBase { */ creditCardIssuer(): string { return this.faker.helpers.objectKey( - this.faker.definitions.finance.credit_card + assertLocaleData( + this.faker.fakerCore.locale.finance?.credit_card, + 'finance.credit_card' + ) ) as string; } @@ -852,7 +871,10 @@ export class FinanceModule extends ModuleBase { */ transactionDescription(): string { return this.faker.helpers.fake( - this.faker.definitions.finance.transaction_description_pattern + assertLocaleData( + this.faker.fakerCore.locale.finance?.transaction_description_pattern, + 'finance.transaction_description_pattern' + ) ); } } diff --git a/src/modules/food/index.ts b/src/modules/food/index.ts index 8dd77741282..3b8cd3923b5 100644 --- a/src/modules/food/index.ts +++ b/src/modules/food/index.ts @@ -1,3 +1,4 @@ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; /** @@ -32,7 +33,10 @@ export class FoodModule extends ModuleBase { */ adjective(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.food.adjective + assertLocaleData( + this.faker.fakerCore.locale.food?.adjective, + 'food.adjective' + ) ); } @@ -46,7 +50,10 @@ export class FoodModule extends ModuleBase { */ description(): string { return this.faker.helpers.fake( - this.faker.definitions.food.description_pattern + assertLocaleData( + this.faker.fakerCore.locale.food?.description_pattern, + 'food.description_pattern' + ) ); } @@ -62,12 +69,19 @@ export class FoodModule extends ModuleBase { // A 50/50 mix of specific dishes and dish_patterns if (this.faker.datatype.boolean()) { return toTitleCase( - this.faker.helpers.fake(this.faker.definitions.food.dish_pattern) + this.faker.helpers.fake( + assertLocaleData( + this.faker.fakerCore.locale.food?.dish_pattern, + 'food.dish_pattern' + ) + ) ); } return toTitleCase( - this.faker.helpers.arrayElement(this.faker.definitions.food.dish) + this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.food?.dish, 'food.dish') + ) ); } @@ -81,7 +95,10 @@ export class FoodModule extends ModuleBase { */ ethnicCategory(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.food.ethnic_category + assertLocaleData( + this.faker.fakerCore.locale.food?.ethnic_category, + 'food.ethnic_category' + ) ); } @@ -94,7 +111,9 @@ export class FoodModule extends ModuleBase { * @since 9.0.0 */ fruit(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.food.fruit); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.food?.fruit, 'food.fruit') + ); } /** @@ -107,7 +126,10 @@ export class FoodModule extends ModuleBase { */ ingredient(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.food.ingredient + assertLocaleData( + this.faker.fakerCore.locale.food?.ingredient, + 'food.ingredient' + ) ); } @@ -120,7 +142,9 @@ export class FoodModule extends ModuleBase { * @since 9.0.0 */ meat(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.food.meat); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.food?.meat, 'food.meat') + ); } /** @@ -132,7 +156,9 @@ export class FoodModule extends ModuleBase { * @since 9.0.0 */ spice(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.food.spice); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.food?.spice, 'food.spice') + ); } /** @@ -145,7 +171,10 @@ export class FoodModule extends ModuleBase { */ vegetable(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.food.vegetable + assertLocaleData( + this.faker.fakerCore.locale.food?.vegetable, + 'food.vegetable' + ) ); } } diff --git a/src/modules/hacker/index.ts b/src/modules/hacker/index.ts index 500f2f907c8..f842df32556 100644 --- a/src/modules/hacker/index.ts +++ b/src/modules/hacker/index.ts @@ -1,3 +1,4 @@ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; /** @@ -26,7 +27,10 @@ export class HackerModule extends ModuleBase { */ abbreviation(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.hacker.abbreviation + assertLocaleData( + this.faker.fakerCore.locale.hacker?.abbreviation, + 'hacker.abbreviation' + ) ); } @@ -40,7 +44,10 @@ export class HackerModule extends ModuleBase { */ adjective(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.hacker.adjective + assertLocaleData( + this.faker.fakerCore.locale.hacker?.adjective, + 'hacker.adjective' + ) ); } @@ -53,7 +60,9 @@ export class HackerModule extends ModuleBase { * @since 2.0.1 */ noun(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.hacker.noun); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.hacker?.noun, 'hacker.noun') + ); } /** @@ -65,7 +74,9 @@ export class HackerModule extends ModuleBase { * @since 2.0.1 */ verb(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.hacker.verb); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.hacker?.verb, 'hacker.verb') + ); } /** @@ -78,7 +89,10 @@ export class HackerModule extends ModuleBase { */ ingverb(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.hacker.ingverb + assertLocaleData( + this.faker.fakerCore.locale.hacker?.ingverb, + 'hacker.ingverb' + ) ); } @@ -92,6 +106,11 @@ export class HackerModule extends ModuleBase { * @since 2.0.1 */ phrase(): string { - return this.faker.helpers.fake(this.faker.definitions.hacker.phrase); + return this.faker.helpers.fake( + assertLocaleData( + this.faker.fakerCore.locale.hacker?.phrase, + 'hacker.phrase' + ) + ); } } diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index fb95988d32e..ab063b56d67 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -1,5 +1,6 @@ import { FakerError } from '../../errors/faker-error'; import type { Faker } from '../../faker'; +import { assertLocaleData } from '../../internal/assert-locale-data'; import { toBase64Url } from '../../internal/base64'; import { ModuleBase } from '../../internal/module-base'; import { charMapping } from './char-mappings'; @@ -197,7 +198,10 @@ export class InternetModule extends ModuleBase { firstName, lastName, provider = this.faker.helpers.arrayElement( - this.faker.definitions.internet.free_email + assertLocaleData( + this.faker.fakerCore.locale.internet?.free_email, + 'internet.free_email' + ) ), allowSpecialCharacters = false, } = options; @@ -272,7 +276,10 @@ export class InternetModule extends ModuleBase { const { firstName, lastName, allowSpecialCharacters = false } = options; const provider = this.faker.helpers.arrayElement( - this.faker.definitions.internet.example_email + assertLocaleData( + this.faker.fakerCore.locale.internet?.example_email, + 'internet.example_email' + ) ); return this.email({ @@ -492,12 +499,18 @@ export class InternetModule extends ModuleBase { ): number { const { types = Object.keys( - this.faker.definitions.internet.http_status_code + assertLocaleData( + this.faker.fakerCore.locale.internet?.http_status_code, + 'internet.http_status_code' + ) ) as HTTPStatusCodeType[], } = options; const httpStatusCodeType = this.faker.helpers.arrayElement(types); return this.faker.helpers.arrayElement( - this.faker.definitions.internet.http_status_code[httpStatusCodeType] + assertLocaleData( + this.faker.fakerCore.locale.internet?.http_status_code, + 'internet.http_status_code' + )[httpStatusCodeType] ); } @@ -559,7 +572,10 @@ export class InternetModule extends ModuleBase { */ domainSuffix(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.internet.domain_suffix + assertLocaleData( + this.faker.fakerCore.locale.internet?.domain_suffix, + 'internet.domain_suffix' + ) ); } @@ -737,7 +753,10 @@ export class InternetModule extends ModuleBase { */ userAgent(): string { return this.faker.helpers.fake( - this.faker.definitions.internet.user_agent_pattern + assertLocaleData( + this.faker.fakerCore.locale.internet?.user_agent_pattern, + 'internet.user_agent_pattern' + ) ); } @@ -946,11 +965,19 @@ export class InternetModule extends ModuleBase { } = {} ): string { const { - types = Object.keys(this.faker.definitions.internet.emoji) as EmojiType[], + types = Object.keys( + assertLocaleData( + this.faker.fakerCore.locale.internet?.emoji, + 'internet.emoji' + ) + ) as EmojiType[], } = options; const emojiType = this.faker.helpers.arrayElement(types); return this.faker.helpers.arrayElement( - this.faker.definitions.internet.emoji[emojiType] + assertLocaleData( + this.faker.fakerCore.locale.internet?.emoji, + 'internet.emoji' + )[emojiType] ); } @@ -967,7 +994,10 @@ export class InternetModule extends ModuleBase { */ jwtAlgorithm(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.internet.jwt_algorithm + assertLocaleData( + this.faker.fakerCore.locale.internet?.jwt_algorithm, + 'internet.jwt_algorithm' + ) ); } diff --git a/src/modules/location/index.ts b/src/modules/location/index.ts index 98bd7879c6c..2556d2d4da1 100644 --- a/src/modules/location/index.ts +++ b/src/modules/location/index.ts @@ -1,5 +1,6 @@ import { FakerError } from '../../errors/faker-error'; import type { Faker } from '../../faker'; +import { assertLocaleData } from '../../internal/assert-locale-data'; import { SimpleModuleBase } from '../../internal/module-base'; /** @@ -256,8 +257,10 @@ export class LocationModule extends SimpleLocationModule { const { state } = options; if (state != null) { - const zipPattern = - this.faker.definitions.location.postcode_by_state[state]; + const zipPattern = assertLocaleData( + this.faker.fakerCore.locale.location?.postcode_by_state, + 'location.postcode_by_state' + )[state]; if (zipPattern == null) { throw new FakerError( @@ -268,7 +271,12 @@ export class LocationModule extends SimpleLocationModule { return this.faker.helpers.fake(zipPattern); } - let { format = this.faker.definitions.location.postcode } = options; + let { + format = assertLocaleData( + this.faker.fakerCore.locale.location?.postcode, + 'location.postcode' + ), + } = options; if (typeof format === 'string') { format = [format]; } @@ -289,7 +297,10 @@ export class LocationModule extends SimpleLocationModule { */ city(): string { return this.faker.helpers.fake( - this.faker.definitions.location.city_pattern + assertLocaleData( + this.faker.fakerCore.locale.location?.city_pattern, + 'location.city_pattern' + ) ); } @@ -303,7 +314,12 @@ export class LocationModule extends SimpleLocationModule { */ buildingNumber(): string { return this.faker.helpers - .arrayElement(this.faker.definitions.location.building_number) + .arrayElement( + assertLocaleData( + this.faker.fakerCore.locale.location?.building_number, + 'location.building_number' + ) + ) .replaceAll(/#+/g, (m) => this.faker.string.numeric({ length: m.length, @@ -322,7 +338,10 @@ export class LocationModule extends SimpleLocationModule { */ street(): string { return this.faker.helpers.fake( - this.faker.definitions.location.street_pattern + assertLocaleData( + this.faker.fakerCore.locale.location?.street_pattern, + 'location.street_pattern' + ) ); } @@ -358,7 +377,10 @@ export class LocationModule extends SimpleLocationModule { const { useFullAddress } = options; - const formats = this.faker.definitions.location.street_address; + const formats = assertLocaleData( + this.faker.fakerCore.locale.location?.street_address, + 'location.street_address' + ); const format = formats[useFullAddress ? 'full' : 'normal']; return this.faker.helpers.fake(format); @@ -392,7 +414,10 @@ export class LocationModule extends SimpleLocationModule { */ postalAddress(): string { return this.faker.helpers.fake( - this.faker.definitions.location.postal_address + assertLocaleData( + this.faker.fakerCore.locale.location?.postal_address, + 'location.postal_address' + ) ); } @@ -407,7 +432,12 @@ export class LocationModule extends SimpleLocationModule { */ secondaryAddress(): string { return this.faker.helpers - .fake(this.faker.definitions.location.secondary_address) + .fake( + assertLocaleData( + this.faker.fakerCore.locale.location?.secondary_address, + 'location.secondary_address' + ) + ) .replaceAll(/#+/g, (m) => this.faker.string.numeric({ length: m.length, @@ -427,7 +457,10 @@ export class LocationModule extends SimpleLocationModule { */ county(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.location.county + assertLocaleData( + this.faker.fakerCore.locale.location?.county, + 'location.county' + ) ); } @@ -441,7 +474,10 @@ export class LocationModule extends SimpleLocationModule { */ country(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.location.country + assertLocaleData( + this.faker.fakerCore.locale.location?.country, + 'location.country' + ) ); } @@ -455,7 +491,10 @@ export class LocationModule extends SimpleLocationModule { */ continent(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.location.continent + assertLocaleData( + this.faker.fakerCore.locale.location?.continent, + 'location.continent' + ) ); } @@ -518,7 +557,10 @@ export class LocationModule extends SimpleLocationModule { })(); return this.faker.helpers.arrayElement( - this.faker.definitions.location.country_code + assertLocaleData( + this.faker.fakerCore.locale.location?.country_code, + 'location.country_code' + ) )[key]; } @@ -553,8 +595,14 @@ export class LocationModule extends SimpleLocationModule { ): string { const { abbreviated = false } = options; const data = abbreviated - ? this.faker.definitions.location.state_abbr - : this.faker.definitions.location.state; + ? assertLocaleData( + this.faker.fakerCore.locale.location?.state_abbr, + 'location.state_abbr' + ) + : assertLocaleData( + this.faker.fakerCore.locale.location?.state, + 'location.state' + ); return this.faker.helpers.arrayElement(data); } @@ -584,7 +632,10 @@ export class LocationModule extends SimpleLocationModule { } = {} ): string { const { abbreviated = false } = options; - const direction = this.faker.definitions.location.direction; + const direction = assertLocaleData( + this.faker.fakerCore.locale.location?.direction, + 'location.direction' + ); const data = abbreviated ? [...direction.cardinal_abbr, ...direction.ordinal_abbr] : [...direction.cardinal, ...direction.ordinal]; @@ -617,7 +668,10 @@ export class LocationModule extends SimpleLocationModule { } = {} ): string { const { abbreviated = false } = options; - const direction = this.faker.definitions.location.direction; + const direction = assertLocaleData( + this.faker.fakerCore.locale.location?.direction, + 'location.direction' + ); const data = abbreviated ? direction.cardinal_abbr : direction.cardinal; return this.faker.helpers.arrayElement(data); @@ -648,7 +702,10 @@ export class LocationModule extends SimpleLocationModule { } = {} ): string { const { abbreviated = false } = options; - const direction = this.faker.definitions.location.direction; + const direction = assertLocaleData( + this.faker.fakerCore.locale.location?.direction, + 'location.direction' + ); const data = abbreviated ? direction.ordinal_abbr : direction.ordinal; return this.faker.helpers.arrayElement(data); @@ -669,7 +726,10 @@ export class LocationModule extends SimpleLocationModule { */ timeZone(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.location.time_zone + assertLocaleData( + this.faker.fakerCore.locale.location?.time_zone, + 'location.time_zone' + ) ); } @@ -690,7 +750,10 @@ export class LocationModule extends SimpleLocationModule { */ language(): Language { return this.faker.helpers.arrayElement( - this.faker.definitions.location.language + assertLocaleData( + this.faker.fakerCore.locale.location?.language, + 'location.language' + ) ); } } diff --git a/src/modules/lorem/index.ts b/src/modules/lorem/index.ts index 880fb1a61c6..8b83ed33d96 100644 --- a/src/modules/lorem/index.ts +++ b/src/modules/lorem/index.ts @@ -1,3 +1,4 @@ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; import { filterWordListByLength } from '../word/filter-word-list-by-length'; @@ -82,7 +83,10 @@ export class LoremModule extends ModuleBase { return this.faker.helpers.arrayElement( filterWordListByLength({ ...options, - wordList: this.faker.definitions.lorem.word, + wordList: assertLocaleData( + this.faker.fakerCore.locale.lorem?.word, + 'lorem.word' + ), }) ); } diff --git a/src/modules/music/index.ts b/src/modules/music/index.ts index e3d56715b1c..1a0bcbe107f 100644 --- a/src/modules/music/index.ts +++ b/src/modules/music/index.ts @@ -1,3 +1,4 @@ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; /** @@ -27,7 +28,9 @@ export class MusicModule extends ModuleBase { * @since 9.0.0 */ album(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.music.album); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.music?.album, 'music.album') + ); } /** @@ -39,7 +42,12 @@ export class MusicModule extends ModuleBase { * @since 9.0.0 */ artist(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.music.artist); + return this.faker.helpers.arrayElement( + assertLocaleData( + this.faker.fakerCore.locale.music?.artist, + 'music.artist' + ) + ); } /** @@ -51,7 +59,9 @@ export class MusicModule extends ModuleBase { * @since 5.2.0 */ genre(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.music.genre); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.music?.genre, 'music.genre') + ); } /** @@ -64,7 +74,10 @@ export class MusicModule extends ModuleBase { */ songName(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.music.song_name + assertLocaleData( + this.faker.fakerCore.locale.music?.song_name, + 'music.song_name' + ) ); } } diff --git a/src/modules/person/index.ts b/src/modules/person/index.ts index 0b905632d1d..de28115a865 100644 --- a/src/modules/person/index.ts +++ b/src/modules/person/index.ts @@ -1,5 +1,6 @@ import type { PersonEntryDefinition } from '../../definitions/person'; import type { Faker } from '../../faker'; +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; /** @@ -118,7 +119,10 @@ export class PersonModule extends ModuleBase { selectDefinition( this.faker, sex, - this.faker.definitions.person.first_name + assertLocaleData( + this.faker.fakerCore.locale.person?.first_name, + 'person.first_name' + ) ) ); } @@ -146,7 +150,14 @@ export class PersonModule extends ModuleBase { } return this.faker.helpers.arrayElement( - selectDefinition(this.faker, sex, this.faker.definitions.person.last_name) + selectDefinition( + this.faker, + sex, + assertLocaleData( + this.faker.fakerCore.locale.person?.last_name, + 'person.last_name' + ) + ) ); } @@ -168,7 +179,10 @@ export class PersonModule extends ModuleBase { selectDefinition( this.faker, sex, - this.faker.definitions.person.middle_name + assertLocaleData( + this.faker.fakerCore.locale.person?.middle_name, + 'person.middle_name' + ) ) ); } @@ -219,7 +233,7 @@ export class PersonModule extends ModuleBase { } = options; const fullNamePattern: string = this.faker.helpers.weightedArrayElement( - this.faker.definitions.person.name + assertLocaleData(this.faker.fakerCore.locale.person?.name, 'person.name') ); const fullName = this.faker.helpers.mustache(fullNamePattern, { @@ -244,7 +258,10 @@ export class PersonModule extends ModuleBase { */ gender(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.person.gender + assertLocaleData( + this.faker.fakerCore.locale.person?.gender, + 'person.gender' + ) ); } @@ -263,7 +280,9 @@ export class PersonModule extends ModuleBase { * @since 8.0.0 */ sex(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.person.sex); + return this.faker.helpers.arrayElement( + assertLocaleData(this.faker.fakerCore.locale.person?.sex, 'person.sex') + ); } /** @@ -312,7 +331,12 @@ export class PersonModule extends ModuleBase { * @since 8.0.0 */ bio(): string { - return this.faker.helpers.fake(this.faker.definitions.person.bio_pattern); + return this.faker.helpers.fake( + assertLocaleData( + this.faker.fakerCore.locale.person?.bio_pattern, + 'person.bio_pattern' + ) + ); } /** @@ -329,7 +353,14 @@ export class PersonModule extends ModuleBase { */ prefix(sex?: SexType): string { return this.faker.helpers.arrayElement( - selectDefinition(this.faker, sex, this.faker.definitions.person.prefix) + selectDefinition( + this.faker, + sex, + assertLocaleData( + this.faker.fakerCore.locale.person?.prefix, + 'person.prefix' + ) + ) ); } @@ -344,7 +375,10 @@ export class PersonModule extends ModuleBase { suffix(): string { // TODO @Shinigami92 2022-03-21: Add female_suffix and male_suffix return this.faker.helpers.arrayElement( - this.faker.definitions.person.suffix + assertLocaleData( + this.faker.fakerCore.locale.person?.suffix, + 'person.suffix' + ) ); } @@ -358,7 +392,10 @@ export class PersonModule extends ModuleBase { */ jobTitle(): string { return this.faker.helpers.fake( - this.faker.definitions.person.job_title_pattern + assertLocaleData( + this.faker.fakerCore.locale.person?.job_title_pattern, + 'person.job_title_pattern' + ) ); } @@ -372,7 +409,10 @@ export class PersonModule extends ModuleBase { */ jobDescriptor(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.person.job_descriptor + assertLocaleData( + this.faker.fakerCore.locale.person?.job_descriptor, + 'person.job_descriptor' + ) ); } @@ -386,7 +426,10 @@ export class PersonModule extends ModuleBase { */ jobArea(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.person.job_area + assertLocaleData( + this.faker.fakerCore.locale.person?.job_area, + 'person.job_area' + ) ); } @@ -400,7 +443,10 @@ export class PersonModule extends ModuleBase { */ jobType(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.person.job_type + assertLocaleData( + this.faker.fakerCore.locale.person?.job_type, + 'person.job_type' + ) ); } @@ -414,7 +460,10 @@ export class PersonModule extends ModuleBase { */ zodiacSign(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.person.western_zodiac_sign + assertLocaleData( + this.faker.fakerCore.locale.person?.western_zodiac_sign, + 'person.western_zodiac_sign' + ) ); } } diff --git a/src/modules/phone/index.ts b/src/modules/phone/index.ts index c55bfeed6e7..0a5f2a7cc91 100644 --- a/src/modules/phone/index.ts +++ b/src/modules/phone/index.ts @@ -1,3 +1,4 @@ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; import { legacyReplaceSymbolWithNumber } from '../helpers'; @@ -42,7 +43,10 @@ export class PhoneModule extends ModuleBase { } = {} ): string { const { style = 'human' } = options; - const formats = this.faker.definitions.phone_number.format; + const formats = assertLocaleData( + this.faker.fakerCore.locale.phone_number?.format, + 'phone_number.format' + ); const definitions = formats[style]; if (!definitions) { diff --git a/src/modules/science/index.ts b/src/modules/science/index.ts index 525ff8bf0cc..6d9ea67a2e0 100644 --- a/src/modules/science/index.ts +++ b/src/modules/science/index.ts @@ -1,3 +1,4 @@ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; /** @@ -49,7 +50,10 @@ export class ScienceModule extends ModuleBase { */ chemicalElement(): ChemicalElement { return this.faker.helpers.arrayElement( - this.faker.definitions.science.chemical_element + assertLocaleData( + this.faker.fakerCore.locale.science?.chemical_element, + 'science.chemical_element' + ) ); } @@ -64,6 +68,11 @@ export class ScienceModule extends ModuleBase { * @since 7.2.0 */ unit(): Unit { - return this.faker.helpers.arrayElement(this.faker.definitions.science.unit); + return this.faker.helpers.arrayElement( + assertLocaleData( + this.faker.fakerCore.locale.science?.unit, + 'science.unit' + ) + ); } } diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index cfa9c04e6b6..d4b26736e78 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -1,3 +1,4 @@ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; const commonFileTypes = ['video', 'audio', 'image', 'text', 'application']; @@ -114,7 +115,12 @@ export class SystemModule extends ModuleBase { * @since 3.1.0 */ mimeType(): string { - const mimeTypeKeys = Object.keys(this.faker.definitions.system.mime_type); + const mimeTypeKeys = Object.keys( + assertLocaleData( + this.faker.fakerCore.locale.system?.mime_type, + 'system.mime_type' + ) + ); return this.faker.helpers.arrayElement(mimeTypeKeys); } @@ -152,7 +158,10 @@ export class SystemModule extends ModuleBase { * @since 3.1.0 */ fileType(): string { - const mimeTypes = this.faker.definitions.system.mime_type; + const mimeTypes = assertLocaleData( + this.faker.fakerCore.locale.system?.mime_type, + 'system.mime_type' + ); const typeSet = new Set( Object.keys(mimeTypes).map((key) => key.split('/')[0]) @@ -172,7 +181,10 @@ export class SystemModule extends ModuleBase { * @since 3.1.0 */ fileExt(mimeType?: string): string { - const mimeTypes = this.faker.definitions.system.mime_type; + const mimeTypes = assertLocaleData( + this.faker.fakerCore.locale.system?.mime_type, + 'system.mime_type' + ); if (typeof mimeType === 'string') { return this.faker.helpers.arrayElement(mimeTypes[mimeType].extensions); @@ -193,7 +205,10 @@ export class SystemModule extends ModuleBase { * @since 3.1.0 */ directoryPath(): string { - const paths = this.faker.definitions.system.directory_path; + const paths = assertLocaleData( + this.faker.fakerCore.locale.system?.directory_path, + 'system.directory_path' + ); return this.faker.helpers.arrayElement(paths); } diff --git a/src/modules/vehicle/index.ts b/src/modules/vehicle/index.ts index d032465a42d..616ebf1ded0 100644 --- a/src/modules/vehicle/index.ts +++ b/src/modules/vehicle/index.ts @@ -1,3 +1,4 @@ +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; /** @@ -32,7 +33,10 @@ export class VehicleModule extends ModuleBase { */ manufacturer(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.vehicle.manufacturer + assertLocaleData( + this.faker.fakerCore.locale.vehicle?.manufacturer, + 'vehicle.manufacturer' + ) ); } @@ -46,7 +50,10 @@ export class VehicleModule extends ModuleBase { */ model(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.vehicle.model + assertLocaleData( + this.faker.fakerCore.locale.vehicle?.model, + 'vehicle.model' + ) ); } @@ -59,7 +66,12 @@ export class VehicleModule extends ModuleBase { * @since 5.0.0 */ type(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.vehicle.type); + return this.faker.helpers.arrayElement( + assertLocaleData( + this.faker.fakerCore.locale.vehicle?.type, + 'vehicle.type' + ) + ); } /** @@ -71,7 +83,12 @@ export class VehicleModule extends ModuleBase { * @since 5.0.0 */ fuel(): string { - return this.faker.helpers.arrayElement(this.faker.definitions.vehicle.fuel); + return this.faker.helpers.arrayElement( + assertLocaleData( + this.faker.fakerCore.locale.vehicle?.fuel, + 'vehicle.fuel' + ) + ); } /** @@ -142,7 +159,10 @@ export class VehicleModule extends ModuleBase { */ bicycle(): string { return this.faker.helpers.arrayElement( - this.faker.definitions.vehicle.bicycle_type + assertLocaleData( + this.faker.fakerCore.locale.vehicle?.bicycle_type, + 'vehicle.bicycle_type' + ) ); } } diff --git a/src/modules/word/index.ts b/src/modules/word/index.ts index cf5719b23d6..e7a868a563b 100644 --- a/src/modules/word/index.ts +++ b/src/modules/word/index.ts @@ -1,4 +1,5 @@ import { FakerError } from '../../errors/faker-error'; +import { assertLocaleData } from '../../internal/assert-locale-data'; import { ModuleBase } from '../../internal/module-base'; import { filterWordListByLength } from './filter-word-list-by-length'; @@ -73,7 +74,10 @@ export class WordModule extends ModuleBase { return this.faker.helpers.arrayElement( filterWordListByLength({ ...options, - wordList: this.faker.definitions.word.adjective, + wordList: assertLocaleData( + this.faker.fakerCore.locale.word?.adjective, + 'word.adjective' + ), }) ); } @@ -145,7 +149,10 @@ export class WordModule extends ModuleBase { return this.faker.helpers.arrayElement( filterWordListByLength({ ...options, - wordList: this.faker.definitions.word.adverb, + wordList: assertLocaleData( + this.faker.fakerCore.locale.word?.adverb, + 'word.adverb' + ), }) ); } @@ -217,7 +224,10 @@ export class WordModule extends ModuleBase { return this.faker.helpers.arrayElement( filterWordListByLength({ ...options, - wordList: this.faker.definitions.word.conjunction, + wordList: assertLocaleData( + this.faker.fakerCore.locale.word?.conjunction, + 'word.conjunction' + ), }) ); } @@ -289,7 +299,10 @@ export class WordModule extends ModuleBase { return this.faker.helpers.arrayElement( filterWordListByLength({ ...options, - wordList: this.faker.definitions.word.interjection, + wordList: assertLocaleData( + this.faker.fakerCore.locale.word?.interjection, + 'word.interjection' + ), }) ); } @@ -361,7 +374,10 @@ export class WordModule extends ModuleBase { return this.faker.helpers.arrayElement( filterWordListByLength({ ...options, - wordList: this.faker.definitions.word.noun, + wordList: assertLocaleData( + this.faker.fakerCore.locale.word?.noun, + 'word.noun' + ), }) ); } @@ -433,7 +449,10 @@ export class WordModule extends ModuleBase { return this.faker.helpers.arrayElement( filterWordListByLength({ ...options, - wordList: this.faker.definitions.word.preposition, + wordList: assertLocaleData( + this.faker.fakerCore.locale.word?.preposition, + 'word.preposition' + ), }) ); } @@ -505,7 +524,10 @@ export class WordModule extends ModuleBase { return this.faker.helpers.arrayElement( filterWordListByLength({ ...options, - wordList: this.faker.definitions.word.verb, + wordList: assertLocaleData( + this.faker.fakerCore.locale.word?.verb, + 'word.verb' + ), }) ); }