-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from nontangent/refactor/clean-code
Refactor/clean code
- Loading branch information
Showing
11 changed files
with
405 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { calculateDistance, convertByWords, parseFilePath, standarization } from './distance'; | ||
|
||
describe('standarization', () => { | ||
it('', () => { | ||
expect(standarization( | ||
'/libs/example/src/lib/domain/models/first-model.ts', | ||
'/libs/example/src/lib/domain/models/second-model.ts', | ||
)).toEqual([ | ||
'/0/1/2/3/4/5/6.7', | ||
'/0/1/2/3/4/5/8.7', | ||
]) | ||
}); | ||
}); | ||
|
||
describe('calculateDistance()', () => { | ||
it('', () => { | ||
expect(calculateDistance( | ||
'/libs/example/src/lib/domain/models/first-model.ts', | ||
'/libs/example/src/lib/domain/models/second-model.ts', | ||
)).toEqual(1) | ||
}); | ||
|
||
it('', () => { | ||
expect(calculateDistance( | ||
'/libs/example/src/lib/domain/models/first-model.component.ts', | ||
'/libs/example/src/lib/domain/models/second-model.module.ts', | ||
)).toEqual(2) | ||
}); | ||
}); | ||
|
||
describe('parseFilePath', () => { | ||
it('should parse file path', () => { | ||
expect(parseFilePath('/projects/app/src/app/_shared/components/example/example.component.ts')).toEqual([ | ||
'/', 'projects', '/', 'app', '/', 'src', '/', 'app', '/', '_shared', '/', | ||
'components', '/', 'example', '/', 'example', '.', 'component', '.', 'ts' | ||
]); | ||
}); | ||
}); | ||
|
||
describe('convertByWords', () => { | ||
xit('should convert from a string to a number', () => { | ||
expect(convertByWords('this/is/test/path', ['this', 'is', 'test', 'path'])).toBe('0/1/2/3'); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
export function standarization(str1: string, str2: string): [string, string] { | ||
const wordSet = new Set([str1, str2].map(path => parseFilePath(path)).flat()); | ||
wordSet.delete('.'); | ||
wordSet.delete('/'); | ||
return [convertByWords(str1, [...wordSet]), convertByWords(str2, [...wordSet])]; | ||
} | ||
|
||
export function calculateDistance(str1: string, str2: string): number { | ||
const [s1, s2] = standarization(str1, str2); | ||
return levenshteinDistance(s1, s2) + depthDistance(str1, str2); | ||
} | ||
|
||
const LANG_MAP = `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=[]{};:'",.<>/?|\\~\` `.split(''); | ||
|
||
export function convertByWords(str: string, words: string[]): string { | ||
return parseFilePath(str).map(c => ~words.indexOf(c) ? LANG_MAP[words.indexOf(c)] : c).join(''); | ||
} | ||
|
||
export function parseFilePath(filePath: string, characters = ['.', '/']): string[] { | ||
const result = []; | ||
let word = ''; | ||
for (let i = 0; i < filePath.length; i++) { | ||
const char = filePath[i]; | ||
if (characters.includes(char)) { | ||
if (word.length) result.push(word); | ||
result.push(char); | ||
word = ''; | ||
} else { | ||
word += char; | ||
} | ||
} | ||
result.push(word); | ||
return result; | ||
} | ||
|
||
function depthDistance(str1: string, str2: string): number { | ||
const depth1 = str1.split('/').length; | ||
const depth2 = str2.split('/').length; | ||
return Math.abs(depth1 - depth2); | ||
} | ||
|
||
|
||
function levenshteinDistance(str1: string, str2: string): number { | ||
const cost = (a: string, b: string): number => { | ||
return a === b ? 0 : 1; | ||
} | ||
|
||
const d = []; | ||
for (let i = 0; i <= str1.length; i++) { | ||
d[i] = []; | ||
d[i][0] = i; | ||
} | ||
for (let j = 0; j <= str2.length; j++) { | ||
d[0][j] = j; | ||
} | ||
|
||
for (let i = 1; i <= str1.length; i++) { | ||
for (let j = 1; j <= str2.length; j++) { | ||
d[i][j] = Math.min( | ||
d[i - 1][j] + 1, | ||
d[i][j - 1] + 1, | ||
d[i - 1][j - 1] + cost(str1[i - 1], str2[j - 1]) | ||
); | ||
} | ||
} | ||
|
||
return d[str1.length][str2.length]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,7 @@ | ||
|
||
export function parseFilePath(filePath: string, characters = ['.', '/']): string[] { | ||
const result = []; | ||
let word = ''; | ||
for (let i = 0; i < filePath.length; i++) { | ||
const char = filePath[i]; | ||
if (characters.includes(char)) { | ||
if (word.length) result.push(word); | ||
result.push(char); | ||
word = ''; | ||
} else { | ||
word += char; | ||
} | ||
} | ||
result.push(word); | ||
return result; | ||
} | ||
|
||
export const convertByWords = (str: string, words: string[]): string => { | ||
return parseFilePath(str).map(c => ~words.indexOf(c) ? words.indexOf(c) : c).join(''); | ||
} | ||
import { calculateDistance } from "./distance"; | ||
|
||
export function getEstimateSimilarFilePaths(path: string, files: string[]): string[] { | ||
const wordSet = new Set([path, ...files].map(path => parseFilePath(path)).flat()); | ||
wordSet.delete('.'); | ||
wordSet.delete('/'); | ||
const words = Array.from(wordSet); | ||
const results = files.map(file => { | ||
const distance = levenshteinDistance(convertByWords(path, words), convertByWords(file, words)) + depthDistance(path, file); | ||
return [distance, file] as [number, string]; | ||
}); | ||
const results = files.map(file => [calculateDistance(path, file), file] as [number, string]); | ||
const min = Math.min(...results.map(([distance]) => distance)); | ||
return results.filter(([distance]) => distance === min).map(([_, file]) => file); | ||
} | ||
|
||
function depthDistance(str1: string, str2: string): number { | ||
const depth1 = str1.split('/').length; | ||
const depth2 = str2.split('/').length; | ||
return Math.abs(depth1 - depth2); | ||
} | ||
|
||
|
||
function levenshteinDistance(str1: string, str2: string): number { | ||
const cost = (a: string, b: string): number => { | ||
return a === b ? 0 : 1; | ||
} | ||
|
||
const d = []; | ||
for (let i = 0; i <= str1.length; i++) { | ||
d[i] = []; | ||
d[i][0] = i; | ||
} | ||
for (let j = 0; j <= str2.length; j++) { | ||
d[0][j] = j; | ||
} | ||
|
||
for (let i = 1; i <= str1.length; i++) { | ||
for (let j = 1; j <= str2.length; j++) { | ||
d[i][j] = Math.min( | ||
d[i - 1][j] + 1, | ||
d[i][j - 1] + 1, | ||
d[i - 1][j - 1] + cost(str1[i - 1], str2[j - 1]) | ||
); | ||
} | ||
} | ||
|
||
return d[str1.length][str2.length]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.