-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (190 loc) · 5.22 KB
/
index.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
var fs = require('fs');
var path = require('path');
var lorem = require('lorem-ipsum');
var fileToArray = function(path) {
var array = fs.readFileSync(path).toString().split(',');
return array;
};
var randomProperty = function (obj) {
var keys = Object.keys(obj);
return keys[Math.floor(Math.random() * keys.length)];
};
var randomArrayItem = function(array) {
return array[Math.floor(Math.random() * array.length)];
}
var randomArrayIndex = function(array) {
return Math.floor(Math.random() * array.length);
}
var randomDate = function() {
var start = new Date(2014,1,1,10,30,30,0);
var end = new Date(2015,1,1,10,30,30,0);
var date = new Date(+start + Math.random() * (end - start));
var hour = Math.random() * 23 | 0;
date.setHours(hour);
return date;
}
var createPassword = function(count) {
var count = count;
var charArray = fileToArray(path.join(__dirname, 'lib', 'chars.txt') );
var password = "";
for (var i = 0; i < count; i++) {
var index = randomArrayIndex(charArray),
item = charArray[index];
//check if it's a letter (not a number) and one of %50
if(index > 9 && index % 2 == 0) item = item.toUpperCase();
password += item;
};
return password;
}
var isArray = function(obj) {
return Object.prototype.toString.call(obj).indexOf('Array') > -1;
};
var transformArray = function(array) {
var objSchema = array[0];
var count = array[1];
var newArray = [];
for (var i = 0; i < count; i++) {
var newObject = {};
for ( schemaKey in objSchema) {
if(objSchema.hasOwnProperty(schemaKey)) {
if(dataFaker[objSchema[schemaKey]] !== undefined) {
if(objSchema[schemaKey] == 'eMail') {
newObject[schemaKey] = dataFaker[objSchema[schemaKey]](newObject);
}
else {
newObject[schemaKey] = dataFaker[objSchema[schemaKey]](count);
}
}
else {
var key = objSchema[schemaKey];
if ( isArray(key) ) {
newObject[schemaKey] = transformArray(key);
}
//Keys and counts (like 'lorem-10' or 'password-8')
else if(key.toString().indexOf('lorem') == 0
|| key.toString().indexOf('password') == 0 ) {
var keyArr = key.split('-'),
keyValue = keyArr[0],
keyCount = keyArr[1];
newObject[schemaKey] = dataFaker[keyValue](keyCount);
}
else {
newObject[schemaKey] = objSchema[schemaKey];
}
}
}
}
newArray.push(newObject);
}
return newArray;
};
var objectKeys = function (obj) {
//Check if schema argument is not array (object)
if(!isArray(obj)) {
Object.keys(obj).forEach(function (key) {
if(typeof obj[key] == "object") {
if(isArray(obj[key])) {
obj[key] = transformArray(obj[key]);
}
}
});
return obj;
}
else {
return transformArray(obj);
}
};
var dataFaker = {
names: {
female: fileToArray(path.join(__dirname, 'lib', 'female.txt')),
male: fileToArray(path.join(__dirname, 'lib', 'male.txt'))
},
surnames: fileToArray(path.join(__dirname, 'lib', 'surnames.txt')),
createUsers: function(count) {
var adjectives = fileToArray(path.join(__dirname, 'lib', 'adjectives.txt')),
nouns = fileToArray(path.join(__dirname, 'lib', 'nouns.txt')),
userNames = [];
for (var i = 0; i < count; i++) {
var item = randomArrayItem(adjectives) + '_' + randomArrayItem(nouns);
userNames.push(item);
};
this.userNames = userNames;
},
gender: function () {
if(! this.gender.gender) {
this.firstName();
}
return this.gender.gender;
},
firstName: function() {
var gender = randomProperty(this.names);
this.gender.gender = gender;
var index = Math.floor(Math.random() * this.names[gender].length);
var firstName = this.names[gender][index];
return firstName;
},
lastName: function() {
return randomArrayItem(this.surnames);
},
userName: function(count) {
//if datafaker.userNames already generated, then pick one
if( this.userNames && this.userNames.length > 0) {
var index = randomArrayIndex(this.userNames),
userName = this.userNames[index];
this.userNames.splice(index, 1);
return userName;
}
// if datafaker.userNames doesn't exist, generate it and return a username
else {
this.createUsers(count);
return this.userName();
}
},
password: function(charCount) {
return createPassword(charCount);
},
eMail: function(objSchema) {
if(objSchema.userName) return objSchema.userName + '@example.com';
else return '[email protected]'
},
date: function() {
return randomDate().toString();
},
lorem: function(wordCount) {
return lorem({
count: wordCount,
units: 'words'
});
},
schema: function(mainObject) {
return objectKeys(mainObject);
},
set: function() {
var propName = arguments[0],
array = arguments[1],
unique, range;
if(arguments.length === 3) {
var unique = arguments[2].unique;
var range = arguments[2].range;
}
this[propName] = function() {
var innerArray = array;
if(range) {
var min = innerArray[0],
max = innerArray[1];
var fakeArray = [];
for (var i = min; i < max + 1; i++) {
fakeArray.push(i);
}
innerArray = fakeArray;
}
var index = randomArrayIndex(innerArray);
var item = innerArray[index];
if(unique) {
innerArray.splice(index, 1);
}
return item;
}
}
};
module.exports = dataFaker;