-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc-rand.js
More file actions
32 lines (26 loc) · 695 Bytes
/
doc-rand.js
File metadata and controls
32 lines (26 loc) · 695 Bytes
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
"use strict";
const fs = require('fs');
const rand = function(magnitude) {
return Math.floor(Math.random() * magnitude);
}
const randElement = function(array) {
return array[rand(array.length)];
}
const fromFile = function(jsonFilePath, outputCount) {
return fromObject(JSON.parse(fs.readFileSync(jsonFilePath, 'utf8')), outputCount);
}
const fromObject = function(documentSet, outputCount) {
const output = [];
for(let i = 0; i < outputCount; i++) {
let doc = {};
for(var property in documentSet) {
doc[property] = randElement(documentSet[property]);
}
output.push(doc);
}
return {documentList: output};
}
module.exports = {
fromFile,
fromObject
}