-
-
Notifications
You must be signed in to change notification settings - Fork 963
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
base: develop
Are you sure you want to change the base?
feat: add symbol/match
#8547
Changes from 7 commits
5af296e
c0500a8
d697130
53451cd
8dc8422
17c22fd
871e4bc
854a7ae
35cc300
e6235db
cf3dac8
5e9fb15
7546e28
35fc564
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,133 @@ | ||||||
| <!-- | ||||||
|
|
||||||
| @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 the matching of a regular expression against a string. | ||||||
|
|
||||||
| <!-- 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 the matching of a regular expression against a string. Used by `String.prototype.match()`. | ||||||
|
||||||
| [`Symbol`][mdn-symbol] which specifies the matching of a regular expression against a string. Used by `String.prototype.match()`. | |
| [`Symbol`][mdn-symbol] which specifies whether an object should be treated as a regular expression. |
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 MatchSymbol is used here. 1) As a method used by String.prototype.match to 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
Outdated
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.
You don't need JSON.stringify.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
|
|
||
| {{alias}} | ||
| Match symbol. | ||
|
|
||
| This symbol specifies the matching of a regular expression against a string. | ||
|
||
| It is used by String.prototype.match(). | ||
|
|
||
| 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 | ||
| -------- | ||
|
|
||
| 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 specifies the matching of a regular expression against a string. Used by String.prototype.match(). | ||
|
||
| * - The symbol is only supported in ES6/ES2015+ environments. For non-supporting environments, the value is `null`. | ||
| */ | ||
| export = Symbol.match; | ||
| 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 IsConcatSpreadable = require( './index' ); | ||
|
||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The exported value is the `isConcatSpreadable` symbol... | ||
| { | ||
| IsConcatSpreadable; | ||
| } | ||
| 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( JSON.stringify( v ) ); | ||
|
||
| // => '["foo"]' | ||
| 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; |
| 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 re = /foo/; | ||
| * var str = 'football'; | ||
| * | ||
| * var v = str.match( re ); | ||
|
||
| * // returns <Array> | ||
| */ | ||
| var MatchSymbol = ( hasMatchSymbolSupport() ) ? Symbol.match : null; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| module.exports = MatchSymbol; | ||
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.