1
- import { clamp , snapValueToStep } from '../src/number' ;
1
+ import { clamp , roundToStepPrecision , snapValueToStep } from '../src/number' ;
2
2
3
3
describe ( 'number utils' , ( ) => {
4
4
describe ( 'clamp' , ( ) => {
@@ -8,6 +8,30 @@ describe('number utils', () => {
8
8
} ) ;
9
9
} ) ;
10
10
11
+ describe ( 'roundToStepPrecision' , ( ) => {
12
+ it ( 'should return the input unchanged for integer steps' , ( ) => {
13
+ expect ( roundToStepPrecision ( 7.123 , 1 ) ) . toBe ( 7.123 ) ;
14
+ expect ( roundToStepPrecision ( 5 , 10 ) ) . toBe ( 5 ) ;
15
+ } ) ;
16
+ it ( 'should round to the correct decimal places for steps with decimals' , ( ) => {
17
+ expect ( roundToStepPrecision ( 1.24 , 0.1 ) ) . toBe ( 1.24 ) ;
18
+ expect ( roundToStepPrecision ( 1.456 , 0.01 ) ) . toBe ( 1.456 ) ;
19
+ // Should not overcount precision
20
+ expect ( roundToStepPrecision ( 2.349 , 0.100 ) ) . toBe ( 2.35 ) ;
21
+ expect ( roundToStepPrecision ( 2.35 , 0.100 ) ) . toBe ( 2.35 ) ;
22
+ // Should handle negative values
23
+ expect ( roundToStepPrecision ( - 1.456 , 0.01 ) ) . toBe ( - 1.456 ) ;
24
+ // Should handle zero value
25
+ expect ( roundToStepPrecision ( 0 , 0.01 ) ) . toBe ( 0 ) ;
26
+ } ) ;
27
+ it ( 'should handle rounding for exponential step values' , ( ) => {
28
+ expect ( roundToStepPrecision ( 0.123456789 , 1e-3 ) ) . toBe ( 0.1235 ) ;
29
+ expect ( roundToStepPrecision ( 0.123456789 , 1e-7 ) ) . toBe ( 0.12345679 ) ;
30
+ // Should handle exponential notation steps regardless of e/E case
31
+ expect ( roundToStepPrecision ( 0.123456789 , 1E-8 ) ) . toBe ( 0.123456789 ) ;
32
+ } ) ;
33
+ } ) ;
34
+
11
35
describe ( 'snapValueToStep' , ( ) => {
12
36
it ( 'should snap value to nearest step based on min and max' , ( ) => {
13
37
expect ( snapValueToStep ( 2 , - 0.5 , 100 , 3 ) ) . toBe ( 2.5 ) ;
0 commit comments