Optimize _.escape, _.escapeRegExp, and _.unescape.

This commit is contained in:
John-David Dalton
2014-07-11 00:23:06 -07:00
parent faa221f162
commit caadd5bb32

View File

@@ -7792,7 +7792,10 @@
* // => 'fred, barney, & pebbles' * // => 'fred, barney, & pebbles'
*/ */
function escape(string) { function escape(string) {
return string == null ? '' : String(string).replace(reUnescapedHtml, escapeHtmlChar); string = string == null ? '' : String(string);
return (reUnescapedHtml.lastIndex = 0, reUnescapedHtml.test(string))
? string.replace(reUnescapedHtml, escapeHtmlChar)
: string;
} }
/** /**
@@ -7810,7 +7813,10 @@
* // => '\[lodash\]\(http://lodash\.com\)' * // => '\[lodash\]\(http://lodash\.com\)'
*/ */
function escapeRegExp(string) { function escapeRegExp(string) {
return string == null ? '' : String(string).replace(reRegExpChars, '\\$&'); string = string == null ? '' : String(string);
return (reRegExpChars.lastIndex = 0, reRegExpChars.test(string))
? string.replace(reRegExpChars, '\\$&')
: string;
} }
/** /**
@@ -8401,11 +8407,10 @@
* // => 'fred, barney & pebbles' * // => 'fred, barney & pebbles'
*/ */
function unescape(string) { function unescape(string) {
if (string == null) { string = string == null ? '' : String(string);
return ''; return (reEscapedHtml.lastIndex = 0, reEscapedHtml.test(string))
} ? string.replace(reEscapedHtml, unescapeHtmlChar)
string = String(string); : string;
return string.indexOf(';') < 0 ? string : string.replace(reEscapedHtml, unescapeHtmlChar);
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/