decode-owl.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { assertArgument } from "../utils/index.js";
  2. const subsChrs = " !#$%&'()*+,-./<=>?@[]^_`{|}~";
  3. const Word = /^[a-z]*$/i;
  4. function unfold(words, sep) {
  5. let initial = 97;
  6. return words.reduce((accum, word) => {
  7. if (word === sep) {
  8. initial++;
  9. }
  10. else if (word.match(Word)) {
  11. accum.push(String.fromCharCode(initial) + word);
  12. }
  13. else {
  14. initial = 97;
  15. accum.push(word);
  16. }
  17. return accum;
  18. }, []);
  19. }
  20. /**
  21. * @_ignore
  22. */
  23. export function decode(data, subs) {
  24. // Replace all the substitutions with their expanded form
  25. for (let i = subsChrs.length - 1; i >= 0; i--) {
  26. data = data.split(subsChrs[i]).join(subs.substring(2 * i, 2 * i + 2));
  27. }
  28. // Get all tle clumps; each suffix, first-increment and second-increment
  29. const clumps = [];
  30. const leftover = data.replace(/(:|([0-9])|([A-Z][a-z]*))/g, (all, item, semi, word) => {
  31. if (semi) {
  32. for (let i = parseInt(semi); i >= 0; i--) {
  33. clumps.push(";");
  34. }
  35. }
  36. else {
  37. clumps.push(item.toLowerCase());
  38. }
  39. return "";
  40. });
  41. /* c8 ignore start */
  42. if (leftover) {
  43. throw new Error(`leftovers: ${JSON.stringify(leftover)}`);
  44. }
  45. /* c8 ignore stop */
  46. return unfold(unfold(clumps, ";"), ":");
  47. }
  48. /**
  49. * @_ignore
  50. */
  51. export function decodeOwl(data) {
  52. assertArgument(data[0] === "0", "unsupported auwl data", "data", data);
  53. return decode(data.substring(1 + 2 * subsChrs.length), data.substring(1, 1 + 2 * subsChrs.length));
  54. }
  55. //# sourceMappingURL=decode-owl.js.map