-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreason.js
74 lines (74 loc) · 1.9 KB
/
reason.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
(function(){
var reason = function () {
};
var isFunction = function (obj) {
return typeof obj === 'function';
}
reason.int = function (min, max) {
if (max == undefined) {
max = min;
min = 0;
}
return function () {
return parseInt(Math.random() * (max - min) + min);
};
};
reason.inc = function (start, step) {
start = start || 0;
step = step || 1;
return function () {
return start += isFunction(step) ? step() : step;
};
};
reason.float = function (min, max, fix) {
return function () {
return parseFloat((Math.random() * (max - min) + min).toFixed(fix || 0));
};
};
reason.boolean = function (probability) {
return function () {
return Math.random() < (probability >=0 ? probability : 0.5);
};
};
reason.string = function (length) {
var chars = 'abcdefghigklmnopqrstuvwxyz'.split(''),
radomint = reason.int(0, 24);
return function () {
var leg = isFunction(length) ? length() : length,
arr = new Array(leg);
while (leg > 0) arr[--leg] = chars[radomint()];
return arr.join('');
};
};
reason.object = function (obj) {
var keys = Object.keys(obj);
return function () {
var result = {};
keys.forEach(function (key) {
result[key] = obj[key]();
});
return result;
};
};
reason.array = function(content, length) {
return function () {
var leg = isFunction(length) ? length() : length,
arr = new Array(leg);
while (leg > 0) arr[--leg] = content();
return arr;
};
};
reason.oneof = function(candidates) {
var randomInt = reason.int(candidates.length);
return function () {
candidates[randomInt()];
}
};
if(define && isFunction(define)){
define(reason);
} else if (window) {
window.reason;
} else if (module && module.exports) {
module.exports = reason;
}
})();