Files
lodash/unescape.js
John-David Dalton 6cb3460fce Remove semicolons.
2017-02-05 22:22:04 -08:00

42 lines
1.1 KiB
JavaScript

import toString from './toString.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`, nd `&#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).
*
* @since 0.6.0
* @category String
* @param {string} [string=''] The string to unescape.
* @returns {string} Returns the unescaped string.
* @see escape, escapeRegExp
* @example
*
* unescape('fred, barney, &amppebbles')
* // => 'fred, barney, & pebbles'
*/
function unescape(string) {
string = toString(string)
return (string && reHasEscapedHtml.test(string))
? string.replace(reEscapedHtml, entity => htmlUnescapes[entity])
: string
}
export default unescape