get-range.ts 782 B

1234567891011121314151617181920212223242526272829303132333435
  1. import isArray from './is-array';
  2. import { default as getMax } from './max';
  3. import { default as getMin } from './min';
  4. export interface RangeType {
  5. readonly min: number;
  6. readonly max: number;
  7. }
  8. const getRange = function (values: number[]): RangeType {
  9. // 存在 NaN 时,min,max 判定会出问题
  10. let filterValues = values.filter((v) => !isNaN(v));
  11. if (!filterValues.length) {
  12. // 如果没有数值则直接返回0
  13. return {
  14. min: 0,
  15. max: 0,
  16. };
  17. }
  18. if (isArray(values[0])) {
  19. let tmp = [];
  20. for (let i = 0; i < values.length; i++) {
  21. tmp = tmp.concat(values[i]);
  22. }
  23. filterValues = tmp;
  24. }
  25. const max = getMax(filterValues);
  26. const min = getMin(filterValues);
  27. return {
  28. min,
  29. max,
  30. };
  31. };
  32. export default getRange;