FromDecimal.mjs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. import {DELIM_OPTIONS} from "../lib/Delim";
  8. import {fromDecimal} from "../lib/Decimal";
  9. /**
  10. * From Decimal operation
  11. */
  12. class FromDecimal extends Operation {
  13. /**
  14. * FromDecimal constructor
  15. */
  16. constructor() {
  17. super();
  18. this.name = "From Decimal";
  19. this.module = "Default";
  20. this.description = "Converts the data from an ordinal integer array back into its raw form.<br><br>e.g. <code>72 101 108 108 111</code> becomes <code>Hello</code>";
  21. this.inputType = "string";
  22. this.outputType = "byteArray";
  23. this.args = [
  24. {
  25. "name": "Delimiter",
  26. "type": "option",
  27. "value": DELIM_OPTIONS
  28. },
  29. {
  30. "name": "Support signed values",
  31. "type": "boolean",
  32. "value": false
  33. }
  34. ];
  35. this.patterns = [
  36. {
  37. match: "^(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])(?: (?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5]))*$",
  38. flags: "",
  39. args: ["Space", false]
  40. },
  41. {
  42. match: "^(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])(?:,(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5]))*$",
  43. flags: "",
  44. args: ["Comma", false]
  45. },
  46. {
  47. match: "^(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])(?:;(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5]))*$",
  48. flags: "",
  49. args: ["Semi-colon", false]
  50. },
  51. {
  52. match: "^(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])(?::(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5]))*$",
  53. flags: "",
  54. args: ["Colon", false]
  55. },
  56. {
  57. match: "^(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])(?:\\n(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5]))*$",
  58. flags: "",
  59. args: ["Line feed", false]
  60. },
  61. {
  62. match: "^(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])(?:\\r\\n(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5]))*$",
  63. flags: "",
  64. args: ["CRLF", false]
  65. },
  66. ];
  67. }
  68. /**
  69. * @param {string} input
  70. * @param {Object[]} args
  71. * @returns {byteArray}
  72. */
  73. run(input, args) {
  74. let data = fromDecimal(input, args[0]);
  75. if (args[1]) { // Convert negatives
  76. data = data.map(v => v < 0 ? 0xFF + v + 1 : v);
  77. }
  78. return data;
  79. }
  80. }
  81. export default FromDecimal;