FromDecimal.mjs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. import {DELIM_OPTIONS} from "../lib/Delim.mjs";
  8. import {fromDecimal} from "../lib/Decimal.mjs";
  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.checks = {
  36. input: {
  37. regex: [
  38. {
  39. 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]))*$",
  40. flags: "",
  41. args: ["Space", false]
  42. },
  43. {
  44. 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]))*$",
  45. flags: "",
  46. args: ["Comma", false]
  47. },
  48. {
  49. 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]))*$",
  50. flags: "",
  51. args: ["Semi-colon", false]
  52. },
  53. {
  54. 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]))*$",
  55. flags: "",
  56. args: ["Colon", false]
  57. },
  58. {
  59. 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]))*$",
  60. flags: "",
  61. args: ["Line feed", false]
  62. },
  63. {
  64. 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]))*$",
  65. flags: "",
  66. args: ["CRLF", false]
  67. }
  68. ]
  69. }
  70. };
  71. }
  72. /**
  73. * @param {string} input
  74. * @param {Object[]} args
  75. * @returns {byteArray}
  76. */
  77. run(input, args) {
  78. let data = fromDecimal(input, args[0]);
  79. if (args[1]) { // Convert negatives
  80. data = data.map(v => v < 0 ? 0xFF + v + 1 : v);
  81. }
  82. return data;
  83. }
  84. }
  85. export default FromDecimal;