Skip to content

Commit 85d8df8

Browse files
committed
Add symbol/replace and has-replace-symbol-support packages
1 parent b18ecdd commit 85d8df8

File tree

23 files changed

+1223
-0
lines changed

23 files changed

+1223
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Replace Symbol Support
22+
23+
> Detect native [`Symbol.replace`][mdn-symbol] support.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
<!-- eslint-disable id-length -->
30+
31+
```javascript
32+
var hasReplaceSymbolSupport = require( '@stdlib/assert/has-replace-symbol-support' );
33+
34+
```
35+
36+
#### hasReplaceSymbolSupport()
37+
38+
Detects if a runtime environment supports ES2018 [`Symbol.asyncIterator`][mdn-symbol].
39+
40+
<!-- eslint-disable id-length -->
41+
42+
```javascript
43+
var bool = hasReplaceSymbolSupport();
44+
// returns <boolean>
45+
```
46+
47+
</section>
48+
49+
<!-- /.usage -->
50+
51+
<section class="examples">
52+
53+
## Examples
54+
55+
<!-- eslint-disable id-length -->
56+
57+
<!-- eslint no-undef: "error" -->
58+
59+
```javascript
60+
var hasReplaceSymbolSupport = require( '@stdlib/assert/has-replace-symbol-support' );
61+
62+
var bool = hasReplaceSymbolSupport();
63+
if ( bool ) {
64+
console.log( 'Environment has Symbol.replace support.' );
65+
} else {
66+
console.log( 'Environment lacks Symbol.replace support.' );
67+
}
68+
```
69+
70+
</section>
71+
72+
<!-- /.examples -->
73+
74+
* * *
75+
76+
<section class="cli">
77+
78+
## CLI
79+
80+
<section class="usage">
81+
82+
### Usage
83+
84+
```text
85+
Usage: has-replace-symbol-support [options]
86+
87+
Options:
88+
89+
-h, --help Print this message.
90+
-V, --version Print the package version.
91+
```
92+
93+
</section>
94+
95+
<!-- /.usage -->
96+
97+
<section class="examples">
98+
99+
### Examples
100+
101+
```bash
102+
$ has-replace-symbol-support
103+
<boolean>
104+
```
105+
106+
</section>
107+
108+
<!-- /.examples -->
109+
110+
</section>
111+
112+
<!-- /.cli -->
113+
114+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
115+
116+
<section class="related">
117+
118+
* * *
119+
120+
## See Also
121+
122+
- <span class="package-name">[`@stdlib/assert/has-iterator-symbol-support`][@stdlib/assert/has-iterator-symbol-support]</span><span class="delimiter">: </span><span class="description">detect native Symbol.iterator support.</span>
123+
- <span class="package-name">[`@stdlib/assert/has-symbol-support`][@stdlib/assert/has-symbol-support]</span><span class="delimiter">: </span><span class="description">detect native Symbol support.</span>
124+
125+
</section>
126+
127+
<!-- /.related -->
128+
129+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
130+
131+
<section class="links">
132+
133+
[mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
134+
135+
<!-- <related-links> -->
136+
137+
[@stdlib/assert/has-iterator-symbol-support]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/has-iterator-symbol-support
138+
139+
[@stdlib/assert/has-symbol-support]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/has-symbol-support
140+
141+
<!-- </related-links> -->
142+
143+
</section>
144+
145+
<!-- /.links -->
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var hasReplaceSymbolSupport = require( './../lib' ); // eslint-disable-line id-length
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var bool;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
// Note: this *could* be optimized by a JS engine. If so, the loop may be removed.
38+
bool = hasReplaceSymbolSupport();
39+
if ( typeof bool !== 'boolean' ) {
40+
b.fail( 'should return a boolean' );
41+
}
42+
}
43+
b.toc();
44+
if ( !isBoolean( bool ) ) {
45+
b.fail( 'should return a boolean' );
46+
}
47+
b.pass( 'benchmark finished' );
48+
b.end();
49+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license Apache-2.0
5+
*
6+
* Copyright (c) 2025 The Stdlib Authors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var resolve = require( 'path' ).resolve;
26+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
27+
var CLI = require( '@stdlib/cli/ctor' );
28+
var hasReplaceSymbolSupport = require( './../lib' ); // eslint-disable-line id-length
29+
30+
31+
// MAIN //
32+
33+
/**
34+
* Main execution sequence.
35+
*
36+
* @private
37+
*/
38+
function main() {
39+
var flags;
40+
var cli;
41+
42+
// Create a command-line interface:
43+
cli = new CLI({
44+
'pkg': require( './../package.json' ),
45+
'options': require( './../etc/cli_opts.json' ),
46+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
47+
'encoding': 'utf8'
48+
})
49+
});
50+
51+
// Get any provided command-line options:
52+
flags = cli.flags();
53+
if ( flags.help || flags.version ) {
54+
return;
55+
}
56+
57+
console.log( hasReplaceSymbolSupport() ); // eslint-disable-line no-console
58+
}
59+
60+
main();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{{alias}}()
2+
Tests for native `Symbol.replace` support.
3+
4+
Returns
5+
-------
6+
bool: boolean
7+
Boolean indicating if an environment supports native `Symbol.replace`.
8+
9+
Examples
10+
--------
11+
> var bool = {{alias}}()
12+
<boolean>
13+
14+
See Also
15+
--------
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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: 4.1
20+
21+
/**
22+
* Tests for native `Symbol.replace` support.
23+
*
24+
* @returns boolean indicating if an environment supports `Symbol.replace`
25+
*
26+
* @example
27+
* var bool = hasReplaceSymbolSupport();
28+
* // returns <boolean>
29+
*/
30+
declare function hasReplaceSymbolSupport(): boolean;
31+
32+
33+
// EXPORTS //
34+
35+
export = hasReplaceSymbolSupport;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 hasReplaceSymbolSupport from './index';
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
hasReplaceSymbolSupport(); // $ExpectType boolean
27+
}
28+
29+
// The compiler throws an error if the function is provided arguments...
30+
{
31+
// @ts-expect-error: intentionally testing invalid args
32+
hasReplaceSymbolSupport( true ); // $ExpectError
33+
34+
// @ts-expect-error: intentionally testing invalid args
35+
hasReplaceSymbolSupport( [], 123 ); // $ExpectError
36+
}
37+
38+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
Usage: has-replace-symbol-support [options]
3+
4+
Options:
5+
6+
-h, --help Print this message.
7+
-V, --version Print the package version.
8+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"boolean": [
3+
"help",
4+
"version"
5+
],
6+
"alias": {
7+
"help": [
8+
"h"
9+
],
10+
"version": [
11+
"V"
12+
]
13+
}
14+
}

0 commit comments

Comments
 (0)