Skip to content

Commit

Permalink
chore: improve readability and type safety for loadDefinitions (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Mar 28, 2022
1 parent 0f74908 commit 94c96ba
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
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

0 comments on commit 94c96ba

Please sign in to comment.