forked from zuriby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.js
317 lines (274 loc) · 7.76 KB
/
random.js
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
var mersenne = require('../vendor/mersenne');
/**
*
* @namespace faker.random
*/
function Random (faker, seed) {
// Use a user provided seed if it exists
if (seed) {
if (Array.isArray(seed) && seed.length) {
mersenne.seed_array(seed);
}
else {
mersenne.seed(seed);
}
}
/**
* returns a single random number based on a max number or range
*
* @method faker.random.number
* @param {mixed} options {min, max, precision}
*/
this.number = function (options) {
if (typeof options === "number") {
options = {
max: options
};
}
options = options || {};
if (typeof options.min === "undefined") {
options.min = 0;
}
if (typeof options.max === "undefined") {
options.max = 99999;
}
if (typeof options.precision === "undefined") {
options.precision = 1;
}
// Make the range inclusive of the max value
var max = options.max;
if (max >= 0) {
max += options.precision;
}
var randomNumber = Math.floor(
mersenne.rand(max / options.precision, options.min / options.precision));
// Workaround problem in Float point arithmetics for e.g. 6681493 / 0.01
randomNumber = randomNumber / (1 / options.precision);
return randomNumber;
}
/**
* returns a single random floating-point number based on a max number or range
*
* @method faker.random.float
* @param {mixed} options
*/
this.float = function (options) {
if (typeof options === "number") {
options = {
precision: options
};
}
options = options || {};
var opts = {};
for (var p in options) {
opts[p] = options[p];
}
if (typeof opts.precision === 'undefined') {
opts.precision = 0.01;
}
return faker.random.number(opts);
}
/**
* takes an array and returns a random element of the array
*
* @method faker.random.arrayElement
* @param {array} array
*/
this.arrayElement = function (array) {
array = array || ["a", "b", "c"];
var r = faker.random.number({ max: array.length - 1 });
return array[r];
}
/**
* takes an array and returns a subset with random elements of the array
*
* @method faker.random.arrayElements
* @param {array} array
* @param {number} count number of elements to pick
*/
this.arrayElements = function (array, count) {
array = array || ["a", "b", "c"];
if (typeof count !== 'number') {
count = faker.random.number({ min: 1, max: array.length });
} else if (count > array.length) {
count = array.length;
} else if (count < 0) {
count = 0;
}
var arrayCopy = array.slice();
var countToRemove = arrayCopy.length - count;
for (var i = 0; i < countToRemove; i++) {
var indexToRemove = faker.random.number({ max: arrayCopy.length - 1 });
arrayCopy.splice(indexToRemove, 1);
}
return arrayCopy;
}
/**
* takes an object and returns the randomly key or value
*
* @method faker.random.objectElement
* @param {object} object
* @param {mixed} field
*/
this.objectElement = function (object, field) {
object = object || { "foo": "bar", "too": "car" };
var array = Object.keys(object);
var key = faker.random.arrayElement(array);
return field === "key" ? key : object[key];
}
/**
* uuid
*
* @method faker.random.uuid
*/
this.uuid = function () {
var RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
var replacePlaceholders = function (placeholder) {
var random = faker.random.number({ min: 0, max: 15 });
var value = placeholder == 'x' ? random : (random &0x3 | 0x8);
return value.toString(16);
};
return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders);
}
/**
* boolean
*
* @method faker.random.boolean
*/
this.boolean = function () {
return !!faker.random.number(1)
}
// TODO: have ability to return specific type of word? As in: noun, adjective, verb, etc
/**
* word
*
* @method faker.random.word
* @param {string} type
*/
this.word = function randomWord (type) {
var wordMethods = [
'commerce.department',
'commerce.productName',
'commerce.productAdjective',
'commerce.productMaterial',
'commerce.product',
'commerce.color',
'company.catchPhraseAdjective',
'company.catchPhraseDescriptor',
'company.catchPhraseNoun',
'company.bsAdjective',
'company.bsBuzz',
'company.bsNoun',
'address.streetSuffix',
'address.county',
'address.country',
'address.state',
'finance.accountName',
'finance.transactionType',
'finance.currencyName',
'hacker.noun',
'hacker.verb',
'hacker.adjective',
'hacker.ingverb',
'hacker.abbreviation',
'name.jobDescriptor',
'name.jobArea',
'name.jobType'];
// randomly pick from the many faker methods that can generate words
var randomWordMethod = faker.random.arrayElement(wordMethods);
return faker.fake('{{' + randomWordMethod + '}}');
}
/**
* randomWords
*
* @method faker.random.words
* @param {number} count defaults to a random value between 1 and 3
*/
this.words = function randomWords (count) {
var words = [];
if (typeof count === "undefined") {
count = faker.random.number({min:1, max: 3});
}
for (var i = 0; i<count; i++) {
words.push(faker.random.word());
}
return words.join(' ');
}
/**
* locale
*
* @method faker.random.image
*/
this.image = function randomImage () {
return faker.image.image();
}
/**
* locale
*
* @method faker.random.locale
*/
this.locale = function randomLocale () {
return faker.random.arrayElement(Object.keys(faker.locales));
};
/**
* alpha. returns lower/upper alpha characters based count and upcase options
*
* @method faker.random.alpha
* @param {mixed} options // defaults to { count: 1, upcase: false }
*/
this.alpha = function alpha(options) {
if (typeof options === "undefined") {
options = {
count: 1
}
} else if (typeof options === "number") {
options = {
count: options,
}
} else if (typeof options.count === "undefined") {
options.count = 1
}
if (typeof options.upcase === "undefined") {
options.upcase = false;
}
var wholeString = "";
for(var i = 0; i < options.count; i++) {
wholeString += faker.random.arrayElement(["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]);
}
return options.upcase ? wholeString.toUpperCase() : wholeString;
};
/**
* alphaNumeric
*
* @method faker.random.alphaNumeric
* @param {number} count defaults to 1
*/
this.alphaNumeric = function alphaNumeric(count) {
if (typeof count === "undefined") {
count = 1;
}
var wholeString = "";
for(var i = 0; i < count; i++) {
wholeString += faker.random.arrayElement(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]);
}
return wholeString;
};
/**
* hexaDecimal
*
* @method faker.random.hexaDecimal
* @param {number} count defaults to 1
*/
this.hexaDecimal = function hexaDecimal(count) {
if (typeof count === "undefined") {
count = 1;
}
var wholeString = "";
for(var i = 0; i < count; i++) {
wholeString += faker.random.arrayElement(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "A", "B", "C", "D", "E", "F"]);
}
return "0x"+wholeString;
};
return this;
}
module['exports'] = Random;