Skip to content

feat: export parseTextDiff #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 47 additions & 44 deletions packages/jsondiffpatch/src/formatters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LineOutputPiece> = {
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,
Expand Down Expand Up @@ -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<LineOutputPiece> = {
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;