File size: 1,265 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
export function getDecimalCount(value) {
    return (String(value).split('.')[1] || '').length;
}
export function roundValue(value, decimalCount) {
    const rounder = Math.pow(10, decimalCount);
    return Math.round(value * rounder) / rounder;
}
export function snapValueToStep(value, min, max, step) {
    const remainder = (value - (isNaN(min) ? 0 : min)) % step;
    let snappedValue = Math.abs(remainder) * 2 >= step
        ? value + Math.sign(remainder) * (step - Math.abs(remainder))
        : value - remainder;
    if (!isNaN(min)) {
        if (snappedValue < min) {
            snappedValue = min;
        }
        else if (!isNaN(max) && snappedValue > max) {
            snappedValue = min + Math.floor((max - min) / step) * step;
        }
    }
    else if (!isNaN(max) && snappedValue > max) {
        snappedValue = Math.floor(max / step) * step;
    }
    const string = step.toString();
    const index = string.indexOf('.');
    const precision = index >= 0 ? string.length - index : 0;
    if (precision > 0) {
        const pow = Math.pow(10, precision);
        snappedValue = Math.round(snappedValue * pow) / pow;
    }
    return snappedValue;
}
export function clamp(min, value, max) {
    return Math.max(min, Math.min(value, max));
}