-
-
Notifications
You must be signed in to change notification settings - Fork 959
feat: add symbol/match
#8495
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
#8495
Changes from all commits
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,143 @@ | ||
| <!-- | ||
| @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 | ||
|
|
||
| > Match [symbol][mdn-symbol] which is used to determine if a constructor object recognizes an object as its instance. | ||
|
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. Description is incorrect. |
||
| <!-- 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 | ||
|
|
||
| Match [`symbol`][mdn-symbol] which is used to determine if a constructor object recognizes an object as its instance. | ||
|
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. Description is incorrect. |
||
|
|
||
| ```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`. | ||
|
|
||
| - The `instanceof` operator uses the following algorithm to determine the return value of `object instanceof constructor`: | ||
|
|
||
| - If `constructor` has a `[MatchSymbol]()` method, the `instanceof` operator calls the method with `object` as the first argument and returns the result (coerced to a boolean). If `constructor` is not an object or if `constructor[MatchSymbol]` is neither `null`, `undefined`, nor a function, the `instanceof` operator raises an exception. | ||
| - Otherwise, if `constructor` does not have a `[MatchSymbol]()` method (i.e., `constructor[MatchSymbol]` is `null` or `undefined`), the `instanceof` operator determines the result using the same algorithm as `Function.prototype[MatchSymbol]()`. If `constructor` is not a function, the `instanceof` operator raises an exception. | ||
|
Comment on lines
+64
to
+67
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. This is all incorrect. You need to update to reflect the actual usage of the match symbol. |
||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var isArray = require( '@stdlib/assert/is-array' ); | ||
| var instanceOf = require( '@stdlib/assert/instance-of' ); | ||
| var defineProperty = require( '@stdlib/utils/define-property' ); | ||
| var MatchSymbol = require( '@stdlib/symbol/match' ); | ||
|
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. This example is not correct. You need to update to reflect the actual use case for the match symbol. |
||
|
|
||
| function ArrayLike() { | ||
| return { | ||
| 'length': 3, | ||
| '0': 4, | ||
| '1': 5, | ||
| '2': 6 | ||
| }; | ||
| } | ||
|
|
||
| function hasInstance( instance ) { | ||
| return isArray( instance ); | ||
| } | ||
|
|
||
| var x = [ 1, 2, 3 ]; | ||
|
|
||
| defineProperty( ArrayLike, MatchSymbol, { | ||
| 'configurable': true, | ||
| 'value': null | ||
| }); | ||
| console.log( instanceOf( x, ArrayLike ) ); | ||
|
|
||
| defineProperty( ArrayLike, MatchSymbol, { | ||
| 'configurable': true, | ||
| 'value': hasInstance | ||
| }); | ||
| console.log( instanceOf( x, ArrayLike ) ); | ||
| ``` | ||
|
|
||
| </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 --> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| {{alias}} | ||
| Match symbol. | ||
|
|
||
| This symbol specifies the method that retrieves the matches of a string | ||
| against a regular expression. | ||
|
|
||
| It is used by methods such as `String.prototype.match()` to customize | ||
| matching behavior. | ||
|
|
||
| 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 is used to determine whether a constructor object recognizes an object as its instance. | ||
|
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. Description is incorrect. |
||
| * - 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 Match = require( './index' ); | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The exported value is the `match` symbol... | ||
| { | ||
| Match; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * @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 isArray = require( '@stdlib/assert/is-array' ); | ||
| var instanceOf = require( '@stdlib/assert/instance-of' ); | ||
| var defineProperty = require( '@stdlib/utils/define-property' ); | ||
| var MatchSymbol = require( './../lib' ); | ||
|
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. Example needs updating. |
||
|
|
||
| function ArrayLike() { | ||
| return { | ||
| 'length': 3, | ||
| '0': 4, | ||
| '1': 5, | ||
| '2': 6 | ||
| }; | ||
| } | ||
|
|
||
| function Match( instance ) { | ||
| return isArray( instance ); | ||
| } | ||
|
|
||
| var x = [ 1, 2, 3 ]; | ||
|
|
||
| defineProperty( ArrayLike, MatchSymbol, { | ||
| 'configurable': true, | ||
| 'value': null | ||
| }); | ||
| console.log( instanceOf( x, ArrayLike ) ); | ||
|
|
||
| defineProperty( ArrayLike, MatchSymbol, { | ||
| 'configurable': true, | ||
| 'value': Match | ||
| }); | ||
| console.log( instanceOf( x, ArrayLike ) ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /** | ||
| * @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'; | ||
|
|
||
| /** | ||
| * Symbol used to determine if a constructor object recognizes an object as its instance. | ||
|
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. Description is incorrect. |
||
| * | ||
| * @module @stdlib/symbol/match | ||
| * | ||
| * @example | ||
| * var isArray = require( '@stdlib/assert/is-array' ); | ||
| * var MatchSymbol = require( '@stdlib/symbol/match' ); | ||
| * | ||
| * function ArrayLike() { | ||
| * return { | ||
| * 'length': 3, | ||
| * '0': 1, | ||
| * '1': 2, | ||
| * '2': 3 | ||
| * }; | ||
| * }; | ||
| * | ||
| * ArrayLike[ MatchSymbol ] = isArray; | ||
| */ | ||
|
|
||
| // MAIN // | ||
|
|
||
| var main = require( './main.js' ); | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| module.exports = main; | ||
| 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 matchSymbolSupport = require( '@stdlib/assert/has-has-instance-symbol-support' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| /** | ||
| * Match symbol. | ||
| * | ||
| * @name MatchSymbol | ||
| * @constant | ||
| * @type {(symbol|null)} | ||
| * | ||
| * @example | ||
| * var isArray = require( '@stdlib/assert/is-array' ); | ||
|
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. Example is not correct. |
||
| * | ||
| * function ArrayLike() { | ||
| * return { | ||
| * 'length': 3, | ||
| * '0': 1, | ||
| * '1': 2, | ||
| * '2': 3 | ||
| * }; | ||
| * }; | ||
| * | ||
| * ArrayLike[ MatchSymbol ] = isArray; | ||
| */ | ||
| var MatchSymbol = ( matchSymbolSupport() ) ? 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.
The filename for this file is misspelled.