refactor: add inbound context helpers

This commit is contained in:
Peter Steinberger
2026-01-17 04:04:17 +00:00
parent a2b5b1f0cb
commit 388b2bce01
9 changed files with 224 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
import { describe, expect, it } from "vitest";
import { normalizeInboundTextNewlines } from "./inbound-text.js";
describe("normalizeInboundTextNewlines", () => {
it("keeps real newlines", () => {
expect(normalizeInboundTextNewlines("a\nb")).toBe("a\nb");
});
it("normalizes CRLF/CR to LF", () => {
expect(normalizeInboundTextNewlines("a\r\nb")).toBe("a\nb");
expect(normalizeInboundTextNewlines("a\rb")).toBe("a\nb");
});
it("decodes literal \\\\n to newlines when no real newlines exist", () => {
expect(normalizeInboundTextNewlines("a\\nb")).toBe("a\nb");
});
});

View File

@@ -0,0 +1,7 @@
export function normalizeInboundTextNewlines(input: string): string {
const text = input.replaceAll("\r\n", "\n").replaceAll("\r", "\n");
if (text.includes("\n")) return text;
if (!text.includes("\\n")) return text;
return text.replaceAll("\\n", "\n");
}