1
- import * as faker from 'faker' ;
1
+ import { allFakers } from '@faker-js/ faker' ;
2
2
import * as moment from 'moment' ;
3
+ const baseLocal = 'en' ;
4
+ let faker = allFakers [ baseLocal ] ;
3
5
4
6
export function getRandomInt ( min : number , max : number ) {
5
- return faker . datatype . number ( { min, max } ) ;
7
+ return faker . number . int ( { min, max } ) ;
6
8
}
7
9
8
10
export function getRandomItem < T > ( array : ReadonlyArray < T > ) : T {
9
11
return array [ getRandomInt ( 0 , array . length - 1 ) ] ;
10
12
}
11
13
12
14
export const stdScalarFakers = {
13
- Int : ( ) => faker . datatype . number ( { min : 0 , max : 99999 , precision : 1 } ) ,
14
- Float : ( ) => faker . datatype . number ( { min : 0 , max : 99999 , precision : 0.01 } ) ,
15
+ Int : ( ) => faker . number . float ( { min : 0 , max : 99999 , precision : 1 } ) ,
16
+ Float : ( ) => faker . number . float ( { min : 0 , max : 99999 , precision : 0.01 } ) ,
15
17
String : ( ) => 'string' ,
16
18
Boolean : ( ) => faker . datatype . boolean ( ) ,
17
- ID : ( ) => toBase64 ( faker . datatype . number ( { max : 9999999999 } ) . toString ( ) ) ,
19
+ ID : ( ) => toBase64 ( faker . number . int ( { max : 9999999999 } ) . toString ( ) ) ,
18
20
} ;
19
21
20
22
function toBase64 ( str ) {
@@ -23,44 +25,44 @@ function toBase64(str) {
23
25
24
26
const fakeFunctions = {
25
27
// Address section
26
- zipCode : ( ) => faker . address . zipCode ( ) ,
27
- city : ( ) => faker . address . city ( ) ,
28
+ zipCode : ( ) => faker . location . zipCode ( ) ,
29
+ city : ( ) => faker . location . city ( ) ,
28
30
// Skipped: faker.address.cityPrefix
29
31
// Skipped: faker.address.citySuffix
30
- streetName : ( ) => faker . address . streetName ( ) ,
32
+ streetName : ( ) => faker . location . street ( ) ,
31
33
streetAddress : {
32
34
args : [ 'useFullAddress' ] ,
33
- func : ( useFullAddress ) => faker . address . streetAddress ( useFullAddress ) ,
35
+ func : ( useFullAddress ) => faker . location . streetAddress ( useFullAddress ) ,
34
36
} ,
35
37
// Skipped: faker.address.streetSuffix
36
38
// Skipped: faker.address.streetPrefix
37
- secondaryAddress : ( ) => faker . address . secondaryAddress ( ) ,
38
- county : ( ) => faker . address . county ( ) ,
39
- country : ( ) => faker . address . country ( ) ,
40
- countryCode : ( ) => faker . address . countryCode ( ) ,
41
- state : ( ) => faker . address . state ( ) ,
42
- stateAbbr : ( ) => faker . address . stateAbbr ( ) ,
43
- latitude : ( ) => faker . address . latitude ( ) ,
44
- longitude : ( ) => faker . address . longitude ( ) ,
39
+ secondaryAddress : ( ) => faker . location . secondaryAddress ( ) ,
40
+ county : ( ) => faker . location . county ( ) ,
41
+ country : ( ) => faker . location . country ( ) ,
42
+ countryCode : ( ) => faker . location . countryCode ( ) ,
43
+ state : ( ) => faker . location . state ( ) ,
44
+ stateAbbr : ( ) => faker . location . state ( { abbreviated : true } ) ,
45
+ latitude : ( ) => faker . location . latitude ( ) ,
46
+ longitude : ( ) => faker . location . longitude ( ) ,
45
47
46
48
// Commerce section
47
- colorName : ( ) => faker . commerce . color ( ) ,
49
+ colorName : ( ) => faker . color . human ( ) ,
48
50
productCategory : ( ) => faker . commerce . department ( ) ,
49
51
productName : ( ) => faker . commerce . productName ( ) ,
50
52
money : {
51
53
args : [ 'minMoney' , 'maxMoney' , 'decimalPlaces' ] ,
52
- func : ( min , max , dec ) => faker . commerce . price ( min , max , dec ) ,
54
+ func : ( min , max , dec ) => faker . commerce . price ( { min, max, dec } ) ,
53
55
} ,
54
56
// Skipped: faker.commerce.productAdjective
55
57
productMaterial : ( ) => faker . commerce . productMaterial ( ) ,
56
58
product : ( ) => faker . commerce . product ( ) ,
57
59
58
60
// Company section
59
61
// Skipped: faker.company.companySuffixes
60
- companyName : ( ) => faker . company . companyName ( ) ,
62
+ companyName : ( ) => faker . company . name ( ) ,
61
63
// Skipped: faker.company.companySuffix
62
64
companyCatchPhrase : ( ) => faker . company . catchPhrase ( ) ,
63
- companyBs : ( ) => faker . company . bs ( ) ,
65
+ companyBs : ( ) => faker . company . buzzPhrase ( ) ,
64
66
// Skipped: faker.company.catchPhraseAdjective
65
67
// Skipped: faker.company.catchPhraseDescriptor
66
68
// Skipped: faker.company.catchPhraseNoun
@@ -125,7 +127,7 @@ const fakeFunctions = {
125
127
}
126
128
127
129
if ( randomize === true ) {
128
- url += '#' + faker . datatype . number ( ) ;
130
+ url += '#' + faker . number . int ( ) ;
129
131
}
130
132
131
133
return url ;
@@ -162,26 +164,25 @@ const fakeFunctions = {
162
164
} ,
163
165
164
166
// Name section
165
- firstName : ( ) => faker . name . firstName ( ) ,
166
- lastName : ( ) => faker . name . lastName ( ) ,
167
- fullName : ( ) => faker . name . findName ( ) ,
168
- jobTitle : ( ) => faker . name . jobTitle ( ) ,
167
+ firstName : ( ) => faker . person . firstName ( ) ,
168
+ lastName : ( ) => faker . person . lastName ( ) ,
169
+ fullName : ( ) => faker . person . fullName ( ) ,
170
+ jobTitle : ( ) => faker . person . jobTitle ( ) ,
169
171
170
172
// Phone section
171
- phoneNumber : ( ) => faker . phone . phoneNumber ( ) ,
173
+ phoneNumber : ( ) => faker . phone . number ( ) ,
172
174
// Skipped: faker.phone.phoneNumberFormat
173
175
// Skipped: faker.phone.phoneFormats
174
176
175
177
// Random section
176
178
number : {
177
179
args : [ 'minNumber' , 'maxNumber' , 'precisionNumber' ] ,
178
- func : ( min , max , precision ) =>
179
- faker . datatype . number ( { min, max, precision } ) ,
180
+ func : ( min , max , precision ) => faker . number . float ( { min, max, precision } ) ,
180
181
} ,
181
- uuid : ( ) => faker . random . uuid ( ) ,
182
- word : ( ) => faker . random . word ( ) ,
183
- words : ( ) => faker . random . words ( ) ,
184
- locale : ( ) => faker . random . locale ( ) ,
182
+ uuid : ( ) => faker . string . uuid ( ) ,
183
+ word : ( ) => faker . lorem . word ( ) ,
184
+ words : ( ) => faker . lorem . words ( ) ,
185
+ locale : ( ) => faker . location . countryCode ( ) ,
185
186
186
187
// System section
187
188
// Skipped: faker.system.fileName
@@ -201,15 +202,14 @@ Object.keys(fakeFunctions).forEach((key) => {
201
202
fakeFunctions [ key ] = { args : [ ] , func : value } ;
202
203
} ) ;
203
204
204
- export function fakeValue ( type , options ?, locale ?) {
205
+ export function fakeValue ( type , options ?, locale ?: string ) {
205
206
const fakeGenerator = fakeFunctions [ type ] ;
206
207
const argNames = fakeGenerator . args ;
207
208
//TODO: add check
208
209
const callArgs = argNames . map ( ( name ) => options [ name ] ) ;
209
-
210
- const localeBackup = faker . locale ;
211
- faker . setLocale ( locale || localeBackup ) ;
210
+ const desiredLocal = allFakers [ locale || baseLocal ] ;
211
+ faker = desiredLocal ;
212
212
const result = fakeGenerator . func ( ...callArgs ) ;
213
- faker . setLocale ( localeBackup ) ;
213
+ faker = allFakers [ baseLocal ] ;
214
214
return result ;
215
215
}
0 commit comments