.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;
}
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");
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",
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]);