1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
4 | var aString = require('../internals/a-string');
|
---|
5 | var hasOwn = require('../internals/has-own-property');
|
---|
6 | var padStart = require('../internals/string-pad').start;
|
---|
7 | var WHITESPACES = require('../internals/whitespaces');
|
---|
8 |
|
---|
9 | var $Array = Array;
|
---|
10 | // eslint-disable-next-line es/no-nonstandard-regexp-properties -- safe
|
---|
11 | var $escape = RegExp.escape;
|
---|
12 | var charAt = uncurryThis(''.charAt);
|
---|
13 | var charCodeAt = uncurryThis(''.charCodeAt);
|
---|
14 | var numberToString = uncurryThis(1.1.toString);
|
---|
15 | var join = uncurryThis([].join);
|
---|
16 | var FIRST_DIGIT_OR_ASCII = /^[0-9a-z]/i;
|
---|
17 | var SYNTAX_SOLIDUS = /^[$()*+./?[\\\]^{|}]/;
|
---|
18 | var OTHER_PUNCTUATORS_AND_WHITESPACES = RegExp('^[!"#%&\',\\-:;<=>@`~' + WHITESPACES + ']');
|
---|
19 | var exec = uncurryThis(FIRST_DIGIT_OR_ASCII.exec);
|
---|
20 |
|
---|
21 | var ControlEscape = {
|
---|
22 | '\u0009': 't',
|
---|
23 | '\u000A': 'n',
|
---|
24 | '\u000B': 'v',
|
---|
25 | '\u000C': 'f',
|
---|
26 | '\u000D': 'r'
|
---|
27 | };
|
---|
28 |
|
---|
29 | var escapeChar = function (chr) {
|
---|
30 | var hex = numberToString(charCodeAt(chr, 0), 16);
|
---|
31 | return hex.length < 3 ? '\\x' + padStart(hex, 2, '0') : '\\u' + padStart(hex, 4, '0');
|
---|
32 | };
|
---|
33 |
|
---|
34 | // Avoiding the use of polyfills of the previous iteration of this proposal
|
---|
35 | var FORCED = !$escape || $escape('ab') !== '\\x61b';
|
---|
36 |
|
---|
37 | // `RegExp.escape` method
|
---|
38 | // https://github.com/tc39/proposal-regex-escaping
|
---|
39 | $({ target: 'RegExp', stat: true, forced: FORCED }, {
|
---|
40 | escape: function escape(S) {
|
---|
41 | aString(S);
|
---|
42 | var length = S.length;
|
---|
43 | var result = $Array(length);
|
---|
44 |
|
---|
45 | for (var i = 0; i < length; i++) {
|
---|
46 | var chr = charAt(S, i);
|
---|
47 | if (i === 0 && exec(FIRST_DIGIT_OR_ASCII, chr)) {
|
---|
48 | result[i] = escapeChar(chr);
|
---|
49 | } else if (hasOwn(ControlEscape, chr)) {
|
---|
50 | result[i] = '\\' + ControlEscape[chr];
|
---|
51 | } else if (exec(SYNTAX_SOLIDUS, chr)) {
|
---|
52 | result[i] = '\\' + chr;
|
---|
53 | } else if (exec(OTHER_PUNCTUATORS_AND_WHITESPACES, chr)) {
|
---|
54 | result[i] = escapeChar(chr);
|
---|
55 | } else {
|
---|
56 | var charCode = charCodeAt(chr, 0);
|
---|
57 | // single UTF-16 code unit
|
---|
58 | if ((charCode & 0xF800) !== 0xD800) result[i] = chr;
|
---|
59 | // unpaired surrogate
|
---|
60 | else if (charCode >= 0xDC00 || i + 1 >= length || (charCodeAt(S, i + 1) & 0xFC00) !== 0xDC00) result[i] = escapeChar(chr);
|
---|
61 | // surrogate pair
|
---|
62 | else {
|
---|
63 | result[i] = chr;
|
---|
64 | result[++i] = charAt(S, i);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | return join(result, '');
|
---|
70 | }
|
---|
71 | });
|
---|