File tree 2 files changed +23
-3
lines changed
2 files changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,6 @@ export default class Duration {
37
37
let value = Number ( durationArray [ index + 1 ] )
38
38
39
39
if ( typeof value === 'number' && ! Number . isNaN ( value ) ) {
40
- this . _precision = fragment . replace ( / s $ / , '' )
41
40
this [ fragment ] = value
42
41
}
43
42
} )
@@ -79,7 +78,23 @@ export default class Duration {
79
78
}
80
79
81
80
82
- get precision ( ) { return this . _precision }
81
+ get precision ( ) {
82
+ let precision
83
+
84
+ // Clone array as .reverse() is in place
85
+ Array . from ( durationFragments )
86
+ . reverse ( )
87
+ . some ( fragment => {
88
+ const value = this [ fragment ]
89
+
90
+ if ( typeof value === 'number' && ! Number . isNaN ( value ) ) {
91
+ precision = fragment . replace ( / s $ / , '' )
92
+ return true
93
+ }
94
+ } )
95
+
96
+ return precision
97
+ }
83
98
set precision ( precision ) {
84
99
this . _precision = precision
85
100
return this
Original file line number Diff line number Diff line change 1
1
import runTest from 'ava'
2
2
import expect from 'unexpected'
3
- import Duration from '../source/index.js '
3
+ import Duration from '../source/index'
4
4
5
5
runTest ( 'precision of P7D is day' , test => {
6
6
const duration = new Duration ( 'P7D' )
7
7
expect ( duration . precision , 'to equal' , 'day' )
8
8
} )
9
9
10
+ runTest ( 'precision of P5Y is year' , test => {
11
+ const duration = new Duration ( 'P5Y' )
12
+ expect ( duration . precision , 'to equal' , 'year' )
13
+ } )
14
+
10
15
runTest ( 'precision of P7D0H is hour' , test => {
11
16
const duration = new Duration ( 'P7D0H' )
12
17
expect ( duration . precision , 'to equal' , 'hour' )
You can’t perform that action at this time.
0 commit comments