From b56747c3c2b44de18e4a359fdac89cbc7306d8b2 Mon Sep 17 00:00:00 2001 From: muena Date: Wed, 20 May 2026 13:30:42 +0200 Subject: [PATCH] Revert "fix(velum): rebalance brackets after pseudonymization" This reverts commit bd3522d28ae9e4616cc78b8da10e2d1d23554c8f. --- src/velum.ts | 66 ++-------------------------------------------------- 1 file changed, 2 insertions(+), 64 deletions(-) diff --git a/src/velum.ts b/src/velum.ts index a2cd096..d8b8681 100644 --- a/src/velum.ts +++ b/src/velum.ts @@ -11,66 +11,6 @@ function escapeRegExp(value: string): string { return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } -const BRACKET_PAIRS: Array<{ open: string; close: string }> = [ - { open: "(", close: ")" }, - { open: "[", close: "]" }, - { open: "{", close: "}" }, -]; - -function countChar(value: string, char: string): number { - let count = 0; - for (const c of value) if (c === char) count += 1; - return count; -} - -function rebalanceBrackets( - text: string, - mapping: PlaceholderMapping, -): { text: string; mapping: PlaceholderMapping } { - let resultText = text; - const resultMapping: PlaceholderMapping = {}; - - for (const [placeholder, entry] of Object.entries(mapping)) { - let original = entry.original; - let lead = ""; - let trail = ""; - - let trimmed = true; - while (trimmed && original.length > 0) { - trimmed = false; - const pair = BRACKET_PAIRS.find((p) => original.startsWith(p.open)); - if (pair && countChar(original, pair.open) > countChar(original, pair.close)) { - lead += original[0]; - original = original.slice(1); - trimmed = true; - } - } - - trimmed = true; - while (trimmed && original.length > 0) { - trimmed = false; - const pair = BRACKET_PAIRS.find((p) => original.endsWith(p.close)); - if (pair && countChar(original, pair.close) > countChar(original, pair.open)) { - trail = original[original.length - 1] + trail; - original = original.slice(0, -1); - trimmed = true; - } - } - - if (lead || trail) { - const escaped = escapeRegExp(placeholder); - resultText = resultText.replace( - new RegExp(escaped, "g"), - `${lead}${placeholder}${trail}`, - ); - } - - resultMapping[placeholder] = { ...entry, original }; - } - - return { text: resultText, mapping: resultMapping }; -} - export function normalizePseudonymizeResponse( response: PseudonymizeResponse, ): PseudonymizeResponse { @@ -94,12 +34,10 @@ export function normalizePseudonymizeResponse( text = text.replace(bareRegex, bracketed); } - const rebalanced = rebalanceBrackets(text, normalizedMapping); - return { ...response, - pseudonymized_text: rebalanced.text, - mapping: rebalanced.mapping, + pseudonymized_text: text, + mapping: normalizedMapping, }; }