reduce-array.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * (c) 2010-2019 Pawel Fus & Daniel Studencki
  3. *
  4. * License: www.highcharts.com/license
  5. */
  6. 'use strict';
  7. import H from '../parts/Globals.js';
  8. import '../parts/Utilities.js';
  9. var reduce = H.reduce;
  10. var reduceArrayMixin = {
  11. /**
  12. * Get min value of array filled by OHLC data.
  13. * @param {array} arr Array of OHLC points (arrays).
  14. * @param {string} index Index of "low" value in point array.
  15. * @returns {number} Returns min value.
  16. */
  17. minInArray: function (arr, index) {
  18. return reduce(arr, function (min, target) {
  19. return Math.min(min, target[index]);
  20. }, Number.MAX_VALUE);
  21. },
  22. /**
  23. * Get max value of array filled by OHLC data.
  24. * @param {array} arr Array of OHLC points (arrays).
  25. * @param {string} index Index of "high" value in point array.
  26. * @returns {number} Returns max value.
  27. */
  28. maxInArray: function (arr, index) {
  29. return reduce(arr, function (min, target) {
  30. return Math.max(min, target[index]);
  31. }, -Number.MAX_VALUE);
  32. },
  33. /**
  34. * Get extremes of array filled by OHLC data.
  35. * @param {array} arr Array of OHLC points (arrays).
  36. * @param {string} minIndex Index of "low" value in point array.
  37. * @param {string} maxIndex Index of "high" value in point array.
  38. * @returns {array} Returns array with min and max value.
  39. */
  40. getArrayExtremes: function (arr, minIndex, maxIndex) {
  41. return reduce(arr, function (prev, target) {
  42. return [
  43. Math.min(prev[0], target[minIndex]),
  44. Math.max(prev[1], target[maxIndex])
  45. ];
  46. }, [Number.MAX_VALUE, -Number.MAX_VALUE]);
  47. }
  48. };
  49. export default reduceArrayMixin;