UNPKG

verifactu-node-lib

Version:

Node.js library for generating VeriFacTu invoices compatible with AEAT

193 lines 6.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toNifStr = exports.toStr500 = exports.toStr120 = exports.toStr100 = exports.toStr64 = exports.toStr60 = exports.toStr50 = exports.toStr30 = exports.toStr20 = exports.toStr2 = void 0; exports.toStringMaxLength = toStringMaxLength; exports.toStringRegexp = toStringRegexp; exports.round2ToString = round2ToString; exports.toDateString = toDateString; exports.toString = toString; exports.toBooleanString = toBooleanString; exports.querySelector = querySelector; exports.querySelectorAll = querySelectorAll; exports.updateDocument = updateDocument; exports.removeElement = removeElement; function toStringMaxLength(maxLength) { return (input) => { if (input === undefined || input === null) { return ""; } const str = String(input); if (str.length > maxLength) { throw new Error(`String exceeds maximum length of ${maxLength}: ${str}`); } return str; }; } function toStringRegexp(regexp) { return (input) => { if (input === undefined || input === null) { return ""; } const str = String(input); if (!regexp.test(str)) { throw new Error(`String does not match required pattern: ${str}`); } return str; }; } function round2ToString(input) { if (input === undefined || input === null) { return ""; } const num = Number(input); if (isNaN(num)) { throw new Error(`Invalid number: ${input}`); } return num.toFixed(2); } function toDateString(input) { if (input === undefined || input === null) { return ""; } let date; if (input instanceof Date) { date = input; } else { date = new Date(String(input)); } if (isNaN(date.getTime())) { throw new Error(`Invalid date: ${input}`); } const day = String(date.getDate()).padStart(2, '0'); const month = String(date.getMonth() + 1).padStart(2, '0'); const year = date.getFullYear(); return `${day}-${month}-${year}`; } function toString(input) { if (input === undefined || input === null) { return ""; } return String(input); } function toBooleanString(input) { if (input === undefined || input === null) { return ""; } return input ? "S" : "N"; } // Validadores específicos exports.toStr2 = toStringMaxLength(2); exports.toStr20 = toStringMaxLength(20); exports.toStr30 = toStringMaxLength(30); exports.toStr50 = toStringMaxLength(50); exports.toStr60 = toStringMaxLength(60); exports.toStr64 = toStringMaxLength(64); exports.toStr100 = toStringMaxLength(100); exports.toStr120 = toStringMaxLength(120); exports.toStr500 = toStringMaxLength(500); exports.toNifStr = toStringRegexp(/^(([a-z|A-Z]{1}\d{7}[a-z|A-Z]{1})|(\d{8}[a-z|A-Z]{1})|([a-z|A-Z]{1}\d{8}))$/); // Función para buscar elementos en el DOM XML - versión optimizada sin bucles infinitos function querySelector(doc, selector) { // Para selectores simples (sin >), buscar recursivamente if (!selector.includes('>')) { return findElementByNameRecursive(doc.documentElement, selector); } const parts = selector.split('>').map(part => part.trim()); let current = doc.documentElement; for (const part of parts) { if (!current) return null; // For nested selectors, search only in direct children first, then fallback to recursive current = findElementByName(current, part) || findElementByNameRecursive(current, part); } return current; } // Función auxiliar para buscar elementos por nombre SOLO en hijos directos function findElementByName(parent, name) { if (!parent || !parent.childNodes) return null; const children = parent.childNodes; for (let i = 0; i < children.length; i++) { const child = children[i]; if (child.nodeType === 1) { // ELEMENT_NODE const tagName = child.tagName || ''; const localName = child.localName || tagName; // Comparar el nombre sin namespace if (localName === name || tagName === name || tagName.endsWith(':' + name)) { return child; } } } return null; } // Función para buscar recursivamente en todo el árbol (solo para selectores simples) function findElementByNameRecursive(parent, name, visited = new Set()) { if (!parent || visited.has(parent)) return null; visited.add(parent); // Primero verificar si el padre mismo coincide if (parent.nodeType === 1) { const tagName = parent.tagName || ''; const localName = parent.localName || tagName; if (localName === name || tagName === name || tagName.endsWith(':' + name)) { return parent; } } // Luego buscar recursivamente en todos los descendientes if (parent.childNodes) { const children = parent.childNodes; for (let i = 0; i < children.length; i++) { const child = children[i]; if (child.nodeType === 1 && !visited.has(child)) { // ELEMENT_NODE const found = findElementByNameRecursive(child, name, visited); if (found) return found; } } } return null; } function querySelectorAll(doc, selector) { const elements = []; function findElements(element) { if (element.nodeType === 1) { // ELEMENT_NODE const tagName = element.tagName || ''; const localName = element.localName || tagName; if (localName === selector || tagName === selector || tagName.endsWith(':' + selector)) { elements.push(element); } } const children = element.childNodes || []; for (let i = 0; i < children.length; i++) { if (children[i].nodeType === 1) { // ELEMENT_NODE findElements(children[i]); } } } if (doc.documentElement) { findElements(doc.documentElement); } return elements; } function updateDocument(doc, selectorsToValues) { for (const [selector, value, convert] of selectorsToValues) { const node = querySelector(doc, selector); if (node) { if (undefined === value && node.parentNode) { node.parentNode.removeChild(node); } else { node.textContent = convert(value); } } } } function removeElement(e) { if (e.parentNode) { e.parentNode.removeChild(e); return true; } return false; } //# sourceMappingURL=utils.js.map