ToDecimal.mjs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 Utils from "../Utils.mjs";
  8. import {DELIM_OPTIONS} from "../lib/Delim.mjs";
  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 = "ArrayBuffer";
  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 {ArrayBuffer} input
  38. * @param {Object[]} args
  39. * @returns {string}
  40. */
  41. run(input, args) {
  42. input = new Uint8Array(input);
  43. const delim = Utils.charRep(args[0]),
  44. signed = args[1];
  45. if (signed) {
  46. input = input.map(v => v > 0x7F ? v - 0xFF - 1 : v);
  47. }
  48. return input.join(delim);
  49. }
  50. }
  51. export default ToDecimal;