URLDecode.mjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. * URL Decode operation
  9. */
  10. class URLDecode extends Operation {
  11. /**
  12. * URLDecode constructor
  13. */
  14. constructor() {
  15. super();
  16. this.name = "URL Decode";
  17. this.module = "URL";
  18. this.description = "Converts URI/URL percent-encoded characters back to their raw values.<br><br>e.g. <code>%3d</code> becomes <code>=</code>";
  19. this.infoURL = "https://wikipedia.org/wiki/Percent-encoding";
  20. this.inputType = "string";
  21. this.outputType = "string";
  22. this.args = [];
  23. this.checks = [
  24. {
  25. pattern: ".*(?:%[\\da-f]{2}.*){4}",
  26. flags: "i",
  27. args: []
  28. },
  29. ];
  30. }
  31. /**
  32. * @param {string} input
  33. * @param {Object[]} args
  34. * @returns {string}
  35. */
  36. run(input, args) {
  37. const data = input.replace(/\+/g, "%20");
  38. try {
  39. return decodeURIComponent(data);
  40. } catch (err) {
  41. return unescape(data);
  42. }
  43. }
  44. }
  45. export default URLDecode;