ToGeohash.mjs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * @author gchq77703 []
  3. * @copyright Crown Copyright 2018
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. import geohash from "ngeohash";
  8. /**
  9. * To Geohash operation
  10. */
  11. class ToGeohash extends Operation {
  12. /**
  13. * ToGeohash constructor
  14. */
  15. constructor() {
  16. super();
  17. this.name = "To Geohash";
  18. this.module = "Crypto";
  19. this.description = "Converts Lat/Long coordinates into a Geohash string. For example, <code>37.8324,112.5584</code> becomes <code>ww8p1r4t8</code>.";
  20. this.infoURL = "https://wikipedia.org/wiki/Geohash";
  21. this.inputType = "string";
  22. this.outputType = "string";
  23. this.args = [
  24. {
  25. name: "Precision",
  26. type: "number",
  27. value: 9
  28. }
  29. ];
  30. }
  31. /**
  32. * @param {string} input
  33. * @param {Object[]} args
  34. * @returns {string}
  35. */
  36. run(input, args) {
  37. const [precision] = args;
  38. return input.split("\n").map(line => {
  39. line = line.replace(/ /g, "");
  40. if (line === "") return "";
  41. return geohash.encode(...line.split(",").map(num => parseFloat(num)), precision);
  42. }).join("\n");
  43. }
  44. }
  45. export default ToGeohash;