AddLineNumbers.mjs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. /**
  8. * Add line numbers operation
  9. */
  10. class AddLineNumbers extends Operation {
  11. /**
  12. * AddLineNumbers constructor
  13. */
  14. constructor() {
  15. super();
  16. this.name = "Add line numbers";
  17. this.module = "Default";
  18. this.description = "Adds line numbers to the output.";
  19. this.inputType = "string";
  20. this.outputType = "string";
  21. this.args = [];
  22. }
  23. /**
  24. * @param {string} input
  25. * @param {Object[]} args
  26. * @returns {string}
  27. */
  28. run(input, args) {
  29. const lines = input.split("\n"),
  30. width = lines.length.toString().length;
  31. let output = "";
  32. for (let n = 0; n < lines.length; n++) {
  33. output += (n+1).toString().padStart(width, " ") + " " + lines[n] + "\n";
  34. }
  35. return output.slice(0, output.length-1);
  36. }
  37. }
  38. export default AddLineNumbers;