ec-sri-invoice-signer
Version:
Ecuador SRI invoice signer.
146 lines (145 loc) • 5.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.processAttributeValue = processAttributeValue;
exports.processTagValue = processTagValue;
const fast_xml_parser_1 = require("fast-xml-parser");
const utils_1 = require("../utils/utils");
function normalizeWhitespaceInAttributeValue(value) {
const trimmed = value.replace(/ /g, ' ');
return trimmed;
}
function encodeEntitiesInTagValue(value) {
const encodings = {
"&": "&",
"<": "<",
">": ">",
"\r": "
"
};
return value.replace(/([<>\r])/gm, function (match, offset) {
return encodings[match];
})
// Replace the ampersand only if it's not part of an entity
.replace(/&(?!(#x[aA-fF\d]+;)|amp;|lt;|gt;)/gm, function (match, offset) {
return encodings[match];
});
}
function decodeUtf8HexEntitiesInTagValue(value) {
var _a;
const notDecodedEntities = new Set(['
']);
const entitiesToReplace = (_a = value.match(/&#x[aA-fF\d]+;/gm)) !== null && _a !== void 0 ? _a : [];
let newValue = value;
for (const entity of entitiesToReplace) {
if (!notDecodedEntities.has(entity)) {
const hexToDecode = entity.match(/[aA-fF\d]+/)[0];
const decimalToDecode = parseInt(hexToDecode, 16);
const decoded = String.fromCodePoint(decimalToDecode);
newValue = newValue.replace(entity, decoded);
}
}
return newValue;
}
function decodeUtf8EntitiesInTagValue(value) {
var _a;
const notDecodedEntities = new Set(['&', '<', '>']);
const entitiesToReplace = (_a = value.match(/&([aA-zZ]+);/gm)) !== null && _a !== void 0 ? _a : [];
let newValue = value;
for (const entity of entitiesToReplace) {
if (!notDecodedEntities.has(entity)) {
const decoded = new TextDecoder('utf-8').decode(new Uint8Array([parseInt(entity.slice(3), 16)]));
newValue = newValue.replace(entity, decoded);
}
}
return newValue;
}
/**
* Replace characters with their respective XML entities.
* & is a special case, as it's not replaced if it's part of an entity.
*/
function encodeEntitiesInAttributeValue(value) {
const encodings = {
"&": "&",
"<": "<",
'"': """,
"\r": "
",
"\n": "
",
"\t": "	"
};
// Replace the ampersand only if it's not part of an entity
return value.replace(/&(?!(#x[aA-fF\d]+;)|amp;|quot;|lt;|apos;)/gm, function (match) {
return encodings[match];
}).replace(/[<"\r\n\t]/gm, function (match) {
return encodings[match];
});
}
function decodeUtf8HexEntitiesInAttributeValue(value) {
var _a;
const notDecodedEntities = new Set(['
', '
', '	']);
const entitiesToReplace = (_a = value.match(/&#x[aA-fF\d]+;/gm)) !== null && _a !== void 0 ? _a : [];
let newValue = value;
for (const entity of entitiesToReplace) {
if (!notDecodedEntities.has(entity)) {
const hexToDecode = entity.match(/[aA-fF\d]+/)[0];
const decimalToDecode = parseInt(hexToDecode, 16);
const decoded = String.fromCodePoint(decimalToDecode);
newValue = newValue.replace(entity, decoded);
}
}
return newValue;
}
function decodeUtf8EntitiesInAttributeValue(value) {
var _a, _b;
const notDecodedEntities = new Set(['&', '<', '"']);
const entitiesToReplace = (_a = value.match(/&[aA-zZ]+;/gm)) !== null && _a !== void 0 ? _a : [];
let newValue = value;
for (const entity of entitiesToReplace) {
if (!notDecodedEntities.has(entity)) {
const decoded = (_b = new fast_xml_parser_1.XMLParser().parse(`<body>${entity}</body>`).body) !== null && _b !== void 0 ? _b : '';
newValue = newValue.replace(entity, decoded);
}
}
return newValue;
}
function removeLeadingZerosInHexEntity(value) {
return value.replace(/0+(?=[\daA-fF]+;)/g, '');
}
function setCapitalsInHexEntities(value) {
var _a;
const entitiesToReplace = (_a = value.match(/&#x[aA-fF\d]+;/gm)) !== null && _a !== void 0 ? _a : [];
let newValue = value;
for (const entity of entitiesToReplace) {
const entityWithoutLeadingZeros = removeLeadingZerosInHexEntity(entity);
newValue = newValue.replace(entity, `&#x${entityWithoutLeadingZeros.slice(3).toUpperCase()}`);
}
return newValue;
}
function convertDecimalEntitiesIntoHexEntities(value) {
var _a;
const entitiesToReplace = (_a = value.match(/&#\d+;/gm)) !== null && _a !== void 0 ? _a : [];
let newValue = value;
for (const entity of entitiesToReplace) {
const hexValue = Number(entity.slice(2, -1)).toString(16).toUpperCase();
newValue = newValue.replace(entity, `&#x${hexValue};`);
}
return newValue;
}
function processAttributeValue(value) {
const processingSteps = [
setCapitalsInHexEntities,
convertDecimalEntitiesIntoHexEntities,
encodeEntitiesInAttributeValue,
decodeUtf8HexEntitiesInAttributeValue,
decodeUtf8EntitiesInAttributeValue,
normalizeWhitespaceInAttributeValue
];
return (0, utils_1.pipe)(processingSteps)(value);
}
function processTagValue(value) {
const processingSteps = [
setCapitalsInHexEntities,
convertDecimalEntitiesIntoHexEntities,
encodeEntitiesInTagValue,
decodeUtf8HexEntitiesInTagValue,
decodeUtf8EntitiesInTagValue
];
return (0, utils_1.pipe)(processingSteps)(value);
}