-
-
Notifications
You must be signed in to change notification settings - Fork 959
feat: add symbol/match
#8547
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
Open
kaushal-kumar-it
wants to merge
14
commits into
stdlib-js:develop
Choose a base branch
from
kaushal-kumar-it:feat/symbol-match
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: add symbol/match
#8547
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5af296e
feat: add symbol/match package
kaushal-kumar-it c0500a8
feat: add symbol/match package
kaushal-kumar-it d697130
feat: add symbol/match package
kaushal-kumar-it 53451cd
Fix symbol/match examples and use JSON.stringify for array output
kaushal-kumar-it 8dc8422
Fix symbol/match examples and use JSON.stringify for array output
kaushal-kumar-it 17c22fd
Remove unused parameter in customMatch function
kaushal-kumar-it 871e4bc
Remove unused parameter in customMatch function
kaushal-kumar-it 854a7ae
fixed Copy-paste mistake
kaushal-kumar-it 35cc300
corrected notes of readme file
kaushal-kumar-it e6235db
corrected notes of readme file
kaushal-kumar-it cf3dac8
corrected index.d.ts file
kaushal-kumar-it 5e9fb15
fixed json and wrong example
kaushal-kumar-it 7546e28
fixed repl.txt
kaushal-kumar-it 35fc564
fixed lint eror
kaushal-kumar-it 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,145 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # MatchSymbol | ||
|
|
||
| > [Symbol][mdn-symbol] which specifies whether an object should be treated as a regular expression. | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- Package usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var MatchSymbol = require( '@stdlib/symbol/match' ); | ||
| ``` | ||
|
|
||
| #### MatchSymbol | ||
|
|
||
| [`Symbol`][mdn-symbol] which specifies whether an object should be treated as a regular expression. | ||
|
|
||
| ```javascript | ||
| var s = typeof MatchSymbol; | ||
| // e.g., returns 'symbol' | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - The [symbol][mdn-symbol] is only supported in environments which support [symbols][mdn-symbol]. In non-supporting environments, the value is `null`. | ||
|
|
||
| - `String.prototype.match()` uses the following algorithm to determine how to match a string: | ||
|
|
||
| - If the first argument is an object with a `[MatchSymbol]()` method, `String.prototype.match()` calls that method with the string as the first argument and returns the result. | ||
| - Otherwise, if the first argument is a [regular expression][mdn-regexp], `String.prototype.match()` performs a standard regular expression match. | ||
| - Otherwise, `String.prototype.match()` coerces the first argument to a string and searches for that substring. | ||
|
|
||
| - The symbol is also used to identify whether an object should be treated as a [regular expression][mdn-regexp]. For example, the methods `String.prototype.startsWith()`, `String.prototype.endsWith()`, and `String.prototype.includes()` check if their first argument is a regular expression and will throw a `TypeError` if it is. By setting `[MatchSymbol]` to `false` (or a [falsy][mdn-falsy] value except `undefined`), an object can indicate that it should not be treated as a regular expression object. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var defineProperty = require( '@stdlib/utils/define-property' ); | ||
| var MatchSymbol = require( '@stdlib/symbol/match' ); | ||
|
|
||
| var obj = {}; | ||
| var re = /foo/; | ||
|
|
||
| // Define a custom match behavior: | ||
| function customMatch() { | ||
| return [ 'custom', 'match', 'result' ]; | ||
| } | ||
|
|
||
| defineProperty( obj, MatchSymbol, { | ||
| 'configurable': false, | ||
| 'enumerable': false, | ||
| 'writable': false, | ||
| 'value': customMatch | ||
| }); | ||
|
|
||
| var v = 'foobar'.match( obj ); | ||
| console.log( v ); | ||
| // => [ 'custom', 'match', 'result' ] | ||
|
|
||
| // Default match behavior: | ||
| v = 'football'.match( re ); | ||
| console.log( v ); | ||
| // => [ 'foo' ] | ||
| ``` | ||
|
|
||
| </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 | ||
|
|
||
| [mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp | ||
|
|
||
| [mdn-falsy]: https://developer.mozilla.org/en-US/docs/Glossary/Falsy | ||
|
|
||
| </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,19 @@ | ||
|
|
||
| {{alias}} | ||
| Match symbol. | ||
|
|
||
| This symbol is used as a method by String.prototype.match() to match an | ||
| input string against the current object and to determine whether an object | ||
| should be treated as a regular expression. | ||
|
|
||
| The symbol is only supported in ES6/ES2015+ environments. For non-supporting | ||
| environments, the value is `null`. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var s = {{alias}} | ||
| e.g., <symbol> | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
31 changes: 31 additions & 0 deletions
31
lib/node_modules/@stdlib/symbol/match/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,31 @@ | ||
| /* | ||
| * @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 // | ||
|
|
||
| /** | ||
| * Match symbol. | ||
| * | ||
| * ## Notes | ||
| * | ||
| * - This symbol is used as a method by `String.prototype.match()` to match an input string against the current object and to determine whether an object should be treated as a regular expression. | ||
| * - The symbol is only supported in ES6/ES2015+ environments. For non-supporting environments, the value is `null`. | ||
| */ | ||
| export = Symbol.match; |
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 Match = require( './index' ); | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The exported value is the `match` symbol... | ||
| { | ||
| Match; | ||
| } |
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,46 @@ | ||
| /** | ||
| * @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'; | ||
|
|
||
| var defineProperty = require( '@stdlib/utils/define-property' ); | ||
| var MatchSymbol = require( './../lib' ); | ||
|
|
||
| var obj = {}; | ||
| var re = /foo/; | ||
|
|
||
| // Define a custom match behavior: | ||
| function customMatch() { | ||
| return [ 'custom', 'match', 'result' ]; | ||
| } | ||
|
|
||
| defineProperty( obj, MatchSymbol, { | ||
| 'configurable': false, | ||
| 'enumerable': false, | ||
| 'writable': false, | ||
| 'value': customMatch | ||
| }); | ||
|
|
||
| var v = 'foobar'.match( obj ); | ||
| console.log( v ); | ||
| // => [ 'custom', 'match', 'result' ] | ||
|
|
||
| // Default match behavior: | ||
| v = 'football'.match( re ); | ||
| console.log( v ); | ||
| // => [ 'foo' ] |
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,53 @@ | ||
| /** | ||
| * @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'; | ||
|
|
||
| /** | ||
| * Match symbol. | ||
| * | ||
| * @module @stdlib/symbol/match | ||
| * | ||
| * @example | ||
| * var MatchSymbol = require( '@stdlib/symbol/match' ); | ||
| * | ||
| * var obj = { | ||
| * 'toString': function toString() { | ||
| * return 'foo'; | ||
| * } | ||
| * }; | ||
| * | ||
| * obj[ MatchSymbol ] = function match( str ) { | ||
| * if ( str.indexOf( this.toString() ) !== -1 ) { | ||
| * return [ this.toString() ]; | ||
| * } | ||
| * return null; | ||
| * }; | ||
| * | ||
| * var str = 'football'; | ||
| * var v = str.match( obj ); | ||
| */ | ||
|
|
||
| // 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,47 @@ | ||
| /** | ||
| * @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 hasMatchSymbolSupport = require( '@stdlib/assert/has-match-symbol-support' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| /** | ||
| * Match symbol. | ||
| * | ||
| * @name MatchSymbol | ||
| * @constant | ||
| * @type {(symbol|null)} | ||
| * | ||
| * @example | ||
| * var obj = {}; | ||
| * | ||
| * obj[ MatchSymbol ] = function match( str ) { | ||
| * return [ 'beep' ]; | ||
| * }; | ||
| */ | ||
| var MatchSymbol = ( hasMatchSymbolSupport() ) ? Symbol.match : null; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| module.exports = MatchSymbol; |
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.
Describe how
MatchSymbolis used here. 1) As a method used byString.prototype.matchto match an input string against the current object. This method is provided a single argument: a string. 2) To determine whether an object should be treated as a regular expression.This is discussed in more depth on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/match