From 2b247b2642c06e210dabb00c9d2c389dd3d1f5a1 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Fri, 28 Oct 2022 11:15:27 -0400 Subject: [PATCH 1/6] Add README of regular expression for durations --- .../@stdlib/regexp/duration-string/README.md | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 lib/node_modules/@stdlib/regexp/duration-string/README.md diff --git a/lib/node_modules/@stdlib/regexp/duration-string/README.md b/lib/node_modules/@stdlib/regexp/duration-string/README.md new file mode 100644 index 000000000000..57543321124a --- /dev/null +++ b/lib/node_modules/@stdlib/regexp/duration-string/README.md @@ -0,0 +1,160 @@ + + +# reDurationString + +> [Regular expression][mdn-regexp] to match a duration string. + +
+ +## Usage + +```javascript +var reDurationString = require( '@stdlib/regexp/duration-string' ); +``` + +#### reDurationString() + +Returns a [regular expression][mdn-regexp] to match a duration string. + +```javascript +var RE_DURATION = reDurationString(); +// returns + +var parts = RE_DURATION.exec( '3d2ms' ); +/* returns + [ + '3d2ms', + '3d', + undefined, + undefined, + undefined, + '2ms', + index: 0, + input: '3d2ms', + groups: undefined + ] +*/ + +parts = RE_SEMVER.exec( '4h3m20s' ); +/* returns + [ + '4h3m20s', + undefined, + '4h', + '3m', + '20s', + undefined, + index: 0, + input: '4h3m20s', + groups: undefined + ] +*/ +``` + +#### reDurationString.REGEXP + +[Regular expression][mdn-regexp] to match a duration string. + +```javascript +var bool = reDurationString.REGEXP.test( '3d12h' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- 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: + + - `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). Time units must be supplied in descending order of magnitude (i.e., days, hours, minutes, seconds, milliseconds). + +- Duration strings are case insensitive. For example, the string `1M3S10MS` is equivalent to `1m3s10ms`. + +- The regular expression captures the following groups: + + 1. The days component. + 2. The hours component. + 3. The minutes component. + 4. The seconds component. + 5. The milliseconds component. + +
+ + + +
+ +## Examples + + + +```javascript +var reDurationString = require( '@stdlib/regexp/duration-string' ); + +var RE_DURATION = reDurationString(); + +var bool = RE_DURATION.test( '3d12h' ); +// returns true + +bool = RE_DURATION.test( '1M3S10MS' ); +// returns true + +bool = RE_DURATION.test( '1y.0w.0d' ); +// returns false + +bool = RE_DURATION.test( '1y3w' ); +// returns false + +bool = RE_DURATION.test( 'beep' ); +// returns false +``` + +
+ + + + + + + + + + + + + + From db44176ada4fb4be578beae55d50a87a9dc2013d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 28 Oct 2022 15:26:31 +0000 Subject: [PATCH 2/6] Scaffold regexp/duration-string package files --- .../duration-string/benchmark/benchmark.js | 48 ++++++++++ .../regexp/duration-string/docs/repl.txt | 25 +++++ .../duration-string/docs/types/index.d.ts | 36 +++++++ .../regexp/duration-string/docs/types/test.ts | 35 +++++++ .../regexp/duration-string/examples/index.js | 43 +++++++++ .../regexp/duration-string/lib/index.js | 53 +++++++++++ .../regexp/duration-string/lib/main.js | 44 +++++++++ .../regexp/duration-string/package.json | 52 ++++++++++ .../regexp/duration-string/test/test.js | 94 +++++++++++++++++++ 9 files changed, 430 insertions(+) create mode 100644 lib/node_modules/@stdlib/regexp/duration-string/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/regexp/duration-string/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/regexp/duration-string/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/regexp/duration-string/examples/index.js create mode 100644 lib/node_modules/@stdlib/regexp/duration-string/lib/index.js create mode 100644 lib/node_modules/@stdlib/regexp/duration-string/lib/main.js create mode 100644 lib/node_modules/@stdlib/regexp/duration-string/package.json create mode 100644 lib/node_modules/@stdlib/regexp/duration-string/test/test.js diff --git a/lib/node_modules/@stdlib/regexp/duration-string/benchmark/benchmark.js b/lib/node_modules/@stdlib/regexp/duration-string/benchmark/benchmark.js new file mode 100644 index 000000000000..526e1a27831e --- /dev/null +++ b/lib/node_modules/@stdlib/regexp/duration-string/benchmark/benchmark.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2022 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var reDurationString = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var re; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + re = reDurationString(); + if ( !re.exec ) { + b.fail( 'should return a regular expression' ); + } + } + b.toc(); + if ( !re.exec ) { + b.fail( 'should return a regular expression' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + diff --git a/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt b/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt new file mode 100644 index 000000000000..5f33c762f802 --- /dev/null +++ b/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt @@ -0,0 +1,25 @@ + +{{alias}}() + Returns a regular expression to match a duration string. + + Returns + ------- + re: RegExp: Regular expression + Regular expression to match a duration string. + + Examples + -------- + > var RE = {{alias}}(); + > var parts = RE.exec( '3d2ms' ) + [ '3d2ms', '3d', undefined, undefined, undefined, '2ms', index: 0, input: + '3d2ms', groups: undefined ] + > parts = RE.exec( '4h3m20s' ) + [ '4h3m20s', undefined, '4h', '3m', '20s', undefined, index: 0, input: + '4h3m20s', groups: undefined ] + + See Also + -------- + + + See Also + -------- diff --git a/lib/node_modules/@stdlib/regexp/duration-string/docs/types/index.d.ts b/lib/node_modules/@stdlib/regexp/duration-string/docs/types/index.d.ts new file mode 100644 index 000000000000..b6ea0d01a7f6 --- /dev/null +++ b/lib/node_modules/@stdlib/regexp/duration-string/docs/types/index.d.ts @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2022 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Returns a regular expression to match a duration string. +* +* @returns regular expression +* +* @example +* var RE_DURATION = reDurationString(); +* // returns +*/ +declare function reDurationString(): RegExp; + + +// EXPORTS // + +export = reDurationString; + diff --git a/lib/node_modules/@stdlib/regexp/duration-string/docs/types/test.ts b/lib/node_modules/@stdlib/regexp/duration-string/docs/types/test.ts new file mode 100644 index 000000000000..b7f4529d7c8f --- /dev/null +++ b/lib/node_modules/@stdlib/regexp/duration-string/docs/types/test.ts @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2022 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import reDurationString = require( './index' ); + + +// TESTS // + +// The function returns a regular expression... +{ + reDurationString(); // $ExpectType RegExp +} + +// The compiler throws an error if the function is provided arguments... +{ + reDurationString( true ); // $ExpectError + reDurationString( [], 123 ); // $ExpectError +} + + diff --git a/lib/node_modules/@stdlib/regexp/duration-string/examples/index.js b/lib/node_modules/@stdlib/regexp/duration-string/examples/index.js new file mode 100644 index 000000000000..f5c325165e2b --- /dev/null +++ b/lib/node_modules/@stdlib/regexp/duration-string/examples/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2022 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var reDurationString = require( './../lib' ); + +var RE_DURATION = reDurationString(); + +var bool = RE_DURATION.test( '3d12h' ); +console.log( bool ); +// => true + +bool = RE_DURATION.test( '1M3S10MS' ); +console.log( bool ); +// => true + +bool = RE_DURATION.test( '1y.0w.0d' ); +console.log( bool ); +// => false + +bool = RE_DURATION.test( '1y3w' ); +console.log( bool ); +// => false + +bool = RE_DURATION.test( 'beep' ); +console.log( bool ); +// => false diff --git a/lib/node_modules/@stdlib/regexp/duration-string/lib/index.js b/lib/node_modules/@stdlib/regexp/duration-string/lib/index.js new file mode 100644 index 000000000000..e8c562229e33 --- /dev/null +++ b/lib/node_modules/@stdlib/regexp/duration-string/lib/index.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2022 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Regular expression to match a duration string. +* +* @module @stdlib/regexp/duration-string +* +* @example +* var reDurationString = require( '@stdlib/regexp/duration-string' ); +* +* var RE_DURATION = reDurationString(); +* // returns +* +* var parts = RE_DURATION.exec( '3d2ms' ); +* // returns [ '3d2ms', '3d', '2ms' ] +* +* parts = RE_DURATION.exec( '2ms' ); +* // returns [ '2ms', '2ms' ] +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var REGEXP = require( './regexp.js' ); + + +// MAIN // + +setReadOnly( main, 'REGEXP', REGEXP ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/regexp/duration-string/lib/main.js b/lib/node_modules/@stdlib/regexp/duration-string/lib/main.js new file mode 100644 index 000000000000..71e322df5752 --- /dev/null +++ b/lib/node_modules/@stdlib/regexp/duration-string/lib/main.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2022 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +// TODO: Load required modules... + + +// MAIN // + +/** +* Returns a regular expression to match a duration string. +* +* @returns {RegExp} regular expression +* +* @example +* var RE_DURATION = reDurationString(); +* // returns +*/ +function reDurationString() { + // TODO: Add implementation... +} + + +// EXPORTS // + +module.exports = reDurationString; diff --git a/lib/node_modules/@stdlib/regexp/duration-string/package.json b/lib/node_modules/@stdlib/regexp/duration-string/package.json new file mode 100644 index 000000000000..826d8d63ef99 --- /dev/null +++ b/lib/node_modules/@stdlib/regexp/duration-string/package.json @@ -0,0 +1,52 @@ +{ + "name": "@stdlib/regexp/duration-string", + "version": "0.0.0", + "description": "", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [] +} diff --git a/lib/node_modules/@stdlib/regexp/duration-string/test/test.js b/lib/node_modules/@stdlib/regexp/duration-string/test/test.js new file mode 100644 index 000000000000..bd34f248e1d9 --- /dev/null +++ b/lib/node_modules/@stdlib/regexp/duration-string/test/test.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2022 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var RE_DURATION = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a regular expression', function test( t ) { + t.ok( true, __filename ); + t.equal( RE_DURATION instanceof RegExp, true, 'main export is a regular expression' ); + t.end(); +}); + +tape( 'the regular expression captures a duration string', function test( t ) { + var values; + var i; + + values = [ + '3d', + '3D', + '3h', + '3H', + '3m', + '3M', + '3s', + '3S', + '3ms', + '3MS', + '3us', + '3US', + '3ns', + '3NS', + '3s4ms', + '3S4MS', + '3s4ms5us', + '3S4MS5US', + '3s4ms5us6ns', + '3S4MS5US6NS', + '3h4m5s', + '3H4M5S', + '3h4m5s6ms', + '3H4M5S6MS', + '3h4m5s6ms7us', + '3H4M5S6MS7US', + '3h4m5s6ms7us8ns', + '3H4M5S6MS7US8NS', + '3d4h5m6s', + '3D4H5M6S', + '3d4h5m6s7ms', + '3D4H5M6S7MS', + '3d4h5m6s7ms8us', + '3D4H5M6S7MS8US', + '3d4h5m6s7ms8us9ns', + '3D4H5M6S7MS8US9NS', + '3d4h5m6s7ms8us9ns10s', + '3D4H5M6S7MS8US9NS10S', + '3d4h5m6s7ms8us9ns10s11ms', + '3D4H5M6S7MS8US9NS10S11MS', + '3d4h5m6s7ms8us9ns10s11ms12us', + '3D4H5M6S7MS8US9NS10S11MS12US', + '3d4h5m6s7ms8us9ns10s11ms12us13ns', + '3D4H5M6S7MS8US9NS10S11MS12US13NS' + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( RE_DURATION.test( values[i] ), true, values[i] ); + } + t.end(); +}); + + +// TODO: Add more tests... + From d2ad3cf90d69627fb4bb26ea91089e2148f52c01 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Mon, 31 Oct 2022 12:51:40 -0400 Subject: [PATCH 3/6] Add initial implementation --- .../duration-string/benchmark/benchmark.js | 36 +++++-- .../regexp/duration-string/docs/repl.txt | 44 +++++++-- .../duration-string/docs/types/index.d.ts | 76 ++++++++++++++- .../regexp/duration-string/docs/types/test.ts | 8 +- .../regexp/duration-string/lib/index.js | 4 +- .../regexp/duration-string/lib/main.js | 29 ++++-- .../regexp/duration-string/lib/regexp.js | 63 +++++++++++++ .../regexp/duration-string/package.json | 14 ++- .../regexp/duration-string/test/test.js | 67 ++----------- .../regexp/duration-string/test/test.main.js | 94 +++++++++++++++++++ .../duration-string/test/test.regexp.js | 91 ++++++++++++++++++ 11 files changed, 437 insertions(+), 89 deletions(-) create mode 100644 lib/node_modules/@stdlib/regexp/duration-string/lib/regexp.js create mode 100644 lib/node_modules/@stdlib/regexp/duration-string/test/test.main.js create mode 100644 lib/node_modules/@stdlib/regexp/duration-string/test/test.regexp.js diff --git a/lib/node_modules/@stdlib/regexp/duration-string/benchmark/benchmark.js b/lib/node_modules/@stdlib/regexp/duration-string/benchmark/benchmark.js index 526e1a27831e..081e7be95aef 100644 --- a/lib/node_modules/@stdlib/regexp/duration-string/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/regexp/duration-string/benchmark/benchmark.js @@ -21,6 +21,7 @@ // MODULES // var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; var pkg = require( './../package.json' ).name; var reDurationString = require( './../lib' ); @@ -28,21 +29,42 @@ var reDurationString = require( './../lib' ); // MAIN // bench( pkg, function benchmark( b ) { - var re; + var values; + var bool; var i; + values = [ + '3d', + '3d2h', + '21h', + '21h3m', + '3m30s', + '3m', + '3m30ms', + '3m30s40ms', + '40ms', + '3d4m', + '3d4m30s', + '3d4m30s40ms', + '3d4h5m6s7ms', + '1.0.0', + '3 days', + '3 days 2 hours', + 'beep boop', + '3d 2h' + ]; + b.tic(); for ( i = 0; i < b.iterations; i++ ) { - re = reDurationString(); - if ( !re.exec ) { - b.fail( 'should return a regular expression' ); + bool = reDurationString.REGEXP.test( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); } } b.toc(); - if ( !re.exec ) { - b.fail( 'should return a regular expression' ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); } b.pass( 'benchmark finished' ); b.end(); }); - diff --git a/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt b/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt index 5f33c762f802..d130937f89eb 100644 --- a/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt +++ b/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt @@ -2,6 +2,33 @@ {{alias}}() Returns a regular expression to match a duration string. + 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: + + - 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). Time units must be supplied in descending order of magnitude + (i.e., days, hours, minutes, seconds, milliseconds). + + Duration strings are case insensitive. For example, the string `1M3S10MS` is + equivalent to `1m3s10ms`. + + The regular expression captures the following groups: + + 1. The days component. + 2. The hours component. + 3. The minutes component. + 4. The seconds component. + 5. The milliseconds component. + Returns ------- re: RegExp: Regular expression @@ -11,15 +38,20 @@ -------- > var RE = {{alias}}(); > var parts = RE.exec( '3d2ms' ) - [ '3d2ms', '3d', undefined, undefined, undefined, '2ms', index: 0, input: - '3d2ms', groups: undefined ] + [...] > parts = RE.exec( '4h3m20s' ) - [ '4h3m20s', undefined, '4h', '3m', '20s', undefined, index: 0, input: - '4h3m20s', groups: undefined ] + [...] - See Also - -------- +{{alias}}.REGEXP + Regular expression to match a duration string. + + Examples + -------- + > var bool = {{alias}}.REGEXP.test( '3d2ms' ) + true + > bool = {{alias}}.REGEXP.test( 'foo' ) + false See Also -------- diff --git a/lib/node_modules/@stdlib/regexp/duration-string/docs/types/index.d.ts b/lib/node_modules/@stdlib/regexp/duration-string/docs/types/index.d.ts index b6ea0d01a7f6..68fe26612e52 100644 --- a/lib/node_modules/@stdlib/regexp/duration-string/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/regexp/duration-string/docs/types/index.d.ts @@ -1,4 +1,4 @@ -/** +/* * @license Apache-2.0 * * Copyright (c) 2022 The Stdlib Authors. @@ -18,19 +18,89 @@ // TypeScript Version: 2.0 +/** +* Interface for a regular expression to match duration strings. +*/ +interface ReDurationString { + /** + * Returns a regular expression to match a duration string. + * + * @returns regular expression + * + * @example + * var RE_DURATION = reDurationString(); + * // returns + * + * var bool = RE_DURATION.test( '20ms' ); + * // returns true + * + * bool = RE_DURATION.test( '3d21h' ); + * // returns true + * + * bool = RE_DURATION.test( '2s 40ms' ); + * // returns false + * + * bool = RE_DURATION.test( '20' ); + * // returns false + */ + (): RegExp; + + /** + * Regular expression to match a duration string. + * + * @example + * var bool = reDurationString.REGEXP.test( '3m20s40ms' ); + * // returns true + */ + REGEXP: RegExp; +} + /** * Returns a regular expression to match a duration string. * +* ## Notes +* +* - 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: +* +* - `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). Time units must be supplied in descending order of magnitude (i.e., days, hours, minutes, seconds, milliseconds). +* +* - Duration strings are case insensitive. For example, the string `1M3S10MS` is equivalent to `1m3s10ms`. +* +* - The regular expression captures the following groups: +* +* 1. The days component. +* 2. The hours component. +* 3. The minutes component. +* 4. The seconds component. +* 5. The milliseconds component. +* * @returns regular expression * * @example * var RE_DURATION = reDurationString(); * // returns +* +* var bool = RE_DURATION.test( '3d23h' ); +* // returns true +* +* bool = RE_DURATION.test( '2H30M' ); +* // returns true +* +* bool = RE_DURATION.test( 'abc' ); +* // returns false +* +* bool = RE_DURATION.test( 'foo bar' ); +* // returns false */ -declare function reDurationString(): RegExp; +declare var reDurationString: ReDurationString; // EXPORTS // export = reDurationString; - diff --git a/lib/node_modules/@stdlib/regexp/duration-string/docs/types/test.ts b/lib/node_modules/@stdlib/regexp/duration-string/docs/types/test.ts index b7f4529d7c8f..3422d9cd8580 100644 --- a/lib/node_modules/@stdlib/regexp/duration-string/docs/types/test.ts +++ b/lib/node_modules/@stdlib/regexp/duration-string/docs/types/test.ts @@ -1,4 +1,4 @@ -/** +/* * @license Apache-2.0 * * Copyright (c) 2022 The Stdlib Authors. @@ -32,4 +32,8 @@ import reDurationString = require( './index' ); reDurationString( [], 123 ); // $ExpectError } - +// Attached to main export is a `REGEXP` property whose value is a regular expression... +{ + // tslint:disable-next-line:no-unused-expression + reDurationString.REGEXP; // $ExpectType RegExp +} diff --git a/lib/node_modules/@stdlib/regexp/duration-string/lib/index.js b/lib/node_modules/@stdlib/regexp/duration-string/lib/index.js index e8c562229e33..836fbcd4579f 100644 --- a/lib/node_modules/@stdlib/regexp/duration-string/lib/index.js +++ b/lib/node_modules/@stdlib/regexp/duration-string/lib/index.js @@ -30,10 +30,10 @@ * // returns * * var parts = RE_DURATION.exec( '3d2ms' ); -* // returns [ '3d2ms', '3d', '2ms' ] +* // returns [...] * * parts = RE_DURATION.exec( '2ms' ); -* // returns [ '2ms', '2ms' ] +* // returns [...] */ // MODULES // diff --git a/lib/node_modules/@stdlib/regexp/duration-string/lib/main.js b/lib/node_modules/@stdlib/regexp/duration-string/lib/main.js index 71e322df5752..ee87177d2766 100644 --- a/lib/node_modules/@stdlib/regexp/duration-string/lib/main.js +++ b/lib/node_modules/@stdlib/regexp/duration-string/lib/main.js @@ -18,16 +18,33 @@ 'use strict'; -// MODULES // - -// TODO: Load required modules... - - // MAIN // /** * Returns a regular expression to match a duration string. * +* ## Notes +* +* - 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: +* +* - `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). Time units must be supplied in descending order of magnitude (i.e., days, hours, minutes, seconds, milliseconds). +* +* - Duration strings are case insensitive. For example, the string `1M3S10MS` is equivalent to `1m3s10ms`. +* +* - The regular expression captures the following groups: +* +* 1. The days component. +* 2. The hours component. +* 3. The minutes component. +* 4. The seconds component. +* 5. The milliseconds component. +* * @returns {RegExp} regular expression * * @example @@ -35,7 +52,7 @@ * // returns */ function reDurationString() { - // TODO: Add implementation... + return /^(\d+d)?(\d+h)?(\d+m)?(\d+s)?(\d+ms)?$/i; } diff --git a/lib/node_modules/@stdlib/regexp/duration-string/lib/regexp.js b/lib/node_modules/@stdlib/regexp/duration-string/lib/regexp.js new file mode 100644 index 000000000000..73d75c7454e5 --- /dev/null +++ b/lib/node_modules/@stdlib/regexp/duration-string/lib/regexp.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2022 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var reDurationString = require( './main.js' ); + + +// MAIN // + +/** +* Matches a duration string. +* +* Regular Expression: `/^(\d+d)?(\d+h)?(\d+m)?(\d+s)?(\d+ms)?$/` +* +* - `^` +* - start of input +* +* - `(\d+d)?` +* - capture a group of one or more digits followed by the literal character `d` for the number of days (optional) + +* - `(\d+h)?` +* - capture a group of one or more digits followed by the literal character `h` for the number of hours (optional) + +* - `(\d+m)?` +* - capture a group of one or more digits followed by the literal character `m` for the number of minutes (optional) + +* - `(\d+s)?` +* - capture a group of one or more digits followed by the literal character `s` for the number of seconds (optional) + +* - `(\d+ms)?` +* - capture a group of one or more digits followed by the literal character `ms` for the number of milliseconds (optional) +* +* - `$` +* - end of input +* +* @constant +* @type {RegExp} +* @default /^(\d+d)?(\d+h)?(\d+m)?(\d+s)?(\d+ms)?$/ +*/ +var RE_DURATION = reDurationString(); + + +// EXPORTS // + +module.exports = RE_DURATION; diff --git a/lib/node_modules/@stdlib/regexp/duration-string/package.json b/lib/node_modules/@stdlib/regexp/duration-string/package.json index 826d8d63ef99..a4d4a7fb3f61 100644 --- a/lib/node_modules/@stdlib/regexp/duration-string/package.json +++ b/lib/node_modules/@stdlib/regexp/duration-string/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/regexp/duration-string", "version": "0.0.0", - "description": "", + "description": "Regular expression to match a duration string.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", @@ -48,5 +48,15 @@ "win32", "windows" ], - "keywords": [] + "keywords": [ + "stdlib", + "regex", + "regexp", + "re", + "regular", + "expression", + "duration", + "string", + "match" + ] } diff --git a/lib/node_modules/@stdlib/regexp/duration-string/test/test.js b/lib/node_modules/@stdlib/regexp/duration-string/test/test.js index bd34f248e1d9..ab3ad851120f 100644 --- a/lib/node_modules/@stdlib/regexp/duration-string/test/test.js +++ b/lib/node_modules/@stdlib/regexp/duration-string/test/test.js @@ -21,74 +21,19 @@ // MODULES // var tape = require( 'tape' ); -var RE_DURATION = require( './../lib' ); +var isRegExp = require( '@stdlib/assert/is-regexp' ); +var reDurationString = require( './../lib' ); // TESTS // -tape( 'main export is a regular expression', function test( t ) { +tape( 'main export is a function', function test( t ) { t.ok( true, __filename ); - t.equal( RE_DURATION instanceof RegExp, true, 'main export is a regular expression' ); + t.equal( typeof reDurationString, 'function', 'main export is a function' ); t.end(); }); -tape( 'the regular expression captures a duration string', function test( t ) { - var values; - var i; - - values = [ - '3d', - '3D', - '3h', - '3H', - '3m', - '3M', - '3s', - '3S', - '3ms', - '3MS', - '3us', - '3US', - '3ns', - '3NS', - '3s4ms', - '3S4MS', - '3s4ms5us', - '3S4MS5US', - '3s4ms5us6ns', - '3S4MS5US6NS', - '3h4m5s', - '3H4M5S', - '3h4m5s6ms', - '3H4M5S6MS', - '3h4m5s6ms7us', - '3H4M5S6MS7US', - '3h4m5s6ms7us8ns', - '3H4M5S6MS7US8NS', - '3d4h5m6s', - '3D4H5M6S', - '3d4h5m6s7ms', - '3D4H5M6S7MS', - '3d4h5m6s7ms8us', - '3D4H5M6S7MS8US', - '3d4h5m6s7ms8us9ns', - '3D4H5M6S7MS8US9NS', - '3d4h5m6s7ms8us9ns10s', - '3D4H5M6S7MS8US9NS10S', - '3d4h5m6s7ms8us9ns10s11ms', - '3D4H5M6S7MS8US9NS10S11MS', - '3d4h5m6s7ms8us9ns10s11ms12us', - '3D4H5M6S7MS8US9NS10S11MS12US', - '3d4h5m6s7ms8us9ns10s11ms12us13ns', - '3D4H5M6S7MS8US9NS10S11MS12US13NS' - ]; - - for ( i = 0; i < values.length; i++ ) { - t.equal( RE_DURATION.test( values[i] ), true, values[i] ); - } +tape( 'attached to the main export is a regular expression', function test( t ) { + t.equal( isRegExp( reDurationString.REGEXP ), true, 'exports a regular expression' ); t.end(); }); - - -// TODO: Add more tests... - diff --git a/lib/node_modules/@stdlib/regexp/duration-string/test/test.main.js b/lib/node_modules/@stdlib/regexp/duration-string/test/test.main.js new file mode 100644 index 000000000000..ec70806de916 --- /dev/null +++ b/lib/node_modules/@stdlib/regexp/duration-string/test/test.main.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2022 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var reDurationString = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof reDurationString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the returned regular expression matches a duration string', function test( t ) { + var values; + var RE; + var i; + + RE = reDurationString(); + values = [ + '3d', + '3d2h', + '21h', + '21h3m', + '3m30s', + '3m', + '3m30ms', + '3m30s40ms', + '40ms', + '3d4m', + '3d4m30s', + '3d4m30s40ms', + '3d4h5m6s7ms', + '3D', + '3D2h', + '21H', + '21h3M', + '3M30S', + '3M', + '3M30MS', + '3M30S40MS', + '40MS', + '3D4M', + '3D4M30S', + '3D4M30S40MS', + '3D4H5M6S7MS' + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( RE.test( values[i] ), true, 'returns expected value when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the returned regular expression does not match a non-duration string', function test( t ) { + var values; + var RE; + var i; + + RE = reDurationString(); + values = [ + '1.0.0', + '3 days', + '3 days 2 hours', + 'beep boop', + '3d 2h', + 'foo' + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( RE.test( values[i] ), false, 'returns expected value when provided '+values[i] ); + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/regexp/duration-string/test/test.regexp.js b/lib/node_modules/@stdlib/regexp/duration-string/test/test.regexp.js new file mode 100644 index 000000000000..d76b08c6d33c --- /dev/null +++ b/lib/node_modules/@stdlib/regexp/duration-string/test/test.regexp.js @@ -0,0 +1,91 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2022 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var RE = require( './../lib/regexp.js' ); + + +// TESTS // + +tape( 'main export is a regular expression', function test( t ) { + t.ok( true, __filename ); + t.equal( RE instanceof RegExp, true, 'main export is a regular expression' ); + t.end(); +}); + +tape( 'the regular expression matches valid duration strings', function test( t ) { + var values; + var i; + + values = [ + '3d', + '3d2h', + '21h', + '21h3m', + '3m30s', + '3m', + '3m30ms', + '3m30s40ms', + '40ms', + '3d4m', + '3d4m30s', + '3d4m30s40ms', + '3d4h5m6s7ms', + '3D', + '3D2h', + '21H', + '21h3M', + '3M30S', + '3M', + '3M30MS', + '3M30S40MS', + '40MS', + '3D4M', + '3D4M30S', + '3D4M30S40MS', + '3D4H5M6S7MS' + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( RE.test( values[i] ), true, 'returns expected value when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the regular expression does not match invalid duration strings', function test( t ) { + var values; + var i; + + values = [ + '1.0.0', + '3 days', + '3 days 2 hours', + 'beep boop', + '3d 2h', + 'foo' + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( RE.test( values[i] ), false, 'returns expected value when provided '+values[i] ); + } + t.end(); +}); From aa8067bd059d6173274bd5ceab20dc62c0d6e835 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Tue, 1 Nov 2022 09:43:37 -0400 Subject: [PATCH 4/6] Add test case --- .../@stdlib/regexp/duration-string/test/test.main.js | 6 ++++++ .../@stdlib/regexp/duration-string/test/test.regexp.js | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/lib/node_modules/@stdlib/regexp/duration-string/test/test.main.js b/lib/node_modules/@stdlib/regexp/duration-string/test/test.main.js index ec70806de916..36f0961c7b80 100644 --- a/lib/node_modules/@stdlib/regexp/duration-string/test/test.main.js +++ b/lib/node_modules/@stdlib/regexp/duration-string/test/test.main.js @@ -72,6 +72,12 @@ tape( 'the returned regular expression matches a duration string', function test t.end(); }); +tape( 'the returned regular expression matches an empty string', function test( t ) { + var RE = reDurationString(); + t.equal( RE.test( '' ), true, 'returns expected value' ); + t.end(); +}); + tape( 'the returned regular expression does not match a non-duration string', function test( t ) { var values; var RE; diff --git a/lib/node_modules/@stdlib/regexp/duration-string/test/test.regexp.js b/lib/node_modules/@stdlib/regexp/duration-string/test/test.regexp.js index d76b08c6d33c..b4513315fd02 100644 --- a/lib/node_modules/@stdlib/regexp/duration-string/test/test.regexp.js +++ b/lib/node_modules/@stdlib/regexp/duration-string/test/test.regexp.js @@ -71,6 +71,11 @@ tape( 'the regular expression matches valid duration strings', function test( t t.end(); }); +tape( 'the regular expression matches an empty string', function test( t ) { + t.equal( RE.test( '' ), true, 'returns expected value' ); + t.end(); +}); + tape( 'the regular expression does not match invalid duration strings', function test( t ) { var values; var i; From fb76d9a6b3158b5a67c9353720c00567e0691b48 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 1 Nov 2022 23:22:36 -0700 Subject: [PATCH 5/6] Fix indentation --- .../@stdlib/regexp/duration-string/docs/repl.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt b/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt index d130937f89eb..e5071e5efe8c 100644 --- a/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt +++ b/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt @@ -6,11 +6,11 @@ unit is a nonnegative integer followed by a unit identifier. The following unit identifiers are supported: - - d: days - - h: hours - - m: minutes - - s: seconds - - ms: milliseconds + - 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). From b495f7176919098bf4d87b0af77a24e4f63c58f1 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 1 Nov 2022 23:23:09 -0700 Subject: [PATCH 6/6] Fix indentation --- .../@stdlib/regexp/duration-string/docs/repl.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt b/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt index e5071e5efe8c..9c753c531c1e 100644 --- a/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt +++ b/lib/node_modules/@stdlib/regexp/duration-string/docs/repl.txt @@ -23,11 +23,11 @@ The regular expression captures the following groups: - 1. The days component. - 2. The hours component. - 3. The minutes component. - 4. The seconds component. - 5. The milliseconds component. + 1. The days component. + 2. The hours component. + 3. The minutes component. + 4. The seconds component. + 5. The milliseconds component. Returns -------