Skip to content

Commit 0f19917

Browse files
committed
feat: add symbol/match package
--- 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 7564e0f commit 0f19917

9 files changed

Lines changed: 470 additions & 0 deletions

File tree

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# MatchSymbol
22+
23+
> [symbol][mdn-symbol] which specifies whether an object should be treated as a regular expression.
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 MatchSymbol = require( '@stdlib/symbol/match' );
41+
```
42+
43+
#### MatchSymbol
44+
45+
[`symbol`][mdn-symbol] which specifies whether an object should be treated as a regular expression.
46+
47+
```javascript
48+
var s = typeof MatchSymbol;
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+
- The `MatchSymbol` is used by `String.prototype.match` to match an input string against an object. When `String.prototype.match` is called, it checks whether the argument has a `MatchSymbol` method. If present, this method is invoked with the input string as its argument, and the returned value becomes the result of the match operation.
64+
- The presence of `MatchSymbol` on an object also determines whether the object should be treated as a regular expression when used with string matching methods.
65+
66+
</section>
67+
68+
<!-- /.notes -->
69+
70+
<!-- Package usage examples. -->
71+
72+
<section class="examples">
73+
74+
## Examples
75+
76+
<!-- eslint no-undef: "error" -->
77+
78+
```javascript
79+
var defineProperty = require( '@stdlib/utils/define-property' );
80+
var MatchSymbol = require( '@stdlib/symbol/match' );
81+
82+
var obj = {};
83+
var re = /foo/;
84+
85+
// Define a custom match behavior:
86+
function customMatch() {
87+
return [ 'custom', 'match', 'result' ];
88+
}
89+
90+
// Attach the custom matcher using Symbol.match:
91+
defineProperty( obj, MatchSymbol, {
92+
'configurable': false,
93+
'enumerable': false,
94+
'writable': false,
95+
'value': customMatch
96+
});
97+
98+
// Using custom match behavior:
99+
var v = 'foobar'.match( obj );
100+
console.log( v );
101+
// => [ 'custom', 'match', 'result' ]
102+
103+
// Default regular expression match:
104+
v = 'football'.match( re );
105+
console.log( v );
106+
// => [ 'foo' ]
107+
```
108+
109+
</section>
110+
111+
<!-- /.examples -->
112+
113+
<!-- 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. -->
114+
115+
<section class="references">
116+
117+
</section>
118+
119+
<!-- /.references -->
120+
121+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
122+
123+
<section class="related">
124+
125+
* * *
126+
127+
## See Also
128+
129+
- <span class="package-name">[`@stdlib/symbol/ctor`][@stdlib/symbol/ctor]</span><span class="delimiter">: </span><span class="description">symbols.</span>
130+
131+
</section>
132+
133+
<!-- /.related -->
134+
135+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="links">
138+
139+
[mdn-symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
140+
141+
<!-- <related-links> -->
142+
143+
[@stdlib/symbol/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/symbol/ctor
144+
145+
<!-- </related-links> -->
146+
147+
</section>
148+
149+
<!-- /.links -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
{{alias}}
3+
Match symbol.
4+
5+
This symbol which specifies whether an object should be treated as a
6+
regular expression.
7+
8+
The symbol is only supported in ES6/ES2015+ environments. For non-
9+
supporting 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) 2026 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+
* Match symbol.
25+
*
26+
* ## Notes
27+
*
28+
* - This symbol which specifies whether an object should be treated as a regular expression.
29+
* - The symbol is only supported in ES6/ES2015+ environments. For non-supporting environments, the value is `null`.
30+
*/
31+
export = Symbol.match;
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) 2026 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 MatchSymbol = require( './index' );
22+
23+
24+
// TESTS //
25+
26+
// The exported value is the match symbol...
27+
{
28+
MatchSymbol;
29+
}
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) 2026 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 MatchSymbol = require( './../lib' );
23+
24+
var obj = {};
25+
var re = /foo/;
26+
27+
function customMatch() {
28+
return [ 'custom', 'match', 'result' ];
29+
}
30+
31+
defineProperty( obj, MatchSymbol, {
32+
'configurable': false,
33+
'enumerable': false,
34+
'writable': false,
35+
'value': customMatch
36+
});
37+
38+
var v = 'foobar'.match( obj );
39+
console.log( v );
40+
// => [ 'custom', 'match', 'result' ]
41+
42+
v = 'football'.match( re );
43+
console.log( v );
44+
// => [ 'foo' ]
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) 2026 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+
* Match symbol.
23+
*
24+
* @module @stdlib/symbol/match
25+
*
26+
* @example
27+
* var MatchSymbol = require( '@stdlib/symbol/match' );
28+
*
29+
* var obj = {};
30+
* obj[ MatchSymbol ] = function () {
31+
* return [ 'matched' ];
32+
* };
33+
*/
34+
35+
// MAIN //
36+
37+
var main = require( './main.js' );
38+
39+
40+
// EXPORTS //
41+
42+
module.exports = main;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 hasMatchSymbolSupport = require( '@stdlib/assert/has-match-symbol-support' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Match symbol.
30+
*
31+
* @name MatchSymbol
32+
* @constant
33+
* @type {(symbol|null)}
34+
*
35+
* @example
36+
* var obj = {};
37+
* obj[ MatchSymbol ] = function () {
38+
* return [ 'matched' ];
39+
* };
40+
*/
41+
42+
var MatchSymbol = ( hasMatchSymbolSupport() ) ? Symbol.match : null;
43+
44+
45+
// EXPORTS //
46+
47+
module.exports = MatchSymbol;

0 commit comments

Comments
 (0)