-
-
Notifications
You must be signed in to change notification settings - Fork 959
feat: add symbol/replace
#8529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
DivyanshuVortex
wants to merge
3
commits into
stdlib-js:develop
from
DivyanshuVortex:feat/symbol-replace
Closed
feat: add symbol/replace
#8529
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2025 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # ReplaceSymbol | ||
|
|
||
| > Well-known [`Symbol.replace`][mdn-symbol], used by `String.prototype.replace()` to customize replacement behavior. | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| /* eslint-disable */ | ||
|
|
||
| var ReplaceSymbol = require( '@stdlib/symbol/replace' ); | ||
|
|
||
| var s = typeof ReplaceSymbol; | ||
| // e.g., returns 'symbol' | ||
|
|
||
| if ( ReplaceSymbol ) { | ||
| function CustomReplacer() {} | ||
|
|
||
| CustomReplacer.prototype[ ReplaceSymbol ] = function namedReplace( str ) { | ||
| return 'REPLACED: ' + str; | ||
| }; | ||
|
|
||
| var out = 'hello world'.replace( new CustomReplacer() ); | ||
| console.log( out ); | ||
| // => 'REPLACED: hello world' | ||
| } else { | ||
| console.log( ReplaceSymbol ); | ||
| // => null | ||
| } | ||
|
|
||
| /* eslint-enable */ | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- 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. --> | ||
|
|
||
| <section class="references"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| {{alias}} | ||
| Replace symbol. | ||
|
|
||
| This well-known symbol specifies the method that replaces matched | ||
| substrings. It is used by `String.prototype.replace()` to delegate | ||
| replacement behavior to an object. | ||
|
|
||
| The symbol is only supported in ES6/ES2015+ environments. In | ||
| unsupported environments, the value is `null`. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var s = {{alias}} | ||
| e.g., <symbol> | ||
|
|
||
| See Also | ||
| -------- |
34 changes: 34 additions & 0 deletions
34
lib/node_modules/@stdlib/symbol/replace/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2025 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // TypeScript Version: 4.1 | ||
|
|
||
| // EXPORTS // | ||
|
|
||
| /** | ||
| * Replace symbol. | ||
| * | ||
| * ## Notes | ||
| * | ||
| * - This well-known symbol specifies the method that replaces matched substrings. | ||
| * It is used by `String.prototype.replace()` to delegate replacement behavior. | ||
| * - The symbol is only supported in ES6/ES2015+ environments. In non-supporting | ||
| * environments, the exported value is `null`. | ||
|
Comment on lines
+28
to
+31
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do not line wrap descriptions. |
||
| */ | ||
| export = Symbol.replace; | ||
|
|
||
29 changes: 29 additions & 0 deletions
29
lib/node_modules/@stdlib/symbol/replace/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2025 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /* eslint-disable @typescript-eslint/no-unused-expressions */ | ||
|
|
||
| import ReplaceSymbol = require( './index' ); | ||
|
|
||
| // TESTS // | ||
|
|
||
| // The exported value is the `replace` symbol... | ||
| { | ||
| ReplaceSymbol; | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2025 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var ReplaceSymbol = require( './../lib' ); | ||
|
|
||
|
|
||
| // VARIABLES // | ||
|
|
||
| var out; | ||
|
|
||
|
|
||
| // FUNCTIONS // | ||
|
|
||
| /** | ||
| * Custom replacer object. | ||
| * | ||
| * @private | ||
| * @constructor | ||
| */ | ||
| function CustomReplacer() { | ||
| // Empty constructor | ||
| } | ||
|
|
||
| /** | ||
| * Replacement method. | ||
| * | ||
| * @private | ||
| * @param {string} str - input string | ||
| * @returns {string} replaced string | ||
| */ | ||
| function replace( str ) { | ||
| return 'REPLACED: ' + str; | ||
| } | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| if ( ReplaceSymbol ) { | ||
| CustomReplacer.prototype[ ReplaceSymbol ] = replace; | ||
|
|
||
| out = 'hello world'.replace( new CustomReplacer() ); | ||
| console.log( out ); | ||
| // => 'REPLACED: hello world' | ||
| } else { | ||
| console.log( ReplaceSymbol ); | ||
| // => null | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||||||
| /** | ||||||||
| * @license Apache-2.0 | ||||||||
| * | ||||||||
| * Copyright (c) 2025 The Stdlib Authors. | ||||||||
| * | ||||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||
| * you may not use this file except in compliance with the License. | ||||||||
| * You may obtain a copy of the License at | ||||||||
| * | ||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||
| * | ||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||
| * See the License for the specific language governing permissions and | ||||||||
| * limitations under the License. | ||||||||
| */ | ||||||||
|
|
||||||||
| 'use strict'; | ||||||||
|
|
||||||||
| /** | ||||||||
| * Well-known symbol `Symbol.replace`. | ||||||||
| * | ||||||||
| * This symbol specifies the method that replaces matched substrings and is used | ||||||||
| * by `String.prototype.replace()` to delegate replacement behavior to an object. | ||||||||
| * | ||||||||
|
Comment on lines
+24
to
+26
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| * @module @stdlib/symbol/replace | ||||||||
| * | ||||||||
| * @example | ||||||||
| * var ReplaceSymbol = require( '@stdlib/symbol/replace' ); | ||||||||
| * | ||||||||
| * if ( ReplaceSymbol ) { | ||||||||
| * function CustomReplacer() {} | ||||||||
| * | ||||||||
| * CustomReplacer.prototype[ ReplaceSymbol ] = function( str ) { | ||||||||
| * return 'REPLACED: ' + str; | ||||||||
| * }; | ||||||||
| * | ||||||||
| * var out = 'hello world'.replace( new CustomReplacer() ); | ||||||||
| * console.log( out ); | ||||||||
| * // => 'REPLACED: hello world' | ||||||||
| * } | ||||||||
| * else { | ||||||||
| * console.log( ReplaceSymbol ); | ||||||||
| * // => null | ||||||||
| * } | ||||||||
| */ | ||||||||
|
|
||||||||
| // MAIN // | ||||||||
|
|
||||||||
| var main = require( './main.js' ); | ||||||||
|
|
||||||||
|
|
||||||||
| // EXPORTS // | ||||||||
|
|
||||||||
| module.exports = main; | ||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2025 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var hasReplaceSymbolSupport = require( '@stdlib/assert/has-replace-symbol-support' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| /** | ||
| * Replace symbol. | ||
| * | ||
| * @name ReplaceSymbol | ||
| * @constant | ||
| * @type {(symbol|null)} | ||
| * | ||
| * @example | ||
| * var ReplaceSymbol = require( '@stdlib/symbol/replace' ); | ||
| * | ||
| * if ( ReplaceSymbol ) { | ||
| * function Replacer() {} | ||
| * | ||
| * Replacer.prototype[ ReplaceSymbol ] = function ( str ) { | ||
| * return 'REPLACED: ' + str; | ||
| * }; | ||
| * | ||
| * var out = 'hello world'.replace( new Replacer() ); | ||
| * console.log( out ); | ||
| * } | ||
| */ | ||
| var ReplaceSymbol = ( hasReplaceSymbolSupport() ) ? Symbol.replace : null; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| module.exports = ReplaceSymbol; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not how our READMEs are structured. Please follow that structure exactly. It is not clear why you removed the typical import statement, the typical heading, etc. The original RFC suggested simply copy-pasting and then tweaking. Here, you decided to deviate from project conventions.