From: Ardis Lu Date: Mon, 4 Sep 2023 20:27:47 +0000 (-0700) Subject: Fix whitespace parsing X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=02ce3af7152cdb0b752594ca910c86ce50f05010;p=seed-phrase-transposer Fix whitespace parsing --- diff --git a/main.ts b/main.ts index 15cb401..62d32cc 100644 --- a/main.ts +++ b/main.ts @@ -2,7 +2,10 @@ export function parseInput(input: string | null) { if (input === null) { throw new Error("No seed phrase entered."); } - const words = input.split(/\s/g).map((w) => w.trim()); + const words = input + .trim() + .replaceAll(/\s+/g, " ") + .split(" ").map((w) => w.trim()); if (words.length % 2 === 1) { throw new Error("Seed phrase length must be even."); } diff --git a/main_test.ts b/main_test.ts index e4267be..8b30638 100644 --- a/main_test.ts +++ b/main_test.ts @@ -14,6 +14,13 @@ Deno.test("parseInput throws on invalid length", () => { assertThrows(() => parseInput("test test test")); }); +Deno.test("parseInput collapses whitespace correctly", () => { + const words = parseInput( + " \n\ntest \n\ntest test test test test\ntest\n \n\ntest \n\n", + ); + assertEquals(words.length, 8); +}); + Deno.test("parseInput parses 12 word seed phrase", () => { const words = parseInput( "test test test test test test test test test test test junk",