Skip to content

Commit 78441ba

Browse files
committed
add-symbol/match
1 parent 72c71e4 commit 78441ba

5 files changed

Lines changed: 55 additions & 46 deletions

File tree

lib/node_modules/@stdlib/symbol/match/README.md

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# MatchSymbol
2222

23-
> [Symbol][mdn-symbol] which provides a method that matches the regular expression against a string.
23+
> [Symbol][mdn-symbol] which provides a method that matches a regular expression against a string.
2424
2525
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
2626

@@ -60,7 +60,6 @@ var s = typeof MatchSymbol;
6060
## Notes
6161

6262
- The [symbol][mdn-symbol] is only supported in environments which support [symbols][mdn-symbol]. In non-supporting environments, the value is `null`.
63-
- When calling `String.prototype.match` and `String.prototype.matchAll` and the `pattern` argument is an object with a `[MatchSymbol]()` method, this method is called with the target string and replacement as arguments.
6463

6564
</section>
6665

@@ -76,27 +75,21 @@ var s = typeof MatchSymbol;
7675

7776
```javascript
7877
var defineProperty = require( '@stdlib/utils/define-property' );
79-
var ReplaceSymbol = require( '@stdlib/symbol/replace' );
80-
81-
function replace( str, replacement ) {
82-
return replacement;
83-
}
78+
var MatchSymbol = require( '@stdlib/symbol/match' );
8479

85-
var obj = {};
80+
var regexp = /foo/;
8681

87-
defineProperty( obj, ReplaceSymbol, {
82+
defineProperty( regexp, MatchSymbol, {
8883
'configurable': true,
89-
'value': null
84+
'enumerable': false,
85+
'writable': true,
86+
'value': false
9087
});
9188

92-
var str = 'beep';
93-
console.log( str.replace( obj, 'boop' ) );
89+
console.log( "/foo/".startsWith( regexp ) );
90+
91+
console.log( "/baz/".endsWith( regexp ) );
9492

95-
defineProperty( obj, ReplaceSymbol, {
96-
'configurable': true,
97-
'value': replace
98-
});
99-
console.log( str.replace( obj, 'boop' ) );
10093
```
10194

10295
</section>

lib/node_modules/@stdlib/symbol/match/docs/repl.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{{alias}}
33
Match symbol.
44

5-
This symbol provides a method that matches the regular expression against a string.
5+
This symbol provides a method that matches a regular expression against a string.
66

77
The symbol is only supported in ES6/ES2015+ environments. For non-supporting
88
environments, the value is `null`.

lib/node_modules/@stdlib/symbol/match/examples/index.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,23 @@
1919
'use strict';
2020

2121
var defineProperty = require( '@stdlib/utils/define-property' );
22-
var ReplaceSymbol = require( './../lib' );
22+
var MatchSymbol = require( '@stdlib/symbol/match' );
2323

24-
function replace( str, replacement ) {
25-
return replacement;
26-
}
24+
var regexp = /foo/;
2725

28-
var obj = {};
29-
30-
defineProperty( obj, ReplaceSymbol, {
31-
'configurable': true,
32-
'value': null
26+
// override the default `Symbol.match` behavior using stdlib's `MatchSymbol`
27+
defineProperty( regexp, MatchSymbol, {
28+
'configurable': true,
29+
'enumerable': false,
30+
'writable': true,
31+
'value': false
3332
});
3433

35-
var str = 'beep';
36-
console.log( str.replace( obj, 'boop' ) );
34+
// Without overriding MatchSymbol, this would normally throw an error in all engines:
35+
// "/foo/".startsWith(regexp);
36+
37+
console.log( "/foo/".startsWith( regexp ) );
38+
// Expected output: true
39+
40+
console.log( "/baz/".endsWith( regexp ) );
3741

38-
defineProperty( obj, ReplaceSymbol, {
39-
'configurable': true,
40-
'value': replace
41-
});
42-
console.log( str.replace( obj, 'boop' ) );

lib/node_modules/@stdlib/symbol/match/lib/index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,27 @@
1919
'use strict';
2020

2121
/**
22-
* Symbol which provides a method for replacing substrings matched by the current object.
22+
* Symbol which provides a method that matches the regular expression against a string.
2323
*
2424
* @module @stdlib/symbol/match
2525
*
2626
* @example
27-
* var ReplaceSymbol = require( '@stdlib/symbol/replace' );
27+
* var defineProperty = require( '@stdlib/utils/define-property' );
28+
*var MatchSymbol = require( '@stdlib/symbol/match' );
2829
*
29-
* function replace( str, replacement ) {
30-
* return replacement;
31-
* }
30+
*var regexp = /foo/;
3231
*
33-
* var obj = {};
34-
* obj[ ReplaceSymbol ] = replace;
32+
*
33+
*defineProperty( regexp, MatchSymbol, {
34+
* 'configurable': true,
35+
* 'enumerable': false,
36+
* 'writable': true,
37+
* 'value': false
38+
*});
39+
*
40+
*console.log( "/foo/".startsWith( regexp ) );
41+
*
42+
*console.log( "/baz/".endsWith( regexp ) );
3543
*/
3644

3745
// MAIN //

lib/node_modules/@stdlib/symbol/match/lib/main.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,21 @@ var hasMatchSymbolSupport = require( '@stdlib/assert/has-match-symbol-support' )
3333
* @type {(symbol|null)}
3434
*
3535
* @example
36-
* function replace( str, replacement ) {
37-
* return replacement;
38-
* }
36+
* var defineProperty = require( '@stdlib/utils/define-property' );
37+
*var MatchSymbol = require( '@stdlib/symbol/match' );
3938
*
40-
* var obj = {};
41-
* obj[ ReplaceSymbol ] = replace;
39+
*var regexp = /foo/;
40+
*
41+
*defineProperty( regexp, MatchSymbol, {
42+
* 'configurable': true,
43+
* 'enumerable': false,
44+
* 'writable': true,
45+
* 'value': false
46+
*});
47+
*
48+
*console.log( "/foo/".startsWith( regexp ) );
49+
*
50+
*console.log( "/baz/".endsWith( regexp ) );
4251
*/
4352
var MatchSymbol = ( hasMatchSymbolSupport() ) ? Symbol.match : null;
4453

0 commit comments

Comments
 (0)