forked from githuboftigran/rn-range-slider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.ts
35 lines (33 loc) · 1.02 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export const isLowCloser = (
downX: number,
lowPosition: number,
highPosition: number,
): boolean => {
if (lowPosition === highPosition) {
return downX < lowPosition;
}
const distanceFromLow = Math.abs(downX - lowPosition);
const distanceFromHigh = Math.abs(downX - highPosition);
return distanceFromLow < distanceFromHigh;
};
export const clamp = (value: number, min: number, max: number): number => {
return Math.min(Math.max(value, min), max);
};
export const getValueForPosition = (
positionInView: number,
containerWidth: number,
thumbWidth: number,
min: number,
max: number,
step: number,
): number => {
const availableSpace = containerWidth - thumbWidth;
const relStepUnit = step / (max - min);
let relPosition = (positionInView - thumbWidth / 2) / availableSpace;
const relOffset = relPosition % relStepUnit;
relPosition -= relOffset;
if (relOffset / relStepUnit >= 0.5) {
relPosition += relStepUnit;
}
return clamp(min + Math.round(relPosition / relStepUnit) * step, min, max);
};