Skip to content

Commit 8ee261f

Browse files
committed
remove custom implementatin of levenshtein
1 parent cd54847 commit 8ee261f

File tree

3 files changed

+3
-75
lines changed

3 files changed

+3
-75
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"aria-query": "^4.2.2",
4545
"chalk": "^4.1.0",
4646
"dom-accessibility-api": "^0.5.4",
47+
"leven": "^3.1.0",
4748
"lz-string": "^1.4.4",
4849
"pretty-format": "^26.6.2"
4950
},

src/__tests__/close-matches.js

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,6 @@
1-
import {
2-
calculateLevenshteinDistance,
3-
getCloseMatchesByAttribute,
4-
} from '../close-matches'
1+
import {getCloseMatchesByAttribute} from '../close-matches'
52
import {render} from './helpers/test-utils'
63

7-
describe('calculateLevenshteinDistance', () => {
8-
test.each([
9-
['', '', 0],
10-
['hello', 'hello', 0],
11-
['greeting', 'greeting', 0],
12-
['react testing library', 'react testing library', 0],
13-
['hello', 'hellow', 1],
14-
['greetimg', 'greeting', 1],
15-
['submit', 'sbmit', 1],
16-
['cance', 'cancel', 1],
17-
['doug', 'dog', 1],
18-
['dogs and cats', 'dogs and cat', 1],
19-
['uncool-div', '12cool-div', 2],
20-
['dogs and cats', 'dogs, cats', 4],
21-
['greeting', 'greetings traveler', 10],
22-
['react testing library', '', 21],
23-
['react testing library', 'y', 20],
24-
['react testing library', 'ty', 19],
25-
['react testing library', 'tary', 17],
26-
['react testing library', 'trary', 16],
27-
['react testing library', 'tlibrary', 13],
28-
['react testing library', 'react testing', 8],
29-
['library', 'testing', 7],
30-
['react library', 'react testing', 7],
31-
[
32-
'The more your tests resemble the way your software is used, the more confidence they can give you.',
33-
'The less your tests resemble the way your software is used, the less confidence they can give you.',
34-
8,
35-
],
36-
])('distance between "%s" and "%s" is %i', (text1, text2, expected) => {
37-
expect(calculateLevenshteinDistance(text1, text2)).toBe(expected)
38-
})
39-
})
40-
414
describe('getCloseMatchesByAttribute', () => {
425
test('should return all closest matches', () => {
436
const {container} = render(`

src/close-matches.js

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,5 @@
11
import {makeNormalizer} from './matches'
2-
3-
const initializeDpTable = (rows, columns) => {
4-
const dp = Array(rows + 1)
5-
.fill()
6-
.map(() => Array(columns + 1).fill())
7-
8-
// fill rows
9-
for (let i = 0; i <= rows; i++) {
10-
dp[i][0] = i
11-
}
12-
13-
// fill columns
14-
for (let i = 0; i <= columns; i++) {
15-
dp[0][i] = i
16-
}
17-
return dp
18-
}
19-
20-
export const calculateLevenshteinDistance = (text1, text2) => {
21-
const dp = initializeDpTable(text1.length, text2.length)
22-
23-
for (let row = 1; row < dp.length; row++) {
24-
for (let column = 1; column < dp[row].length; column++) {
25-
if (text1[row - 1] === text2[column - 1]) {
26-
dp[row][column] = dp[row - 1][column - 1]
27-
} else {
28-
dp[row][column] =
29-
Math.min(
30-
dp[row - 1][column - 1],
31-
dp[row][column - 1],
32-
dp[row - 1][column],
33-
) + 1
34-
}
35-
}
36-
}
37-
return dp[text1.length][text2.length]
38-
}
2+
import calculateLevenshteinDistance from 'leven'
393

404
const MAX_LEVENSHTEIN_DISTANCE = 4
415

0 commit comments

Comments
 (0)