Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions lib/node_modules/@stdlib/symbol/match/README.md
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`.

Copy link
Member

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

- `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 -->
19 changes: 19 additions & 0 deletions lib/node_modules/@stdlib/symbol/match/docs/repl.txt
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 lib/node_modules/@stdlib/symbol/match/docs/types/index.d.ts
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;
29 changes: 29 additions & 0 deletions lib/node_modules/@stdlib/symbol/match/docs/types/test.ts
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;
}
46 changes: 46 additions & 0 deletions lib/node_modules/@stdlib/symbol/match/examples/index.js
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' ]
53 changes: 53 additions & 0 deletions lib/node_modules/@stdlib/symbol/match/lib/index.js
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;
47 changes: 47 additions & 0 deletions lib/node_modules/@stdlib/symbol/match/lib/main.js
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;
Loading