1 |
|
2 |
|
3 |
|
4 | 'use strict'
|
5 |
|
6 | var ObjectID = require('mongodb').ObjectID
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | module.exports.randomId = function () {
|
13 | return new ObjectID()
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | module.exports.ObjectId = module.exports.ObjectID = ObjectID
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | module.exports.randomStr = function (len, alphabet) {
|
28 | var i, str = ''
|
29 | len = len || 7
|
30 | alphabet = alphabet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
31 | for (i = 0; i < len; i++) {
|
32 | str += alphabet[Math.floor(Math.random() * alphabet.length)]
|
33 | }
|
34 | return str
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | module.exports.randomHex = function (len) {
|
43 | return module.exports.randomStr(len, '0123456789abcdef')
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | module.exports.randomCode = function (len) {
|
52 | return module.exports.randomStr(len, '0123456789')
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | module.exports.randomEmail = function (domain) {
|
61 | domain = domain || 'example.com'
|
62 | return 'test-' + module.exports.randomId() + '@' + domain
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | module.exports.randomUrl = function (base) {
|
71 | base = base || 'http://example.com'
|
72 | return base + '/' + module.exports.randomId()
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | module.exports.random = function (min, max) {
|
85 | if (min === undefined && max === undefined) {
|
86 | return Math.random()
|
87 | } else if (max === undefined) {
|
88 | return Math.random() * min
|
89 | } else {
|
90 | return min + Math.random() * (max - min)
|
91 | }
|
92 | }
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | module.exports.randomInt = function (min, max) {
|
103 | if (min === undefined && max === undefined) {
|
104 | return module.exports.randomInt(0, 100)
|
105 | } else if (max === undefined) {
|
106 | return module.exports.randomInt(0, min)
|
107 | } else {
|
108 | return Math.floor(module.exports.random(min, max))
|
109 | }
|
110 | }
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | module.exports.randomBool = function () {
|
116 | return Math.random() < 0.5
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | module.exports.randomDate = function (interval, baseDate) {
|
126 | if (interval instanceof Date) {
|
127 | baseDate = interval
|
128 | interval = 86.4e6
|
129 | } else {
|
130 | interval = interval || 86.4e6
|
131 | baseDate = baseDate || new Date
|
132 | }
|
133 | return new Date(baseDate.getTime() - Math.random() * interval)
|
134 | }
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | module.exports.randomOf = function () {
|
142 | return arguments[Math.floor(Math.random() * arguments.length)]
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | module.exports.empty = {} |
\ | No newline at end of file |