FromOctal.mjs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @author Matt C [matt@artemisbot.uk]
  3. * @copyright Crown Copyright 2017
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. import Utils from "../Utils.mjs";
  8. import {DELIM_OPTIONS} from "../lib/Delim.mjs";
  9. /**
  10. * From Octal operation
  11. */
  12. class FromOctal extends Operation {
  13. /**
  14. * FromOctal constructor
  15. */
  16. constructor() {
  17. super();
  18. this.name = "From Octal";
  19. this.module = "Default";
  20. this.description = "Converts an octal byte string back into its raw value.<br><br>e.g. <code>316 223 316 265 316 271 316 254 40 317 203 316 277 317 205</code> becomes the UTF-8 encoded string <code>Γειά σου</code>";
  21. this.infoURL = "https://wikipedia.org/wiki/Octal";
  22. this.inputType = "string";
  23. this.outputType = "byteArray";
  24. this.args = [
  25. {
  26. "name": "Delimiter",
  27. "type": "option",
  28. "value": DELIM_OPTIONS
  29. }
  30. ];
  31. this.checks = [
  32. {
  33. pattern: "^(?:[0-7]{1,2}|[123][0-7]{2})(?: (?:[0-7]{1,2}|[123][0-7]{2}))*$",
  34. flags: "",
  35. args: ["Space"]
  36. },
  37. {
  38. pattern: "^(?:[0-7]{1,2}|[123][0-7]{2})(?:,(?:[0-7]{1,2}|[123][0-7]{2}))*$",
  39. flags: "",
  40. args: ["Comma"]
  41. },
  42. {
  43. pattern: "^(?:[0-7]{1,2}|[123][0-7]{2})(?:;(?:[0-7]{1,2}|[123][0-7]{2}))*$",
  44. flags: "",
  45. args: ["Semi-colon"]
  46. },
  47. {
  48. pattern: "^(?:[0-7]{1,2}|[123][0-7]{2})(?::(?:[0-7]{1,2}|[123][0-7]{2}))*$",
  49. flags: "",
  50. args: ["Colon"]
  51. },
  52. {
  53. pattern: "^(?:[0-7]{1,2}|[123][0-7]{2})(?:\\n(?:[0-7]{1,2}|[123][0-7]{2}))*$",
  54. flags: "",
  55. args: ["Line feed"]
  56. },
  57. {
  58. pattern: "^(?:[0-7]{1,2}|[123][0-7]{2})(?:\\r\\n(?:[0-7]{1,2}|[123][0-7]{2}))*$",
  59. flags: "",
  60. args: ["CRLF"]
  61. }
  62. ];
  63. }
  64. /**
  65. * @param {string} input
  66. * @param {Object[]} args
  67. * @returns {byteArray}
  68. */
  69. run(input, args) {
  70. const delim = Utils.charRep(args[0] || "Space");
  71. if (input.length === 0) return [];
  72. return input.split(delim).map(val => parseInt(val, 8));
  73. }
  74. }
  75. export default FromOctal;