Skip to content

Commit c2136cb

Browse files
committed
Refactor as factory function
1 parent a783d92 commit c2136cb

File tree

13 files changed

+408
-91
lines changed

13 files changed

+408
-91
lines changed

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

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

2929
```javascript
30-
var RE_NATIVE_FUNCTION = require( '@stdlib/regexp/native-function' );
30+
var reNativeFunction = require( '@stdlib/regexp/native-function' );
3131
```
3232

33-
#### RE_NATIVE_FUNCTION
33+
#### reNativeFunction()
3434

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

3737
```javascript
38+
var RE_NATIVE_FUNCTION = reNativeFunction();
3839
var bool = RE_NATIVE_FUNCTION.test( Date.toString() );
3940
// returns true
4041
```
4142

43+
#### reNativeFunction.REGEXP
44+
45+
[Regular expression][regexp] to match a native `function`.
46+
47+
```javascript
48+
var bool = reNativeFunction.REGEXP.test( Date.toString() );
49+
// returns true
50+
```
51+
4252
</section>
4353

4454
<!-- /.usage -->
@@ -53,8 +63,9 @@ var bool = RE_NATIVE_FUNCTION.test( Date.toString() );
5363

5464
```javascript
5565
var Int8Array = require( '@stdlib/array/int8' );
56-
var RE_NATIVE_FUNCTION = require( '@stdlib/regexp/native-function' );
66+
var reNativeFunction = require( '@stdlib/regexp/native-function' );
5767

68+
var RE_NATIVE_FUNCTION = reNativeFunction();
5869
function isNativeFunction( fcn ) {
5970
return RE_NATIVE_FUNCTION.test( fcn.toString() );
6071
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
var bench = require( '@stdlib/bench' );
2424
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2525
var pkg = require( './../package.json' ).name;
26-
var RE_NATIVE_FUNCTION = require( './../lib' );
26+
var reNativeFunction = require( './../lib' );
2727

2828

2929
// VARIABLES //
@@ -53,7 +53,7 @@ bench( pkg, function benchmark( b ) {
5353

5454
b.tic();
5555
for ( i = 0; i < b.iterations; i++ ) {
56-
bool = RE_NATIVE_FUNCTION.test( values[ i%values.length ] );
56+
bool = reNativeFunction.REGEXP.test( values[ i%values.length ] );
5757
if ( !isBoolean( bool ) ) {
5858
b.fail( 'should return a boolean' );
5959
}

lib/node_modules/@stdlib/regexp/native-function/docs/repl.txt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11

2-
{{alias}}
2+
{{alias}}()
3+
Returns a regular expression to match a native function.
4+
5+
Returns
6+
-------
7+
re: RegExp
8+
Regular expression.
9+
10+
Examples
11+
--------
12+
> var RE = {{alias}}();
13+
> var bool = RE.test( Date.toString() )
14+
true
15+
> bool = RE.test( (function noop() {}).toString() )
16+
false
17+
18+
19+
{{alias}}.REGEXP
320
Regular expression to match a native function.
421

522
Examples
623
--------
7-
> var bool = {{alias}}.test( Date.toString() )
24+
> var bool = {{alias}}.REGEXP.test( Date.toString() )
825
true
9-
> bool = {{alias}}.test( (function noop() {}).toString() )
26+
> bool = {{alias}}.REGEXP.test( (function noop() {}).toString() )
1027
false
1128

1229
See Also
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 match a native function.
23+
*/
24+
interface ReNativeFunction {
25+
/**
26+
* Returns a regular expression to match a native function.
27+
*
28+
* @returns regular expression
29+
*
30+
* @example
31+
* var RE_NATIVE_FUNCTION = reNativeFunction();
32+
* var bool = RE_NATIVE_FUNCTION.test( Date.toString() );
33+
* // returns true
34+
*/
35+
(): RegExp;
36+
37+
/**
38+
* Regular expression to match a native function.
39+
*
40+
* @example
41+
* var bool = reNativeFunction.REGEXP.test( Date.toString() );
42+
* // returns true
43+
*/
44+
REGEXP: RegExp;
45+
}
46+
47+
/**
48+
* Returns a regular expression to match a native function.
49+
*
50+
* @returns regular expression
51+
*
52+
* @example
53+
* var RE_NATIVE_FUNCTION = reNativeFunction();
54+
* var bool = RE_NATIVE_FUNCTION.test( Date.toString() );
55+
* // returns true
56+
*
57+
* @example
58+
* var bool = reNativeFunction.REGEXP.test( Date.toString() );
59+
* // returns true
60+
*/
61+
declare var reNativeFunction: ReNativeFunction;
62+
63+
64+
// EXPORTS //
65+
66+
export = reNativeFunction;
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 reNativeFunction = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a regular expression...
25+
{
26+
reNativeFunction(); // $ExpectType RegExp
27+
}
28+
29+
// The compiler throws an error if the function is provided an unsupported number of arguments...
30+
{
31+
reNativeFunction( 1 ); // $ExpectError
32+
reNativeFunction( 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+
reNativeFunction.REGEXP; // $ExpectType RegExp
39+
}

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

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

2323
var Int8Array = require( '@stdlib/array/int8' );
24-
var RE_NATIVE_FUNCTION = require( './../lib' );
24+
var reNativeFunction = require( './../lib' );
2525

26+
var RE_NATIVE_FUNCTION = reNativeFunction();
2627
function isNativeFunction( fcn ) {
2728
return RE_NATIVE_FUNCTION.test( fcn.toString() );
2829
}

lib/node_modules/@stdlib/regexp/native-function/lib/index.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
'use strict';
2020

2121
/**
22-
* Regular expression to match a native function.
22+
* Return a regular expression to match a native function.
2323
*
2424
* @module @stdlib/regexp/native-function
25-
* @type {RegExp}
2625
*
2726
* @example
28-
* var RE_NATIVE_FUNCTION = require( '@stdlib/regexp/native-function' );
27+
* var reNativeFunction = require( '@stdlib/regexp/native-function' );
2928
*
29+
* var RE_NATIVE_FUNCTION = reNativeFunction();
3030
* function isNativeFunction( fcn ) {
3131
* return RE_NATIVE_FUNCTION.test( fcn.toString() );
3232
* }
@@ -47,15 +47,21 @@
4747

4848
// MAIN //
4949

50-
/**
51-
* Match a native function.
52-
*
53-
* @constant
54-
* @type {RegExp}
55-
*/
56-
var RE_NATIVE_FUNCTION = require( './regexp.js' );
50+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
51+
var reNativeFunction = require( './main.js' );
52+
var REGEXP = require( './regexp.js' );
53+
54+
55+
// MAIN //
56+
57+
setReadOnly( reNativeFunction, 'REGEXP', REGEXP );
58+
59+
60+
// EXPORTS //
61+
62+
module.exports = reNativeFunction;
5763

5864

5965
// EXPORTS //
6066

61-
module.exports = RE_NATIVE_FUNCTION;
67+
module.exports = reNativeFunction;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
'use strict';
20+
21+
// MAIN //
22+
23+
/**
24+
* Returns a regular expression to capture everything that is not a space immediately after the `function` keyword and before the first left parenthesis.
25+
*
26+
* @returns {RegExp} regular expression
27+
*
28+
* @example
29+
* var RE_NATIVE_FUNCTION = reNativeFunction();
30+
* var bool = RE_NATIVE_FUNCTION.test( Date.toString() );
31+
* // returns true
32+
*/
33+
function reNativeFunction() {
34+
var str = '';
35+
36+
// Use a native function as a template:
37+
str += Function.prototype.toString.call( Function );
38+
39+
// Escape special RegExp characters:
40+
str = str.replace( /([.*+?^=!:$(){}|[\]\/\\])/g, '\\$1' ); // eslint-disable-line no-useless-escape
41+
42+
// Replace any mentions of `Function` to make template generic and replace `for ...` and additional info provided in other environments, such as Rhino.
43+
str = str.replace( /Function|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?' );
44+
45+
// Bracket the regular expression:
46+
str = '^' + str + '$';
47+
48+
// Create the regular expression:
49+
return new RegExp( str );
50+
}
51+
52+
53+
// EXPORTS //
54+
55+
module.exports = reNativeFunction;

lib/node_modules/@stdlib/regexp/native-function/lib/regexp.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,22 @@
1818

1919
'use strict';
2020

21-
// MAIN //
22-
23-
var str;
24-
var re;
25-
26-
str = '';
27-
28-
// Use a native function as a template:
29-
str += Function.prototype.toString.call( Function );
21+
// MODULES //
3022

31-
// Escape special RegExp characters:
32-
str = str.replace( /([.*+?^=!:$(){}|[\]\/\\])/g, '\\$1' ); // eslint-disable-line no-useless-escape
23+
var reNativeFunction = require( './main.js' );
3324

34-
// Replace any mentions of `Function` to make template generic and replace `for ...` and additional info provided in other environments, such as Rhino.
35-
str = str.replace( /Function|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?' );
3625

37-
// Bracket the regular expression:
38-
str = '^' + str + '$';
26+
// MAIN //
3927

40-
// Create the regular expression:
41-
re = new RegExp( str );
28+
/**
29+
* Match a native function.
30+
*
31+
* @constant
32+
* @type {RegExp}
33+
*/
34+
var RE_NATIVE_FUNCTION = reNativeFunction();
4235

4336

4437
// EXPORTS //
4538

46-
module.exports = re;
39+
module.exports = RE_NATIVE_FUNCTION;

lib/node_modules/@stdlib/regexp/native-function/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@stdlib/regexp/native-function",
33
"version": "0.0.0",
4-
"description": "Regular expression to match a native function.",
4+
"description": "Return a regular expression to match a native function.",
55
"license": "Apache-2.0",
66
"author": {
77
"name": "The Stdlib Authors",
@@ -21,6 +21,7 @@
2121
"lib": "./lib",
2222
"test": "./test"
2323
},
24+
"types": "./docs/types",
2425
"scripts": {},
2526
"homepage": "https://github.com/stdlib-js/stdlib",
2627
"repository": {

0 commit comments

Comments
 (0)