Skip to content

Commit

Permalink
remove custom implementatin of levenshtein
Browse files Browse the repository at this point in the history
  • Loading branch information
dougbacelar committed Dec 5, 2020
1 parent cd54847 commit 8ee261f
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 75 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"aria-query": "^4.2.2",
"chalk": "^4.1.0",
"dom-accessibility-api": "^0.5.4",
"leven": "^3.1.0",
"lz-string": "^1.4.4",
"pretty-format": "^26.6.2"
},
Expand Down
39 changes: 1 addition & 38 deletions src/__tests__/close-matches.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,6 @@
import {
calculateLevenshteinDistance,
getCloseMatchesByAttribute,
} from '../close-matches'
import {getCloseMatchesByAttribute} from '../close-matches'
import {render} from './helpers/test-utils'

describe('calculateLevenshteinDistance', () => {
test.each([
['', '', 0],
['hello', 'hello', 0],
['greeting', 'greeting', 0],
['react testing library', 'react testing library', 0],
['hello', 'hellow', 1],
['greetimg', 'greeting', 1],
['submit', 'sbmit', 1],
['cance', 'cancel', 1],
['doug', 'dog', 1],
['dogs and cats', 'dogs and cat', 1],
['uncool-div', '12cool-div', 2],
['dogs and cats', 'dogs, cats', 4],
['greeting', 'greetings traveler', 10],
['react testing library', '', 21],
['react testing library', 'y', 20],
['react testing library', 'ty', 19],
['react testing library', 'tary', 17],
['react testing library', 'trary', 16],
['react testing library', 'tlibrary', 13],
['react testing library', 'react testing', 8],
['library', 'testing', 7],
['react library', 'react testing', 7],
[
'The more your tests resemble the way your software is used, the more confidence they can give you.',
'The less your tests resemble the way your software is used, the less confidence they can give you.',
8,
],
])('distance between "%s" and "%s" is %i', (text1, text2, expected) => {
expect(calculateLevenshteinDistance(text1, text2)).toBe(expected)
})
})

describe('getCloseMatchesByAttribute', () => {
test('should return all closest matches', () => {
const {container} = render(`
Expand Down
38 changes: 1 addition & 37 deletions src/close-matches.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
import {makeNormalizer} from './matches'

const initializeDpTable = (rows, columns) => {
const dp = Array(rows + 1)
.fill()
.map(() => Array(columns + 1).fill())

// fill rows
for (let i = 0; i <= rows; i++) {
dp[i][0] = i
}

// fill columns
for (let i = 0; i <= columns; i++) {
dp[0][i] = i
}
return dp
}

export const calculateLevenshteinDistance = (text1, text2) => {
const dp = initializeDpTable(text1.length, text2.length)

for (let row = 1; row < dp.length; row++) {
for (let column = 1; column < dp[row].length; column++) {
if (text1[row - 1] === text2[column - 1]) {
dp[row][column] = dp[row - 1][column - 1]
} else {
dp[row][column] =
Math.min(
dp[row - 1][column - 1],
dp[row][column - 1],
dp[row - 1][column],
) + 1
}
}
}
return dp[text1.length][text2.length]
}
import calculateLevenshteinDistance from 'leven'

const MAX_LEVENSHTEIN_DISTANCE = 4

Expand Down

0 comments on commit 8ee261f

Please sign in to comment.