diff --git a/packages/cfdi/xml/README.md b/packages/cfdi/xml/README.md index 7f0f54a..c9da1fd 100644 --- a/packages/cfdi/xml/README.md +++ b/packages/cfdi/xml/README.md @@ -23,8 +23,8 @@
  • - - @cfdi/xml2json + + @cfdi/2json
  • diff --git a/packages/cfdi/xsd/src/index.ts b/packages/cfdi/xsd/src/index.ts index 63b8d09..62e11ad 100644 --- a/packages/cfdi/xsd/src/index.ts +++ b/packages/cfdi/xsd/src/index.ts @@ -1,2 +1 @@ -export { default as Transform } from './transform'; export { default as Schema } from './schema'; diff --git a/packages/cfdi/xsd/src/transform.ts b/packages/cfdi/xsd/src/transform.ts deleted file mode 100644 index b3aa554..0000000 --- a/packages/cfdi/xsd/src/transform.ts +++ /dev/null @@ -1,128 +0,0 @@ -// @ts-ignore - -import { js2xml, json2xml, xml2js } from 'xml-js'; -import { readFileSync, writeFileSync } from 'fs'; - -import Ajv from 'ajv'; -// @ts-ignore -import { Xsd2JsonSchema } from 'xsd2jsonschema'; - -type XsdElement = { - name: string; - minOccurs?: string; - elements?: XsdElement[]; - attributes?: Record; -}; - -type JsonSchema = { - $schema: string; - type: string; - properties: Record; - required: string[]; - additionalProperties: false; -}; - -function cleanObjectKeys(obj: any): any { - if (typeof obj !== 'object') { - return obj; - } - - if (Array.isArray(obj)) { - return obj.map(cleanObjectKeys); - } - - const cleanedObj = {}; - - for (const key in obj) { - if (Object.prototype.hasOwnProperty.call(obj, key)) { - if (key === '_attributes') { - const attributes = obj[key]; - for (const attrKey in attributes) { - if (Object.prototype.hasOwnProperty.call(attributes, attrKey)) { - if (!attrKey.includes(':')) { - // @ts-ignore - cleanedObj[`@${attrKey}`] = attributes[attrKey]; - } - } - } - } else { - const cleanedKey = key.split(':').pop(); - // @ts-ignore - cleanedObj[cleanedKey] = cleanObjectKeys(obj[key]); - } - } - } - - return cleanedObj; -} - -//export const CFDIXsd = LoadXsd.getInstance(); - -export default class Transform { - xml: any = {}; - xslPath = ''; - fullPath = ''; - constructor(xml: any) { - this.xml = xml; - } - - async run() { - const rear = await this.obtenerValores(this.xml['cfdi:Comprobante']); - return `||${rear.filter((e) => e).join('|')}||`; - } - - json(xslPath: string) { - this.xslPath = xslPath; - return this; - } - - warnings(type: string = 'silent') { - return this; - } - - private obtenerValores(obj: any, options: any = { tagKey: 'comprobante' }) { - const { tagKey = 'comprobante', ignore = false } = options; - let valores: (string | number)[] = []; - const omitKeys = [ - 'xmlns:cfdi', - 'xmlns:xsi', - 'xsi:schemaLocation', - 'Certificado', - 'xmlns:destruccion', - 'xmlns:iedu', - 'xmlns:pago10', - ]; - const miObjeto = { - comprobante: [ - '_attributes', - 'cfdi:InformacionGlobal', - 'cfdi:CfdiRelacionados', - 'cfdi:Emisor', - 'cfdi:Receptor', - 'cfdi:Conceptos', - 'cfdi:Impuestos', - 'cfdi:Complemento', - ], - concepto: [], - }; - - const ingnore: string[] = ['Sello']; - // @ts-ignore - const clavesOrdenadas = ignore ? Object.keys(obj) : miObjeto[tagKey]; - for (let key of clavesOrdenadas) { - if (!omitKeys.includes(key)) { - if (typeof obj[key] === 'object') { - valores = valores.concat( - this.obtenerValores(obj[key], { ignore: true }) - ); - } else { - if (!ingnore.includes(key)) { - valores.push(obj[key]); - } - } - } - } - - return valores; - } -}