From 02ce3af7152cdb0b752594ca910c86ce50f05010 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Mon, 4 Sep 2023 13:27:47 -0700 Subject: [PATCH] Fix whitespace parsing --- main.ts | 5 ++++- main_test.ts | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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", -- 2.47.3