URLDecode.mjs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  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.inputType = "string";
  20. this.outputType = "string";
  21. this.args = [];
  22. this.patterns = [
  23. {
  24. match: ".*(?:%[\\da-f]{2}.*){4}",
  25. flags: "i",
  26. args: []
  27. },
  28. ];
  29. }
  30. /**
  31. * @param {string} input
  32. * @param {Object[]} args
  33. * @returns {string}
  34. */
  35. run(input, args) {
  36. const data = input.replace(/\+/g, "%20");
  37. try {
  38. return decodeURIComponent(data);
  39. } catch (err) {
  40. return unescape(data);
  41. }
  42. }
  43. }
  44. export default URLDecode;