]> git.ardis.lu - seed-phrase-transposer/commitdiff
Fix whitespace parsing
authorArdis Lu <ardisglu@gmail.com>
Mon, 4 Sep 2023 20:27:47 +0000 (13:27 -0700)
committerArdis Lu <ardisglu@gmail.com>
Mon, 4 Sep 2023 20:27:47 +0000 (13:27 -0700)
main.ts
main_test.ts

diff --git a/main.ts b/main.ts
index 15cb401410fa252d194f8d46a88b573c80a22394..62d32cc17fbdc1d2c1d1802bd29b1049e8763c84 100644 (file)
--- 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.");
   }
index e4267be841708aefd2ecfaf93df6ba462e98ce13..8b306382c8345cc1150dae2121c0b1dd800475e0 100644 (file)
@@ -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",
This page took 0.03441 seconds and 4 git commands to generate.