From 20cae4bf84765ef61ab96da4a45b9b33dcea95eb Mon Sep 17 00:00:00 2001 From: Lucas <65281038+apple-phi@users.noreply.github.com> Date: Mon, 12 Aug 2024 17:27:48 +0100 Subject: [PATCH] Fix name extraction --- src/gedcom.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/gedcom.ts b/src/gedcom.ts index 9ca4485..bc890cf 100644 --- a/src/gedcom.ts +++ b/src/gedcom.ts @@ -32,11 +32,21 @@ function pointerToId(pointer: string): string { /** Extracts the first and last name from a GEDCOM name field. */ function extractName(name: string): { firstName?: string; lastName?: string } { - const arr = name.split('/'); - if (arr.length === 1) { - return { firstName: arr[0].trim() }; + const matches = name.match(/\/([^\/]+)\//); + if (matches) { + const lastName = matches[1].trim(); + // Replace the last name part (including slashes) and trim the result + let firstName = name.replace(matches[0], '').trim(); + // Replace any occurrence of double spaces with a single space + firstName = firstName.replace(/\s\s+/g, ' '); + return { + firstName: firstName || undefined, + lastName: lastName + }; + } else { + // If no last name is found, just return the trimmed and cleaned firstName + return { firstName: name.trim().replace(/\s\s+/g, ' ') }; } - return { firstName: arr[0].trim(), lastName: arr[1].trim() }; } /** Maps month abbreviations used in GEDCOM to month numbers. */