-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.js
121 lines (103 loc) · 3.05 KB
/
utils.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
const toString = Object.prototype.toString;
/* Used to match HTML entities and HTML characters. */
exports.reEscapedHtml = /&(?:amp|lt|gt|quot|#39|#96);/g;
exports.reUnescapedHtml = /[&<>"'`]/g;
exports.reHasEscapedHtml = new RegExp(exports.reEscapedHtml.source);
exports.reHasUnescapedHtml = new RegExp(exports.reUnescapedHtml.source);
/* Used to match [ES template delimiters](http://ecma-international.org/ecma-262/6.0/#sec-template-literal-lexical-components). */
exports.reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
/* Used to match template delimiters. */
exports.reEscape = /<%-([\s\S]+?)%>/g;
exports.reEvaluate = /<%([\s\S]+?)%>/g;
exports.reInterpolate = /<%=([\s\S]+?)%>/g;
exports.delimiters = /<%-([\s\S]+?)%>|<%=([\s\S]+?)%>|\$\{([^\\}]*(?:\\.[^\\}]*)*)\}|<%([\s\S]+?)%>$/g;
/* Used to ensure capturing order of template delimiters. */
exports.reNoMatch = /($^)/;
exports.reUnescapedString = /['\n\r\u2028\u2029\\]/g;
exports.reEmptyStringLeading = /\b__p \+= '';/g;
exports.reEmptyStringMiddle = /\b(__p \+=) '' \+/g;
exports.reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
/* Used to map characters to HTML entities. */
exports.htmlEscapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'`': '`'
};
/* Used to map HTML entities to characters. */
exports.htmlUnescapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'",
'`': '`'
};
/**
* Used by `escape` to convert characters to HTML entities.
*
* @private
* @param {string} chr The matched character to escape.
* @returns {string} Returns the escaped character.
*/
exports.escapeHtmlChar = ch => exports.htmlEscapes[ch] || ch;
/**
* Returns true if `val` is an object.
*/
exports.isObject = val => {
return typeof val === 'function' || (val && typeof val === 'object' && !Array.isArray(val));
};
/**
* Converts the characters "&", "<", ">", '"', "'", and "\`", in `string` to
* their corresponding HTML entities.
*
* @category String
* @param {string} [string=''] The string to escape.
* @returns {string} Returns the escaped string.
*/
exports.escape = input => {
let str = exports.baseToString(input);
return (str && exports.reHasUnescapedHtml.test(str))
? str.replace(exports.reUnescapedHtml, exports.escapeHtmlChar)
: str;
};
/**
* Converts `value` to a string if it's not one. An empty string is returned
* for `null` or `undefined` values.
*
* @private
* @param {*} value The value to process.
* @returns {string} Returns the string.
*/
exports.baseToString = value => value ? value + '' : '';
exports.tryCatch = fn => {
try {
return fn();
} catch (err) {
return err;
}
};
exports.escapeStringChar = ch => {
return '\\' + ({
'\\': '\\',
"'": "'",
'\n': 'n',
'\r': 'r',
'\u2028': 'u2028',
'\u2029': 'u2029'
})[ch];
};
exports.omit = (obj, keys) => {
let res = {};
if (!obj) return res;
keys = [].concat(keys || []);
for (let key of Object.keys(obj)) {
if (!keys.includes(key)) {
res[key] = obj[key];
}
}
return res;
};