source: frontend/node_modules/es-abstract/2025/EncodeForRegExpEscape.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.0 KB
Line 
1'use strict';
2
3var NumberToString = require('./Number/toString');
4var StringIndexOf = require('./StringIndexOf');
5var StringPad = require('./StringPad');
6// var StringToCodePoints = require('./StringToCodePoints');
7var UnicodeEscape = require('./UnicodeEscape');
8var UTF16EncodeCodePoint = require('./UTF16EncodeCodePoint');
9
10var isLeadingSurrogate = require('../helpers/isLeadingSurrogate');
11var isTrailingSurrogate = require('../helpers/isTrailingSurrogate');
12
13var $TypeError = require('es-errors/type');
14
15var isCodePoint = require('../helpers/isCodePoint');
16var forEach = require('for-each');
17var regexTester = require('safe-regex-test');
18
19var isWhiteSpace = regexTester(/^\s$/);
20var isLineTerminator = regexTester(/^[\n\r\u2028\u2029]$/);
21
22// var punctuators = "(){}[]|,.?*+-^$=<>/#&!%:;@~'`\"\\"; // step 1
23var syntaxCharacter = '^$\\.*+?()[]{}|';
24
25var otherPunctuators = ",-=<>#&!%:;@~'`\""; // step 3
26// var toEscape = StringToCodePoints(otherPunctuators); // step 4
27
28var table64 = {
29 '\u0009': 't',
30 '\u000a': 'n',
31 '\u000b': 'v',
32 '\u000c': 'f',
33 '\u000d': 'r',
34 __proto__: null
35};
36
37module.exports = function EncodeForRegExpEscape(c) {
38 if (!isCodePoint(c)) {
39 throw new $TypeError('Assertion failed: `c` must be a valid Unicode code point');
40 }
41
42 var encoded = UTF16EncodeCodePoint(c);
43
44 if (StringIndexOf(syntaxCharacter, encoded, 0) > -1 || encoded === '\u002F') { // step 1
45 return '\\' + encoded; // step 1.a
46 } else if (encoded in table64) { // step 2
47 return '\\' + table64[encoded]; // step 2.a
48 }
49
50 if (
51 StringIndexOf(otherPunctuators, encoded, 0) > -1
52 || isWhiteSpace(encoded)
53 || isLineTerminator(encoded)
54 || isLeadingSurrogate(c)
55 || isTrailingSurrogate(c)
56 ) { // step 5
57 if (c < 0xFF) { // step 5.a
58 var hex = NumberToString(c, 16); // step 5.a.i
59 return '\\x' + StringPad(hex, 2, '0', 'START'); // step 5.a.ii
60 }
61
62 var escaped = ''; // step 5.b
63
64 var codeUnits = encoded; // step 5.c
65
66 forEach(codeUnits, function (cu) { // step 5.d
67 escaped += UnicodeEscape(cu); // step 5.d.i
68 });
69
70 return escaped; // step 5.e
71 }
72
73 return encoded; // step 6
74};
Note: See TracBrowser for help on using the repository browser.