@newdash/newdash
Version:
javascript/typescript utility library
51 lines (50 loc) • 1.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.unescape = void 0;
/**
* Used to map HTML entities to characters.
* @ignore
*/
const htmlUnescapes = {
"&": "&",
"<": "<",
">": ">",
""": '"',
"'": "'"
};
/**
* Used to match HTML entities and HTML characters.
* @ignore
*/
const reEscapedHtml = /&(?:amp|lt|gt|quot|#(0+)?39);/g;
/**
* @ignore
*/
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).
*
* @since 5.12.0
* @category String
* @param string The string to unescape.
* @returns string Returns the unescaped string.
* @see [[escape]], [[escapeRegExp]]
* @example
*
* ```js
* unescape('fred, barney, & pebbles')
* // => 'fred, barney, & pebbles'
* ```
*/
function unescape(string) {
return (string && reHasEscapedHtml.test(string))
? string.replace(reEscapedHtml, (entity) => (htmlUnescapes[entity] || "'"))
: (string || "");
}
exports.unescape = unescape;
exports.default = unescape;