]> git.ardis.lu - seed-phrase-transposer/commitdiff
Allow odd-length seed phrase with error catching
authorArdis Lu <ardisglu@gmail.com>
Mon, 4 Sep 2023 20:31:37 +0000 (13:31 -0700)
committerArdis Lu <ardisglu@gmail.com>
Mon, 4 Sep 2023 20:31:37 +0000 (13:31 -0700)
E.g., 15 or 21-word seed phrases

main.ts
main_test.ts

diff --git a/main.ts b/main.ts
index 62d32cc17fbdc1d2c1d1802bd29b1049e8763c84..91777d46a288f4a18990c480b4dfb693c0e99bb2 100644 (file)
--- 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");
index 8b306382c8345cc1150dae2121c0b1dd800475e0..df1910e36670abc33d0fc0378087722665c0eedf 100644 (file)
@@ -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<number>(factors, [3]);
+  assertFalse(factors.includes(5));
+});
+
 Deno.test("getLowerFactors returns correct factors of 24", () => {
   const factors = getLowerFactors(24);
   assertArrayIncludes<number>(factors, [2, 3, 4]);
This page took 0.044311 seconds and 4 git commands to generate.