@@ -8,11 +8,145 @@ export interface FakerOptions {
88 localeFallback ?: string ;
99}
1010
11+ export interface DefinitionTypes {
12+ name : string [ ] ;
13+ address : string [ ] ;
14+ animal : string [ ] ;
15+ company : string [ ] ;
16+ lorem : string [ ] ;
17+ hacker : string [ ] ;
18+ phone_number : string [ ] ;
19+ finance : string [ ] ;
20+ internet : string [ ] ;
21+ commerce : string [ ] ;
22+ database : string [ ] ;
23+ system : string [ ] ;
24+ date : string [ ] ;
25+ vehicle : string [ ] ;
26+ music : string [ ] ;
27+ word : string [ ] ;
28+ title : string | string [ ] ;
29+ separator : string | string [ ] ;
30+ }
31+
1132export class Faker {
1233 locales : string [ ] | { } ;
1334 locale : string ;
1435 localeFallback : string ;
1536
37+ readonly definitions = { } ;
38+ private readonly definitionTypes : DefinitionTypes = {
39+ name : [
40+ 'first_name' ,
41+ 'last_name' ,
42+ 'prefix' ,
43+ 'suffix' ,
44+ 'binary_gender' ,
45+ 'gender' ,
46+ 'title' ,
47+ 'male_prefix' ,
48+ 'female_prefix' ,
49+ 'male_first_name' ,
50+ 'female_first_name' ,
51+ 'male_middle_name' ,
52+ 'female_middle_name' ,
53+ 'male_last_name' ,
54+ 'female_last_name' ,
55+ ] ,
56+ address : [
57+ 'city_name' ,
58+ 'city_prefix' ,
59+ 'city_suffix' ,
60+ 'street_suffix' ,
61+ 'county' ,
62+ 'country' ,
63+ 'country_code' ,
64+ 'country_code_alpha_3' ,
65+ 'state' ,
66+ 'state_abbr' ,
67+ 'street_prefix' ,
68+ 'postcode' ,
69+ 'postcode_by_state' ,
70+ 'direction' ,
71+ 'direction_abbr' ,
72+ 'time_zone' ,
73+ ] ,
74+ animal : [
75+ 'dog' ,
76+ 'cat' ,
77+ 'snake' ,
78+ 'bear' ,
79+ 'lion' ,
80+ 'cetacean' ,
81+ 'insect' ,
82+ 'crocodilia' ,
83+ 'cow' ,
84+ 'bird' ,
85+ 'fish' ,
86+ 'rabbit' ,
87+ 'horse' ,
88+ 'type' ,
89+ ] ,
90+ company : [
91+ 'adjective' ,
92+ 'noun' ,
93+ 'descriptor' ,
94+ 'bs_adjective' ,
95+ 'bs_noun' ,
96+ 'bs_verb' ,
97+ 'suffix' ,
98+ ] ,
99+ lorem : [ 'words' ] ,
100+ hacker : [ 'abbreviation' , 'adjective' , 'noun' , 'verb' , 'ingverb' , 'phrase' ] ,
101+ phone_number : [ 'formats' ] ,
102+ finance : [
103+ 'account_type' ,
104+ 'transaction_type' ,
105+ 'currency' ,
106+ 'iban' ,
107+ 'credit_card' ,
108+ ] ,
109+ internet : [
110+ 'avatar_uri' ,
111+ 'domain_suffix' ,
112+ 'free_email' ,
113+ 'example_email' ,
114+ 'password' ,
115+ ] ,
116+ commerce : [
117+ 'color' ,
118+ 'department' ,
119+ 'product_name' ,
120+ 'price' ,
121+ 'categories' ,
122+ 'product_description' ,
123+ ] ,
124+ database : [ 'collation' , 'column' , 'engine' , 'type' ] ,
125+ system : [ 'mimeTypes' , 'directoryPaths' ] ,
126+ date : [ 'month' , 'weekday' ] ,
127+ vehicle : [
128+ 'vehicle' ,
129+ 'manufacturer' ,
130+ 'model' ,
131+ 'type' ,
132+ 'fuel' ,
133+ 'vin' ,
134+ 'color' ,
135+ ] ,
136+ music : [ 'genre' ] ,
137+ word : [
138+ 'adjective' ,
139+ 'adverb' ,
140+ 'conjunction' ,
141+ 'interjection' ,
142+ 'noun' ,
143+ 'preposition' ,
144+ 'verb' ,
145+ ] ,
146+ title : '' ,
147+ separator : '' ,
148+ } ;
149+
16150 seedValue ?: any [ ] | any ;
17151
18152 readonly mersenne : Mersenne = new Mersenne ( ) ;
@@ -23,13 +157,62 @@ export class Faker {
23157 this . locales = this . locales || opts . locales || { } ;
24158 this . locale = this . locale || opts . locale || 'en' ;
25159 this . localeFallback = this . localeFallback || opts . localeFallback || 'en' ;
160+
161+ this . loadDefinitions ( this . definitionTypes ) ;
162+ }
163+
164+ /**
165+ * Load the definitions contained in the locales file for the given types
166+ *
167+ * @param types
168+ */
169+ private loadDefinitions ( types : DefinitionTypes ) : void {
170+ Object . keys ( types ) . forEach ( ( t : string ) => {
171+ if ( typeof this . definitions [ t ] === 'undefined' ) {
172+ this . definitions [ t ] = { } ;
173+ }
174+
175+ if ( typeof types [ t ] === 'string' ) {
176+ this . definitions [ t ] = types [ t ] ;
177+ return ;
178+ }
179+
180+ types [ t ] . forEach ( ( p ) => {
181+ Object . defineProperty ( this . definitions [ t ] , p , {
182+ get : ( ) => {
183+ if (
184+ typeof this . locales [ this . locale ] [ t ] === 'undefined' ||
185+ typeof this . locales [ this . locale ] [ t ] [ p ] === 'undefined'
186+ ) {
187+ // certain localization sets contain less data then others.
188+ // in the case of a missing definition, use the default localeFallback
189+ // to substitute the missing set data
190+ // throw new Error('unknown property ' + d + p)
191+ return this . locales [ this . localeFallback ] [ t ] [ p ] ;
192+ } else {
193+ // return localized data
194+ return this . locales [ this . locale ] [ t ] [ p ] ;
195+ }
196+ } ,
197+ } ) ;
198+ } ) ;
199+ } ) ;
26200 }
27201
28202 seed ( value ?: any [ ] | any ) {
29203 this . seedValue = value ;
30204 this . random = new Random ( this , this . seedValue ) ;
31205 this . datatype = new Datatype ( this , this . seedValue ) ;
32206 }
207+
208+ /**
209+ * Set Faker's locale
210+ *
211+ * @param locale
212+ */
213+ setLocale ( locale : string ) : void {
214+ this . locale = locale ;
215+ }
33216}
34217
35218export default Faker ;
0 commit comments