Skip to content

Commit 97bd030

Browse files
committed
Add est. birth year to place-only birth fact.
1 parent f9db6e6 commit 97bd030

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

gx-fix.js

+38-28
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ function getRecordYear(gx) {
7575
// Ensure that anyone with an age has at least an estimated birth year.
7676
// - First use an age from a qualifier on an event with a date.
7777
// - Next use an age field and the date of the primary event, if any, or else the record date.
78-
// Also, REMOVE an estimated birth year (i.e., with "about" on date, and/or a PR_EST_BIR_DATE field)
79-
// when another birth event is also there.
78+
// Also, REMOVE an estimated birth year (i.e., with "about" on date
79+
// when another birth event with a non-'about' date is also there.
8080
function updateEstimatedBirthYear(gx) {
81-
function countBirthFacts(person) {
81+
function countBirthFactsWithDates(person) {
8282
let numBirthFacts = 0;
8383
for (let fact of getList(person, "facts")) {
8484
if (fact.type && fact.type.endsWith("/Birth") && fact.date) {
@@ -89,58 +89,68 @@ function updateEstimatedBirthYear(gx) {
8989
}
9090

9191
function findEstimatedBirthFact(facts) {
92-
function hasLabelId(fact, labelId) {
93-
for (let field of getList(fact, "fields")) {
94-
for (let fieldValue of getList(field.values)) {
95-
if (fieldValue.labelId === labelId) {
96-
return true;
97-
}
98-
}
92+
for (let f = 0; f < facts.length; f++) {
93+
let fact = facts[f];
94+
if (fact.type && fact.type.endsWith("/Birth") && fact.date && fact.date.original && fact.date.original.startsWith("about")) {
95+
return f;
9996
}
100-
return false;
10197
}
98+
return -1;
99+
}
102100

103-
for (let f = 0; f < facts.length; f++) {
104-
let fact = facts[f];
105-
if (fact.type && fact.type.endsWith("/Birth") && fact.date && fact.date.original && !fact.place) {
106-
if (fact.date.original.startsWith("about") || hasLabelId(fact, "PR_EST_BIR_DATE")) {
107-
return f;
101+
function findBirthFactWithOnlyPlace(person) {
102+
if (person.facts) {
103+
for (let fact of person.facts) {
104+
if (fact.type === "http://gedcomx.org/Birth" && fact.place && !fact.date) {
105+
return fact;
108106
}
109107
}
110108
}
111-
return -1;
109+
return null;
112110
}
113111

114112
function addEstimatedBirthFact(person, birthYear) {
115113
if (!person.facts) {
116114
person.facts = [];
117115
}
118116
let estBirthYear = "about " + birthYear;
119-
person.facts.push({
120-
"id": generateLocalId("f_"),
121-
"type": "http://gedcomx.org/Birth",
122-
"date": {
123-
"original": estBirthYear
124-
}
125-
});
117+
let placeOnlyBirthFact = findBirthFactWithOnlyPlace(person);
118+
if (placeOnlyBirthFact) {
119+
placeOnlyBirthFact.date = {"original": estBirthYear};
120+
}
121+
else {
122+
person.facts.push({
123+
"id": generateLocalId("f_"),
124+
"type": "http://gedcomx.org/Birth",
125+
"date": {
126+
"original": estBirthYear
127+
}
128+
});
129+
}
126130
}
127131

128132
if (!gx) {
129133
return;
130134
}
131135
let recordDate = getRecordYear(gx);
132136
for (let person of getList(gx, "persons")) {
133-
let numBirthFacts = countBirthFacts(person);
134-
if (numBirthFacts === 0) {
137+
let numBirthFactsWithDates = countBirthFactsWithDates(person);
138+
if (numBirthFactsWithDates === 0) {
135139
let birthYear = estimateBirthYearFromPersonAge(person, recordDate);
136140
if (birthYear) {
137141
addEstimatedBirthFact(person, birthYear);
138142
}
139143
}
140-
else if (numBirthFacts > 1) {
144+
else if (numBirthFactsWithDates > 1) {
141145
let estimatedBirthFactIndex = findEstimatedBirthFact(person.facts);
142146
if (estimatedBirthFactIndex >= 0) {
143-
person.facts.splice(estimatedBirthFactIndex, 1);
147+
let fact = person.facts[estimatedBirthFactIndex];
148+
if (fact.place) {
149+
fact.date = null;
150+
}
151+
else {
152+
person.facts.splice(estimatedBirthFactIndex, 1);
153+
}
144154
}
145155
}
146156
}

0 commit comments

Comments
 (0)