UNPKG

@nodecfdi/cfdi-cleaner

Version:

Librería para limpiar comprobantes fiscales digitales v3.3 y v4.0

58 lines (57 loc) 2.51 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ts_mixer_1 = require("ts-mixer"); const cfdi_x_path_1 = __importDefault(require("#src/internal/cfdi_x_path")); const schema_location_1 = __importDefault(require("#src/internal/schema_location")); const xml_attribute_methods_1 = __importDefault(require("#src/mixins/xml_attribute_methods")); class RemoveIncompleteSchemaLocations extends (0, ts_mixer_1.Mixin)(xml_attribute_methods_1.default) { clean(document) { const xpath = cfdi_x_path_1.default.createFromDocument(document); const schemaLocations = xpath.querySchemaLocations(); for (const schemaLocation of schemaLocations) { const value = this.cleanSchemaLocationValue(schemaLocation.value); this.attributeSetValueOrRemoveIfEmpty(schemaLocation, value); } } /** * @param schemaLocationValue - SchemaLocation */ cleanSchemaLocationValue(schemaLocationValue) { const pairs = this.schemaLocationValueNamespaceXsdPairToArray(schemaLocationValue); const schemaLocation = new schema_location_1.default(pairs); return schemaLocation.asValue(); } /** * Parses schema location value skipping namespaces without xsd locations (identified by .xsd extension) * * @param schemaLocationValue - SchemaLocation */ schemaLocationValueNamespaceXsdPairToArray(schemaLocationValue) { const components = schema_location_1.default.valueToComponents(schemaLocationValue); const pairs = {}; for (let c = 0; c < components.length; c += 1) { const namespace = components[c]; if (this.uriEndsWithXsd(namespace)) { // Namespace is a location continue; } const location = components[c + 1] ?? ''; if (!this.uriEndsWithXsd(location)) { // Location is a namespace continue; } // Namespace match with location that ends with xsd pairs[namespace] = location; // eslint-disable-next-line sonarjs/updated-loop-counter c += 1; // Skip ns declaration } return pairs; } uriEndsWithXsd(uri) { return uri.toLowerCase().endsWith('.xsd'); } } exports.default = RemoveIncompleteSchemaLocations;