utils/charMapping.js

  1. /**
  2. * Builder for building a character map.
  3. */
  4. class CharacterMapBuilder {
  5. /**
  6. * Creates a new character map builder.
  7. */
  8. constructor() {
  9. /**
  10. * Mapping of characters to character codes.
  11. * @type {Object.<string, number>}
  12. */
  13. this.map = {};
  14. }
  15. /**
  16. * Adds a range mapping to the map.
  17. * @param {string} start Start character.
  18. * @param {string} end End character.
  19. * @param {number} base Value corresponding to the start character.
  20. */
  21. addRange(start, end, base) {
  22. const startCode = start.charCodeAt(0);
  23. const endCode = end.charCodeAt(0);
  24. for (let code = startCode; code <= endCode; ++code)
  25. this.map[String.fromCharCode(code)] = code - startCode + base;
  26. }
  27. }
  28. const charMapping = {
  29. /**
  30. * Creates a builder for building a character map.
  31. * @returns {CharacterMapBuilder} Character map builder.
  32. */
  33. createBuilder: () => new CharacterMapBuilder(),
  34. };
  35. export default charMapping;