reduce-array.js 1.8 KB

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