ethers.js/packages/wordlists/src.ts/decode-owla.ts

28 lines
961 B
TypeScript
Raw Permalink Normal View History

2022-04-11 17:09:17 -04:00
import { decodeBits } from "./bit-reader.js";
import { decodeOwl } from "./decode-owl.js";
export function decodeOwlA(data: string, accents: string): Array<string> {
let words = decodeOwl(data).join(",");
// Inject the accents
accents.split(/,/g).forEach((accent) => {
const match = accent.match(/^([a-z]*)([0-9]+)([0-9])(.*)$/);
if (!match) { throw new Error("internal error parsing accents"); }
let posOffset = 0;
const positions = decodeBits(parseInt(match[3]), match[4]);
const charCode = parseInt(match[2]);
const regex = new RegExp(`([${ match[1] }])`, "g");
words = words.replace(regex, (all, letter) => {
const rem = --positions[posOffset];
if (rem === 0) {
letter = String.fromCharCode(letter.charCodeAt(0), charCode);
posOffset++;
}
return letter;
});
});
return words.split(",");
}