-
-
Notifications
You must be signed in to change notification settings - Fork 225
/
fake.ts
204 lines (183 loc) 路 6.57 KB
/
fake.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//import * as faker from 'faker';
const faker = require('faker');
import * as moment from 'moment';
export function getRandomInt(min:number, max:number) {
return faker.random.number({min, max});
}
export function getRandomItem<T>(array: ReadonlyArray<T>): T {
return array[getRandomInt(0, array.length - 1)];
}
export const stdScalarFakers = {
'Int': () => faker.random.number({ min: 0, max: 99999, precision: 1 }),
'Float': () => faker.random.number({ min: 0, max: 99999, precision: 0.01 }),
'String': () => 'string',
'Boolean': () => faker.random.boolean(),
'ID': () => toBase64(faker.random.number({max: 9999999999}).toString()),
};
function toBase64(str) {
return Buffer.from(str).toString('base64');
}
const fakeFunctions = {
// Address section
zipCode: () => faker.address.zipCode(),
city: () => faker.address.city(),
// Skipped: faker.address.cityPrefix
// Skipped: faker.address.citySuffix
streetName: () => faker.address.streetName(),
streetAddress: {
args: ['useFullAddress'],
func: (useFullAddress) => faker.address.streetAddress(useFullAddress),
},
// Skipped: faker.address.streetSuffix
// Skipped: faker.address.streetPrefix
secondaryAddress: () => faker.address.secondaryAddress(),
county: () => faker.address.county(),
country: () => faker.address.country(),
countryCode: () => faker.address.countryCode(),
state: () => faker.address.state(),
stateAbbr: () => faker.address.stateAbbr(),
latitude: () => faker.address.latitude(),
longitude: () => faker.address.longitude(),
// Commerce section
colorName: () => faker.commerce.color(),
productCategory: () => faker.commerce.department(),
productName: () => faker.commerce.productName(),
money: {
args: ['minMoney', 'maxMoney', 'decimalPlaces'],
func: (min, max, dec) => faker.commerce.price(min, max, dec),
},
// Skipped: faker.commerce.productAdjective
productMaterial: () => faker.commerce.productMaterial(),
product: () => faker.commerce.product(),
// Company section
// Skipped: faker.company.companySuffixes
companyName: () => faker.company.companyName(),
// Skipped: faker.company.companySuffix
companyCatchPhrase: () => faker.company.catchPhrase(),
companyBs: () => faker.company.bs(),
// Skipped: faker.company.catchPhraseAdjective
// Skipped: faker.company.catchPhraseDescriptor
// Skipped: faker.company.catchPhraseNoun
// Skipped: faker.company.companyBsAdjective
// Skipped: faker.company.companyBsBuzz
// Skipped: faker.company.companyBsNoun
// Database section
dbColumn: () => faker.database.column(),
dbType: () => faker.database.type(),
dbCollation: () => faker.database.collation(),
dbEngine: () => faker.database.engine(),
// Date section
pastDate: {
args: ['dateFormat'],
func: (dateFormat) => {
const date = faker.date.past()
return (dateFormat !== undefined ? moment(date).format(dateFormat) : date)
}
},
futureDate: {
args: ['dateFormat'],
func: (dateFormat) => {
const date = faker.date.future()
return (dateFormat !== undefined ? moment(date).format(dateFormat) : date)
}
},
recentDate: {
args: ['dateFormat'],
func: (dateFormat) => {
const date = faker.date.recent()
return (dateFormat !== undefined ? moment(date).format(dateFormat) : date)
}
},
// Finance section
financeAccountName: () => faker.finance.accountName(),
//TODO: investigate finance.mask
financeTransactionType: () => faker.finance.transactionType(),
currencyCode: () => faker.finance.currencyCode(),
currencyName: () => faker.finance.currencyName(),
currencySymbol: () => faker.finance.currencySymbol(),
bitcoinAddress: () => faker.finance.bitcoinAddress(),
internationalBankAccountNumber: () => faker.finance.iban(),
bankIdentifierCode: () => faker.finance.bic(),
// Hacker section
hackerAbbr: () => faker.hacker.itAbbr(),
hackerPhrase: () => faker.hacker.phrase(),
// Image section
imageUrl: {
args: ['imageHeight', 'imageWidth', 'imageCategory', 'randomizeImageUrl'],
func: (height, width, category, randomize) =>
faker.image.imageUrl(height, width, category, randomize, false),
},
// Internet section
avatarUrl: () => faker.internet.avatar(),
email: {
args: ['emailProvider'],
func: (provider) => faker.internet.email(undefined, undefined, provider),
},
url: () => faker.internet.url(),
domainName: () => faker.internet.domainName(),
ipv4Address: () => faker.internet.ip(),
ipv6Address: () => faker.internet.ipv6(),
userAgent: () => faker.internet.userAgent(),
colorHex: {
args: ['baseColor'],
func: ({red255, green255, blue255}) => {
return faker.internet.color(red255, green255, blue255);
},
},
macAddress: () => faker.internet.mac(),
password: {
args: ['passwordLength'],
func: (len) => faker.internet.password(len),
},
// Lorem section
lorem: {
args: ['loremSize'],
func: (size) => faker.lorem[size || 'paragraphs'](),
},
// Name section
firstName: () => faker.name.firstName(),
lastName: () => faker.name.lastName(),
fullName: () => faker.name.findName(),
jobTitle: () => faker.name.jobTitle(),
// Phone section
phoneNumber: () => faker.phone.phoneNumber(),
// Skipped: faker.phone.phoneNumberFormat
// Skipped: faker.phone.phoneFormats
// Random section
number: {
args: ['minNumber', 'maxNumber', 'precisionNumber'],
func: (min, max, precision) => faker.random.number({ min, max, precision }),
},
uuid: () => faker.random.uuid(),
word: () => faker.random.word(),
words: () => faker.random.words(),
locale: () => faker.random.locale(),
// System section
// Skipped: faker.system.fileName
// TODO: Add ext and type
filename: () => faker.system.commonFileName(),
mimeType: () => faker.system.mimeType(),
// Skipped: faker.system.fileType
// Skipped: faker.system.commonFileType
// Skipped: faker.system.commonFileExt
fileExtension: () => faker.system.fileExt(),
semver: () => faker.system.semver(),
};
Object.keys(fakeFunctions).forEach(key => {
var value = fakeFunctions[key];
if (typeof fakeFunctions[key] === 'function')
fakeFunctions[key] = {args: [], func: value};
});
export function fakeValue(type, options?, locale?) {
const fakeGenerator = fakeFunctions[type];
const argNames = fakeGenerator.args;
//TODO: add check
const callArgs = argNames.map(name => options[name]);
const localeBackup = faker.locale;
//faker.setLocale(locale || localeBackup);
faker.locale = locale || localeBackup;
const result = fakeGenerator.func(...callArgs);
//faker.setLocale(localeBackup);
faker.locale = localeBackup;
return result;
}