From b09e8282967500adb62b1eb86a13797c1dd1c5d4 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Wed, 26 Oct 2022 09:50:12 -0400 Subject: [PATCH 1/6] Add README for package to convert ms to duration --- .../@stdlib/time/ms2duration/README.md | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 lib/node_modules/@stdlib/time/ms2duration/README.md diff --git a/lib/node_modules/@stdlib/time/ms2duration/README.md b/lib/node_modules/@stdlib/time/ms2duration/README.md new file mode 100644 index 000000000000..33a23bd9deab --- /dev/null +++ b/lib/node_modules/@stdlib/time/ms2duration/README.md @@ -0,0 +1,195 @@ + + +# ms2duration + +> Convert a number of milliseconds to a string duration. + +
+ +## Usage + +```javascript +var ms2duration = require( '@stdlib/time/ms2duration' ); +``` + +#### ms2duration( str ) + +Converts a number of milliseconds to a string duration. + +```javascript +var duration = ms2duration( 1030 ); +// returns '1s30ms' + +duration = ms2duration( 3600000 ); +// returns '1h' +``` + +
+ + + +
+ +## Notes + +- 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: + + - `d`: days + - `h`: hours + - `m`: minutes + - `s`: seconds + - `ms`: milliseconds + + 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). A duration string must contain at least one time unit. + +
+ + + +
+ +## Examples + + + +```javascript +var ms2duration = require( '@stdlib/time/ms2duration' ); + +var duration = ms2duration( 1030 ); +// returns '1s30ms' + +duration = ms2duration( 3600000 ); +// returns '1h' + +duration = ms2duration( 0 ); +// returns '0ms' + +duration = ms2duration( 86400000 ); +// returns '1d' + +duration = ms2duration( 86400000+3600000+60000+1000+100 ); +// returns '1d1h1m1s100ms' +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: ms2duration [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. +``` + +
+ + + + + +
+ +### Notes + +- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes. + + ```bash + # Not escaped... + $ echo -n $'3000\n25300' | ms2duration --split /\r?\n/ + + # Escaped... + $ echo -n $'3000\n25300' | ms2duration --split /\\r?\\n/ + ``` + +- The implementation ignores trailing delimiters. + +
+ + + +
+ +### Examples + +```bash +$ ms2duration 1000 +1s +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n '1000\n2000' | ms2duration +1s +2s +``` + +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. + +```bash +$ echo -n '1350,2000' | ms2duration --split ',' +1s350ms +2s +``` + +
+ + + +
+ + + + + + From 360fe72d4cb3f852597ccf563c66b784730419a2 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Thu, 27 Oct 2022 15:56:15 -0400 Subject: [PATCH 5/6] Use floor --- lib/node_modules/@stdlib/time/ms2duration/lib/main.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/time/ms2duration/lib/main.js b/lib/node_modules/@stdlib/time/ms2duration/lib/main.js index 9118cbbf96ea..a9a28517414e 100644 --- a/lib/node_modules/@stdlib/time/ms2duration/lib/main.js +++ b/lib/node_modules/@stdlib/time/ms2duration/lib/main.js @@ -21,6 +21,7 @@ // MODULES // var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var floor = require( '@stdlib/math/base/special/floor' ); // MAIN // @@ -50,19 +51,19 @@ function ms2duration( ms ) { } out = ''; if ( ms >= 86400000 ) { - out += (ms/86400000|0) + 'd'; + out += floor(ms/86400000) + 'd'; ms %= 86400000; } if ( ms >= 3600000 ) { - out += (ms/3600000|0) + 'h'; + out += floor(ms/3600000) + 'h'; ms %= 3600000; } if ( ms >= 60000 ) { - out += (ms/60000|0) + 'm'; + out += floor(ms/60000) + 'm'; ms %= 60000; } if ( ms >= 1000 ) { - out += (ms/1000|0) + 's'; + out += floor(ms/1000) + 's'; ms %= 1000; } if ( ms > 0 ) { From ebc4ccea0e217441fc61f21b7e7d0ec31e3ae178 Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 27 Oct 2022 14:35:13 -0700 Subject: [PATCH 6/6] Format error message --- lib/node_modules/@stdlib/time/ms2duration/lib/main.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/time/ms2duration/lib/main.js b/lib/node_modules/@stdlib/time/ms2duration/lib/main.js index a9a28517414e..575670167e18 100644 --- a/lib/node_modules/@stdlib/time/ms2duration/lib/main.js +++ b/lib/node_modules/@stdlib/time/ms2duration/lib/main.js @@ -22,6 +22,7 @@ var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; var floor = require( '@stdlib/math/base/special/floor' ); +var format = require( '@stdlib/string/format' ); // MAIN // @@ -44,7 +45,7 @@ var floor = require( '@stdlib/math/base/special/floor' ); function ms2duration( ms ) { var out; if ( !isNonNegativeInteger( ms ) ) { - throw new TypeError( 'invalid argument. Must provide a nonnegative integer. Value: `' + ms + '`.' ); + throw new TypeError( format( 'invalid argument. Must provide a nonnegative integer. Value: `%s`.', ms ) ); } if ( ms === 0 ) { return '0ms';