Skip to content

Commit

Permalink
add feature detection to RegExp.escape
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jul 31, 2024
1 parent be09853 commit 19b1a68
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/core-js/modules/esnext.regexp.escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var padStart = require('../internals/string-pad').start;
var WHITESPACES = require('../internals/whitespaces');

var $Array = Array;
var $escape = RegExp.escape;
var charAt = uncurryThis(''.charAt);
var charCodeAt = uncurryThis(''.charCodeAt);
var numberToString = uncurryThis(1.1.toString);
Expand All @@ -29,9 +30,12 @@ var escapeChar = function (chr) {
return hex.length < 3 ? '\\x' + padStart(hex, 2, '0') : '\\u' + padStart(hex, 4, '0');
};

// Avoiding the use of polyfills of the previous iteration of this proposal
var FORCED = !!$escape && $escape('ab') !== '\\x61b';

// `RegExp.escape` method
// https://github.com/tc39/proposal-regex-escaping
$({ target: 'RegExp', stat: true }, {
$({ target: 'RegExp', stat: true, forced: FORCED }, {
escape: function escape(S) {
aString(S);
var length = S.length;
Expand Down
2 changes: 1 addition & 1 deletion tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1777,7 +1777,7 @@ GLOBAL.tests = {
return Promise['try'];
}],
'esnext.regexp.escape': function () {
return RegExp.escape;
return RegExp.escape('ab') === '\\x61b';
},
'esnext.set.add-all': function () {
return Set.prototype.addAll;
Expand Down

0 comments on commit 19b1a68

Please sign in to comment.