A1Z26CipherDecode.mjs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @author Jarmo van Lenthe [github.com/jarmovanlenthe]
  3. * @copyright Crown Copyright 2018
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. import Utils from "../Utils";
  8. import {DELIM_OPTIONS} from "../lib/Delim";
  9. import OperationError from "../errors/OperationError";
  10. /**
  11. * A1Z26 Cipher Decode operation
  12. */
  13. class A1Z26CipherDecode extends Operation {
  14. /**
  15. * A1Z26CipherDecode constructor
  16. */
  17. constructor() {
  18. super();
  19. this.name = "A1Z26 Cipher Decode";
  20. this.module = "Ciphers";
  21. this.description = "Converts alphabet order numbers into their corresponding alphabet character.<br><br>e.g. <code>1</code> becomes <code>a</code> and <code>2</code> becomes <code>b</code>.";
  22. this.infoURL = "";
  23. this.inputType = "string";
  24. this.outputType = "string";
  25. this.args = [
  26. {
  27. name: "Delimiter",
  28. type: "option",
  29. value: DELIM_OPTIONS
  30. }
  31. ];
  32. }
  33. /**
  34. * @param {string} input
  35. * @param {Object[]} args
  36. * @returns {string}
  37. */
  38. run(input, args) {
  39. const delim = Utils.charRep(args[0] || "Space");
  40. if (input.length === 0) {
  41. return [];
  42. }
  43. const bites = input.split(delim);
  44. let latin1 = "";
  45. for (let i = 0; i < bites.length; i++) {
  46. if (bites[i] < 1 || bites[i] > 26) {
  47. throw new OperationError("Error: all numbers must be between 1 and 26.");
  48. }
  49. latin1 += Utils.chr(parseInt(bites[i], 10) + 96);
  50. }
  51. return latin1;
  52. }
  53. }
  54. export default A1Z26CipherDecode;