Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get parents from FAMS in case they're not married #63

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/ancestor-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ export class AncestorChart<IndiT extends Indi, FamT extends Fam>
if (!fam) {
continue;
}
const [father, mother] =
entry.family!.id === this.options.startFam &&
this.options.swapStartSpouses
? [fam.getMother(), fam.getFather()]
: [fam.getFather(), fam.getMother()];
const [father, mother] = this.getParents(fam, entry);
if (!father && !mother) {
continue;
}
Expand Down Expand Up @@ -134,4 +130,29 @@ export class AncestorChart<IndiT extends Indi, FamT extends Fam>
this.util.updateSvgDimensions(info);
return Object.assign(info, { animationPromise });
}

private getParents(fam: Fam, entry: TreeNode) {
let [father, mother] = [fam.getFather(), fam.getMother()];
if(!father && !mother) {
// get indis whose FAMS contains this family
const res: any[] = Array.from((this.options.data as any).indis.entries())
.filter((e: any[]) => e[1]?.json?.fams && e[1].json.fams.includes(entry.family?.id));
const getFromRes = function(pRes: any, sex: "M" | "F") {
const r = pRes.find((e: any) => e[1].json.sex === sex);
return r.length === 2
? r[1].json?.id || null
: null;
}
father = getFromRes(res, "M");
mother = getFromRes(res, "F");
}

if(this.options.swapStartSpouses) {
const pivot = father;
father = mother;
mother = pivot;
}

return [father || null, mother || null];
}
}
28 changes: 27 additions & 1 deletion tests/ancestor-chart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,36 @@ describe('Ancestor chart', () => {
const data = new JsonDataProvider(json);
const chart = new AncestorChart({
data,
startFam: 'F1',
startFam: json.fams[0].id,
renderer: new FakeRenderer(),
svgSelector: 'svg',
});
chart.render();
});

it('should work with FAM values only', () => {
// I1+I2
// F1
// |
// I3
const json: JsonGedcomData = {
fams: [
{id: 'F1', children: []},
],
indis: [
{id: 'I1', fams: ['F1'], sex: "M"},
{id: 'I2', fams: ['F1'], sex: "F"},
{id: 'I3', famc: 'F1', sex: "F"},
],
};
const data = new JsonDataProvider(json);
const chart = new AncestorChart({
data,
startFam: json.fams[0].id,
renderer: new FakeRenderer(),
svgSelector: 'svg',
});
chart.render();
});

});