Skip to content

Commit bfd220f

Browse files
authored
feat: getFileDistance() #3638
1 parent abcbcd1 commit bfd220f

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

src/shared/filesystemUtilities.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,35 @@ export function isInDirectory(d: string, p: string): boolean {
156156
})
157157
}
158158

159+
/**
160+
*
161+
* @returns file distance between fileA and fileB
162+
* For example:
163+
* The file distance between A/B/C.java and A/B/D.java is 0
164+
* The file distance between A/B/C.java and A/D.java is 1
165+
*/
166+
export function getFileDistance(fileA: string, fileB: string): number {
167+
let filePathA = pathutils.normalize(fileA).split('/')
168+
filePathA = filePathA.slice(0, filePathA.length - 1)
169+
170+
let filePathB = pathutils.normalize(fileB).split('/')
171+
filePathB = filePathB.slice(0, filePathB.length - 1)
172+
173+
let i = 0
174+
while (i < Math.min(filePathA.length, filePathB.length)) {
175+
const dir1 = filePathA[i]
176+
const dir2 = filePathB[i]
177+
178+
if (dir1 !== dir2) {
179+
break
180+
}
181+
182+
i++
183+
}
184+
185+
return filePathA.slice(i).length + filePathB.slice(i).length
186+
}
187+
159188
/**
160189
* Returns `name.suffix` if it does not already exist in directory `dir`, else appends
161190
* a number ("foo-1.txt", "foo-2.txt", etc.).

src/test/shared/filesystemUtilities.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { writeFile, remove } from 'fs-extra'
99
import * as path from 'path'
1010
import {
1111
fileExists,
12+
getFileDistance,
1213
getNonexistentFilename,
1314
isInDirectory,
1415
makeTemporaryToolkitFolder,
@@ -119,4 +120,79 @@ describe('filesystemUtilities', function () {
119120
assert.ok(!isInDirectory('/foo/bar/baz/', '/FOO/BAR/BAZ/A.TXT'))
120121
}
121122
})
123+
124+
describe('getFileDistance', function () {
125+
let fileA: string
126+
let fileB: string
127+
128+
it('distance 0', function () {
129+
fileA = 'foo/bar/a.java'
130+
fileB = 'foo/bar/b.java'
131+
const actual = getFileDistance(fileA, fileB)
132+
assert.strictEqual(actual, 0)
133+
})
134+
135+
it('root distance 0', function () {
136+
fileA = 'a.txt'
137+
fileB = 'b.txt'
138+
const actual = getFileDistance(fileA, fileB)
139+
assert.strictEqual(actual, 0)
140+
})
141+
142+
it('distance 1', function () {
143+
fileA = 'foo/bar/a.java'
144+
fileB = 'foo/b.java'
145+
const actual = getFileDistance(fileA, fileB)
146+
assert.strictEqual(actual, 1)
147+
})
148+
149+
it('distance 3', function () {
150+
fileA = 'foo/bar/a.java'
151+
fileB = 'lzz/b.java'
152+
const actual = getFileDistance(fileA, fileB)
153+
assert.strictEqual(actual, 3)
154+
})
155+
156+
it('distance 4', function () {
157+
fileA = 'foo/bar/a.java'
158+
fileB = 'lzz/baz/b.java'
159+
const actual = getFileDistance(fileA, fileB)
160+
assert.strictEqual(actual, 4)
161+
})
162+
163+
it('another distance 4', function () {
164+
fileA = 'foo/a.py'
165+
fileB = 'foo/foo/foo/foo/foo/b.py'
166+
const actual = getFileDistance(fileA, fileB)
167+
assert.strictEqual(actual, 4)
168+
})
169+
170+
it('distance 5', function () {
171+
fileA = 'foo/bar/a.java'
172+
fileB = 'lzz/baz/zoo/b.java'
173+
const actual = getFileDistance(fileA, fileB)
174+
assert.strictEqual(actual, 5)
175+
})
176+
177+
it('distance 6', function () {
178+
fileA = 'foo/zoo/a.java'
179+
fileB = 'bar/baz/bee/bww/b.java'
180+
const actual = getFileDistance(fileA, fileB)
181+
assert.strictEqual(actual, 6)
182+
})
183+
184+
it('backslash distance 1', function () {
185+
fileA = 'C:\\FOO\\BAR\\BAZ\\A.TXT'
186+
fileB = 'C:\\FOO\\BAR\\B.TXT'
187+
const actual = getFileDistance(fileA, fileB)
188+
assert.strictEqual(actual, 1)
189+
})
190+
191+
it('backslash distnace 3', function () {
192+
fileA = 'C:\\FOO\\BAR\\BAZ\\LOO\\WOW\\A.txt'
193+
fileB = 'C:\\FOO\\BAR\\B.txt'
194+
const actual = getFileDistance(fileA, fileB)
195+
assert.strictEqual(actual, 3)
196+
})
197+
})
122198
})

0 commit comments

Comments
 (0)