decode-owl.js 1.8 KB

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