Skip to content

Commit afc5766

Browse files
committed
Refactor dirname regular expression package
1 parent 4d84a08 commit afc5766

File tree

11 files changed

+440
-68
lines changed

11 files changed

+440
-68
lines changed

lib/node_modules/@stdlib/regexp/dirname/README.md

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,54 @@ limitations under the License.
2727
## Usage
2828

2929
```javascript
30-
var RE_DIRNAME = require( '@stdlib/regexp/dirname' );
30+
var reDirname = require( '@stdlib/regexp/dirname' );
3131
```
3232

33-
#### RE_DIRNAME
33+
#### reDirname( \[platform] )
3434

35-
[Regular expression][regexp] to capture a path [dirname][dirname].
35+
Returns a [regular expression][regexp] to capture a path [dirname][dirname].
3636

37-
#### RE_DIRNAME.posix
37+
```javascript
38+
var RE = reDirname();
39+
// returns <RegExp>
40+
41+
RE = reDirname( 'posix' );
42+
// returns <RegExp>
43+
44+
var dir = RE.exec( '/foo/bar/index.js' )[ 1 ];
45+
// returns '/foo/bar'
46+
47+
RE = reDirname( 'win32' );
48+
// returns <RegExp>
49+
50+
dir = RE.exec( 'C:\\foo\\bar\\index.js' )[ 1 ];
51+
// returns 'C:\\foo\\bar'
52+
```
53+
54+
#### reBasename.REGEXP
55+
56+
[Regular expression][regexp] to capture a path dirname.
57+
58+
```javascript
59+
var bool = ( reDirname.REGEXP.toString() === reDirname().toString() );
60+
// returns true
61+
```
62+
63+
#### reDirname.REGEXP_POSIX
3864

3965
[Regular expression][@stdlib/regexp/dirname-posix] to capture a [POSIX][posix] path dirname.
4066

4167
```javascript
42-
var dir = RE_DIRNAME.posix.exec( '/foo/bar/index.js' )[ 1 ];
68+
var dir = reDirname.REGEXP_POSIX.exec( '/foo/bar/index.js' )[ 1 ];
4369
// returns '/foo/bar'
4470
```
4571

46-
#### RE_DIRNAME.win32
72+
#### reDirname.REGEXP_WIN32
4773

4874
[Regular expression][@stdlib/regexp/dirname-windows] to capture a Windows path dirname.
4975

5076
```javascript
51-
var dir = RE_DIRNAME.win32.exec( 'C:\\foo\\bar\\index.js' )[ 1 ];
77+
var dir = reDirname.REGEXP_WIN32.exec( 'C:\\foo\\bar\\index.js' )[ 1 ];
5278
// returns 'C:\\foo\\bar'
5379
```
5480

@@ -60,7 +86,7 @@ var dir = RE_DIRNAME.win32.exec( 'C:\\foo\\bar\\index.js' )[ 1 ];
6086

6187
## Notes
6288

63-
- The main exported [regular expression][regexp] is [platform][@stdlib/assert/is-windows]-dependent. If the current process is running on Windows, `re === re.win32`; otherwise, `re === re.posix`.
89+
- The as `REGEXP` exported [regular expression][regexp] is [platform][@stdlib/assert/is-windows]-dependent. If the current process is running on Windows, `REGEXP === REGEXP_WIN32`; otherwise, `REGEXP === REGEXP_POSIX`.
6490

6591
</section>
6692

@@ -73,18 +99,18 @@ var dir = RE_DIRNAME.win32.exec( 'C:\\foo\\bar\\index.js' )[ 1 ];
7399
<!-- eslint no-undef: "error" -->
74100

75101
```javascript
76-
var RE_DIRNAME = require( '@stdlib/regexp/dirname' );
77-
102+
var reDirname = require( '@stdlib/regexp/dirname' );
103+
var RE_DIRNAME = reDirname();
78104
var dir;
79105

80106
// Assuming a POSIX platform...
81107
dir = RE_DIRNAME.exec( '/foo/bar/index.js' )[ 1 ];
82108
// returns '/foo/bar'
83109

84-
dir = RE_DIRNAME.posix.exec( '/foo/bar/home.html' )[ 1 ];
110+
dir = reDirname.REGEXP_POSIX.exec( '/foo/bar/home.html' )[ 1 ];
85111
// returns '/foo/bar'
86112

87-
dir = RE_DIRNAME.win32.exec( 'C:\\foo\\bar\\home.html' )[ 1 ];
113+
dir = reDirname.REGEXP_WIN32.exec( 'C:\\foo\\bar\\home.html' )[ 1 ];
88114
// returns 'C:\\foo\\bar'
89115
```
90116

lib/node_modules/@stdlib/regexp/dirname/benchmark/benchmark.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var IS_WINDOWS = require( '@stdlib/assert/is-windows' );
2525
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
2626
var fromCodePoint = require( '@stdlib/string/from-code-point' );
2727
var pkg = require( './../package.json' ).name;
28-
var RE_DIRNAME = require( './../lib' );
28+
var reDirname = require( './../lib' );
2929

3030

