mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 09:47:48 +00:00
Consolidate escape and unescape modules.
This commit is contained in:
12
escape.js
12
escape.js
@@ -1,6 +1,14 @@
|
||||
import escapeHtmlChar from './.internal/escapeHtmlChar.js';
|
||||
import toString from './toString.js';
|
||||
|
||||
/** Used to map characters to HTML entities. */
|
||||
const htmlEscapes = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": '''
|
||||
};
|
||||
|
||||
/** Used to match HTML entities and HTML characters. */
|
||||
const reUnescapedHtml = /[&<>"']/g;
|
||||
const reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
|
||||
@@ -34,7 +42,7 @@ const reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
|
||||
function escape(string) {
|
||||
string = toString(string);
|
||||
return (string && reHasUnescapedHtml.test(string))
|
||||
? string.replace(reUnescapedHtml, escapeHtmlChar)
|
||||
? string.replace(reUnescapedHtml, chr => htmlEscapes[chr])
|
||||
: string;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user