/* Prüft den Steuerzeichen-Filter aus webdesign-helfer.js isoliert. Bewusst mit String.fromCharCode statt echten Steuerzeichen im Quelltext. */ const f = (w, max = 2000) => String(w).replace(/[^\P{Cc}\n\t]/gu, "").trim().slice(0, max); const NUL = String.fromCharCode(0); const BELL = String.fromCharCode(7); const CR = String.fromCharCode(13); const ESC = String.fromCharCode(27); const DEL = String.fromCharCode(127); const NL = "\n"; const TAB = "\t"; const proben = [ ["einfacher Text", "Hallo Welt", "Hallo Welt"], ["Zeilenumbruch bleibt", "Zeile1" + NL + "Zeile2", "Zeile1" + NL + "Zeile2"], ["Tabulator bleibt", "Tab" + TAB + "hier", "Tab" + TAB + "hier"], ["Nullbyte raus", "Text" + NUL + "mehr", "Textmehr"], ["Bell raus", "Bell" + BELL + "X", "BellX"], ["Escape raus (Terminal-Trick)", ESC + "[31mrot", "[31mrot"], ["DEL raus", "A" + DEL + "B", "AB"], /* Erwartung bewusst ohne führenden Umbruch: der Filter entfernt zwar nur den Wagenrücklauf, das anschließende trim() räumt den dann alleine stehenden Zeilenumbruch am Rand aber mit weg. Genau so ist es gewollt. */ ["Wagenruecklauf raus", CR + NL + "Text" + CR, "Text"], ["Randleerzeichen weg", " Rand ", "Rand"], ["Umlaute bleiben", "Umlaute äöüß bleiben", "Umlaute äöüß bleiben"], ["Emoji bleibt", "Emoji \u{1F43A} bleibt", "Emoji \u{1F43A} bleibt"], ]; let ok = 0, schlecht = 0; for (const [name, ein, erwartet] of proben) { const raus = f(ein); if (raus === erwartet) { ok++; console.log(" ok " + name); } else { schlecht++; console.log(" FEHLT " + name + " -> " + JSON.stringify(raus) + " statt " + JSON.stringify(erwartet)); } } const lang = f("x".repeat(5000), 10); if (lang.length === 10) { ok++; console.log(" ok Laengenbegrenzung greift"); } else { schlecht++; console.log(" FEHLT Laengenbegrenzung: " + lang.length); } console.log(`\n=== ${ok} ok, ${schlecht} falsch ===`); process.exit(schlecht === 0 ? 0 : 1);