-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (77 loc) · 2.23 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
exports.sex = function(ascendancyNum){
if(validNumber(ascendancyNum) == false){
return "error";
}
var gender = undefined;
if(ascendancyNum % 2 == 1) {
gender = 'female';
ascendancyNum -= 1;
} else {
gender = 'male';
}
return gender;
};
exports.line = function (ascendancyNum){
if(validNumber(ascendancyNum) == false){
return "error";
}
var line = checkLine(ascendancyNum);
// If the number is already 1,2, or 3, no further calculations need to be run.
// Higher or missing numbers return as "undetermined"
if(line != 'undetermined') {
return line;
} else {
//If the number is odd, subtract 1, unless the number is 3 because that
// means the mother's line and that is as far as this should go.
//Otherwise it returns everyone as fathersLine
// The loop always returns 1,2,3 or undetermined
for(i = ascendancyNum; line == 'undetermined'; i /= 2) {
if(i % 2 == 1 && i != 3) {
i -= 1;
}
line = checkLine(i);
}
return line;
}
};
exports.fullLine = function(ascendancyNum) {
if(validNumber(ascendancyNum) == false){
return "error";
}
var ascendancy = [];
var i = ascendancyNum;
//M = Mother, F = Father
//This loop calculates like the one in exports.line
do {
if(i % 2 == 1) {
ascendancy.splice(0,0,"M");
if(i > 3){
i -= 1;
}
} else if(i % 2 == 0){
ascendancy.splice(0,0,"F");
} else {
throw "The ascendancy path can only be generated for Ahnen numbers higher than 1.";
}
i /= 2;
}
while(i >= 2);
return ascendancy;
};
var checkLine = function(ascendancyNum) {
switch(ascendancyNum) {
case 1:
return 'rootPerson';
case 2:
return 'fathersLine';
case 3:
return 'mothersLine';
default:
return 'undetermined';
}
};
var validNumber = function(ascendancyNum){
if(Number.isInteger(ascendancyNum) === false || ascendancyNum <= 0){
return false;
} else {return true;}
};