Skip to content

Commit b1b4447

Browse files
committed
Refactor as factory function
1 parent 8b99c9f commit b1b4447

File tree

19 files changed

+408
-81
lines changed

19 files changed

+408
-81
lines changed

lib/node_modules/@stdlib/_tools/eslint/rules/jsdoc-doctest/test/fixtures/invalid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ test = {
290290
'',
291291
'var isFunction = require( \'@stdlib/assert/is-function\' );',
292292
'var hasFunctionNameSupport = require( \'@stdlib/assert/has-function-name-support\' );',
293-
'var RE = require( \'@stdlib/regexp/function-name\' );',
293+
'var RE = require( \'@stdlib/regexp/function-name\' ).REGEXP;',
294294
'',
295295
'',
296296
'// VARIABLES //',

lib/node_modules/@stdlib/_tools/eslint/rules/jsdoc-doctest/test/fixtures/valid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ test = {
382382
'',
383383
'var isFunction = require( \'@stdlib/assert/is-function\' );',
384384
'var hasFunctionNameSupport = require( \'@stdlib/assert/has-function-name-support\' );',
385-
'var RE = require( \'@stdlib/regexp/function-name\' );',
385+
'var RE = require( \'@stdlib/regexp/function-name\' ).REGEXP;',
386386
'',
387387
'',
388388
'// VARIABLES //',

lib/node_modules/@stdlib/namespace/lib/namespace/r.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,16 +1053,6 @@ ns.push({
10531053
]
10541054
});
10551055

1056-
ns.push({
1057-
'alias': 'RE_FUNCTION_NAME',
1058-
'path': '@stdlib/regexp/function-name',
1059-
'value': require( '@stdlib/regexp/function-name' ),
1060-
'type': 'RegExp',
1061-
'related': [
1062-
'@stdlib/utils/function-name'
1063-
]
1064-
});
1065-
10661056
ns.push({
10671057
'alias': 'RE_NATIVE_FUNCTION',
10681058
'path': '@stdlib/regexp/native-function',
@@ -1286,6 +1276,16 @@ ns.push({
12861276
'related': []
12871277
});
12881278

1279+
ns.push({
1280+
'alias': 'reFunctionName',
1281+
'path': '@stdlib/regexp/function-name',
1282+
'value': require( '@stdlib/regexp/function-name' ),
1283+
'type': 'Function',
1284+
'related': [
1285+
'@stdlib/utils/function-name'
1286+
]
1287+
});
1288+
12891289
ns.push({
12901290
'alias': 'reim',
12911291
'path': '@stdlib/complex/reim',

lib/node_modules/@stdlib/regexp/function-name/README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,34 @@ limitations under the License.
2727
## Usage
2828

2929
```javascript
30-
var RE_FUNCTION_NAME = require( '@stdlib/regexp/function-name' );
30+
var reFunctionName = require( '@stdlib/regexp/function-name' );
3131
```
3232

33-
#### RE_FUNCTION_NAME
33+
#### reFunctionName
3434

35-
[Regular expression][regexp] to capture a `function` name.
35+
Returns a [regular expression][regexp] to capture a `function` name.
3636

3737
```javascript
3838
function beep() {
3939
return 'boop';
4040
}
4141

42+
var RE_FUNCTION_NAME = reFunctionName();
4243
var str = RE_FUNCTION_NAME.exec( beep.toString() )[ 1 ];
4344
// returns 'beep'
4445
```
4546

47+
#### reFunctionName.REGEXP
48+
49+
[Regular expression][regexp] to capture a `function` name.
50+
51+
<!-- eslint-disable stdlib/no-builtin-math -->
52+
53+
```javascript
54+
var str = reFunctionName.REGEXP.exec( Math.sqrt.toString() )[ 1 ];
55+
// returns 'sqrt'
56+
```
57+
4658
</section>
4759

4860
<!-- /.usage -->
@@ -57,7 +69,8 @@ var str = RE_FUNCTION_NAME.exec( beep.toString() )[ 1 ];
5769

5870
```javascript
5971
var Int8Array = require( '@stdlib/array/int8' );
60-
var RE_FUNCTION_NAME = require( '@stdlib/regexp/function-name' );
72+
var reFunctionName = require( '@stdlib/regexp/function-name' );
73+
var RE_FUNCTION_NAME = reFunctionName();
6174

6275
function fname( fcn ) {
6376
return RE_FUNCTION_NAME.exec( fcn.toString() )[ 1 ];

lib/node_modules/@stdlib/regexp/function-name/benchmark/benchmark.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var bench = require( '@stdlib/bench' );
2424
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
2525
var fromCodePoint = require( '@stdlib/string/from-code-point' );
2626
var pkg = require( './../package.json' ).name;
27-
var RE_FUNCTION_NAME = require( './../lib' );
27+
var reFunctionName = require( './../lib' );
2828

2929

3030
// MAIN //
@@ -37,7 +37,7 @@ bench( pkg, function benchmark( b ) {
3737
b.tic();
3838
for ( i = 0; i < b.iterations; i++ ) {
3939
str = 'function be'+fromCodePoint( 97 + (i%26) )+'ep () {}';
40-
out = RE_FUNCTION_NAME.exec( str )[ 1 ];
40+
out = reFunctionName.REGEXP.exec( str )[ 1 ];
4141
if ( !isString( out ) ) {
4242
b.fail( 'should return a string' );
4343
}
Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11

2-
{{alias}}
3-
Regular expression to capture a function name.
2+
{{alias}}()
3+
Return a regular expression to capture a function name.
4+
5+
Returns
6+
-------
7+
re: RegExp
8+
Regular expression.
49

510
Examples
611
--------
12+
> var RE_FUNCTION_NAME = {{alias}}();
713
> function beep() { return 'boop'; };
8-
> var name = {{alias}}.exec( beep.toString() )[ 1 ]
14+
> var name = RE_FUNCTION_NAME.exec( beep.toString() )[ 1 ]
915
'beep'
10-
> name = {{alias}}.exec( function () {} )[ 1 ]
16+
> name = RE_FUNCTION_NAME.exec( function () {} )[ 1 ]
1117
''
1218

19+
20+
{{alias}}.REGEXP
21+
Regular expression to capture a function name.
22+
23+
Examples
24+
--------
25+
> var str = {{alias}}.REGEXP.exec( Math.sqrt.toString() )[ 1 ]
26+
'sqrt'
27+
1328
See Also
1429
--------
1530

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
/**
22+
* Interface for a regular expression to capture everything that is not a space immediately after the `function` keyword and before the first left parenthesis.
23+
*/
24+
interface ReFunctionName {
25+
/**
26+
* Returns a regular expression to capture everything that is not a space immediately after the `function` keyword and before the first left parenthesis.
27+
*
28+
* @returns regular expression
29+
*
30+
* @example
31+
* var RE_FUNCTION_NAME = reFunctionName();
32+
* function beep() {
33+
* return 'boop';
34+
* }
35+
*
36+
* var str = RE_FUNCTION_NAME.exec( beep.toString() )[ 1 ];
37+
* // returns 'beep'
38+
*/
39+
(): RegExp;
40+
41+
/**
42+
* Regular expression to capture everything that is not a space immediately after the `function` keyword and before the first left parenthesis.
43+
*
44+
* @example
45+
* var str = reFunctionName.REGEXP.exec( Math.sqrt.toString() )[ 1 ];
46+
* // returns 'sqrt'
47+
*/
48+
REGEXP: RegExp;
49+
}
50+
51+
/**
52+
* Returns a regular expression to capture everything that is not a space immediately after the `function` keyword and before the first left parenthesis.
53+
*
54+
* @returns regular expression
55+
*
56+
* @example
57+
* var RE_FUNCTION_NAME = reFunctionName();
58+
* function beep() {
59+
* return 'boop';
60+
* }
61+
*
62+
* var str = RE_FUNCTION_NAME.exec( beep.toString() )[ 1 ];
63+
* // returns 'beep'
64+
*
65+
* @example
66+
* var str = reFunctionName.REGEXP.exec( Math.sqrt.toString() )[ 1 ];
67+
* // returns 'sqrt'
68+
*/
69+
declare var reFunctionName: ReFunctionName;
70+
71+
72+
// EXPORTS //
73+
74+
export = reFunctionName;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 reFunctionName = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a regular expression...
25+
{
26+
reFunctionName(); // $ExpectType RegExp
27+
}
28+
29+
// The compiler throws an error if the function is provided an unsupported number of arguments...
30+
{
31+
reFunctionName( 1 ); // $ExpectError
32+
reFunctionName( 1, 2 ); // $ExpectError
33+
}
34+
35+
// Attached to main export is a `REGEXP` property that is a regular expression...
36+
{
37+
// tslint:disable-next-line:no-unused-expression
38+
reFunctionName.REGEXP; // $ExpectType RegExp
39+
}

lib/node_modules/@stdlib/regexp/function-name/examples/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
'use strict';
2222

2323
var Int8Array = require( '@stdlib/array/int8' );
24-
var RE_FUNCTION_NAME = require( './../lib' );
24+
var reFunctionName = require( './../lib' );
25+
var RE_FUNCTION_NAME = reFunctionName();
2526

2627
function fname( fcn ) {
2728
return RE_FUNCTION_NAME.exec( fcn.toString() )[ 1 ];
Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 The Stdlib Authors.
4+
* Copyright (c) 2021 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -22,10 +22,10 @@
2222
* Regular expression to capture everything that is not a space immediately after the `function` keyword and before the first left parenthesis.
2323
*
2424
* @module @stdlib/regexp/function-name
25-
* @type {RegExp}
2625
*
2726
* @example
28-
* var RE_FUNCTION_NAME = require( '@stdlib/regexp/function-name' );
27+
* var reFunctionName = require( '@stdlib/regexp/function-name' );
28+
* var RE_FUNCTION_NAME = reFunctionName();
2929
*
3030
* function fname( fcn ) {
3131
* return RE_FUNCTION_NAME.exec( fcn.toString() )[ 1 ];
@@ -44,39 +44,18 @@
4444
* // returns ''
4545
*/
4646

47+
// MODULES //
48+
49+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
50+
var reFunctionName = require( './main.js' );
51+
var REGEXP = require( './regexp.js' );
52+
4753

4854
// MAIN //
4955

50-
/**
51-
* Captures everything that is not a space immediately after the `function` keyword and before the first left parenthesis.
52-
*
53-
* Regular expression: `/^\s*function\s*([^(]*)/i`
54-
*
55-
* - `/^\s*`
56-
* - Match zero or more spaces at beginning
57-
*
58-
* - `function`
59-
* - Match the word `function`
60-
*
61-
* - `\s*`
62-
* - Match zero or more spaces after the word `function`
63-
*
64-
* - `()`
65-
* - Capture
66-
*
67-
* - `[^(]*`
68-
* - Match anything except a left parenthesis `(` zero or more times
69-
*
70-
* - `/i`
71-
* - ignore case
72-
*
73-
* @constant
74-
* @type {RegExp}
75-
* @default /^\s*function\s*([^(]*)/i
76-
*/
77-
var RE_FUNCTION_NAME = /^\s*function\s*([^(]*)/i;
56+
setReadOnly( reFunctionName, 'REGEXP', REGEXP );
7857

7958

8059
// EXPORTS //
8160

82-
module.exports = RE_FUNCTION_NAME;
61+
module.exports = reFunctionName;

0 commit comments

Comments
 (0)