-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbasic.js
352 lines (294 loc) · 9.57 KB
/
basic.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* yod-mock
* https://github.com/qiu8310/yod-mock
*
* Copyright (c) 2015 Zhonglei Qiu
* Licensed under the MIT license.
*/
module.exports = function(yod, def, _) {
yod.type('Boolean & Bool', def(function() {
/**
* Generate a random boolean value.
*
* @name Boolean
* @alias Bool
*
* @rule ([Number probability = 0.5]) -> Boolean
* @rule (String percentage) -> Boolean
*
* @example
*
* // Ten percent's probability return true
* @Bool(0.1);
* @Bool('10%');
*/
return _.prob(this.$has('probability') ? this.probability : this.percentage);
}));
yod.type('Double & Float', def(function() {
/**
*
* Generate a random float value.
*
* __Note: when you specified format to `2`, you may get a random value `0.10`, and the last `0` will be ignored, so you will get `0.1` eventually.__
*
* @name Double
* @alias Float
*
* @defaults {min: 0, max: 1, format: '1-4'}
*
* @rule ([[Number max], [String format]]) -> float
* @rule (Number min, Number max, [String format]) -> float
* @rule (Number min, Number max, Number format) -> float
*
* @example
*
* @Float('2'); // generate value likes: 0.34
* @Float(5, '1-3'); // generate value likes: 3.215 or 2.1 or 4.23
* @Float(8, 9, '1'); // generate value likes: 8.2
*
*/
var result = _.random(this.min, this.max, true);
// 如果 format 设置不规范,就使用 1-10 模式,设置 '-5' => '1-5', '5-' => '5-10', '5' => '5-5'
var dMin = 1, dMax = 10;
var format = String(this.format).split('-').slice(0, 2);
if (format.length < 2) { format.unshift(format[0]); }
format = _.map(format, function(n, i) {
var r = parseInt(n, 10);
return _.isNaN(r) ? (i ? dMax : dMin) : r;
});
result = result.toFixed(_.random(format[0], format[1]));
// 最后几位如果是 0,精度会丢失
return parseFloat(result);
}));
yod.type('Integer & Int', def(function() {
/**
* Generate a random integer between `min` to `max` (include `min` and `max`).
*
* @name Integer
* @alias Int
* @rule ([[Integer min = 0, ] Integer max = 1000]) -> Integer
*
* @example
*
* @Int(); // return a integer between 0 - 1000
* @Int(10); // return a integer between 0 - 10
* @Int(10, 100); // return a integer between 10 - 100
*
*/
return _.random(this.min, this.max);
}));
yod.type('Number', def(function() {
/**
*
* Generator a random number value (can be a random float or integer value).
*
* @name Number
* @rule () -> Number
*/
return _.prob() ? yod('@Double') : yod('@Integer');
}));
// config
var chars = {
vowel: 'aeiou',
hash: '0123456789abcdef',
consonant: 'bcdfghjklmnpqrstvwxyz',
lower: 'abcdefghijklmnopqrstuvwxyz',
upper: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
number: '0123456789',
symbol: '!@#$%^&*()[]'
};
chars.alpha = chars.lower + chars.upper + chars.number;
chars.all = chars.alpha + chars.symbol;
// 使用 yod.config 的目的是使的用户可以修改这些默认的值
yod.config('system.character', chars, {category: 'alpha'});
// define
yod.type('Character & Char', def(function() {
/**
*
* Generate a random character.
*
* Available pre-defined pools: `vowel`, `hash`, `consonant`, `lower`, `upper`, `number`, `symbol`, `alpha` and `all`
*
* - If parameter `pool` not in pre-defined pools, then itself will become a characters pool.
* - You can set `useAsPool` to `true` to force pre-defined pool key words to become a normal characters pool.
*
* @name Character
* @alias Char
* @rule ([String pool = 'alpha' [, Boolean useAsPool = false]]) -> Char
*
* @example
*
* @Char('vowel'); // will return random character in 'aeiou'
* @Char('vowel', true); // will return random character in 'vowel'
*
*/
if (chars[this.pool] && !this.useAsPool) {
return _.sys('character', {category: this.pool});
}
return _.sample(this.pool);
}));
yod.type('String & Str', def(function() {
/**
* Generate a random string.
*
* - you can specified the string's character generate pool
* - you can specified string length by setting `length` argument
* - you can set `min` and `max` to get a random length string which length is between `min` to `max`
*
* @name String
* @alias Str
*
* @defaults {min: 2, max: 20}
*
* @rule ([String pool = 'alpha' ] ) -> string
* @rule ([String pool = 'alpha' ,] int length) -> string
* @rule ([String pool = 'alpha' ,] int min, int max) -> string
*/
var length = this.$has('length') ? this.length : _.random(this.min, this.max);
return yod('@Char("%s").repeat(%d, "")', this.pool, length);
}));
function toIntJSDate(key, relative, otherwise) {
key = String(key);
// 1410715640.579, 1410715640, 1410715640579
if (/^(\d{10})\.?(\d{1,3})?$/.test(key)) {
var s = RegExp.$1, ms = RegExp.$2 || 0;
return (s - 0) * 1000 + (ms - 0);
} else if (/^-?[\d.]+$/.test(key)) {
var float = parseFloat(key);
return _.isNaN(float) ? otherwise : relative + float * 1000;
} else {
var d = (new Date(key)).getTime();
if (_.isNaN(d)) {
return otherwise;
}
return d;
}
}
yod.type('Date', def(function() {
/**
* Generate a random timestamp or formatted date.
*
* Caller | Description
* --------------------------------- | ----------------
* @Date() | last ten year's random timestamp
* @Date(0) | last ten year to next ten year's random timestamp
* @Date(-2) | last two year's timestamp
* @Date(3) | next three year's timestamp
* @Date(-1, 3600) | next one hour's timestamp
* @Date("3600", "7200") | next one hour to next two hour's timestamp
* @Date("2011-1-1", "2011-12-31") | between 2011-1-1 00:00:00 to 2011-12-31 00:00:00
*
*
* Default format is timestamp, you can specified date format in the first argument, for example:
*
* `@Date('YYYY-MM-DD HH:mm:ss', -2)`
*
* __Date format reference:http://momentjs.com/docs/#/displaying/format/__
*
* @name Date
*
* @defaults {format: timestamp}
*
* @rule ([String format,] [Integer flag = -10, [Nature range]]) -> String
* @rule ([String format,] String from, String to) -> String
*
*/
var from, to, now = new Date().getTime(), oneYearMs = 3600000 * 24 * 365;
if (this.$has('flag')) {
var flag = this.flag;
var range = this.$has('range') ? this.range * 1000 : Math.abs(flag || 10) * oneYearMs;
from = flag > 0 ? now : now - range;
to = flag < 0 ? now : now + range;
} else {
from = toIntJSDate(this.from, now, now - oneYearMs);
to = toIntJSDate(this.to, now, now + oneYearMs);
}
var random = _.random(from, to);
if (this.format === 'timestamp') {
return Math.round(random / 1000);
} else if (this.format === 'jsTimestamp') {
return Math.round(random);
} else {
return _.moment(random).format(this.format);
}
}));
yod.type('Range', def(function() {
/**
* Generate a specified integer range.
*
* @name Range
*
* @defaults {start: 0, stop: 10, step: 1}
*
* @rule () -> Array
* @rule (Integer stop) -> Array
* @rule (Integer start, Integer stop) -> Array
* @rule (Integer start, Integer stop, Integer step) -> Array
*/
return _.range(this.start, this.stop, this.step);
}));
var seqSeeds = {};
yod.type('Id & Sequence & Seq', def(function() {
/**
* Generate a rand auto increment id.
*
* @name Id
* @alias Sequence
* @alias Seq
*
* @rule ([String seed = '_d'], [Integer start = 1 [, Integer step = 1]]) -> int
*/
if (!(this.seed in seqSeeds)) {
seqSeeds[this.seed] = this.start;
return this.start;
}
seqSeeds[this.seed] += this.step;
return seqSeeds[this.seed];
}));
yod.type('Guid & GUID & Uuid & UUID', def(function() {
/**
*
* Generate a random [guid](http://zh.wikipedia.org/wiki/%E5%85%A8%E5%B1%80%E5%94%AF%E4%B8%80%E6%A0%87%E8%AF%86%E7%AC%A6)
*
* __format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx__
*
* - x is replaced with a random hexadecimal digit from 0 to f
* - y is replaced with a random hexadecimal digit from 8 to b
*
* @name Guid
* @alias GUID
* @alias Uuid
* @alias UUID
*
* @rule () -> string
*
*/
/* jshint ignore:start */
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
/* jshint ignore:end */
}));
yod.type('Objectid & ObjectId & Oid', def(function() {
/**
* Generate a random [object id](http://docs.mongodb.org/manual/reference/object-id/)
*
* @name Objectid
* @alias ObjectId
* @alias Oid
* @rule () -> string
*/
return yod('@Char(hash).repeat(24, "")');
}));
yod.type('Md5 & MD5', def(function() {
/**
* Generate a random md5 encrypted value.
*
* @name Md5
* @alias MD5
* @rule () -> string
*/
return yod('@Char(hash).repeat(32, "")');
}));
};