From d632d61d056ceb18e9dfa2d88d7f71a7c0436eb9 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Mon, 4 Sep 2023 13:31:37 -0700 Subject: [PATCH] Allow odd-length seed phrase with error catching E.g., 15 or 21-word seed phrases --- main.ts | 9 ++++++--- main_test.ts | 10 ++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/main.ts b/main.ts index 62d32cc..91777d4 100644 --- a/main.ts +++ b/main.ts @@ -6,9 +6,6 @@ export function parseInput(input: string | null) { .trim() .replaceAll(/\s+/g, " ") .split(" ").map((w) => w.trim()); - if (words.length % 2 === 1) { - throw new Error("Seed phrase length must be even."); - } return words; } @@ -52,6 +49,12 @@ if (import.meta.main) { const input = prompt("Seed phrase:"); const words = parseInput(input); const factors = getLowerFactors(words.length); + if (factors.length === 0) { + console.log( + `\nNo permutations found. Seed phrase of length ${words.length} can't be arranged into a grid.`, + ); + Deno.exit(0); + } const permutations = factors.map((columns) => getPermutations(words, columns)) .flat(); console.log("\n"); diff --git a/main_test.ts b/main_test.ts index 8b30638..df1910e 100644 --- a/main_test.ts +++ b/main_test.ts @@ -10,10 +10,6 @@ Deno.test("parseInput throws on null", () => { assertThrows(() => parseInput(null)); }); -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", @@ -141,6 +137,12 @@ Deno.test("getLowerFactors returns correct factors of 12", () => { assertFalse(factors.includes(6)); }); +Deno.test("getLowerFactors returns correct factors of 15", () => { + const factors = getLowerFactors(15); + assertArrayIncludes(factors, [3]); + assertFalse(factors.includes(5)); +}); + Deno.test("getLowerFactors returns correct factors of 24", () => { const factors = getLowerFactors(24); assertArrayIncludes(factors, [2, 3, 4]); -- 2.47.3