MCPcopy Create free account
hub / github.com/danvk/webdiff / filePairDisplayName

Function filePairDisplayName

ts/utils.ts:8–47  ·  view source on GitHub ↗
(filePair: FilePair)

Source from the content-addressed store, hash-verified

6 * filePair is like {type, path, a, b}
7 */
8export function filePairDisplayName(filePair: FilePair) {
9 if (filePair.type !== 'move') {
10 return filePair.a || filePair.b;
11 }
12
13 // Factor out shared components (folder names, basename, extension) in the
14 // old and new names. This might be overkill, but we have a differ, so why
15 // not?
16 const split_re = /([./])/; // split to folders and extension
17 const split = (path: string) => path.split(split_re).filter(x => x);
18 const partsA = split(filePair.a);
19 const partsB = split(filePair.b);
20
21 const diffs = Diff.diffArrays(partsA, partsB);
22 let out = '';
23
24 for (let i = 0; i < diffs.length; i++) {
25 const diff = diffs[i];
26 const v = diff.value.join('');
27 if (diff.removed) {
28 if (i < diffs.length - 1 && diffs[i + 1].added) {
29 // replace
30 i += 1;
31 const v2 = diffs[i].value.join('');
32 out += '{' + v + ' → ' + v2 + '}';
33 } else {
34 // remove
35 out += '{' + v + ' → }';
36 }
37 } else if (diff.added) {
38 // add
39 out += '{ → ' + v + '}';
40 } else {
41 // equal
42 out += v;
43 }
44 }
45
46 return out;
47}
48
49/** Checks whether the diff is one-sided, i.e. an add or delete. */
50export function isOneSided(filePair: FilePair) {

Callers 6

RootFunction · 0.90
FileListFunction · 0.90
linkOrNoneFunction · 0.90
FileDropdownFunction · 0.90
util.test.tsFile · 0.90
renameFunction · 0.90

Calls 1

splitFunction · 0.85

Tested by 1

renameFunction · 0.72