Skip to content

Commit 0b21908

Browse files
Planeshifterstdlib-botkgryte
authored
Add package to convert number of milliseconds to a duration string (#563)
* Add README for package to convert ms to duration * Scaffold time/ms2duration package files * Finish implementation * Update lib/node_modules/@stdlib/time/ms2duration/README.md Co-authored-by: Athan <kgryte@gmail.com> * Use floor * Format error message Co-authored-by: stdlib-bot <noreply@stdlib.io> Co-authored-by: Athan <kgryte@gmail.com>
1 parent 8d906db commit 0b21908

File tree

15 files changed

+1147
-0
lines changed

15 files changed

+1147
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
# ms2duration
22+
23+
> Convert a number of milliseconds to a string duration.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var ms2duration = require( '@stdlib/time/ms2duration' );
31+
```
32+
33+
#### ms2duration( str )
34+
35+
Converts a number of milliseconds to a string duration.
36+
37+
```javascript
38+
var duration = ms2duration( 1030 );
39+
// returns '1s30ms'
40+
41+
duration = ms2duration( 3600000 );
42+
// returns '1h'
43+
```
44+
45+
</section>
46+
47+
<!-- /.usage -->
48+
49+
<section class="notes">
50+
51+
## Notes
52+
53+
- A duration string is a string containing a sequence of time units. A time unit is a non-negative integer followed by a unit identifier. The following unit identifiers are supported:
54+
55+
- `d`: days
56+
- `h`: hours
57+
- `m`: minutes
58+
- `s`: seconds
59+
- `ms`: milliseconds
60+
61+
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).
62+
63+
</section>
64+
65+
<!-- /.notes -->
66+
67+
<section class="examples">
68+
69+
## Examples
70+
71+
<!-- eslint no-undef: "error" -->
72+
73+
```javascript
74+
var ms2duration = require( '@stdlib/time/ms2duration' );
75+
76+
var duration = ms2duration( 1030 );
77+
// returns '1s30ms'
78+
79+
duration = ms2duration( 3600000 );
80+
// returns '1h'
81+
82+
duration = ms2duration( 0 );
83+
// returns '0ms'
84+
85+
duration = ms2duration( 86400000 );
86+
// returns '1d'
87+
88+
duration = ms2duration( 86400000+3600000+60000+1000+100 );
89+
// returns '1d1h1m1s100ms'
90+
```
91+
92+
</section>
93+
94+
<!-- /.examples -->
95+
96+
* * *
97+
98+
<section class="cli">
99+
100+
## CLI
101+
102+
<section class="usage">
103+
104+
### Usage
105+
106+
```text
107+
Usage: ms2duration [options] [<string>]
108+
109+
Options:
110+
111+
-h, --help Print this message.
112+
-V, --version Print the package version.
113+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
114+
```
115+
116+
</section>
117+
118+
<!-- /.usage -->
119+
120+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
121+
122+
<section class="notes">
123+
124+
### Notes
125+
126+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
127+
128+
```bash
129+
# Not escaped...
130+
$ echo -n $'3000\n25300' | ms2duration --split /\r?\n/
131+
132+
# Escaped...
133+
$ echo -n $'3000\n25300' | ms2duration --split /\\r?\\n/
134+
```
135+
136+
- The implementation ignores trailing delimiters.
137+
138+
</section>
139+
140+
<!-- /.notes -->
141+
142+
<section class="examples">
143+
144+
### Examples
145+
146+
```bash
147+
$ ms2duration 1000
148+
1s
149+
```
150+
151+
To use as a [standard stream][standard-streams],
152+
153+
```bash
154+
$ echo -n '1000\n2000' | ms2duration
155+
1s
156+
2s
157+
```
158+
159+
By default, when used as a [standard stream][standard-streams], the implementation assumes newline-delimited data. To specify an alternative delimiter, set the `split` option.
160+
161+
```bash
162+
$ echo -n '1350,2000' | ms2duration --split ','
163+
1s350ms
164+
2s
165+
```
166+
167+
</section>
168+
169+
<!-- /.examples -->
170+
171+
</section>
172+
173+
<!-- /.cli -->
174+
175+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
176+
177+
<section class="related">
178+
179+
<!-- /.related -->
180+
181+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
182+
183+
<section class="links">
184+
185+
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
186+
187+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
188+
189+
<!-- <related-links> -->
190+
191+
<!-- </related-links> -->
192+
193+
</section>
194+
195+
<!-- /.links -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var ms2duration = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var str;
34+
var i;
35+
36+
values = [
37+
0,
38+
1000,
39+
3600000,
40+
86400000,
41+
604800000,
42+
30000
43+
];
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
str = ms2duration( values[ i % values.length ] );
48+
if ( typeof str !== 'string' ) {
49+
b.fail( 'should return a string' );
50+
}
51+
}
52+
b.toc();
53+
if ( !isString( str ) ) {
54+
b.fail( 'should return a string' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license Apache-2.0
5+
*
6+
* Copyright (c) 2018 The Stdlib Authors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var resolve = require( 'path' ).resolve;
26+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
27+
var CLI = require( '@stdlib/cli/ctor' );
28+
var stdin = require( '@stdlib/process/read-stdin' );
29+
var stdinStream = require( '@stdlib/streams/node/stdin' );
30+
var RE_EOL = require( '@stdlib/regexp/eol' ).REGEXP;
31+
var isRegExpString = require( '@stdlib/assert/is-regexp-string' );
32+
var reFromString = require( '@stdlib/utils/regexp-from-string' );
33+
var ms2duration = require( './../lib' );
34+
35+
36+
// MAIN //
37+
38+
/**
39+
* Main execution sequence.
40+
*
41+
* @private
42+
* @returns {void}
43+
*/
44+
function main() {
45+
var split;
46+
var flags;
47+
var args;
48+
var cli;
49+
50+
// Create a command-line interface:
51+
cli = new CLI({
52+
'pkg': require( './../package.json' ),
53+
'options': require( './../etc/cli_opts.json' ),
54+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
55+
'encoding': 'utf8'
56+
})
57+
});
58+
59+
// Get any provided command-line options:
60+
flags = cli.flags();
61+
if ( flags.help || flags.version ) {
62+
return;
63+
}
64+
65+
// Get any provided command-line arguments:
66+
args = cli.args();
67+
68+
// Check if we are receiving data from `stdin`...
69+
if ( !stdinStream.isTTY ) {
70+
if ( flags.split ) {
71+
if ( !isRegExpString( flags.split ) ) {
72+
flags.split = '/'+flags.split+'/';
73+
}
74+
split = reFromString( flags.split );
75+
} else {
76+
split = RE_EOL;
77+
}
78+
return stdin( onRead );
79+
}
80+
console.log( ms2duration( parseInt( args[ 0 ], 10 ) ) ); // eslint-disable-line no-console
81+
82+
/**
83+
* Callback invoked upon reading from `stdin`.
84+
*
85+
* @private
86+
* @param {(Error|null)} error - error object
87+
* @param {Buffer} data - data
88+
* @returns {void}
89+
*/
90+
function onRead( error, data ) {
91+
var lines;
92+
var i;
93+
if ( error ) {
94+
return cli.error( error );
95+
}
96+
lines = data.toString().split( split );
97+
98+
// Remove any trailing separators (e.g., trailing newline)...
99+
if ( lines[ lines.length-1 ] === '' ) {
100+
lines.pop();
101+
}
102+
for ( i = 0; i < lines.length; i++ ) {
103+
console.log( ms2duration( parseInt( lines[ i ], 10 ) ) ); // eslint-disable-line no-console
104+
}
105+
}
106+
}
107+
108+
main();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( ms )
3+
Converts a number of milliseconds to a string duration.
4+
5+
Parameters
6+
----------
7+
ms: integer
8+
Number of milliseconds to convert.
9+
10+
Returns
11+
-------
12+
out: string
13+
Duration string.
14+
15+
Examples
16+
--------
17+
> var d = {{alias}}( 1030 )
18+
'1s30ms'
19+
> d = {{alias}}( 3600000 )
20+
'1h'
21+
> d = {{alias}}( 0 )
22+
'0ms'
23+
> d = {{alias}}( 86400000 )
24+
'1d'
25+
> d = {{alias}}( 86400000 + 3600000 + 60000 + 1000 + 100 )
26+
'1d1h1m1s100ms'
27+
28+
See Also
29+
--------

0 commit comments

Comments
 (0)