diff options
| -rw-r--r-- | src/faker.ts | 49 | ||||
| -rw-r--r-- | test/faker.spec.ts | 21 |
2 files changed, 44 insertions, 26 deletions
diff --git a/src/faker.ts b/src/faker.ts index 142009e1..1ace8501 100644 --- a/src/faker.ts +++ b/src/faker.ts @@ -102,41 +102,38 @@ export class Faker { } /** - * Load the definitions contained in the locales file for the given types + * Load the definitions contained in the locales file for the given types. + * + * Background: Certain localization sets contain less data then others. + * In the case of a missing definition, use the localeFallback's values + * to substitute the missing data. */ private loadDefinitions(): void { // TODO @Shinigami92 2022-01-11: Find a way to load this even more dynamically // In a way so that we don't accidentally miss a definition - Object.entries(DEFINITIONS).forEach(([t, v]) => { - if (this.definitions[t] == null) { - this.definitions[t] = {}; + for (const [moduleName, entryNames] of Object.entries(DEFINITIONS)) { + if (typeof entryNames === 'string') { + // For 'title' and 'separator' + Object.defineProperty(this.definitions, moduleName, { + get: (): unknown /* string */ => + this.locales[this.locale][moduleName] ?? + this.locales[this.localeFallback][moduleName], + }); + continue; } - if (typeof v === 'string') { - this.definitions[t] = v; - return; + if (this.definitions[moduleName] == null) { + this.definitions[moduleName] = {}; } - v.forEach((p) => { - Object.defineProperty(this.definitions[t], p, { - get: () => { - if ( - this.locales[this.locale][t] == null || - this.locales[this.locale][t][p] == null - ) { - // certain localization sets contain less data then others. - // in the case of a missing definition, use the default localeFallback - // to substitute the missing set data - // throw new Error('unknown property ' + d + p) - return this.locales[this.localeFallback][t][p]; - } else { - // return localized data - return this.locales[this.locale][t][p]; - } - }, + for (const entryName of entryNames) { + Object.defineProperty(this.definitions[moduleName], entryName, { + get: (): unknown => + this.locales[this.locale][moduleName]?.[entryName] ?? + this.locales[this.localeFallback][moduleName]?.[entryName], }); - }); - }); + } + } } seed(seed?: number | number[]): void { diff --git a/test/faker.spec.ts b/test/faker.spec.ts index 283cf999..24486b48 100644 --- a/test/faker.spec.ts +++ b/test/faker.spec.ts @@ -28,6 +28,27 @@ describe('faker', () => { ); }); + describe('title', () => { + it.each(Object.keys(faker.locales))('title (%s)', (locale) => { + faker.locale = locale; + expect(faker.definitions.title).toBe(faker.locales[locale].title); + }); + }); + + describe('separator', () => { + it.each(Object.keys(faker.locales))('separator (%s)', (locale) => { + faker.locale = locale; + expect(faker.definitions.separator).toBeTypeOf('string'); + }); + + it('separator (with fallback)', () => { + // Use a language that doesn't have a separator specified + expect(faker.locales['en_US'].separator).toBeUndefined(); + // Check that the fallback works + expect(faker.definitions.separator).toBe(faker.locales['en'].separator); + }); + }); + // This is only here for coverage // The actual test is in mersenne.spec.ts describe('seed()', () => { |
