UNPKG

vue-instantsearch

Version:

👀 Lightning-fast Algolia search for Vue apps

43 lines (39 loc) • 1.23 kB
/** * 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 = { '&amp;': '&', '&lt;': '<', '&gt;': '>', '&quot;': '"', '&#39;': "'", }; /** 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 * `&amp;`, `&lt;`, `&gt;`, `&quot;`, and `&#39;` 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, &amp; pebbles'); * // => 'fred, barney, & pebbles' */ export function unescape(string) { return string && reHasEscapedHtml.test(string) ? string.replace(reEscapedHtml, (character) => htmlUnescapes[character]) : string; }