ReduceArray.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. *
  3. * (c) 2010-2021 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 '../Core/Utilities.js';
  12. var reduceArrayMixin = {
  13. /**
  14. * Get min value of array filled by OHLC data.
  15. * @private
  16. * @param {Array<*>} arr Array of OHLC points (arrays).
  17. * @param {string} index Index of "low" value in point array.
  18. * @return {number} Returns min value.
  19. */
  20. minInArray: function (arr, index) {
  21. return arr.reduce(function (min, target) {
  22. return Math.min(min, target[index]);
  23. }, Number.MAX_VALUE);
  24. },
  25. /**
  26. * Get max value of array filled by OHLC data.
  27. * @private
  28. * @param {Array<*>} arr Array of OHLC points (arrays).
  29. * @param {string} index Index of "high" value in point array.
  30. * @return {number} Returns max value.
  31. */
  32. maxInArray: function (arr, index) {
  33. return arr.reduce(function (max, target) {
  34. return Math.max(max, target[index]);
  35. }, -Number.MAX_VALUE);
  36. },
  37. /**
  38. * Get extremes of array filled by OHLC data.
  39. * @private
  40. * @param {Array<*>} arr Array of OHLC points (arrays).
  41. * @param {string} minIndex Index of "low" value in point array.
  42. * @param {string} maxIndex Index of "high" value in point array.
  43. * @return {Array<number,number>} Returns array with min and max value.
  44. */
  45. getArrayExtremes: function (arr, minIndex, maxIndex) {
  46. return arr.reduce(function (prev, target) {
  47. return [
  48. Math.min(prev[0], target[minIndex]),
  49. Math.max(prev[1], target[maxIndex])
  50. ];
  51. }, [Number.MAX_VALUE, -Number.MAX_VALUE]);
  52. }
  53. };
  54. export default reduceArrayMixin;