aarnal80 commited on
Commit
56fccc4
·
verified ·
1 Parent(s): 69d90a0

Update js/labAnalysisModule.js

Browse files
Files changed (1) hide show
  1. js/labAnalysisModule.js +55 -39
js/labAnalysisModule.js CHANGED
@@ -115,7 +115,7 @@ export async function analyzeLabResults(text) {
115
  }
116
 
117
 
118
- // --- INICIO: Función displayLabResults (MODIFICADA con REGEX y LOGS) ---
119
  /**
120
  * Muestra los resultados de laboratorio formateados en la UI.
121
  * @param {string} resultsText - El texto formateado recibido de la IA.
@@ -135,56 +135,72 @@ export function displayLabResults(resultsText, containerElement, filterAltered)
135
  }
136
 
137
  const lines = resultsText.split('\n').filter(line => line.trim() !== '');
138
- let linesToDisplay = lines;
139
 
 
140
  if (filterAltered) {
141
- console.log("--- INICIO FILTRO ALTERADOS ---"); // Log inicio filtro
142
- const filteredLines = [];
143
- let currentHeader = null;
144
- let isCurrentHeaderAdded = false;
145
- // Regex para detectar el patrón **...** de forma más robusta
146
- const alterationRegex = /\*\*.*?\*\*/; // Busca dos asteriscos, cualquier cosa (no greedy), otros dos asteriscos
147
-
148
- for (const line of lines) {
149
- const trimmedLine = line.trim();
150
- const isHeader = trimmedLine.endsWith(':');
151
- // !! USAR REGEX para detectar alteración !!
152
- const hasAlteration = alterationRegex.test(line);
153
-
154
- // !! LOG DE DEPURACIÓN !!
155
- console.log(`Linea: "${line}" | Header: ${isHeader} | Alterado (Regex): ${hasAlteration}`);
156
-
157
- if (isHeader) {
158
- currentHeader = line;
159
- isCurrentHeaderAdded = false; // Resetear para la nueva cabecera
160
- } else if (hasAlteration) {
161
- // Si es un resultado alterado, añadir cabecera si no se ha hecho
162
- if (currentHeader && !isCurrentHeaderAdded) {
163
- filteredLines.push(currentHeader);
164
- isCurrentHeaderAdded = true; // Marcar cabecera como añadida
165
- console.log(` -> Cabecera añadida: "${currentHeader}"`); // Log cabecera
 
166
  }
167
- // Añadir la línea alterada
168
- filteredLines.push(line);
169
- console.log(` -> Línea alterada añadida: "${line}"`); // Log línea
 
 
 
 
 
 
 
 
 
 
 
170
  }
171
- } // Fin del bucle
172
 
173
- if (filteredLines.length === 0 && lines.length > 0) {
174
- linesToDisplay = ["No se encontraron resultados marcados como alterados."];
175
- console.log("--- FIN FILTRO ALTERADOS (Resultado: Vacío) ---");
 
176
  } else {
177
- linesToDisplay = filteredLines;
178
- console.log("--- FIN FILTRO ALTERADOS (Resultado: Mostrando filtrados) ---");
179
  }
180
- console.log("Líneas finales a mostrar:", linesToDisplay); // Log líneas finales
181
- }
 
182
 
183
- // Formateo y renderizado (sin cambios aquí)
184
  const fragment = document.createDocumentFragment();
