File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change 11import debounce from 'utils/debounce'
22import { computeTooltipPosition } from 'utils/compute-positions'
3+ import { cssTimeToMs } from 'utils/css-time-to-ms'
34
45// Tell Jest to mock all timeout functions
56jest . useRealTimers ( )
@@ -106,4 +107,24 @@ describe('debounce', () => {
106107 expect ( func ) . not . toHaveBeenCalled ( )
107108 } )
108109} )
110+
111+ describe ( 'css time to ms' , ( ) => {
112+ test ( 'converts time correctly' , ( ) => {
113+ expect ( cssTimeToMs ( '1s' ) ) . toBe ( 1000 )
114+ expect ( cssTimeToMs ( '1ms' ) ) . toBe ( 1 )
115+ expect ( cssTimeToMs ( '1.5s' ) ) . toBe ( 1500 )
116+ expect ( cssTimeToMs ( '1.5ms' ) ) . toBe ( 1.5 )
117+ } )
118+
119+ test ( 'returns 0 if no time is provided' , ( ) => {
120+ expect ( cssTimeToMs ( '' ) ) . toBe ( 0 )
121+ } )
122+
123+ test ( 'returns 0 if unsupported unit' , ( ) => {
124+ expect ( cssTimeToMs ( '1h' ) ) . toBe ( 0 )
125+ } )
126+
127+ test ( 'returns 0 if no unit' , ( ) => {
128+ expect ( cssTimeToMs ( '1000' ) ) . toBe ( 0 )
129+ } )
109130} )
Original file line number Diff line number Diff line change 11export const cssTimeToMs = ( time : string ) : number => {
2- const match = time . match ( / ^ ( [ \d . ] + ) ( m ? s ? ) $ / )
2+ const match = time . match ( / ^ ( [ \d . ] + ) ( m s | s ) $ / )
33 if ( ! match ) {
44 return 0
55 }
66 const [ , amount , unit ] = match
7- if ( unit !== 's' && unit !== 'ms' ) {
8- return 0
9- }
107 return Number ( amount ) * ( unit === 'ms' ? 1 : 1000 )
118}
You can’t perform that action at this time.
0 commit comments