forked from adleroliveira/dreamjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdream.js
More file actions
431 lines (350 loc) · 10 KB
/
dream.js
File metadata and controls
431 lines (350 loc) · 10 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
'use strict'
var
_ = require('lodash'),
RandExp = require('randexp'),
chance = require('chance').Chance(),
djson = require('describe-json');
var _schemas = [];
var _customTypes = [
{
name: 'number',
customType: function () {
return chance.natural();
}
},
{
name: 'boolean',
customType: function () {
return chance.bool();
}
},
{
name: 'null',
customType: function () {
return null;
}
}
];
var _dreamHelper = {
chance: chance,
oneOf: function(collection){
return _.sample(collection);
}
};
var _defaultOutput = {
Dream: 'Hello World'
};
var _genericSchema = {
name: 'generic',
schema: {
default: String
}
};
function Dream() {
this._selectedSchema;
this._output;
this._dreamHelper = _dreamHelper;
this._input;
this.defaultSchema = function defaultSchema(schema) {
_genericSchema = validateAndReturnSchema(schema);
return this;
}.bind(this);
this.useSchema = function useSchema(schema) {
var
schemaToUse,
dreamInstance;
schemaToUse = validateAndReturnSchema(schema);
dreamInstance = new Dream();
dreamInstance.schema(schemaToUse)
dreamInstance._selectedSchema = schemaToUse;
return dreamInstance;
}.bind(this);
this.input = function input(input) {
this._dreamHelper.input = this._input = input;
return (this);
}.bind(this);
this.generateSchema = function generateSchema() {
var
describedJson,
schemaName,
jsonInput,
validatedJsonInput,
guessProperties,
newSchema,
args = [];
Array.prototype.push.apply(args, arguments);
args.forEach(function (argument) {
switch (typeof (argument)) {
case 'string':
schemaName = argument;
break;
case 'object':
jsonInput = argument;
break;
case 'boolean':
guessProperties = argument;
break;
}
});
validatedJsonInput = _.first(_.flatten([jsonInput]));
describedJson = djson.describe(validatedJsonInput || {});
newSchema = {
name: schemaName || 'generic',
schema: describedJson
};
if (guessProperties === true) {
newSchema.schema = guessCustomTypes(describedJson);
};
addOrReplace(_schemas, newSchema);
return this;
}.bind(this);
this.customType = function customType(typeName, customType) {
var
newCustomType = {},
validTypeName;
validTypeName = typeof (typeName) === 'string' ? typeName : 'generic';
if (customType.constructor === RegExp) {
newCustomType = {
name: validTypeName,
customType: function () {
return new RandExp(customType).gen();
}
};
} else if (typeof (customType) === 'function') {
newCustomType = {
name: validTypeName,
customType: customType
};
} else {
newCustomType = {
name: validTypeName,
customType: function () {
return '[Invalid Custom Type]';
}
};
};
addOrReplace(_customTypes, newCustomType);
return this;
}.bind(this);
this.output = function output(callback) {
var output;
output = this._output || generateOutput();
this.cleanse();
if (typeof (callback) === 'function') {
callback(null, output);
return this;
} else {
return output;
}
}.bind(this);
this.cleanse = function cleanse() {
this._output = null;
this._selectedSchema = null;
}.bind(this);
this.flushSchemas = function flushSchemas() {
_schemas = [];
return this;
}.bind(this);
this.schema = function schema(schema) {
var
validatedSchema,
newSchema,
args = [];
Array.prototype.push.apply(args, arguments);
if (args.length > 1) {
newSchema = {
name: typeof (args[0]) === 'string' ? args.shift() : 'generic',
schema: typeof (args[0]) === 'object' ? args.shift() : {}
};
} else {
newSchema = schema;
}
validatedSchema = validateAndReturnSchema(newSchema);
if (validatedSchema.name === 'generic') {
this._selectedSchema = validatedSchema;
} else {
addOrReplace(_schemas, validatedSchema);
if (_schemas.length === 1) {
this._selectedSchema = validatedSchema;
};
}
return this;
}.bind(this);
this.generate = function generate(amount, generateRandomData) {
var
outputItem,
iterations = amount || 1,
outputArray = [],
i = 0;
for (; i < iterations; i++) {
outputItem = generateOutputFromSchema(selectAvailableSchema(), generateRandomData)
outputArray.push(outputItem);
};
this._output = outputArray.length === 1 ? outputArray[0] : outputArray;
return this;
}.bind(this);
this.generateRnd = function generateRnd(amount) {
return this.generate(amount, true);
};
var addOrReplace = function addOrReplace(collection, item) {
var
index;
index = _.indexOf(collection, _.find(collection, { name: item.name }));
if (index >= 0) {
collection.splice(index, 1, item);
} else {
collection.push(item);
};
return collection;
};
var guessCustomTypes = function guessCustomTypes(schemaObject) {
var
customTypeExists,
temporaryList = [];
_.forIn(schemaObject, function (value, key) {
if (typeof (value) === 'object') {
if (Array.isArray(value)) {
value.forEach(function (item) {
if (typeof (item) === 'object') {
temporaryList.push(guessCustomTypes(item));
} else {
temporaryList.push(item.toString());
};
});
schemaObject[key] = temporaryList;
} else {
schemaObject[key] = guessCustomTypes(value);
};
} else {
customTypeExists = _.find(_customTypes, { name: key.toString() });
if (typeof (chance[key.toString()]) === 'function' || customTypeExists !== undefined) {
schemaObject[key] = key.toString();
};
};
});
return schemaObject;
};
var validateAndReturnSchema = function validateAndReturnSchema(schema) {
if (isValidSchema(schema)) return schema;
if (typeof (schema) === 'string') {
var foundSchema = _.findWhere(_schemas, { name: schema });
return isValidSchema(foundSchema) ? foundSchema : _genericSchema;
};
if (typeof (schema) === 'object') {
return {
name: 'generic',
schema: schema
};
};
return _genericSchema;
}.bind(this);
var selectAvailableSchema = function selectAvailableSchema() {
if (this._selectedSchema) {
return this._selectedSchema;
};
if (thereIsSchema() && _schemas.length === 1) {
return _schemas[0];
};
return _genericSchema;
}.bind(this);
var generateOutput = function generateOutput() {
if (this._selectedSchema) {
return generateOutputFromSchema(this._selectedSchema);
} else {
return _defaultOutput;
};
}.bind(this);
var generateOutputFromSchema = function generateOutputFromSchema(schema, generateValues) {
var
outputObject = {},
schemaToUse = validateAndReturnSchema(schema);
_.forIn(schemaToUse.schema, function (value, key) {
outputObject[key] = getValueFromType(value, generateValues);
});
return outputObject;
}.bind(this);
var getValueFromType = function getValueFromType(propertyType, generateValues) {
var
value,
temporaryList = [],
temporaryObject = {},
temporaryValue,
customTypeIndex,
customTypeNeedle,
context = this,
types = {
'number': Number,
'string': String,
'boolean': Boolean,
'array': Array,
'object': Object,
'function': Function,
'date': Date
};
if (propertyType.constructor === RegExp) {
if (generateValues) {
return new RandExp(propertyType).gen();
} else {
return types[typeof (new RandExp(propertyType).gen())]();
};
};
switch (typeof (propertyType)) {
case 'string':
customTypeNeedle = _.find(_customTypes, { name: propertyType });
customTypeIndex = _.indexOf(_customTypes, customTypeNeedle);
if (customTypeIndex >= 0) {
temporaryValue = customTypeNeedle.customType(this._dreamHelper);
} else {
if (propertyType === 'array') {
temporaryValue = [];
} else {
temporaryValue = (typeof (chance[propertyType]) === 'function') ? chance[propertyType]() : '[Unknown Custom Type]';
};
};
if (generateValues) {
value = temporaryValue;
} else {
value = types[typeof (temporaryValue)]();
};
break;
case 'function':
temporaryValue = propertyType();
if (propertyType === Date) {
value = new Date(temporaryValue);
} else {
if (generateValues) {
value = isNative(propertyType) ? chance[typeof (temporaryValue)]() : temporaryValue;
} else {
value = Array.isArray(temporaryValue) ? types['array']() : types[typeof (temporaryValue)]();
}
}
break;
case 'object':
if (Array.isArray(propertyType)) {
propertyType.forEach(function (item) {
temporaryList.push(getValueFromType.call(context, item, generateValues));
});
value = temporaryList;
} else {
_.forIn(propertyType, function (value, key) {
temporaryObject[key] = getValueFromType.call(context, value, generateValues);
});
value = temporaryObject;
};
break;
default:
value = '[Invalid Property]';
};
return value;
}.bind(this);
var isValidSchema = function isValidSchema(schema) {
return _.has(schema, 'name') && _.has(schema, 'schema');
};
var thereIsSchema = function thereIsSchema() {
return _schemas.length > 0;
};
var isNative = function isNative(func) {
return !('prototype' in func);
};
};
module.exports = new Dream();