Skip to content

chore: improve readability and type safety for loadDefinitions #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions src/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions test/faker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down