decode-owla.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. import { assertArgument } from "../utils/index.js";
  2. import { decodeBits } from "./bit-reader.js";
  3. import { decodeOwl } from "./decode-owl.js";
  4. /**
  5. * @_ignore
  6. */
  7. export function decodeOwlA(data: string, accents: string): Array<string> {
  8. let words = decodeOwl(data).join(",");
  9. // Inject the accents
  10. accents.split(/,/g).forEach((accent) => {
  11. const match = accent.match(/^([a-z]*)([0-9]+)([0-9])(.*)$/);
  12. assertArgument(match !== null, "internal error parsing accents", "accents", accents);
  13. let posOffset = 0;
  14. const positions = decodeBits(parseInt(match[3]), match[4]);
  15. const charCode = parseInt(match[2]);
  16. const regex = new RegExp(`([${ match[1] }])`, "g");
  17. words = words.replace(regex, (all, letter) => {
  18. const rem = --positions[posOffset];
  19. if (rem === 0) {
  20. letter = String.fromCharCode(letter.charCodeAt(0), charCode);
  21. posOffset++;
  22. }
  23. return letter;
  24. });
  25. });
  26. return words.split(",");
  27. }