hexo/node_modules/unescape/index.js

104 lines
1.9 KiB
JavaScript

'use strict';
var extend = require('extend-shallow');
var regexCache = {};
var all;
var charSets = {
default: {
'"': '"',
'"': '"',
''': '\'',
''': '\'',
'&': '&',
'&': '&',
'>': '>',
'>': '>',
'&lt;': '<',
'&#60;': '<'
},
extras: {
'&cent;': '¢',
'&#162;': '¢',
'&copy;': '©',
'&#169;': '©',
'&euro;': '€',
'&#8364;': '€',
'&pound;': '£',
'&#163;': '£',
'&reg;': '®',
'&#174;': '®',
'&yen;': '¥',
'&#165;': '¥'
}
};
// don't merge char sets unless "all" is explicitly called
Object.defineProperty(charSets, 'all', {
get: function() {
return all || (all = extend({}, charSets.default, charSets.extras));
}
});
/**
* Convert HTML entities to HTML characters.
*
* @param {String} `str` String with HTML entities to un-escape.
* @return {String}
*/
function unescape(str, type) {
if (!isString(str)) return '';
var chars = charSets[type || 'default'];
var regex = toRegex(type, chars);
return str.replace(regex, function(m) {
return chars[m];
});
}
function toRegex(type, chars) {
if (regexCache[type]) {
return regexCache[type];
}
var keys = Object.keys(chars).join('|');
var regex = new RegExp('(?=(' + keys + '))\\1', 'g');
regexCache[type] = regex;
return regex;
}
/**
* Returns true if str is a non-empty string
*/
function isString(str) {
return str && typeof str === 'string';
}
/**
* Expose charSets
*/
unescape.chars = charSets.default;
unescape.extras = charSets.extras;
// don't trip the "charSets" getter unless it's explicitly called
Object.defineProperty(unescape, 'all', {
get: function() {
return charSets.all;
}
});
/**
* Expose `unescape`
*/
module.exports = unescape;