185
  linesToDisplay.forEach(line => {
186
  const p = document.createElement('p');
187
  p.style.margin = '0 0 0.3em 0';
 
188
  p.innerHTML = line.replace(
189
  /\*\*(.*?)\*\*/g,
190
  '<strong class="text-red-600 font-bold">$1</strong>'
 
115
  }
116
 
117
 
118
+ // --- INICIO: Función displayLabResults (MODIFICADA con filtro a NIVEL PARÁMETRO) ---
119
  /**
120
  * Muestra los resultados de laboratorio formateados en la UI.
121
  * @param {string} resultsText - El texto formateado recibido de la IA.
 
135
  }
136
 
137
  const lines = resultsText.split('\n').filter(line => line.trim() !== '');
138
+ let linesToDisplay = lines; // Por defecto, mostrar todas
139
 
140
+ // Aplicar filtro A NIVEL DE PARÁMETRO si está activo
141
  if (filterAltered) {
142
+ console.log("--- INICIO FILTRO ALTERADOS (Nivel Parámetro) ---");
143
+ const filteredOutputLines = []; // Aquí guardaremos las líneas reconstruidas
144
+ const alterationRegex = /\*\*.*?\*\*/; // Regex para detectar **...**
145
+
146
+ for (const line of lines) { // Iterar sobre las líneas originales de la IA
147
+ // Intentar separar la línea en "Cabecera: Contenido"
148
+ const headerMatch = line.match(/^([^:]+):\s*(.*)$/);
149
+
150
+ if (headerMatch) {
151
+ // Si coincide el formato "Cabecera: Contenido"
152
+ const headerName = headerMatch[1].trim(); // Ej: "Hematología"
153
+ const contentPart = headerMatch[2].trim(); // Ej: "Hematíes... | Hemoglobina..."
154
+
155
+ // Dividir el contenido en parámetros individuales usando "|"
156
+ const parameters = contentPart.split('|')
157
+ .map(p => p.trim()) // Quitar espacios extra de cada parámetro
158
+ .filter(p => p); // Quitar posibles parámetros vacíos
159
+
160
+ const alteredParametersInLine = []; // Guardar solo los parámetros alterados de ESTA línea
161
+
162
+ // Revisar cada parámetro de la línea actual
163
+ for (const param of parameters) {
164
+ if (alterationRegex.test(param)) { // Si el parámetro contiene **...**
165
+ alteredParametersInLine.push(param); // Añadirlo a la lista de alterados de esta línea
166
+ console.log(` -> Parámetro Alterado Encontrado: "${param}" bajo "${headerName}"`);
167
+ }
168
  }
169
+
170
+ // Si encontramos algún parámetro alterado en esta línea...
171
+ if (alteredParametersInLine.length > 0) {
172
+ // ...reconstruir la línea solo con la cabecera y esos parámetros alterados.
173
+ const filteredLine = `${headerName}: ${alteredParametersInLine.join(' | ')}`;
174
+ filteredOutputLines.push(filteredLine); // Añadir la línea reconstruida al resultado final
175
+ console.log(` -> Línea Filtrada Construida: "${filteredLine}"`);
176
+ } else {
177
+ // Si no hubo alterados en esta línea, simplemente la ignoramos (no se añade a filteredOutputLines)
178
+ console.log(` -> Cabecera "${headerName}" sin parámetros alterados, omitiendo línea completa.`);
179
+ }
180
+ } else {
181
+ // Si una línea no tiene el formato "Cabecera: Contenido", la ignoramos al filtrar
182
+ console.log(` -> Línea ignorada en filtro (no coincide formato Cabecera: Contenido): "${line}"`);
183
  }
184
+ } // Fin del bucle sobre las líneas originales
185
 
186
+ // Asignar el resultado del filtrado a linesToDisplay
187
+ if (filteredOutputLines.length === 0 && lines.length > 0) {
188
+ linesToDisplay = ["No se encontraron resultados marcados como alterados."];
189
+ console.log("--- FIN FILTRO ALTERADOS (Resultado: Vacío) ---");
190
  } else {
191
+ linesToDisplay = filteredOutputLines; // Usar las líneas reconstruidas y filtradas
192
+ console.log("--- FIN FILTRO ALTERADOS (Resultado: Mostrando filtrados por parámetro) ---");
193
  }
194
+ console.log("Líneas finales a mostrar (filtrado por parámetro):", linesToDisplay);
195
+
196
+ } // Fin if(filterAltered)
197
 
198
+ // Formateo y renderizado (sin cambios, opera sobre linesToDisplay)
199
  const fragment = document.createDocumentFragment();
200
  linesToDisplay.forEach(line => {
201
  const p = document.createElement('p');
202
  p.style.margin = '0 0 0.3em 0';
203
+ // Reemplazar **valor** por <strong> con estilo
204
  p.innerHTML = line.replace(
205
  /\*\*(.*?)\*\*/g,
206
  '<strong class="text-red-600 font-bold">$1</strong>'