chars.js
1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
var chars = {}, unesc, temp;
function reverse(object, prepender) {
return Object.keys(object).reduce(function(reversed, key) {
var newKey = prepender ? prepender + key : key; // Optionally prepend a string to key.
reversed[object[key]] = newKey; // Swap key and value.
return reversed; // Return the result.
}, {});
}
/**
* Regex for common characters
*/
chars.escapeRegex = {
'?': /\?/g,
'@': /\@/g,
'!': /\!/g,
'+': /\+/g,
'*': /\*/g,
'(': /\(/g,
')': /\)/g,
'[': /\[/g,
']': /\]/g
};
/**
* Escape characters
*/
chars.ESC = {
'?': '__UNESC_QMRK__',
'@': '__UNESC_AMPE__',
'!': '__UNESC_EXCL__',
'+': '__UNESC_PLUS__',
'*': '__UNESC_STAR__',
',': '__UNESC_COMMA__',
'(': '__UNESC_LTPAREN__',
')': '__UNESC_RTPAREN__',
'[': '__UNESC_LTBRACK__',
']': '__UNESC_RTBRACK__'
};
/**
* Unescape characters
*/
chars.UNESC = unesc || (unesc = reverse(chars.ESC, '\\'));
chars.ESC_TEMP = {
'?': '__TEMP_QMRK__',
'@': '__TEMP_AMPE__',
'!': '__TEMP_EXCL__',
'*': '__TEMP_STAR__',
'+': '__TEMP_PLUS__',
',': '__TEMP_COMMA__',
'(': '__TEMP_LTPAREN__',
')': '__TEMP_RTPAREN__',
'[': '__TEMP_LTBRACK__',
']': '__TEMP_RTBRACK__'
};
chars.TEMP = temp || (temp = reverse(chars.ESC_TEMP));
module.exports = chars;