@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
30 lines (29 loc) • 1.12 kB
JavaScript
/**
* Decode a caller's supported HTML text references exactly once.
*
* Replacement scans the original string: a decoded ampersand is never
* reinterpreted as the beginning of another reference. Unknown references
* stay literal. This is text conversion, not HTML output sanitization.
*/
export function decodeHtmlTextEntitiesOnce(value, entities, decodeDecimalReferences = false) {
return value.replace(/&([a-z]+|#\d+);/gi, (match, body) => {
const reference = body.toLowerCase();
const decoded = Object.hasOwn(entities, reference)
? entities[reference]
: undefined;
if (typeof decoded === "string") {
return decoded;
}
if (!decodeDecimalReferences || !reference.startsWith("#")) {
return match;
}
const codePoint = Number(reference.slice(1));
if (!Number.isInteger(codePoint) ||
codePoint <= 0 ||
codePoint > 0x10ffff ||
(codePoint >= 0xd800 && codePoint <= 0xdfff)) {
return match;
}
return String.fromCodePoint(codePoint);
});
}