A1Z26CipherEncode.mjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. /**
  10. * A1Z26 Cipher Encode operation
  11. */
  12. class A1Z26CipherEncode extends Operation {
  13. /**
  14. * A1Z26CipherEncode constructor
  15. */
  16. constructor() {
  17. super();
  18. this.name = "A1Z26 Cipher Encode";
  19. this.module = "Ciphers";
  20. this.description = "Converts alphabet characters into their corresponding alphabet order number.<br><br>e.g. <code>a</code> becomes <code>1</code> and <code>b</code> becomes <code>2</code>.<br><br>Non-alphabet characters are dropped.";
  21. this.infoURL = "";
  22. this.inputType = "string";
  23. this.outputType = "string";
  24. this.args = [
  25. {
  26. name: "Delimiter",
  27. type: "option",
  28. value: DELIM_OPTIONS
  29. }
  30. ];
  31. }
  32. /**
  33. * @param {string} input
  34. * @param {Object[]} args
  35. * @returns {string}
  36. */
  37. run(input, args) {
  38. const delim = Utils.charRep(args[0] || "Space");
  39. let output = "";
  40. const sanitizedinput = input.toLowerCase(),
  41. charcode = Utils.strToCharcode(sanitizedinput);
  42. for (let i = 0; i < charcode.length; i++) {
  43. const ordinal = charcode[i] - 96;
  44. if (ordinal > 0 && ordinal <= 26) {
  45. output += ordinal.toString(10) + delim;
  46. }
  47. }
  48. return output.slice(0, -delim.length);
  49. }
  50. }
  51. export default A1Z26CipherEncode;