3131
// MAIN //
@@ -39,7 +39,7 @@ bench( pkg, function benchmark( b ) {
3939
b.tic();
4040
for ( i = 0; i < b.iterations; i++ ) {
4141
str = 'C:\\foo\\bar\\beep\\boop'+fromCodePoint( 97 + (i%26) )+'.js';
42-
out = RE_DIRNAME.exec( str );
42+
out = reDirname.REGEXP.exec( str );
4343
if ( !out || !isString( out[ 1 ] ) ) {
4444
b.fail( 'should capture a dirname' );
4545
}
@@ -49,7 +49,7 @@ bench( pkg, function benchmark( b ) {
4949
b.tic();
5050
for ( i = 0; i < b.iterations; i++ ) {
5151
str = '/foo/bar/beep/boop'+fromCodePoint( 97 + (i%26) )+'.js';
52-
out = RE_DIRNAME.exec( str );
52+
out = reDirname.REGEXP.exec( str );
5353
if ( !out || !isString( out[ 1 ] ) ) {
5454
b.fail( 'should capture a dirname' );
5555
}
@@ -71,7 +71,7 @@ bench( pkg+':posix', function benchmark( b ) {
7171
b.tic();
7272
for ( i = 0; i < b.iterations; i++ ) {
7373
str = '/foo/bar/beep/boop'+fromCodePoint( 97 + (i%26) )+'.js';
74-
out = RE_DIRNAME.posix.exec( str );
74+
out = reDirname.REGEXP_POSIX.exec( str );
7575
if ( !out || !isString( out[ 1 ] ) ) {
7676
b.fail( 'should capture a dirname' );
7777
}
@@ -92,7 +92,7 @@ bench( pkg+':win32', function benchmark( b ) {
9292
b.tic();
9393
for ( i = 0; i < b.iterations; i++ ) {
9494
str = 'C:\\foo\\bar\\beep\\boop'+fromCodePoint( 97 + (i%26) )+'.js';
95-
out = RE_DIRNAME.win32.exec( str );
95+
out = reDirname.REGEXP_WIN32.exec( str );
9696
if ( !out || !isString( out[ 1 ] ) ) {
9797
b.fail( 'should capture a dirname' );
9898
}

lib/node_modules/@stdlib/regexp/dirname/docs/repl.txt

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,60 @@
11

2-
{{alias}}
3-
Regular expression to capture a path dirname.
2+
{{alias}}( [platform] )
3+
Returns a regular expression to capture a path dirname.
44

55
The regular expression is platform-dependent. If the current process is
6-
running on Windows, the regular expression is `*.win32`; otherwise,
7-
`*.posix`.
6+
running on Windows, the regular expression is `*.REGEXP_WIN32`; otherwise,
7+
`*.REGEXP_POSIX`.
8+
9+
Parameters
10+
----------
11+
platform: string (optional)
12+
Path platform (either `posix` or `win32`).
13+
14+
Returns
15+
-------
16+
re: RegExp
17+
Regular expression.
18+
19+
Examples
20+
--------
21+
> var RE = {{alias}}()
22+
<RegExp>
23+
> var RE_POSIX = {{alias}}( 'posix' );
24+
> var dir = RE_POSIX.exec( '/foo/bar/index.js' )[ 1 ]
25+
'/foo/bar'
26+
> var RE_WIN32 = {{alias}}( 'win32' );
27+
> var dir = RE_WIN32.exec( 'C:\\foo\\bar\\index.js' )[ 1 ]
28+
'C:\\foo\\bar'
29+
> var str = RE.toString();
30+
> var bool = ( str === RE_POSIX.toString() || str === RE_WIN32.toString() )
31+
true
32+
33+
34+
{{alias}}.REGEXP
35+
Regular expression to capture a path dirname.
36+
37+
Examples
38+
--------
39+
> var RE = {{alias}}.REGEXP
40+
<RegExp>
841

942

10-
{{alias}}.posix
43+
{{alias}}.REGEXP_POSIX
1144
Regular expression to capture a POSIX path dirname.
1245

1346
Examples
1447
--------
15-
> var dir = {{alias}}.exec( '/foo/bar/index.js' )[ 1 ]
48+
> var dir = {{alias}}.REGEXP_POSIX.exec( '/foo/bar/index.js' )[ 1 ]
1649
'/foo/bar'
1750

1851

19-
{{alias}}.win32
52+
{{alias}}.REGEXP_WIN32
2053
Regular expression to capture a Windows path dirname.
2154

2255
Examples
2356
--------
24-
> var dir = {{alias}}.exec( 'C:\\foo\\bar\\index.js' )[ 1 ]
57+
> var dir = {{alias}}.REGEXP_WIN32.exec( 'C:\\foo\\bar\\index.js' )[ 1 ]
2558
'C:\\foo\\bar'
2659

2760
See Also
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
type PLATFORM = 'win32' | 'posix';
22+
23+
/**
24+
* Interface for a regular expression to capture a path dirname.
25+
*/
26+
interface ReDirname {
27+
/**
28+
* Returns a regular expression to capture a path dirname.
29+
*
30+
* @param platform - path platform (`win32` or `posix`)
31+
* @returns regular expression
32+
*
33+
* @example
34+
* // Returns a platform-dependent regular expression when no `platform` is supplied:
35+
* var RE = reDirname();
36+
* // returns <RegExp>
37+
*
38+
* var bool = ( RE === reDirname( 'win32' ) ) || ( RE === reDirname( 'posix' ) );
39+
* // returns true
40+
*/
41+
( platform?: PLATFORM ): RegExp;
42+
43+
/**
44+
* Regular expression to capture a path dirname.
45+
*
46+
* @example
47+
* var RE = reDirname.REGEXP;
48+
* // returns <RegExp>
49+
*/
50+
REGEXP: RegExp;
51+
52+
/**
53+
* Regular expression to capture a Windows path dirname.
54+
*
55+
* @example
56+
* var dir = reDirname.REGEXP_WIN32.exec( 'C:\\foo\\bar\\index.js' )[ 1 ];
57+
* // returns 'C:\\foo\\bar'
58+
*/
59+
REGEXP_WIN32: RegExp;
60+
61+
/**
62+
* Regular expression to capture a POSIX path dirname.
63+
*
64+
* @example
65+
* var dir = reDirname.REGEXP_POSIX.exec( 'foo/bar/index.js' )[ 1 ]
66+
* // returns 'foo/bar'
67+
*/
68+
REGEXP_POSIX: RegExp;
69+
}
70+
71+
/**
72+
* Returns a regular expression to capture a path dirname.
73+
*
74+
* @returns regular expression
75+
*
76+
* @example
77+
* var RE_DIRNAME = reDirname();
78+
* // returns <RegExp>
79+
*
80+
* @example
81+
* var RE_DIRNAME = reDirname( 'posix' );
82+
* var dir = RE_DIRNAME.exec( '/foo/bar/index.js' )[ 1 ];
83+
* // returns '/foo/bar/'
84+
*
85+
* @example
86+
* var RE_DIRNAME = reDirname( 'win32' );
87+
* var dir = RE_DIRNAME.exec( 'C:\\foo\\bar\\index.js' )[ 1 ];
88+
* // returns 'C:\\foo\\bar'
89+
*/
90+
declare var reDirname: ReDirname;
91+
92+
93+
// EXPORTS //
94+
95+
export = reDirname;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
import reDirname = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a regular expression...
25+
{
26+
reDirname(); // $ExpectType RegExp
27+
reDirname( 'win32' ); // $ExpectType RegExp
28+
reDirname( 'posix' ); // $ExpectType RegExp
29+
}
30+
31+
// The compiler throws an error if the function is provided an invalid argument...
32+
{
33+
reDirname( 123 ); // $ExpectError
34+
reDirname( 'abc' ); // $ExpectError
35+
reDirname( [] ); // $ExpectError
36+
reDirname( {} ); // $ExpectError
37+
reDirname( true ); // $ExpectError
38+
reDirname( false ); // $ExpectError
39+
reDirname( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The compiler throws an error if the function is provided an unsupported number of arguments...
43+
{
44+
reDirname( 'win32', 2 ); // $ExpectError
45+
}
46+
47+
// Attached to main export is a `REGEXP` property that is a regular expression...
48+
{
49+
// tslint:disable-next-line:no-unused-expression
50+
reDirname.REGEXP; // $ExpectType RegExp
51+
}
52+
53+
// Attached to main export is a `REGEXP_POSIX` property that is a regular expression...
54+
{
55+
// tslint:disable-next-line:no-unused-expression
56+
reDirname.REGEXP_POSIX; // $ExpectType RegExp
57+
}
58+
59+
// Attached to main export is a `REGEXP_WIN32` property that is a regular expression...
60+
{
61+
// tslint:disable-next-line:no-unused-expression
62+
reDirname.REGEXP_WIN32; // $ExpectType RegExp
63+
}

lib/node_modules/@stdlib/regexp/dirname/examples/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818

1919
'use strict';
2020

21-
var RE_DIRNAME = require( './../lib' );
22-
21+
var reDirname = require( './../lib' );
22+
var RE_DIRNAME = reDirname();
2323
var dir;
2424

2525
// Assuming a POSIX platform...
2626
dir = RE_DIRNAME.exec( '/foo/bar/index.js' )[ 1 ];
2727
console.log( dir );
2828
// => '/foo/bar'
2929

30-
dir = RE_DIRNAME.posix.exec( '/foo/bar/home.html' )[ 1 ];
30+
dir = reDirname.REGEXP_POSIX.exec( '/foo/bar/home.html' )[ 1 ];
3131
console.log( dir );
3232
// => '/foo/bar'
3333

34-
dir = RE_DIRNAME.win32.exec( 'C:\\foo\\bar\\home.html' )[ 1 ];
34+
dir = reDirname.REGEXP_WIN32.exec( 'C:\\foo\\bar\\home.html' )[ 1 ];
3535
console.log( dir );
3636
// => 'C:\\foo\\bar'

0 commit comments

Comments
 (0)