-
Notifications
You must be signed in to change notification settings - Fork 66
/
generate-family.js
59 lines (51 loc) · 1.34 KB
/
generate-family.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
/**
* Generates random family data and outputs to stdout as formatted JSON.
*/
var chance = require('chance').Chance(),
argv = require('minimist')(process.argv.slice(2)),
generations = argv.n;
if(typeof generations === 'undefined'){
console.log('-n required to specify number of generations');
process.exit();
}
// Int format and descrement by one to account for first person
generations = parseInt(generations, 10) - 1;
var person = generatePerson();
person._parents = [
generateParents(generations - 1),
generateParents(generations - 1)
];
person._children = [
generateChildren(generations - 1),
generateChildren(generations - 1),
generateChildren(generations - 1)
];
// Print to stdout in formatted JSON
console.log(JSON.stringify(person, null, 2));
function generatePerson(){
return {
name: chance.name(),
id: chance.guid()
};
}
function generateParents(generations){
var person = generatePerson();
if(generations > 0){
person._parents = [
generateParents(generations - 1),
generateParents(generations - 1)
];
}
return person;
}
function generateChildren(generations){
var person = generatePerson();
if(generations > 0){
person._children = [
generateChildren(generations - 1),
generateChildren(generations - 1),
generateChildren(generations - 1)
];
}
return person;
}