-
-
Save jlongster/d6580e1051d7505e8231edd79a29e185 to your computer and use it in GitHub Desktop.
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
const levenshtein = require('damerau-levenshtein'); | |
export function matchTransaction(name1, name2) { | |
// It's important that target is the longer of the two strings, | |
// since we'll try to do a fuzzy substring match of src into target | |
const target = scrub(name1.length > name2.length ? name1 : name2); | |
const src = scrub(name1.length > name2.length ? name2 : name1); | |
// Run a levenshtein distance on the src string and a substring of | |
// the target string that is about equal length of src. If it fails, | |
// keep walking through target up to each word boundary, and repeat | |
let searchStr = target; | |
while (true) { | |
const subStr = searchStr.slice(0, src.length + 2); | |
const result = levenshtein(src, subStr); | |
if (result.similarity > 0.6) { | |
return true; | |
} | |
const idx = searchStr.indexOf(' '); | |
if (idx === -1) { | |
break; | |
} | |
searchStr = searchStr.slice(idx + 1); | |
} | |
return false; | |
} |
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
function scrub(str) { | |
return str.replace(/[^a-zA-Z ]/g, '').toLowerCase(); | |
} |
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
expect(matchTransaction('Macsy wkout', 'macys weekout')).toBe(true); | |
expect(matchTransaction("Papa John's", 'papa john')).toBe(true); | |
expect( | |
matchTransaction("#001 fenn st Macy's is good 33333 EMX", 'macys good') | |
).toBe(true); | |
expect( | |
matchTransaction("#001 fenn st Macy's is good 33333 EMX", 'good place') | |
).toBe(false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment