From 91f8385f43e09bd5d1adee7184194e8fa3d15ba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atle=20Sj=C3=B8n=C3=B8st?= Date: Sat, 29 Mar 2025 11:53:45 +0100 Subject: [PATCH] feat: export parseTextDiff --- packages/jsondiffpatch/src/formatters/base.ts | 91 ++++++++++--------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/packages/jsondiffpatch/src/formatters/base.ts b/packages/jsondiffpatch/src/formatters/base.ts index d12979d4..60406994 100644 --- a/packages/jsondiffpatch/src/formatters/base.ts +++ b/packages/jsondiffpatch/src/formatters/base.ts @@ -454,50 +454,7 @@ abstract class BaseFormatter< return "unknown"; } - parseTextDiff(value: string) { - const output = []; - const lines = value.split("\n@@ "); - for (const line of lines) { - const lineOutput: { - pieces: LineOutputPiece[]; - location?: LineOutputLocation; - } = { - pieces: [], - }; - const location = /^(?:@@ )?[-+]?(\d+),(\d+)/.exec(line)?.slice(1); - if (!location) { - throw new Error("invalid text diff format"); - } - assertArrayHasAtLeast2(location); - lineOutput.location = { - line: location[0], - chr: location[1], - }; - const pieces = line.split("\n").slice(1); - for ( - let pieceIndex = 0, piecesLength = pieces.length; - pieceIndex < piecesLength; - pieceIndex++ - ) { - const piece = pieces[pieceIndex]; - if (piece === undefined || !piece.length) { - continue; - } - const pieceOutput: Partial = { - type: "context", - }; - if (piece.substring(0, 1) === "+") { - pieceOutput.type = "added"; - } else if (piece.substring(0, 1) === "-") { - pieceOutput.type = "deleted"; - } - pieceOutput.text = piece.slice(1); - lineOutput.pieces.push(pieceOutput as LineOutputPiece); - } - output.push(lineOutput as LineOutput); - } - return output; - } + parseTextDiff = parseTextDiff; abstract rootBegin( context: TContext, @@ -602,4 +559,50 @@ abstract class BaseFormatter< ): void; } +export function parseTextDiff(value: string) { + const output = []; + const lines = value.split("\n@@ "); + for (const line of lines) { + const lineOutput: { + pieces: LineOutputPiece[]; + location?: LineOutputLocation; + } = { + pieces: [], + }; + const location = /^(?:@@ )?[-+]?(\d+),(\d+)/.exec(line)?.slice(1); + if (!location) { + throw new Error("invalid text diff format"); + } + assertArrayHasAtLeast2(location); + lineOutput.location = { + line: location[0], + chr: location[1], + }; + const pieces = line.split("\n").slice(1); + for ( + let pieceIndex = 0, piecesLength = pieces.length; + pieceIndex < piecesLength; + pieceIndex++ + ) { + const piece = pieces[pieceIndex]; + if (piece === undefined || !piece.length) { + continue; + } + const pieceOutput: Partial = { + type: "context", + }; + if (piece.substring(0, 1) === "+") { + pieceOutput.type = "added"; + } else if (piece.substring(0, 1) === "-") { + pieceOutput.type = "deleted"; + } + pieceOutput.text = piece.slice(1); + lineOutput.pieces.push(pieceOutput as LineOutputPiece); + } + output.push(lineOutput as LineOutput); + } + return output; +} + + export default BaseFormatter;