Skip to content

Commit 678b30d

Browse files
Planeshifterstdlib-botkgryte
authored
Add regular expression for duration strings (#589)
* Add README of regular expression for durations * Scaffold regexp/duration-string package files * Add initial implementation * Add test case * Fix indentation * Fix indentation Co-authored-by: stdlib-bot <noreply@stdlib.io> Co-authored-by: Athan <kgryte@gmail.com>
1 parent 41c4722 commit 678b30d

File tree

13 files changed

+949
-0
lines changed

13 files changed

+949
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# reDurationString
22+
23+
> [Regular expression][mdn-regexp] to match a duration string.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var reDurationString = require( '@stdlib/regexp/duration-string' );
31+
```
32+
33+
#### reDurationString()
34+
35+
Returns a [regular expression][mdn-regexp] to match a duration string.
36+
37+
```javascript
38+
var RE_DURATION = reDurationString();
39+
// returns <RegExp>
40+
41+
var parts = RE_DURATION.exec( '3d2ms' );
42+
/* returns
43+
[
44+
'3d2ms',
45+
'3d',
46+
undefined,
47+
undefined,
48+
undefined,
49+
'2ms',
50+
index: 0,
51+
input: '3d2ms',
52+
groups: undefined
53+
]
54+
*/
55+
56+
parts = RE_SEMVER.exec( '4h3m20s' );
57+
/* returns
58+
[
59+
'4h3m20s',
60+
undefined,
61+
'4h',
62+
'3m',
63+
'20s',
64+
undefined,
65+
index: 0,
66+
input: '4h3m20s',
67+
groups: undefined
68+
]
69+
*/
70+
```
71+
72+
#### reDurationString.REGEXP
73+
74+
[Regular expression][mdn-regexp] to match a duration string.
75+
76+
```javascript
77+
var bool = reDurationString.REGEXP.test( '3d12h' );
78+
// returns true
79+
```
80+
81+
</section>
82+
83+
<!-- /.usage -->
84+
85+
<section class="notes">
86+
87+
## Notes
88+
89+
- A duration string is a string containing a sequence of time units. A time unit is a nonnegative integer followed by a unit identifier. The following unit identifiers are supported:
90+
91+
- `d`: days
92+
- `h`: hours
93+
- `m`: minutes
94+
- `s`: seconds
95+
- `ms`: milliseconds
96+
97+
For example, the string `1m3s10ms` is a duration string containing three time units: `1m` (1 minute), `3s` (3 seconds), and `10ms` (10 milliseconds). The string `60m` is a duration string containing a single time unit: `60m` (60 minutes). Time units must be supplied in descending order of magnitude (i.e., days, hours, minutes, seconds, milliseconds).
98+
99+
- Duration strings are case insensitive. For example, the string `1M3S10MS` is equivalent to `1m3s10ms`.
100+
101+
- The regular expression captures the following groups:
102+
103+
1. The days component.
104+
2. The hours component.
105+
3. The minutes component.
106+
4. The seconds component.
107+
5. The milliseconds component.
108+
109+
</section>
110+
111+
<!-- /.notes -->
112+
113+
<section class="examples">
114+
115+
## Examples
116+
117+
<!-- eslint no-undef: "error" -->
118+
119+
```javascript
120+
var reDurationString = require( '@stdlib/regexp/duration-string' );
121+
122+
var RE_DURATION = reDurationString();
123+
124+
var bool = RE_DURATION.test( '3d12h' );
125+
// returns true
126+
127+
bool = RE_DURATION.test( '1M3S10MS' );
128+
// returns true
129+
130+
bool = RE_DURATION.test( '1y.0w.0d' );
131+
// returns false
132+
133+
bool = RE_DURATION.test( '1y3w' );
134+
// returns false
135+
136+
bool = RE_DURATION.test( 'beep' );
137+
// returns false
138+
```
139+
140+
</section>
141+
142+
<!-- /.examples -->
143+
144+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
145+
146+
<section class="related">
147+
148+
</section>
149+
150+
<!-- /.related -->
151+
152+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
153+
154+
<section class="links">
155+
156+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
157+
158+
</section>
159+
160+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var reDurationString = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var bool;
34+
var i;
35+
36+
values = [
37+
'3d',
38+
'3d2h',
39+
'21h',
40+
'21h3m',
41+
'3m30s',
42+
'3m',
43+
'3m30ms',
44+
'3m30s40ms',
45+
'40ms',
46+
'3d4m',
47+
'3d4m30s',
48+
'3d4m30s40ms',
49+
'3d4h5m6s7ms',
50+
'1.0.0',
51+
'3 days',
52+
'3 days 2 hours',
53+
'beep boop',
54+
'3d 2h'
55+
];
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
bool = reDurationString.REGEXP.test( values[ i%values.length ] );
60+
if ( typeof bool !== 'boolean' ) {
61+
b.fail( 'should return a boolean' );
62+
}
63+
}
64+
b.toc();
65+
if ( !isBoolean( bool ) ) {
66+
b.fail( 'should return a boolean' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
{{alias}}()
3+
Returns a regular expression to match a duration string.
4+
5+
A duration string is a string containing a sequence of time units. A time
6+
unit is a nonnegative integer followed by a unit identifier. The following
7+
unit identifiers are supported:
8+
9+
- d: days
10+
- h: hours
11+
- m: minutes
12+
- s: seconds
13+
- ms: milliseconds
14+
15+
For example, the string `1m3s10ms` is a duration string containing three
16+
time units: `1m` (1 minute), `3s` (3 seconds), and `10ms` (10 milliseconds).
17+
The string `60m` is a duration string containing a single time unit: `60m`
18+
(60 minutes). Time units must be supplied in descending order of magnitude
19+
(i.e., days, hours, minutes, seconds, milliseconds).
20+
21+
Duration strings are case insensitive. For example, the string `1M3S10MS` is
22+
equivalent to `1m3s10ms`.
23+
24+
The regular expression captures the following groups:
25+
26+
1. The days component.
27+
2. The hours component.
28+
3. The minutes component.
29+
4. The seconds component.
30+
5. The milliseconds component.
31+
32+
Returns
33+
-------
34+
re: RegExp: Regular expression
35+
Regular expression to match a duration string.
36+
37+
Examples
38+
--------
39+
> var RE = {{alias}}();
40+
> var parts = RE.exec( '3d2ms' )
41+
[...]
42+
> parts = RE.exec( '4h3m20s' )
43+
[...]
44+
45+
46+
{{alias}}.REGEXP
47+
Regular expression to match a duration string.
48+
49+
Examples
50+
--------
51+
> var bool = {{alias}}.REGEXP.test( '3d2ms' )
52+
true
53+
> bool = {{alias}}.REGEXP.test( 'foo' )
54+
false
55+
56+
See Also
57+
--------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
/**
22+
* Interface for a regular expression to match duration strings.
23+
*/
24+
interface ReDurationString {
25+
/**
26+
* Returns a regular expression to match a duration string.
27+
*
28+
* @returns regular expression
29+
*
30+
* @example
31+
* var RE_DURATION = reDurationString();
32+
* // returns <RegExp>
33+
*
34+
* var bool = RE_DURATION.test( '20ms' );
35+
* // returns true
36+
*
37+
* bool = RE_DURATION.test( '3d21h' );
38+
* // returns true
39+
*
40+
* bool = RE_DURATION.test( '2s 40ms' );
41+
* // returns false
42+
*
43+
* bool = RE_DURATION.test( '20' );
44+
* // returns false
45+
*/
46+
(): RegExp;
47+
48+
/**
49+
* Regular expression to match a duration string.
50+
*
51+
* @example
52+
* var bool = reDurationString.REGEXP.test( '3m20s40ms' );
53+
* // returns true
54+
*/
55+
REGEXP: RegExp;
56+
}
57+
58+
/**
59+
* Returns a regular expression to match a duration string.
60+
*
61+
* ## Notes
62+
*
63+
* - A duration string is a string containing a sequence of time units. A time unit is a nonnegative integer followed by a unit identifier. The following unit identifiers are supported:
64+
*
65+
* - `d`: days
66+
* - `h`: hours
67+
* - `m`: minutes
68+
* - `s`: seconds
69+
* - `ms`: milliseconds
70+
*
71+
* For example, the string `1m3s10ms` is a duration string containing three time units: `1m` (1 minute), `3s` (3 seconds), and `10ms` (10 milliseconds). The string `60m` is a duration string containing a single time unit: `60m` (60 minutes). Time units must be supplied in descending order of magnitude (i.e., days, hours, minutes, seconds, milliseconds).
72+
*
73+
* - Duration strings are case insensitive. For example, the string `1M3S10MS` is equivalent to `1m3s10ms`.
74+
*
75+
* - The regular expression captures the following groups:
76+
*
77+
* 1. The days component.
78+
* 2. The hours component.
79+
* 3. The minutes component.
80+
* 4. The seconds component.
81+
* 5. The milliseconds component.
82+
*
83+
* @returns regular expression
84+
*
85+
* @example
86+
* var RE_DURATION = reDurationString();
87+
* // returns <RegExp>
88+
*
89+
* var bool = RE_DURATION.test( '3d23h' );
90+
* // returns true
91+
*
92+
* bool = RE_DURATION.test( '2H30M' );
93+
* // returns true
94+
*
95+
* bool = RE_DURATION.test( 'abc' );
96+
* // returns false
97+
*
98+
* bool = RE_DURATION.test( 'foo bar' );
99+
* // returns false
100+
*/
101+
declare var reDurationString: ReDurationString;
102+
103+
104+
// EXPORTS //
105+
106+
export = reDurationString;

0 commit comments

Comments
 (0)