Skip to content

Commit 2b5f993

Browse files
Shinigami92ST-DDT
authored andcommitted
chore: move faker into own file (faker-js#548)
Co-authored-by: ST-DDT <[email protected]>
1 parent b6316ed commit 2b5f993

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+215
-202
lines changed

scripts/generateLocales.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function generateLocaleFile(locale: string): void {
7070
let content = `
7171
${autoGeneratedCommentHeader}
7272
73-
import { Faker } from '..';
73+
import { Faker } from '../faker';
7474
import ${locale} from '../locales/${locale}';
7575
${locale !== 'en' ? "import en from '../locales/en';" : ''}
7676

src/faker.ts

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import { Address } from './address';
2+
import { Animal } from './animal';
3+
import { Commerce } from './commerce';
4+
import { Company } from './company';
5+
import { Database } from './database';
6+
import { Datatype } from './datatype';
7+
import { _Date } from './date';
8+
import type { LocaleDefinition } from './definitions';
9+
import { DEFINITIONS } from './definitions';
10+
import { Fake } from './fake';
11+
import { Finance } from './finance';
12+
import { Git } from './git';
13+
import { Hacker } from './hacker';
14+
import { Helpers } from './helpers';
15+
import { Image } from './image';
16+
import { Internet } from './internet';
17+
import type { KnownLocale } from './locales';
18+
import { Lorem } from './lorem';
19+
import { Mersenne } from './mersenne';
20+
import { Music } from './music';
21+
import { Name } from './name';
22+
import { Phone } from './phone';
23+
import { Random } from './random';
24+
import { System } from './system';
25+
import { Time } from './time';
26+
import { Unique } from './unique';
27+
import { Vehicle } from './vehicle';
28+
import { Word } from './word';
29+
30+
// https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609
31+
type LiteralUnion<T extends U, U = string> = T | (U & { zz_IGNORE_ME?: never });
32+
33+
export type UsableLocale = LiteralUnion<KnownLocale>;
34+
export type UsedLocales = Partial<Record<UsableLocale, LocaleDefinition>>;
35+
36+
export interface FakerOptions {
37+
locales?: UsedLocales;
38+
locale?: UsableLocale;
39+
localeFallback?: UsableLocale;
40+
}
41+
42+
export class Faker {
43+
locales: UsedLocales;
44+
locale: UsableLocale;
45+
localeFallback: UsableLocale;
46+
47+
// Will be lazy init
48+
readonly definitions: LocaleDefinition = {} as LocaleDefinition;
49+
50+
seedValue?: any[] | any;
51+
52+
readonly fake: Fake['fake'] = new Fake(this).fake;
53+
readonly unique: Unique['unique'] = new Unique().unique;
54+
55+
readonly mersenne: Mersenne = new Mersenne();
56+
random: Random = new Random(this);
57+
58+
readonly helpers: Helpers = new Helpers(this);
59+
60+
datatype: Datatype = new Datatype(this);
61+
62+
readonly address: Address = new Address(this);
63+
readonly animal: Animal = new Animal(this);
64+
readonly commerce: Commerce = new Commerce(this);
65+
readonly company: Company = new Company(this);
66+
readonly database: Database = new Database(this);
67+
readonly date: _Date = new _Date(this);
68+
readonly finance = new Finance(this);
69+
readonly git: Git = new Git(this);
70+
readonly hacker: Hacker = new Hacker(this);
71+
// TODO @Shinigami92 2022-01-12: iban was not used
72+
// readonly iban = new (require('./iban'))(this);
73+
readonly image: Image = new Image(this);
74+
readonly internet: Internet = new Internet(this);
75+
readonly lorem: Lorem = new Lorem(this);
76+
readonly music: Music = new Music(this);
77+
readonly name: Name = new Name(this);
78+
readonly phone: Phone = new Phone(this);
79+
readonly system: System = new System(this);
80+
readonly time: Time = new Time();
81+
readonly vehicle: Vehicle = new Vehicle(this);
82+
readonly word: Word = new Word(this);
83+
84+
constructor(opts: FakerOptions = {}) {
85+
this.locales = this.locales || opts.locales || {};
86+
this.locale = this.locale || opts.locale || 'en';
87+
this.localeFallback = this.localeFallback || opts.localeFallback || 'en';
88+
89+
this.loadDefinitions();
90+
}
91+
92+
/**
93+
* Load the definitions contained in the locales file for the given types
94+
*/
95+
private loadDefinitions(): void {
96+
// TODO @Shinigami92 2022-01-11: Find a way to load this even more dynamically
97+
// In a way so that we don't accidentally miss a definition
98+
Object.entries(DEFINITIONS).forEach(([t, v]) => {
99+
if (typeof this.definitions[t] === 'undefined') {
100+
this.definitions[t] = {};
101+
}
102+
103+
if (typeof v === 'string') {
104+
this.definitions[t] = v;
105+
return;
106+
}
107+
108+
v.forEach((p) => {
109+
Object.defineProperty(this.definitions[t], p, {
110+
get: () => {
111+
if (
112+
typeof this.locales[this.locale][t] === 'undefined' ||
113+
typeof this.locales[this.locale][t][p] === 'undefined'
114+
) {
115+
// certain localization sets contain less data then others.
116+
// in the case of a missing definition, use the default localeFallback
117+
// to substitute the missing set data
118+
// throw new Error('unknown property ' + d + p)
119+
return this.locales[this.localeFallback][t][p];
120+
} else {
121+
// return localized data
122+
return this.locales[this.locale][t][p];
123+
}
124+
},
125+
});
126+
});
127+
});
128+
}
129+
130+
seed(value?: any[] | any): void {
131+
this.seedValue = value;
132+
this.random = new Random(this, this.seedValue);
133+
this.datatype = new Datatype(this, this.seedValue);
134+
}
135+
136+
/**
137+
* Set Faker's locale
138+
*
139+
* @param locale The locale to set (e.g. `en` or `en_AU`, `en_AU_ocker`).
140+
*/
141+
setLocale(locale: UsableLocale): void {
142+
this.locale = locale;
143+
}
144+
}

src/index.ts

Lines changed: 5 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import { Address } from './address';
2-
import { Animal } from './animal';
3-
import { Commerce } from './commerce';
4-
import { Company } from './company';
5-
import { Database } from './database';
6-
import { Datatype } from './datatype';
7-
import { _Date } from './date';
8-
import type { LocaleDefinition } from './definitions';
1+
import { Faker } from './faker';
2+
import allLocales from './locales';
3+
94
export type {
105
AddressDefinitions,
116
AnimalDefinitions,
@@ -31,143 +26,8 @@ export type {
3126
VehicleDefinitions,
3227
WordDefinitions,
3328
} from './definitions';
34-
import { DEFINITIONS } from './definitions';
35-
import { Fake } from './fake';
36-
import { Finance } from './finance';
37-
import { Git } from './git';
38-
import { Hacker } from './hacker';
39-
import { Helpers } from './helpers';
40-
import { Image } from './image';
41-
import { Internet } from './internet';
42-
import type { KnownLocale } from './locales';
43-
import allLocales from './locales';
44-
import { Lorem } from './lorem';
45-
import { Mersenne } from './mersenne';
46-
import { Music } from './music';
47-
import { Name } from './name';
48-
import { Phone } from './phone';
49-
import { Random } from './random';
50-
import { System } from './system';
51-
import { Time } from './time';
52-
import { Unique } from './unique';
53-
import { Vehicle } from './vehicle';
54-
import { Word } from './word';
55-
56-
// https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609
57-
type LiteralUnion<T extends U, U = string> = T | (U & { zz_IGNORE_ME?: never });
58-
59-
export type UsableLocale = LiteralUnion<KnownLocale>;
60-
export type UsedLocales = Partial<Record<UsableLocale, LocaleDefinition>>;
61-
62-
export interface FakerOptions {
63-
locales?: UsedLocales;
64-
locale?: UsableLocale;
65-
localeFallback?: UsableLocale;
66-
}
67-
68-
export class Faker {
69-
locales: UsedLocales;
70-
locale: UsableLocale;
71-
localeFallback: UsableLocale;
72-
73-
// Will be lazy init
74-
readonly definitions: LocaleDefinition = {} as LocaleDefinition;
75-
76-
seedValue?: any[] | any;
77-
78-
readonly fake: Fake['fake'] = new Fake(this).fake;
79-
readonly unique: Unique['unique'] = new Unique().unique;
80-
81-
readonly mersenne: Mersenne = new Mersenne();
82-
random: Random = new Random(this);
83-
84-
readonly helpers: Helpers = new Helpers(this);
85-
86-
datatype: Datatype = new Datatype(this);
87-
88-
readonly address: Address = new Address(this);
89-
readonly animal: Animal = new Animal(this);
90-
readonly commerce: Commerce = new Commerce(this);
91-
readonly company: Company = new Company(this);
92-
readonly database: Database = new Database(this);
93-
readonly date: _Date = new _Date(this);
94-
readonly finance = new Finance(this);
95-
readonly git: Git = new Git(this);
96-
readonly hacker: Hacker = new Hacker(this);
97-
// TODO @Shinigami92 2022-01-12: iban was not used
98-
// readonly iban = new (require('./iban'))(this);
99-
readonly image: Image = new Image(this);
100-
readonly internet: Internet = new Internet(this);
101-
readonly lorem: Lorem = new Lorem(this);
102-
readonly music: Music = new Music(this);
103-
readonly name: Name = new Name(this);
104-
readonly phone: Phone = new Phone(this);
105-
readonly system: System = new System(this);
106-
readonly time: Time = new Time();
107-
readonly vehicle: Vehicle = new Vehicle(this);
108-
readonly word: Word = new Word(this);
109-
110-
constructor(opts: FakerOptions = {}) {
111-
this.locales = this.locales || opts.locales || {};
112-
this.locale = this.locale || opts.locale || 'en';
113-
this.localeFallback = this.localeFallback || opts.localeFallback || 'en';
114-
115-
this.loadDefinitions();
116-
}
117-
118-
/**
119-
* Load the definitions contained in the locales file for the given types
120-
*/
121-
private loadDefinitions(): void {
122-
// TODO @Shinigami92 2022-01-11: Find a way to load this even more dynamically
123-
// In a way so that we don't accidentally miss a definition
124-
Object.entries(DEFINITIONS).forEach(([t, v]) => {
125-
if (typeof this.definitions[t] === 'undefined') {
126-
this.definitions[t] = {};
127-
}
128-
129-
if (typeof v === 'string') {
130-
this.definitions[t] = v;
131-
return;
132-
}
133-
134-
v.forEach((p) => {
135-
Object.defineProperty(this.definitions[t], p, {
136-
get: () => {
137-
if (
138-
typeof this.locales[this.locale][t] === 'undefined' ||
139-
typeof this.locales[this.locale][t][p] === 'undefined'
140-
) {
141-
// certain localization sets contain less data then others.
142-
// in the case of a missing definition, use the default localeFallback
143-
// to substitute the missing set data
144-
// throw new Error('unknown property ' + d + p)
145-
return this.locales[this.localeFallback][t][p];
146-
} else {
147-
// return localized data
148-
return this.locales[this.locale][t][p];
149-
}
150-
},
151-
});
152-
});
153-
});
154-
}
155-
156-
seed(value?: any[] | any): void {
157-
this.seedValue = value;
158-
this.random = new Random(this, this.seedValue);
159-
this.datatype = new Datatype(this, this.seedValue);
160-
}
161-
162-
/**
163-
* Set Faker's locale
164-
*
165-
* @param locale The locale to set (e.g. `en` or `en_AU`, `en_AU_ocker`).
166-
*/
167-
setLocale(locale: UsableLocale): void {
168-
this.locale = locale;
169-
}
170-
}
29+
export type { FakerOptions, UsableLocale, UsedLocales } from './faker';
30+
export { Faker };
17131

17232
// since we are requiring the top level of faker, load all locales by default
17333
export const faker: Faker = new Faker({

src/locale/af_ZA.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Run 'pnpm run generate:locales' to update.
44
*/
55

6-
import { Faker } from '..';
6+
import { Faker } from '../faker';
77
import af_ZA from '../locales/af_ZA';
88
import en from '../locales/en';
99

src/locale/ar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Run 'pnpm run generate:locales' to update.
44
*/
55

6-
import { Faker } from '..';
6+
import { Faker } from '../faker';
77
import ar from '../locales/ar';
88
import en from '../locales/en';
99

src/locale/az.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Run 'pnpm run generate:locales' to update.
44
*/
55

6-
import { Faker } from '..';
6+
import { Faker } from '../faker';
77
import az from '../locales/az';
88
import en from '../locales/en';
99

src/locale/cz.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Run 'pnpm run generate:locales' to update.
44
*/
55

6-
import { Faker } from '..';
6+
import { Faker } from '../faker';
77
import cz from '../locales/cz';
88
import en from '../locales/en';
99

src/locale/de.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Run 'pnpm run generate:locales' to update.
44
*/
55

6-
import { Faker } from '..';
6+
import { Faker } from '../faker';
77
import de from '../locales/de';
88
import en from '../locales/en';
99

src/locale/de_AT.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Run 'pnpm run generate:locales' to update.
44
*/
55

6-
import { Faker } from '..';
6+
import { Faker } from '../faker';
77
import de_AT from '../locales/de_AT';
88
import en from '../locales/en';
99

src/locale/de_CH.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Run 'pnpm run generate:locales' to update.
44
*/
55

6-
import { Faker } from '..';
6+
import { Faker } from '../faker';
77
import de_CH from '../locales/de_CH';
88
import en from '../locales/en';
99

src/locale/el.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Run 'pnpm run generate:locales' to update.
44
*/
55

6-
import { Faker } from '..';
6+
import { Faker } from '../faker';
77
import el from '../locales/el';
88
import en from '../locales/en';
99

src/locale/en.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Run 'pnpm run generate:locales' to update.
44
*/
55

6-
import { Faker } from '..';
6+
import { Faker } from '../faker';
77
import en from '../locales/en';
88

99
const faker = new Faker({

0 commit comments

Comments
 (0)