MCPcopy Create free account
hub / github.com/donghaxkim/react-rewrite / findTextDiff

Function findTextDiff

packages/cli/src/transform.ts:1339–1363  ·  view source on GitHub ↗
(oldText: string, newText: string)

Source from the content-addressed store, hash-verified

1337}
1338
1339function findTextDiff(oldText: string, newText: string): { oldSubstring: string; newSubstring: string; prefixLen: number } | null {
1340 if (oldText === newText) return null;
1341
1342 let prefixLen = 0;
1343 while (prefixLen < oldText.length && prefixLen < newText.length && oldText[prefixLen] === newText[prefixLen]) {
1344 prefixLen++;
1345 }
1346
1347 let oldSuffixStart = oldText.length;
1348 let newSuffixStart = newText.length;
1349 while (
1350 oldSuffixStart > prefixLen &&
1351 newSuffixStart > prefixLen &&
1352 oldText[oldSuffixStart - 1] === newText[newSuffixStart - 1]
1353 ) {
1354 oldSuffixStart--;
1355 newSuffixStart--;
1356 }
1357
1358 const oldSubstring = oldText.slice(prefixLen, oldSuffixStart);
1359 const newSubstring = newText.slice(prefixLen, newSuffixStart);
1360
1361 if (!oldSubstring && !newSubstring) return null;
1362 return { oldSubstring, newSubstring, prefixLen };
1363}
1364
1365function anchorMatchesAt(renderedText: string, start: number, oldSubstring: string, textAnchor: TextEditAnchor): boolean {
1366 const oldLength = textAnchor.end - textAnchor.start;

Callers 1

mutateTextContentFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected