Skip to content

Commit 48181b3

Browse files
committed
fix(utils): nearest ensures min and max range for value
1 parent 6cfc67e commit 48181b3

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

packages/utils/src/__tests__/nearest.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,16 @@ describe("nearest", () => {
3333
expect(nearest(22.3, 20, 50, 50, 50)).toBe(22);
3434
expect(nearest(12.3, 20, 50, 50, 50)).toBe(20);
3535
expect(nearest(0, 30, 50, 100, 100)).toBe(30);
36+
37+
// it's possible for the value to be larger than the min or max for range
38+
// sliders now that it supports a "current drag value" vs _real_ value
39+
const min = 100;
40+
const max = 10000;
41+
const range = max - min;
42+
const steps = range;
43+
const minValue = 2000;
44+
const maxValue = 8000;
45+
expect(nearest(10000, min, maxValue, steps, range)).toBe(maxValue);
46+
expect(nearest(0, minValue, max, steps, range)).toBe(minValue);
3647
});
3748
});

packages/utils/src/nearest.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@ export function nearest(
2828
? range % steps
2929
: step.toString().split(".")[1].length;
3030

31-
return parseFloat((zeroToOne * range + min).toFixed(decimals));
31+
return Math.min(
32+
max,
33+
Math.max(min, parseFloat((zeroToOne * range + min).toFixed(decimals)))
34+
);
3235
}

0 commit comments

Comments
 (0)