ToDecimal.mjs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 Utils from "../Utils";
  8. import {DELIM_OPTIONS} from "../lib/Delim";
  9. /**
  10. * To Decimal operation
  11. */
  12. class ToDecimal extends Operation {
  13. /**
  14. * ToDecimal constructor
  15. */
  16. constructor() {
  17. super();
  18. this.name = "To Decimal";
  19. this.module = "Default";
  20. this.description = "Converts the input data to an ordinal integer array.<br><br>e.g. <code>Hello</code> becomes <code>72 101 108 108 111</code>";
  21. this.inputType = "byteArray";
  22. this.outputType = "string";
  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. }
  36. /**
  37. * @param {byteArray} input
  38. * @param {Object[]} args
  39. * @returns {string}
  40. */
  41. run(input, args) {
  42. const delim = Utils.charRep(args[0]),
  43. signed = args[1];
  44. if (signed) {
  45. input = input.map(v => v > 0x7F ? v - 0xFF - 1 : v);
  46. }
  47. return input.join(delim);
  48. }
  49. }
  50. export default ToDecimal;