mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 09:25:43 +01:00
Handle overlapping replacements
This commit is contained in:
@@ -98,4 +98,37 @@ describe("text split and replace", () => {
|
||||
expect(result[7]).toStrictEqual({ text: "We are going to die." });
|
||||
expect(result[8]).toStrictEqual({ text: "“" });
|
||||
});
|
||||
|
||||
it("should ignore conflicting replacements", () => {
|
||||
const result = textSplitAndReplace<Wrapped>(
|
||||
"'So this is it,' said Arthur, 'We are going to die.'",
|
||||
[
|
||||
{ textToReplace: "said Arthur", replacement: { text: "to be replaced" } },
|
||||
{ textToReplace: " said", replacement: { text: "to be ignored 1" }, replaceAll: true },
|
||||
{ textToReplace: "d A", replacement: { text: "to be ignored 2" }, replaceAll: true },
|
||||
{ textToReplace: "Arthur,", replacement: { text: "to be ignored 3" }, replaceAll: true }
|
||||
],
|
||||
testWrapper
|
||||
);
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result[0]).toStrictEqual({ text: "'So this is it,' " });
|
||||
expect(result[1]).toStrictEqual({ text: "to be replaced" });
|
||||
expect(result[2]).toStrictEqual({ text: ", 'We are going to die.'" });
|
||||
});
|
||||
|
||||
it("should replace adjacent texts", () => {
|
||||
const result = textSplitAndReplace<Wrapped>(
|
||||
"'So this is it,' said Arthur, 'We are going to die.'",
|
||||
[
|
||||
{ textToReplace: "'So this is it,'", replacement: { text: "one" } },
|
||||
{ textToReplace: " said Arthur, ", replacement: { text: "two" } },
|
||||
{ textToReplace: "'We are going to die.'", replacement: { text: "three" } }
|
||||
],
|
||||
testWrapper
|
||||
);
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result[0]).toStrictEqual({ text: "one" });
|
||||
expect(result[1]).toStrictEqual({ text: "two" });
|
||||
expect(result[2]).toStrictEqual({ text: "three" });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -34,6 +34,14 @@ type PartToReplace<T> = {
|
||||
replacement: T;
|
||||
};
|
||||
|
||||
function hasConflict<T>(alreadyFoundReplacements: PartToReplace<T>[], newReplacement: PartToReplace<T>) {
|
||||
return !!alreadyFoundReplacements.find(
|
||||
existing =>
|
||||
(existing.start <= newReplacement.start && existing.start + existing.length > newReplacement.start) ||
|
||||
(newReplacement.start <= existing.start && newReplacement.start + newReplacement.length > existing.start)
|
||||
);
|
||||
}
|
||||
|
||||
export default function textSplitAndReplace<T>(
|
||||
text: string,
|
||||
replacements: Replacement<T>[],
|
||||
@@ -47,7 +55,10 @@ export default function textSplitAndReplace<T>(
|
||||
const start = text.indexOf(replacement.textToReplace, lastIndex);
|
||||
if (start >= 0) {
|
||||
const length = replacement.textToReplace.length;
|
||||
partsToReplace.push({ start, length, replacement: replacement.replacement });
|
||||
const newReplacement = { start, length, replacement: replacement.replacement };
|
||||
if (!hasConflict(partsToReplace, newReplacement)) {
|
||||
partsToReplace.push(newReplacement);
|
||||
}
|
||||
lastIndex = start + length;
|
||||
} else {
|
||||
lastIndex = -1;
|
||||
|
||||
Reference in New Issue
Block a user