1 |
|
2 |
|
3 |
|
4 |
|
5 | function Datatype (faker, seed) {
|
6 |
|
7 | if (Array.isArray(seed) && seed.length) {
|
8 | faker.mersenne.seed_array(seed);
|
9 | }
|
10 | else if(!isNaN(seed)) {
|
11 | faker.mersenne.seed(seed);
|
12 | }
|
13 |
|
14 | |
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | this.number = function (options) {
|
21 |
|
22 | if (typeof options === "number") {
|
23 | options = {
|
24 | max: options
|
25 | };
|
26 | }
|
27 |
|
28 | options = options || {};
|
29 |
|
30 | if (typeof options.min === "undefined") {
|
31 | options.min = 0;
|
32 | }
|
33 |
|
34 | if (typeof options.max === "undefined") {
|
35 | options.max = 99999;
|
36 | }
|
37 | if (typeof options.precision === "undefined") {
|
38 | options.precision = 1;
|
39 | }
|
40 |
|
41 |
|
42 | var max = options.max;
|
43 | if (max >= 0) {
|
44 | max += options.precision;
|
45 | }
|
46 |
|
47 | var randomNumber = Math.floor(
|
48 | faker.mersenne.rand(max / options.precision, options.min / options.precision));
|
49 |
|
50 | randomNumber = randomNumber / (1 / options.precision);
|
51 |
|
52 | return randomNumber;
|
53 |
|
54 | };
|
55 |
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | this.float = function (options) {
|
63 | if (typeof options === "number") {
|
64 | options = {
|
65 | precision: options
|
66 | };
|
67 | }
|
68 | options = options || {};
|
69 | var opts = {};
|
70 | for (var p in options) {
|
71 | opts[p] = options[p];
|
72 | }
|
73 | if (typeof opts.precision === 'undefined') {
|
74 | opts.precision = 0.01;
|
75 | }
|
76 | return faker.datatype.number(opts);
|
77 | };
|
78 |
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | this.datetime = function (options) {
|
87 | if (typeof options === "number") {
|
88 | options = {
|
89 | max: options
|
90 | };
|
91 | }
|
92 |
|
93 | var minMax = 8640000000000000;
|
94 |
|
95 | options = options || {};
|
96 |
|
97 | if (typeof options.min === "undefined" || options.min < minMax*-1) {
|
98 | options.min = new Date().setFullYear(1990, 1, 1);
|
99 | }
|
100 |
|
101 | if (typeof options.max === "undefined" || options.max > minMax) {
|
102 | options.max = new Date().setFullYear(2100,1,1);
|
103 | }
|
104 |
|
105 | var random = faker.datatype.number(options);
|
106 | return new Date(random);
|
107 | };
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | this.string = function (length) {
|
117 | if(length === undefined ){
|
118 | length = 10;
|
119 | }
|
120 |
|
121 | var maxLength = Math.pow(2, 20);
|
122 | if(length >= (maxLength)){
|
123 | length = maxLength;
|
124 | }
|
125 |
|
126 | var charCodeOption = {
|
127 | min: 33,
|
128 | max: 125
|
129 | };
|
130 |
|
131 | var returnString = '';
|
132 |
|
133 | for(var i = 0; i < length; i++){
|
134 | returnString += String.fromCharCode(faker.datatype.number(charCodeOption));
|
135 | }
|
136 | return returnString;
|
137 | };
|
138 |
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 | this.uuid = function () {
|
145 | var RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
|
146 | var replacePlaceholders = function (placeholder) {
|
147 | var random = faker.datatype.number({ min: 0, max: 15 });
|
148 | var value = placeholder == 'x' ? random : (random &0x3 | 0x8);
|
149 | return value.toString(16);
|
150 | };
|
151 | return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders);
|
152 | };
|
153 |
|
154 | |
155 |
|
156 |
|
157 |
|
158 |
|
159 | this.boolean = function () {
|
160 | return !!faker.datatype.number(1);
|
161 | };
|
162 |
|
163 |
|
164 | |
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | this.hexaDecimal = function hexaDecimal(count) {
|
171 | if (typeof count === "undefined") {
|
172 | count = 1;
|
173 | }
|
174 |
|
175 | var wholeString = "";
|
176 | for(var i = 0; i < count; i++) {
|
177 | 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"]);
|
178 | }
|
179 |
|
180 | return "0x"+wholeString;
|
181 | };
|
182 |
|
183 | |
184 |
|
185 |
|
186 |
|
187 |
|
188 | this.json = function json() {
|
189 |
|
190 | var properties = ['foo', 'bar', 'bike', 'a', 'b', 'name', 'prop'];
|
191 |
|
192 | var returnObject = {};
|
193 | properties.forEach(function(prop){
|
194 | returnObject[prop] = faker.datatype.boolean() ?
|
195 | faker.datatype.string() : faker.datatype.number();
|
196 | });
|
197 |
|
198 | return JSON.stringify(returnObject);
|
199 | };
|
200 |
|
201 | |
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | this.array = function array(length) {
|
209 |
|
210 |
|
211 | if(length === undefined){
|
212 | length = 10;
|
213 | }
|
214 | var returnArray = new Array(length);
|
215 | for(var i = 0; i < length; i++){
|
216 | returnArray[i] = faker.datatype.boolean() ?
|
217 | faker.datatype.string() : faker.datatype.number();
|
218 | }
|
219 | return returnArray;
|
220 |
|
221 | };
|
222 |
|
223 | return this;
|
224 | }
|
225 |
|
226 | module['exports'] = Datatype;
|