facturacionelectronicapy-ts-xmlgen
Version:
Genera el contenido del archivo XML del Documento electrónico exigido por la SET
62 lines (61 loc) • 2.27 kB
TypeScript
import { z } from 'zod';
type SchemaPart = [header: string, value: any];
type Schema = Record<number, SchemaPart>;
type Headers<S extends Schema> = {
[key in keyof S]: key extends number ? S[key][0] : never;
};
type RawRow<S extends Schema> = {
[key in keyof S]: key extends number ? S[key][1] : never;
};
type _Combine<T, K extends PropertyKey = T extends unknown ? keyof T : never> = T extends unknown ? T & Partial<Record<Exclude<K, keyof T>, never>> : never;
type Combine<T> = Required<{
[K in keyof _Combine<T>]: _Combine<T>[K];
}>;
type PairToObject<T extends SchemaPart> = {
[key in T[0]]: T[1];
};
type SchemaGroups<S extends Schema> = {
[key in keyof S]: key extends number ? PairToObject<S[key]> : never;
};
type Row<S extends Schema> = Combine<SchemaGroups<S>[keyof SchemaGroups<S>]>;
type NotFoundErrorData = {
ctx: z.RefinementCtx;
fieldName: string;
message?: string;
};
/**
* @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],
* ],
* );
*/
export default class Table<S extends {
0: [header: '_id', value: string | number];
} & Schema, RR extends RawRow<S> = RawRow<S>, ID extends RR[0] = RR[0], R extends Row<S> = Row<S>> {
readonly headers: Headers<S>;
readonly data: RR[];
idIndex: Map<ID, number>;
constructor(headers: Headers<S>, data: RR[]);
private manageNotFoundError;
normalizeRow(rawRow: RR): R;
/**Retorna el resultado que coincide con el id especificado, o null si no se encuentra ninguna coincidencia.*/
findById(id: ID, _notFoundErrorData?: NotFoundErrorData): R | null;
_findById(id: ID, _notFoundErrorData?: NotFoundErrorData): R;
_findByIdIfExist(id: ID | undefined, _notFoundErrorData?: NotFoundErrorData): R | null;
/**Retorna todos los resultados que cumplan con la condicion especificada.*/
findWhere(expected: Partial<R>): R[];
/**Retorna el primer resultado que cumpla con la condicion especificada, o null si no se encuentra ninguna coincidencia.*/
findUniqueWhere(expected: Partial<R>): R | null;
}
export {};