Optimizaciones de rendimiento: consultas paralelas en API, caché de Regex en parser de laboratorio y pre-cálculo de claves en sortCamas
This commit is contained in:
+35
-46
@@ -53,55 +53,44 @@ export function isCamaFueraDeGrupo(
|
||||
}
|
||||
|
||||
export function sortCamas<T extends { numero: string }>(camas: T[]): T[] {
|
||||
const parseBedNumber = (numero: string) => {
|
||||
const match = (numero || '').match(/^(\d{3})-(\d+)$/);
|
||||
if (!match) return { sala: 0, cama: 0 };
|
||||
return {
|
||||
sala: parseInt(match[1], 10),
|
||||
cama: parseInt(match[2], 10)
|
||||
};
|
||||
if (!camas || camas.length <= 1) return camas || [];
|
||||
|
||||
const parseBedKey = (numero: string) => {
|
||||
const clean = (numero || '').trim();
|
||||
const match = clean.match(/^(\d{3})-(\d+)$/);
|
||||
if (!match) return { weight: 10, salaDir: 0, cama: 0 };
|
||||
|
||||
const sala = parseInt(match[1], 10);
|
||||
const cama = parseInt(match[2], 10);
|
||||
const grupo = Math.floor(sala / 100);
|
||||
const esPar = sala % 2 === 0;
|
||||
|
||||
let weight = 10;
|
||||
let isDesc = false;
|
||||
|
||||
if (grupo === 2) {
|
||||
if (!esPar) { weight = 1; isDesc = true; }
|
||||
else { weight = 2; isDesc = false; }
|
||||
} else if (grupo === 3) {
|
||||
if (esPar) { weight = 3; isDesc = false; }
|
||||
else { weight = 4; isDesc = true; }
|
||||
} else if (grupo === 4) {
|
||||
if (!esPar) { weight = 5; isDesc = true; }
|
||||
else { weight = 6; isDesc = false; }
|
||||
}
|
||||
|
||||
const salaDir = isDesc ? -sala : sala;
|
||||
return { weight, salaDir, cama };
|
||||
};
|
||||
|
||||
return [...camas].sort((a, b) => {
|
||||
const pa = parseBedNumber(a.numero);
|
||||
const pb = parseBedNumber(b.numero);
|
||||
|
||||
const getOrden = (sala: number) => {
|
||||
const grupo = Math.floor(sala / 100);
|
||||
const esPar = sala % 2 === 0;
|
||||
|
||||
if (grupo === 2) {
|
||||
if (!esPar) return { weight: 1, order: 'desc', sala }; // 2XX impares, mayor a menor
|
||||
return { weight: 2, order: 'asc', sala }; // 2XX pares, menor a mayor
|
||||
}
|
||||
if (grupo === 3) {
|
||||
if (esPar) return { weight: 3, order: 'asc', sala }; // 3XX pares, menor a mayor
|
||||
return { weight: 4, order: 'desc', sala }; // 3XX impares, mayor a menor
|
||||
}
|
||||
if (grupo === 4) {
|
||||
if (!esPar) return { weight: 5, order: 'desc', sala }; // 4XX impares, mayor a menor
|
||||
return { weight: 6, order: 'asc', sala }; // 4XX pares, menor a mayor
|
||||
}
|
||||
return { weight: 10, order: 'asc', sala };
|
||||
};
|
||||
|
||||
const oa = getOrden(pa.sala);
|
||||
const ob = getOrden(pb.sala);
|
||||
|
||||
if (oa.weight !== ob.weight) {
|
||||
return oa.weight - ob.weight;
|
||||
}
|
||||
|
||||
if (oa.sala !== ob.sala) {
|
||||
if (oa.order === 'desc') {
|
||||
return ob.sala - oa.sala;
|
||||
} else {
|
||||
return oa.sala - ob.sala;
|
||||
}
|
||||
}
|
||||
|
||||
return pa.cama - pb.cama;
|
||||
const mapped = camas.map(item => ({ item, key: parseBedKey(item.numero) }));
|
||||
mapped.sort((a, b) => {
|
||||
if (a.key.weight !== b.key.weight) return a.key.weight - b.key.weight;
|
||||
if (a.key.salaDir !== b.key.salaDir) return a.key.salaDir - b.key.salaDir;
|
||||
return a.key.cama - b.key.cama;
|
||||
});
|
||||
|
||||
return mapped.map(m => m.item);
|
||||
}
|
||||
|
||||
export function computeSector(numero: string): "En Área" | "Fuera de Área" {
|
||||
|
||||
Reference in New Issue
Block a user