Skip to content

Commit a68d5d9

Browse files
committed
feat: add symbol/replace
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 97218a1 commit a68d5d9

File tree

9 files changed

+452
-0
lines changed

9 files changed

+452
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
# ReplaceSymbol
22+
23+
> [Symbol][mdn-symbol] which provides a method for replacing substrings matched by the current object.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var ReplaceSymbol = require( '@stdlib/symbol/replace' );
41+
```
42+
43+
#### ReplaceSymbol
44+
45+
[`symbol`][mdn-symbol] which provides a method for replacing substrings matched by the current object.
46+
47+
```javascript
48+
var s = typeof ReplaceSymbol;
49+
// e.g., returns 'symbol'
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
57+
58+
<section class="notes">
59+
60+
## Notes
61+
62+
- The [symbol][mdn-symbol] is only supported in environments which support [symbols][mdn-symbol]. In non-supporting environments, the value is `null`.
63+
- When calling `String.prototype.replace` and `String.prototype.replaceAll` and the `pattern` argument is an object with a `[ReplaceSymbol]()` method, this method is called with the target string and replacement as arguments.
64+
65+
</section>
66+
67+
<!-- /.notes -->
68+
69+
<!-- Package usage examples. -->
70+
71+
<section class="examples">
72+
73+
## Examples
74+
75+
<!-- eslint no-undef: "error" -->
76+
77+
```javascript
78+
var defineProperty = require( '@stdlib/utils/define-property' );
79+
var ReplaceSymbol = require( '@stdlib/symbol/replace' );
80+
81+
function replace( str, replacement ) {
82+
return replacement;
83+
}
84+
85+
var obj = {};
86+
87+
defineProperty( obj, ReplaceSymbol, {
88+
'configurable': true,
89+
'value': null
90+
});
91+
92+
var str = 'beep';
93+
console.log( str.replace( obj, 'boop' ) );
94+
95+
defineProperty( obj, ReplaceSymbol, {
96+
'configurable': true,
97+
'value': replace
98+
});
99+
console.log( str.replace( obj, 'boop' ) );
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
107+
108+
<section class="references">
109+
110+
</section>
111+
112+
<!-- /.references -->
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+
</section>
119+
120+
<!-- /.related -->
121+
122+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
123+
124+
<section class="links">
125+
126+
[mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
127+
128+
</section>
129+
130+
<!-- /.links -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
{{alias}}
3+
Replace symbol.
4+
5+
This symbol provides a method for replacing substrings matched by the
6+
current object.
7+
8+
The symbol is only supported in ES6/ES2015+ environments. For non-supporting
9+
environments, the value is `null`.
10+
11+
Examples
12+
--------
13+
> var s = {{alias}}
14+
e.g., <symbol>
15+
16+
See Also
17+
--------
18+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
// EXPORTS //
22+
23+
/**
24+
* Replace symbol.
25+
*
26+
* ## Notes
27+
*
28+
* - This symbol provides a method for replacing substrings matched by the current object.
29+
* - The symbol is only supported in ES6/ES2015+ environments. For non-supporting environments, the value is `null`.
30+
*/
31+
export = Symbol.replace;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
/* eslint-disable @typescript-eslint/no-unused-expressions */
20+
21+
import ReplaceSymbol = require( './index' );
22+
23+
24+
// TESTS //
25+
26+
// The exported value is the `replace` symbol...
27+
{
28+
ReplaceSymbol;
29+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
var defineProperty = require( '@stdlib/utils/define-property' );
22+
var ReplaceSymbol = require( './../lib' );
23+
24+
function replace( str, replacement ) {
25+
return replacement;
26+
}
27+
28+
var obj = {};
29+
30+
defineProperty( obj, ReplaceSymbol, {
31+
'configurable': true,
32+
'value': null
33+
});
34+
35+
var str = 'beep';
36+
console.log( str.replace( obj, 'boop' ) );
37+
38+
defineProperty( obj, ReplaceSymbol, {
39+
'configurable': true,
40+
'value': replace
41+
});
42+
console.log( str.replace( obj, 'boop' ) );
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
/**
22+
* Symbol which provides a method for replacing substrings matched by the current object.
23+
*
24+
* @module @stdlib/symbol/replace
25+
*
26+
* @example
27+
* var ReplaceSymbol = require( '@stdlib/symbol/replace' );
28+
*
29+
* function replace( str, replacement ) {
30+
* return replacement;
31+
* }
32+
*
33+
* var obj = {};
34+
* obj[ ReplaceSymbol ] = replace;
35+
*/
36+
37+
// MAIN //
38+
39+
var main = require( './main.js' );
40+
41+
42+
// EXPORTS //
43+
44+
module.exports = main;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 hasReplaceSymbolSupport = require( '@stdlib/assert/has-replace-symbol-support' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Replace symbol.
30+
*
31+
* @name ReplaceSymbol
32+
* @constant
33+
* @type {(symbol|null)}
34+
*
35+
* @example
36+
* function replace( str, replacement ) {
37+
* return replacement;
38+
* }
39+
*
40+
* var obj = {};
41+
* obj[ ReplaceSymbol ] = replace;
42+
*/
43+
var ReplaceSymbol = ( hasReplaceSymbolSupport() ) ? Symbol.replace : null;
44+
45+
46+
// EXPORTS //
47+
48+
module.exports = ReplaceSymbol;

0 commit comments

Comments
 (0)