-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.js
105 lines (96 loc) · 2.55 KB
/
generator.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
module.exports = function(schema,num,undefined){
var mongoose = require('mongoose');
function randomDate(start, end) {
if(start == undefined) start = new Date(1970)
if(end == undefined) end = new Date()
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
function randomIntFromInterval(min,max)
{
return Math.floor(Math.random()*(max-min+1)+min);
}
function range(lowEnd,highEnd){
var arr = [],
c = highEnd - lowEnd + 1;
while ( c-- ) {
arr[c] = highEnd--
}
return arr;
}
var gen = {
objectid: function(){
return mongoose.Types.ObjectId()
},
date: function(){
return randomDate().toString();
},
boolean: function(){
return this.bool()
},
bool: function(){
return (Math.random() < 0.5)
},
number: function(){
return Number(Math.random().toString().split(".")[1])
},
string: function(){
return Math.random().toString(36).replace(/[^a-z]+/g, '')
},
array: function(){
return new Array(num)
}
}
;
var get = function(type){
// console.log(type.name,type)
// if(type.length == 1 && typeof type[0] == "object")
// console.log(type.length,type.length == 1 && typeof type == "object", type)
console.log(type.name)
var s = "";
if(type.name != undefined ) {
s = type.name
}else if(type.type != undefined && type.type.name){
s = type.type.name
}else if(type.length == 1 && typeof type == "object" && type[0].tree!=undefined){
console.log(type)
s = "schema"
return init(type[0].tree)
}else if(typeof type=="object", type instanceof Object == true){
return init_object(type,0)
}else{
console.log(type.length, typeof type, type instanceof Object)
}
if(s!=undefined) s = s.toString().toLowerCase()
// console.log(s)
if(gen[s] != undefined) return gen[s]();
return "type_not_found: "+ type
}
var exc = ['_id','id','__v'];
function init_object (schema){
var arr = {};
for(prop in schema){
if(exc.indexOf(prop) !== -1) continue;
var type = schema[prop];
arr[prop] = get(type);
}//end loop schema
return arr
}
function init (schema,num){
var arr = [];
var i = 0;
num = num || randomIntFromInterval(0,10)
for (el in range(0, num)){
arr[i] = {};
for(prop in schema){
if(exc.indexOf(prop) !== -1) continue;
var type = schema[prop];
// console.log(type,'==')
if(type instanceof Array) arr[i][prop] = init(type[0].schema.tree,num);
else arr[i][prop] = get(type);
}//end loop schema
i++;//increment by 1
}//end range
return arr;
}
return init(schema,num)//start init
}