UNPKG

facturacionelectronicapy-ts-xmlgen

Version:

Genera el contenido del archivo XML del Documento electrónico exigido por la SET

104 lines (103 loc) 3.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const zod_1 = require("zod"); /** * @example * type S = { * 0: ['_id', E]; * 1: ['description', string]; * 2: ['value', number]; * }; * * const table = new Table<S>( * 'test', * ['_id', 'description', 'value'],^ * [ * [0, 'description 0', 0], * [1, 'description 1', 1], * ], * ); */ class Table { headers; data; idIndex = new Map(); constructor(headers, data) { this.headers = headers; this.data = data; for (const index in data) { const row = data[index]; this.idIndex.set(row[0], Number(index)); } } manageNotFoundError(result, notFoundErrorData) { if (!result && notFoundErrorData) { const { ctx, fieldName } = notFoundErrorData; const path = ctx.path.concat(fieldName); const pathStr = `'${notFoundErrorData.ctx.path.join('.')}.${notFoundErrorData.fieldName}'`; notFoundErrorData.ctx.addIssue({ code: zod_1.z.ZodIssueCode.custom, message: notFoundErrorData.message ?? `El valor del campo ${pathStr} no es válido`, path, }); } } normalizeRow(rawRow) { let obj = {}; for (const index in this.headers) { const header = this.headers[index]; const value = rawRow[index]; obj[header] = value; } return obj; } /**Retorna el resultado que coincide con el id especificado, o null si no se encuentra ninguna coincidencia.*/ findById(id, _notFoundErrorData) { const index = this.idIndex.get(id); if (index == null) return null; const result = this.data.at(index); this.manageNotFoundError(result, _notFoundErrorData); if (!result) return null; return this.normalizeRow(result); } _findById(id, _notFoundErrorData) { let index = this.idIndex.get(id); if (index == undefined) this.manageNotFoundError(undefined, _notFoundErrorData); index ??= 0; const result = this.data.at(index); return this.normalizeRow(result); } _findByIdIfExist(id, _notFoundErrorData) { if (id == undefined) return null; return this._findById(id, _notFoundErrorData); } /**Retorna todos los resultados que cumplan con la condicion especificada.*/ findWhere(expected) { // de acuerdo a headers generar un objeto con las claves y el index en el rr const keyIndex = {}; for (const key in expected) { const headerIndex = this.headers.indexOf(key); keyIndex[key] = headerIndex; } const result = this.data.filter((row) => { for (const key in expected) { const indexInRow = keyIndex[key]; if (row[indexInRow] !== expected[key]) return false; } return true; }); return result.map((row) => this.normalizeRow(row)); } /**Retorna el primer resultado que cumpla con la condicion especificada, o null si no se encuentra ninguna coincidencia.*/ findUniqueWhere(expected) { const result = this.findWhere(expected); return result.at(0) ?? null; } } exports.default = Table;