From 26b12b215bef697828393fec8976cd46b6343cb6 Mon Sep 17 00:00:00 2001 From: Przemek Wiech Date: Fri, 17 Jan 2020 23:23:42 +0100 Subject: [PATCH] Code cleanup in wikitree.ts --- src/wikitree.ts | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/wikitree.ts b/src/wikitree.ts index 94509e7..e5b80fd 100644 --- a/src/wikitree.ts +++ b/src/wikitree.ts @@ -85,14 +85,6 @@ async function getRelatives(keys: string[], handleCors: boolean) { return response[0].items.map((x: {person: Person}) => x.person) as Person[]; } -/** Creates a family identifier given 2 spouse identifiers. */ -function getFamilyId(spouse1: number, spouse2: number) { - if (spouse2 > spouse1) { - return `${spouse1}_${spouse2}`; - } - return `${spouse2}_${spouse1}`; -} - /** * Loads data from WikiTree to populate an hourglass chart starting from the * given person ID. @@ -111,6 +103,7 @@ export async function loadWikiTree( everyone.push(...ancestors); }); + // Fetch descendants recursively. let toFetch = [key]; while (toFetch.length > 0) { const people = await getRelatives(toFetch, handleCors); @@ -127,18 +120,11 @@ export async function loadWikiTree( // Map from person id to the set of families where they are a spouse. const families = new Map>(); + // Map from family id to the set of children. const children = new Map>(); + // Map from famliy id to the spouses. const spouses = new Map(); - function getSet(map: Map>, id: K): Set { - const set = map.get(id); - if (set) { - return set; - } - const newSet = new Set(); - map.set(id, newSet); - return newSet; - } - + // Map from numerical id to human-readable id. const idToName = new Map(); everyone.forEach((person) => { @@ -156,7 +142,6 @@ export async function loadWikiTree( }); const indis: JsonIndi[] = []; - const converted = new Set(); everyone.forEach((person) => { if (converted.has(person.Id)) { @@ -191,6 +176,14 @@ export async function loadWikiTree( return {chartData: {indis, fams}, gedcom}; } +/** Creates a family identifier given 2 spouse identifiers. */ +function getFamilyId(spouse1: number, spouse2: number) { + if (spouse2 > spouse1) { + return `${spouse1}_${spouse2}`; + } + return `${spouse2}_${spouse1}`; +} + function convertPerson(person: Person): JsonIndi { const indi: JsonIndi = { id: person.Name, @@ -226,6 +219,10 @@ function convertPerson(person: Person): JsonIndi { return indi; } +/** + * Parses a date in the format returned by WikiTree and converts in to + * the format defined by Topola. + */ function parseDate(date: string, dataStatus?: string) { if (!date) { return undefined; @@ -294,3 +291,17 @@ function buildGedcom(indis: JsonIndi[]): GedcomData { other: {}, }; } + +/** + * Returns a set which is a value from a SetMultimap. If the key doesn't exist, + * an empty set is added to the map. + */ +function getSet(map: Map>, key: K): Set { + const set = map.get(key); + if (set) { + return set; + } + const newSet = new Set(); + map.set(key, newSet); + return newSet; +}