@nodecfdi/cfdi-cleaner
Version:
Librería para limpiar comprobantes fiscales digitales v3.3 y v4.0
52 lines (51 loc) • 1.48 kB
JavaScript
/**
* Helper class to work with xsi:schemaLocation attribute
*/
export default class SchemaLocation {
_pairs;
/**
* SchemaLocation constructor
*
* @param pairs - On each entry: key is namespace, value is location
*
*/
constructor(pairs) {
this._pairs = pairs;
}
static createFromValue(value) {
return SchemaLocation.createFromComponents(SchemaLocation.valueToComponents(value));
}
static valueToComponents(schemaLocationValue) {
return schemaLocationValue
.replaceAll(/\s/g, ' ')
.split(' ')
.filter((x) => x && x !== '');
}
static createFromComponents(components) {
const pairs = {};
for (let index = 0; index < components.length; index += 2) {
pairs[components[index]] = components[index + 1];
}
return new SchemaLocation(pairs);
}
getPairs() {
return this._pairs;
}
setPair(namespace, location) {
this._pairs[namespace] = location;
}
filterUsingNamespace(filterFunction) {
this._pairs = Object.fromEntries(Object.entries(this._pairs).filter((object) => filterFunction(object[0])));
}
asValue() {
return Object.entries(this._pairs)
.map((object) => `${object[0]} ${object[1]}`)
.join(' ');
}
import(source) {
this._pairs = {
...this._pairs,
...source._pairs,
};
}
}