Skip to content

Commit

Permalink
refactor(locale): use null as not applicable (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Apr 23, 2023
1 parent f795269 commit acb9cf5
Show file tree
Hide file tree
Showing 33 changed files with 88 additions and 76 deletions.
6 changes: 3 additions & 3 deletions src/definitions/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import type { WordDefinitions } from './word';
/**
* Wrapper type for all definition categories that will make all properties optional and allow extra properties.
*/
export type LocaleEntry<T extends Record<string, unknown>> = Partial<T> &
// Unsupported & custom entries
Record<string, unknown>;
export type LocaleEntry<T extends Record<string, unknown>> = {
[P in keyof T]?: T[P] | null;
} & Record<string, unknown>; // Unsupported & custom entries

/**
* The definitions as used by the translations/locales.
Expand Down
40 changes: 20 additions & 20 deletions src/definitions/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ export type PersonDefinitions = LocaleEntry<{
gender: string[];
sex: string[];

prefix?: string[];
female_prefix?: string[];
male_prefix?: string[];
prefix: string[];
female_prefix: string[];
male_prefix: string[];

first_name?: string[];
female_first_name?: string[];
male_first_name?: string[];
first_name: string[];
female_first_name: string[];
male_first_name: string[];

middle_name?: string[];
female_middle_name?: string[];
male_middle_name?: string[];
middle_name: string[];
female_middle_name: string[];
male_middle_name: string[];

last_name?: string[];
female_last_name?: string[];
male_last_name?: string[];
last_name: string[];
female_last_name: string[];
male_last_name: string[];

suffix: string[];

Expand All @@ -33,11 +33,11 @@ export type PersonDefinitions = LocaleEntry<{
/**
* A weighted list of patterns used to generate last names.
*/
last_name_pattern?: Array<{ value: string; weight: number }>;
male_last_name_pattern?: Array<{ value: string; weight: number }>;
female_last_name_pattern?: Array<{ value: string; weight: number }>;
last_name_pattern: Array<{ value: string; weight: number }>;
male_last_name_pattern: Array<{ value: string; weight: number }>;
female_last_name_pattern: Array<{ value: string; weight: number }>;

bio_pattern?: string[];
bio_pattern: string[];

title: PersonTitleDefinitions;

Expand All @@ -47,8 +47,8 @@ export type PersonDefinitions = LocaleEntry<{
/**
* The possible definitions related to people's titles.
*/
export interface PersonTitleDefinitions {
descriptor?: string[];
export type PersonTitleDefinitions = LocaleEntry<{
descriptor: string[];
job: string[];
level?: string[];
}
level: string[];
}>;
2 changes: 1 addition & 1 deletion src/locales/az/company/suffix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/az/location/state.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/az/person/female_prefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/az/person/male_prefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
6 changes: 1 addition & 5 deletions src/locales/az/person/prefix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
import { mergeArrays } from './../../../internal/merge';
import female_prefix from './female_prefix';
import male_prefix from './male_prefix';

export default mergeArrays(female_prefix, male_prefix);
export default null;
2 changes: 1 addition & 1 deletion src/locales/az/person/suffix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/id_ID/person/female_prefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/id_ID/person/male_prefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
6 changes: 1 addition & 5 deletions src/locales/id_ID/person/prefix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
import { mergeArrays } from './../../../internal/merge';
import female_prefix from './female_prefix';
import male_prefix from './male_prefix';

export default mergeArrays(female_prefix, male_prefix);
export default null;
2 changes: 1 addition & 1 deletion src/locales/it/person/suffix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/mk/person/suffix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/pt_BR/location/city_prefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/pt_PT/location/city_prefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/pt_PT/location/city_suffix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/pt_PT/person/suffix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/ru/person/female_prefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/ru/person/male_prefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
6 changes: 1 addition & 5 deletions src/locales/ru/person/prefix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
import { mergeArrays } from './../../../internal/merge';
import female_prefix from './female_prefix';
import male_prefix from './male_prefix';

export default mergeArrays(female_prefix, male_prefix);
export default null;
2 changes: 1 addition & 1 deletion src/locales/ru/person/suffix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/sk/location/state.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/sk/location/state_abbr.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/th/location/city_prefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/th/location/city_suffix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/zh_CN/person/female_prefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/zh_CN/person/male_prefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
6 changes: 1 addition & 5 deletions src/locales/zh_CN/person/prefix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
import { mergeArrays } from './../../../internal/merge';
import female_prefix from './female_prefix';
import male_prefix from './male_prefix';

export default mergeArrays(female_prefix, male_prefix);
export default null;
2 changes: 1 addition & 1 deletion src/locales/zh_TW/person/female_prefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
2 changes: 1 addition & 1 deletion src/locales/zh_TW/person/male_prefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [];
export default null;
6 changes: 1 addition & 5 deletions src/locales/zh_TW/person/prefix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
import { mergeArrays } from './../../../internal/merge';
import female_prefix from './female_prefix';
import male_prefix from './male_prefix';

export default mergeArrays(female_prefix, male_prefix);
export default null;
8 changes: 4 additions & 4 deletions test/person.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('person', () => {
...fakerMK.definitions.person.female_prefix,
...fakerMK.definitions.person.female_first_name,
...fakerMK.definitions.person.female_last_name,
...fakerMK.definitions.person.suffix,
// ...fakerMK.definitions.person.suffix, // Not applicable
];

const fullName = fakerMK.person.fullName({ sex: 'female' });
Expand All @@ -141,7 +141,7 @@ describe('person', () => {
...fakerMK.definitions.person.male_prefix,
...fakerMK.definitions.person.male_first_name,
...fakerMK.definitions.person.male_last_name,
...fakerMK.definitions.person.suffix,
// ...fakerMK.definitions.person.suffix, // Not applicable
];

const fullName = fakerMK.person.fullName({ sex: 'male' });
Expand All @@ -157,7 +157,7 @@ describe('person', () => {
...fakerMK.definitions.person.female_prefix,
'firstName',
'lastName',
...fakerMK.definitions.person.suffix,
// ...fakerMK.definitions.person.suffix, // Not applicable
];

const fullName = fakerMK.person.fullName({
Expand All @@ -177,7 +177,7 @@ describe('person', () => {
...fakerMK.definitions.person.male_prefix,
'firstName',
'lastName',
...fakerMK.definitions.person.suffix,
//...fakerMK.definitions.person.suffix, // Not applicable
];

const fullName = fakerMK.person.fullName({
Expand Down
32 changes: 32 additions & 0 deletions test/utils/merge-locales.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,38 @@ describe('mergeLocales', () => {
});
});

it('should not overwrite not applicable data', () => {
const locale1: LocaleDefinition = {
metadata: { title: 'a' },
location: {
city: null,
country: null,
},
};
const locale2: LocaleDefinition = {
metadata: { title: 'b' },
location: {
city: ['b'],
},
};
const locale3: LocaleDefinition = {
metadata: { title: 'c' },
location: {
city: ['c'],
country: ['c'],
},
};
const merged = mergeLocales([locale1, locale2, locale3]);

expect(merged).toEqual({
metadata: { title: 'a' },
location: {
city: null,
country: null,
},
});
});

it('should extend categories', () => {
const locale1: LocaleDefinition = {
metadata: { title: 'a' },
Expand Down

0 comments on commit acb9cf5

Please sign in to comment.