EscapeUnicodeCharacters.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. /**
  8. * Escape Unicode Characters operation
  9. */
  10. class EscapeUnicodeCharacters extends Operation {
  11. /**
  12. * EscapeUnicodeCharacters constructor
  13. */
  14. constructor() {
  15. super();
  16. this.name = "Escape Unicode Characters";
  17. this.module = "Default";
  18. this.description = "Converts characters to their unicode-escaped notations.<br><br>Supports the prefixes:<ul><li><code>\\u</code></li><li><code>%u</code></li><li><code>U+</code></li></ul>e.g. <code>σου</code> becomes <code>\\u03C3\\u03BF\\u03C5</code>";
  19. this.inputType = "string";
  20. this.outputType = "string";
  21. this.args = [
  22. {
  23. "name": "Prefix",
  24. "type": "option",
  25. "value": ["\\u", "%u", "U+"]
  26. },
  27. {
  28. "name": "Encode all chars",
  29. "type": "boolean",
  30. "value": false
  31. },
  32. {
  33. "name": "Padding",
  34. "type": "number",
  35. "value": 4
  36. },
  37. {
  38. "name": "Uppercase hex",
  39. "type": "boolean",
  40. "value": true
  41. }
  42. ];
  43. this.checks = {
  44. input: {
  45. regex: [
  46. {
  47. match: "\\\\u(?:[\\da-f]{4,6})",
  48. flags: "i",
  49. args: ["\\u"]
  50. },
  51. {
  52. match: "%u(?:[\\da-f]{4,6})",
  53. flags: "i",
  54. args: ["%u"]
  55. },
  56. {
  57. match: "U\\+(?:[\\da-f]{4,6})",
  58. flags: "i",
  59. args: ["U+"]
  60. }
  61. ]
  62. }
  63. };
  64. }
  65. /**
  66. * @param {string} input
  67. * @param {Object[]} args
  68. * @returns {string}
  69. */
  70. run(input, args) {
  71. const regexWhitelist = /[ -~]/i,
  72. [prefix, encodeAll, padding, uppercaseHex] = args;
  73. let output = "",
  74. character = "";
  75. for (let i = 0; i < input.length; i++) {
  76. character = input[i];
  77. if (!encodeAll && regexWhitelist.test(character)) {
  78. // It’s a printable ASCII character so don’t escape it.
  79. output += character;
  80. continue;
  81. }
  82. let cp = character.codePointAt(0).toString(16);
  83. if (uppercaseHex) cp = cp.toUpperCase();
  84. output += prefix + cp.padStart(padding, "0");
  85. }
  86. return output;
  87. }
  88. }
  89. export default EscapeUnicodeCharacters;