1- const unit = Object . create ( null )
2- const m = 60000 , h = m * 60 , d = h * 24 , y = d * 365.25
1+ const unit = Object . create ( null ) ;
32
4- unit . year = unit . yr = unit . y = y
5- unit . month = unit . mo = unit . mth = y / 12
6- unit . week = unit . wk = unit . w = d * 7
7- unit . day = unit . d = d
8- unit . hour = unit . hr = unit . h = h
9- unit . minute = unit . min = unit . m = m
10- unit . second = unit . sec = unit . s = 1000
11- unit . millisecond = unit . millisec = unit . ms = 1
12- unit . microsecond = unit . microsec = unit . us = unit . µs = 1e-3
13- unit . nanosecond = unit . nanosec = unit . ns = 1e-6
3+ const m = 60000 ,
4+ h = m * 60 ,
5+ d = h * 24 ,
6+ y = d * 365.25 ;
147
15- unit . group = ','
16- unit . decimal = '.'
17- unit . placeholder = ' _'
8+ unit . year = unit . yr = unit . y = y ;
9+ unit . month = unit . mo = unit . mth = y / 12 ;
10+ unit . week = unit . wk = unit . w = d * 7 ;
11+ unit . day = unit . d = d ;
12+ unit . hour = unit . hr = unit . h = h ;
13+ unit . minute = unit . min = unit . m = m ;
14+ unit . second = unit . sec = unit . s = 1000 ;
15+ unit . millisecond = unit . millisec = unit . ms = 1 ;
16+ unit . microsecond = unit . microsec = unit . us = unit . µs = 1e-3 ;
17+ unit . nanosecond = unit . nanosec = unit . ns = 1e-6 ;
1818
19+ unit . group = ',' ;
20+ unit . decimal = '.' ;
21+ unit . placeholder = ' _' ;
1922
20-
21- const durationRE = / ( (?: \d { 1 , 16 } (?: \. \d { 1 , 16 } ) ? | \. \d { 1 , 16 } ) (?: [ e E ] [ - + ] ? \d { 1 , 4 } ) ? ) \s ? ( [ \p{ L} ] { 0 , 14 } ) / gu
23+ const durationRE =
24+ / ( (?: \d { 1 , 16 } (?: \. \d { 1 , 16 } ) ? | \. \d { 1 , 16 } ) (?: [ e E ] [ - + ] ? \d { 1 , 4 } ) ? ) \s ? ( [ \p{ L} ] { 0 , 14 } ) / gu;
2225
2326parse . unit = unit ;
2427
@@ -30,26 +33,38 @@ parse.unit = unit;
3033 * @return {number }
3134 */
3235export default function parse ( str = '' , format = 'ms' ) {
33- let result = null , prevUnits
34-
35- String ( str )
36- . replace ( new RegExp ( `(\\d)[${ parse . unit . placeholder } ${ parse . unit . group } ](\\d)` , 'g' ) , '$1$2' ) // clean up group separators / placeholders
37- . replace ( parse . unit . decimal , '.' ) // normalize decimal separator
38- . replace ( durationRE , ( _ , n , units ) => {
39- // if no units, find next smallest units or fall back to format value
40- // eg. 1h30 -> 1h30m
41- if ( ! units ) {
42- if ( prevUnits ) {
43- for ( const u in parse . unit ) if ( parse . unit [ u ] < prevUnits ) { units = u ; break }
44- }
45- else units = format
46- }
47- else units = units . toLowerCase ( )
48-
49- prevUnits = units = parse . unit [ units ] || parse . unit [ units . replace ( / s $ / , '' ) ]
50-
51- if ( units ) result = ( result || 0 ) + n * units
52- } )
53-
54- return result && ( ( result / ( parse . unit [ format ] || 1 ) ) * ( str [ 0 ] === '-' ? - 1 : 1 ) )
55- }
36+ let result = null , prevUnits ;
37+
38+ String ( str )
39+ . replace (
40+ new RegExp ( `(\\d)[${ parse . unit . placeholder } ${ parse . unit . group } ](\\d)` , 'g' ) ,
41+ '$1$2'
42+ ) // clean up group separators / placeholders
43+ . replace ( parse . unit . decimal , '.' ) // normalize decimal separator
44+ . replace ( durationRE , ( _ , n , units ) => {
45+ // if no units, find next smallest units or fall back to format value
46+ // eg. 1h30 -> 1h30m
47+ if ( ! units ) {
48+ if ( prevUnits ) {
49+ for ( const u in parse . unit ) {
50+ if ( parse . unit [ u ] < prevUnits ) {
51+ units = u ;
52+ break ;
53+ }
54+ }
55+ } else {
56+ units = format ;
57+ }
58+ } else {
59+ units = units . toLowerCase ( ) ;
60+ }
61+
62+ prevUnits = units = parse . unit [ units ] || parse . unit [ units . replace ( / s $ / , '' ) ] ;
63+
64+ if ( units ) {
65+ result = ( result || 0 ) + n * units ;
66+ }
67+ } ) ;
68+
69+ return result && ( result / ( parse . unit [ format ] || 1 ) ) * ( str [ 0 ] === '-' ? - 1 : 1 ) ;
70+ }
0 commit comments