Replace all instead of just replace

This commit is contained in:
Andras Schmelczer 2025-11-22 19:57:52 +00:00
parent 9c3dedad76
commit f11c8db6d2
5 changed files with 29 additions and 7 deletions

View file

@ -13,7 +13,7 @@ export function lineAndColumnToPosition(
line: number,
column: number
): number {
const lines = text.replace("\r", "").split("\n");
const lines = text.replaceAll("\r", "").split("\n");
if (line >= lines.length) {
throw new Error(`Line number ${line} is out of range.`);

View file

@ -43,6 +43,28 @@ describe("positionToLineAndColumn", () => {
});
});
test("with multiple carriage returns", () => {
// Test that all \r characters are removed, not just the first one
const text = "line1\r\nline2\r\nline3\r\n";
assert.deepStrictEqual(positionToLineAndColumn(text, 0), {
line: 0,
column: 0
});
// Position 6 = start of 'line2' after all \r removed
assert.deepStrictEqual(positionToLineAndColumn(text, 6), {
line: 1,
column: 0
});
// Position 12 = start of 'line3' after all \r removed
assert.deepStrictEqual(positionToLineAndColumn(text, 12), {
line: 2,
column: 0
});
});
test("handles empty input", () => {
assert.deepStrictEqual(positionToLineAndColumn("", 0), {
line: 0,

View file

@ -14,7 +14,7 @@ export function positionToLineAndColumn(
throw new Error("Position cannot be negative");
}
text = text.replace("\r", "");
text = text.replaceAll("\r", "");
if (
position >