Median.mjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * @author bwhitn [brian.m.whitney@outlook.com]
  3. * @author d98762625 [d98762625@gmail.com]
  4. * @copyright Crown Copyright 2018
  5. * @license Apache-2.0
  6. */
  7. import BigNumber from "bignumber.js";
  8. import Operation from "../Operation";
  9. import { median, createNumArray } from "../lib/Arithmetic";
  10. import { ARITHMETIC_DELIM_OPTIONS } from "../lib/Delim";
  11. /**
  12. * Median operation
  13. */
  14. class Median extends Operation {
  15. /**
  16. * Median constructor
  17. */
  18. constructor() {
  19. super();
  20. this.name = "Median";
  21. this.module = "Default";
  22. this.description = "Computes the median of a number list. If an item in the string is not a number it is excluded from the list.<br><br>e.g. <code>0x0a 8 1 .5</code> becomes <code>4.5</code>";
  23. this.infoURL = "https://wikipedia.org/wiki/Median";
  24. this.inputType = "string";
  25. this.outputType = "BigNumber";
  26. this.args = [
  27. {
  28. "name": "Delimiter",
  29. "type": "option",
  30. "value": ARITHMETIC_DELIM_OPTIONS,
  31. }
  32. ];
  33. }
  34. /**
  35. * @param {string} input
  36. * @param {Object[]} args
  37. * @returns {BigNumber}
  38. */
  39. run(input, args) {
  40. const val = median(createNumArray(input, args[0]));
  41. return BigNumber.isBigNumber(val) ? val : new BigNumber(NaN);
  42. }
  43. }
  44. export default Median;