File tree 4 files changed +42
-6
lines changed
4 files changed +42
-6
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ "presets" : [
3
+ " babel-preset-es2015"
4
+ ],
5
+ "plugins" : [
6
+ " syntax-trailing-function-commas"
7
+ ]
8
+ }
Original file line number Diff line number Diff line change 7
7
"test" : " test"
8
8
},
9
9
"scripts" : {
10
- "build" : " babel --quiet --presets es2015 source --out-dir build" ,
11
- "test" : " npm run build && ava --fail-fast" ,
12
- "prepublish" : " npm test"
10
+ "build" : " babel --quiet source --out-dir build" ,
11
+ "test" : " ava --require babel-register --fail-fast" ,
12
+ "prepublish" : " npm test && npm run build "
13
13
},
14
14
"keywords" : [
15
15
" duration" ,
21
21
"ava" : " ^0.12.0" ,
22
22
"babel-cli" : " ^6.6.5" ,
23
23
"babel-preset-es2015" : " ^6.6.0" ,
24
+ "babel-register" : " ^6.6.5" ,
24
25
"unexpected" : " ^10.4.0"
25
26
},
26
27
"repository" : {
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import durationFragments from './fragments'
3
3
export default class Duration {
4
4
constructor ( durationString ) {
5
5
let durationPattern =
6
- 'P' +
6
+ '^ P' +
7
7
'(?:(\\d+)Y)?' + // Years
8
8
'(?:(\\d+)M)?' + // Months
9
9
'(?:(\\d+)W)?' + // Weeks
@@ -12,18 +12,32 @@ export default class Duration {
12
12
'(?:(\\d+)H)?' + // Hours
13
13
'(?:(\\d+)M)?' + // Minutes
14
14
'(?:(\\d+)?' + // Seconds
15
- '\\.?(\\d+)?S)?' // Milliseconds
15
+ '\\.?(\\d+)?S)?' + // Milliseconds
16
+ '$'
16
17
17
18
let regex = new RegExp ( durationPattern , 'i' )
18
19
let durationArray = durationString . match ( regex )
19
20
21
+ if ( ! durationArray ) {
22
+ throw new Error ( `"${ durationString } " is an invalid duration string` )
23
+ }
24
+
20
25
durationFragments . forEach ( ( fragment , index ) => {
21
26
let value = Number ( durationArray [ index + 1 ] )
22
- if ( value )
27
+
28
+ if ( typeof value === 'number' && ! Number . isNaN ( value ) ) {
29
+ this . _precision = fragment . replace ( 's' , '' )
23
30
this [ fragment ] = value
31
+ }
24
32
} )
25
33
}
26
34
35
+ get precision ( ) { return this . _precision }
36
+ set precision ( precision ) {
37
+ this . _precision = precision
38
+ return this
39
+ }
40
+
27
41
get string ( ) {
28
42
return durationFragments
29
43
. reduce (
Original file line number Diff line number Diff line change
1
+ import runTest from 'ava'
2
+ import expect from 'unexpected'
3
+ import Duration from '../source/index.js'
4
+
5
+ runTest ( 'precision of P7D is day' , test => {
6
+ const duration = new Duration ( 'P7D' )
7
+ expect ( duration . precision , 'to equal' , 'day' )
8
+ } )
9
+
10
+ runTest ( 'precision of P7D0H is hour' , test => {
11
+ const duration = new Duration ( 'P7D0H' )
12
+ expect ( duration . precision , 'to equal' , 'hour' )
13
+ } )
You can’t perform that action at this time.
0 commit comments