forked from RoundingWellOS/faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatype.js
226 lines (188 loc) · 5.6 KB
/
datatype.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
/**
*
* @namespace faker.datatype
*/
function Datatype (faker, seed) {
// Use a user provided seed if it is an array or number
if (Array.isArray(seed) && seed.length) {
faker.mersenne.seed_array(seed);
}
else if(!isNaN(seed)) {
faker.mersenne.seed(seed);
}
/**
* returns a single random number based on a max number or range
*
* @method faker.datatype.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(
faker.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.datatype.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.datatype.number(opts);
};
/**
* method returns a Date object using a random number of milliseconds since 1. Jan 1970 UTC
* Caveat: seeding is not working
*
* @method faker.datatype.datetime
* @param {mixed} options, pass min OR max as number of milliseconds since 1. Jan 1970 UTC
*/
this.datetime = function (options) {
if (typeof options === "number") {
options = {
max: options
};
}
var minMax = 8640000000000000;
options = options || {};
if (typeof options.min === "undefined" || options.min < minMax*-1) {
options.min = new Date().setFullYear(1990, 1, 1);
}
if (typeof options.max === "undefined" || options.max > minMax) {
options.max = new Date().setFullYear(2100,1,1);
}
var random = faker.datatype.number(options);
return new Date(random);
};
/**
* Returns a string, containing UTF-16 chars between 33 and 125 ('!' to '}')
*
*
* @method faker.datatype.string
* @param { number } length: length of generated string, default = 10, max length = 2^20
*/
this.string = function (length) {
if(length === undefined ){
length = 10;
}
var maxLength = Math.pow(2, 20);
if(length >= (maxLength)){
length = maxLength;
}
var charCodeOption = {
min: 33,
max: 125
};
var returnString = '';
for(var i = 0; i < length; i++){
returnString += String.fromCharCode(faker.datatype.number(charCodeOption));
}
return returnString;
};
/**
* uuid
*
* @method faker.datatype.uuid
*/
this.uuid = function () {
var RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
var replacePlaceholders = function (placeholder) {
var random = faker.datatype.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.datatype.boolean
*/
this.boolean = function () {
return !!faker.datatype.number(1);
};
/**
* hexaDecimal
*
* @method faker.datatype.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;
};
/**
* returns json object with 7 pre-defined properties
*
* @method faker.datatype.json
*/
this.json = function json() {
var properties = ['foo', 'bar', 'bike', 'a', 'b', 'name', 'prop'];
var returnObject = {};
properties.forEach(function(prop){
returnObject[prop] = faker.datatype.boolean() ?
faker.datatype.string() : faker.datatype.number();
});
return JSON.stringify(returnObject);
};
/**
* returns an array with values generated by faker.datatype.number and faker.datatype.string
*
* @method faker.datatype.array
* @param { number } length of the returned array
*/
this.array = function array(length) {
if(length === undefined){
length = 10;
}
var returnArray = new Array(length);
for(var i = 0; i < length; i++){
returnArray[i] = faker.datatype.boolean() ?
faker.datatype.string() : faker.datatype.number();
}
return returnArray;
};
return this;
}
module['exports'] = Datatype;