1
+ import { Time } from './constants' ;
2
+
1
3
const tokens = new Map ( [
2
- [ 'nanosecond' , 1 / 1e6 ] ,
3
- [ 'nanoseconds' , 1 / 1e6 ] ,
4
- [ 'ns' , 1 / 1e6 ] ,
5
-
6
- [ 'millisecond' , 1 ] ,
7
- [ 'milliseconds' , 1 ] ,
8
- [ 'ms' , 1 ] ,
9
-
10
- [ 'second' , 1000 ] ,
11
- [ 'seconds' , 1000 ] ,
12
- [ 'sec' , 1000 ] ,
13
- [ 'secs' , 1000 ] ,
14
- [ 's' , 1000 ] ,
15
-
16
- [ 'minute' , 1000 * 60 ] ,
17
- [ 'minutes' , 1000 * 60 ] ,
18
- [ 'min' , 1000 * 60 ] ,
19
- [ 'mins' , 1000 * 60 ] ,
20
- [ 'm' , 1000 * 60 ] ,
21
-
22
- [ 'hour' , 1000 * 60 * 60 ] ,
23
- [ 'hours' , 1000 * 60 * 60 ] ,
24
- [ 'hr' , 1000 * 60 * 60 ] ,
25
- [ 'hrs' , 1000 * 60 * 60 ] ,
26
- [ 'h' , 1000 * 60 * 60 ] ,
27
-
28
- [ 'day' , 1000 * 60 * 60 * 24 ] ,
29
- [ 'days' , 1000 * 60 * 60 * 24 ] ,
30
- [ 'd' , 1000 * 60 * 60 * 24 ] ,
31
-
32
- [ 'week' , 1000 * 60 * 60 * 24 * 7 ] ,
33
- [ 'weeks' , 1000 * 60 * 60 * 24 * 7 ] ,
34
- [ 'wk' , 1000 * 60 * 60 * 24 * 7 ] ,
35
- [ 'wks' , 1000 * 60 * 60 * 24 * 7 ] ,
36
- [ 'w' , 1000 * 60 * 60 * 24 * 7 ] ,
37
-
38
- [ 'month' , 1000 * 60 * 60 * 24 * ( 365.25 / 12 ) ] ,
39
- [ 'months' , 1000 * 60 * 60 * 24 * ( 365.25 / 12 ) ] ,
40
- [ 'b' , 1000 * 60 * 60 * 24 * ( 365.25 / 12 ) ] ,
41
- [ 'mo' , 1000 * 60 * 60 * 24 * ( 365.25 / 12 ) ] ,
42
-
43
- [ 'year' , 1000 * 60 * 60 * 24 * 365.25 ] ,
44
- [ 'years' , 1000 * 60 * 60 * 24 * 365.25 ] ,
45
- [ 'yr' , 1000 * 60 * 60 * 24 * 365.25 ] ,
46
- [ 'yrs' , 1000 * 60 * 60 * 24 * 365.25 ] ,
47
- [ 'y' , 1000 * 60 * 60 * 24 * 365.25 ]
4
+ [ 'nanosecond' , Time . Nanosecond ] ,
5
+ [ 'nanoseconds' , Time . Nanosecond ] ,
6
+ [ 'ns' , Time . Nanosecond ] ,
7
+
8
+ [ 'microsecond' , Time . Microsecond ] ,
9
+ [ 'microseconds' , Time . Microsecond ] ,
10
+ [ 'μs' , Time . Microsecond ] ,
11
+ [ 'us' , Time . Microsecond ] ,
12
+
13
+ [ 'millisecond' , Time . Millisecond ] ,
14
+ [ 'milliseconds' , Time . Millisecond ] ,
15
+ [ 'ms' , Time . Millisecond ] ,
16
+
17
+ [ 'second' , Time . Second ] ,
18
+ [ 'seconds' , Time . Second ] ,
19
+ [ 'sec' , Time . Second ] ,
20
+ [ 'secs' , Time . Second ] ,
21
+ [ 's' , Time . Second ] ,
22
+
23
+ [ 'minute' , Time . Minute ] ,
24
+ [ 'minutes' , Time . Minute ] ,
25
+ [ 'min' , Time . Minute ] ,
26
+ [ 'mins' , Time . Minute ] ,
27
+ [ 'm' , Time . Minute ] ,
28
+
29
+ [ 'hour' , Time . Hour ] ,
30
+ [ 'hours' , Time . Hour ] ,
31
+ [ 'hr' , Time . Hour ] ,
32
+ [ 'hrs' , Time . Hour ] ,
33
+ [ 'h' , Time . Hour ] ,
34
+
35
+ [ 'day' , Time . Day ] ,
36
+ [ 'days' , Time . Day ] ,
37
+ [ 'd' , Time . Day ] ,
38
+
39
+ [ 'week' , Time . Week ] ,
40
+ [ 'weeks' , Time . Week ] ,
41
+ [ 'wk' , Time . Week ] ,
42
+ [ 'wks' , Time . Week ] ,
43
+ [ 'w' , Time . Week ] ,
44
+
45
+ [ 'month' , Time . Month ] ,
46
+ [ 'months' , Time . Month ] ,
47
+ [ 'b' , Time . Month ] ,
48
+ [ 'mo' , Time . Month ] ,
49
+
50
+ [ 'year' , Time . Year ] ,
51
+ [ 'years' , Time . Year ] ,
52
+ [ 'yr' , Time . Year ] ,
53
+ [ 'yrs' , Time . Year ] ,
54
+ [ 'y' , Time . Year ]
48
55
] ) ;
49
56
57
+ const mappings = new Map ( [
58
+ [ Time . Nanosecond , 'nanoseconds' ] ,
59
+ [ Time . Microsecond , 'microseconds' ] ,
60
+ [ Time . Millisecond , 'milliseconds' ] ,
61
+ [ Time . Second , 'seconds' ] ,
62
+ [ Time . Minute , 'minutes' ] ,
63
+ [ Time . Hour , 'hours' ] ,
64
+ [ Time . Day , 'days' ] ,
65
+ [ Time . Week , 'weeks' ] ,
66
+ [ Time . Month , 'months' ] ,
67
+ [ Time . Year , 'years' ]
68
+ ] as const ) ;
69
+
50
70
/**
51
71
* Converts duration strings into ms and future dates
52
72
*/
@@ -57,66 +77,111 @@ export class Duration {
57
77
public offset : number ;
58
78
59
79
/**
60
- * Create a new Duration instance
61
- * @param pattern The string to parse
80
+ * The amount of nanoseconds extracted from the text.
62
81
*/
63
- public constructor ( pattern : string ) {
64
- this . offset = Duration . parse ( pattern . toLowerCase ( ) ) ;
65
- }
82
+ public nanoseconds = 0 ;
66
83
67
84
/**
68
- * Get the date from now
85
+ * The amount of microseconds extracted from the text.
69
86
*/
70
- public get fromNow ( ) : Date {
71
- return this . dateFrom ( new Date ( ) ) ;
72
- }
87
+ public microseconds = 0 ;
73
88
74
89
/**
75
- * Get the date from
76
- * @param date The Date instance to get the date from
90
+ * The amount of milliseconds extracted from the text.
77
91
*/
78
- public dateFrom ( date : Date ) : Date {
79
- return new Date ( date . getTime ( ) + this . offset ) ;
80
- }
92
+ public milliseconds = 0 ;
81
93
82
94
/**
83
- * The RegExp used for the pattern parsing
95
+ * The amount of seconds extracted from the text.
84
96
*/
85
- private static readonly kPatternRegex = / ( - ? \d * \. ? \d + (?: e [ - + ] ? \d + ) ? ) \s * ( [ a - z μ ] * ) / gi ;
97
+ public seconds = 0 ;
86
98
87
99
/**
88
- * The RegExp used for removing commas
100
+ * The amount of minutes extracted from the text.
89
101
*/
90
- private static readonly kCommaRegex = / , / g ;
102
+ public minutes = 0 ;
91
103
92
104
/**
93
- * The RegExp used for replacing a/an with 1
105
+ * The amount of hours extracted from the text.
106
+ */
107
+ public hours = 0 ;
108
+
109
+ /**
110
+ * The amount of days extracted from the text.
111
+ */
112
+ public days = 0 ;
113
+
114
+ /**
115
+ * The amount of weeks extracted from the text.
116
+ */
117
+ public weeks = 0 ;
118
+
119
+ /**
120
+ * The amount of months extracted from the text.
121
+ */
122
+ public months = 0 ;
123
+
124
+ /**
125
+ * The amount of years extracted from the text.
94
126
*/
95
- private static readonly kAanRegex = / \b a n ? \b / gi ;
127
+ public years = 0 ;
96
128
97
129
/**
98
- * Parse the pattern
99
- * @param pattern The pattern to parse
130
+ * Create a new Duration instance
131
+ * @param pattern The string to parse
100
132
*/
101
- private static parse ( pattern : string ) : number {
133
+ public constructor ( pattern : string ) {
102
134
let result = 0 ;
103
135
let valid = false ;
104
136
105
137
pattern
138
+ . toLowerCase ( )
106
139
// ignore commas
107
- . replace ( Duration . kCommaRegex , '' )
140
+ . replace ( Duration . commaRegex , '' )
108
141
// a / an = 1
109
- . replace ( Duration . kAanRegex , '1' )
142
+ . replace ( Duration . aAndAnRegex , '1' )
110
143
// do math
111
- . replace ( Duration . kPatternRegex , ( _ , i , units ) => {
144
+ . replace ( Duration . patternRegex , ( _ , i , units ) => {
112
145
const token = tokens . get ( units ) ;
113
146
if ( token !== undefined ) {
114
- result += Number ( i ) * token ;
147
+ const n = Number ( i ) ;
148
+ result += n * token ;
149
+ this [ mappings . get ( token ) ! ] += n ;
115
150
valid = true ;
116
151
}
117
152
return '' ;
118
153
} ) ;
119
154
120
- return valid ? result : NaN ;
155
+ this . offset = valid ? result : NaN ;
156
+ }
157
+
158
+ /**
159
+ * Get the date from now
160
+ */
161
+ public get fromNow ( ) : Date {
162
+ return this . dateFrom ( new Date ( ) ) ;
163
+ }
164
+
165
+ /**
166
+ * Get the date from
167
+ * @param date The Date instance to get the date from
168
+ */
169
+ public dateFrom ( date : Date ) : Date {
170
+ return new Date ( date . getTime ( ) + this . offset ) ;
121
171
}
172
+
173
+ /**
174
+ * The RegExp used for the pattern parsing
175
+ */
176
+ private static readonly patternRegex = / ( - ? \d * \. ? \d + (?: e [ - + ] ? \d + ) ? ) \s * ( [ a - z μ ] * ) / gi;
177
+
178
+ /**
179
+ * The RegExp used for removing commas
180
+ */
181
+ private static readonly commaRegex = / , / g;
182
+
183
+ /**
184
+ * The RegExp used for replacing a/an with 1
185
+ */
186
+ private static readonly aAndAnRegex = / \b a n ? \b / gi;
122
187
}
0 commit comments