vue-instantsearch
Version:
👀 Lightning-fast Algolia search for Vue apps
43 lines (39 loc) • 1.23 kB
JavaScript
/**
* This implementation is taken from Lodash implementation.
* See: https://github.com/lodash/lodash/blob/4.17.11-npm/unescape.js
*/
/** Used to map HTML entities to characters. */
const htmlUnescapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'",
};
/** Used to match HTML entities and HTML characters. */
const reEscapedHtml = /&(?:amp|lt|gt|quot|#39);/g;
const reHasEscapedHtml = RegExp(reEscapedHtml.source);
/**
* The inverse of `_.escape`; this method converts the HTML entities
* `&`, `<`, `>`, `"`, and `'` in `string` to
* their corresponding characters.
*
* **Note:** No other HTML entities are unescaped. To unescape additional
* HTML entities use a third-party library like [_he_](https://mths.be/he).
*
* @static
* @memberOf _
* @since 0.6.0
* @category String
* @param {string} [string=''] The string to unescape.
* @returns {string} Returns the unescaped string.
* @example
*
* _.unescape('fred, barney, & pebbles');
* // => 'fred, barney, & pebbles'
*/
export function unescape(string) {
return string && reHasEscapedHtml.test(string)
? string.replace(reEscapedHtml, (character) => htmlUnescapes[character])
: string;
}