1
0
mirror of https://github.com/musix-org/musix-oss synced 2024-12-23 13:03:18 +00:00
musix-oss/node_modules/ent/decode.js
2020-03-03 22:30:50 +02:00

33 lines
1.0 KiB
JavaScript

var punycode = require('punycode');
var entities = require('./entities.json');
module.exports = decode;
function decode (str) {
if (typeof str !== 'string') {
throw new TypeError('Expected a String');
}
return str.replace(/&(#?[^;\W]+;?)/g, function (_, match) {
var m;
if (m = /^#(\d+);?$/.exec(match)) {
return punycode.ucs2.encode([ parseInt(m[1], 10) ]);
} else if (m = /^#[Xx]([A-Fa-f0-9]+);?/.exec(match)) {
return punycode.ucs2.encode([ parseInt(m[1], 16) ]);
} else {
// named entity
var hasSemi = /;$/.test(match);
var withoutSemi = hasSemi ? match.replace(/;$/, '') : match;
var target = entities[withoutSemi] || (hasSemi && entities[match]);
if (typeof target === 'number') {
return punycode.ucs2.encode([ target ]);
} else if (typeof target === 'string') {
return target;
} else {
return '&' + match;
}
}
});
}