Skip to content

Instantly share code, notes, and snippets.

@jlongster
Last active September 21, 2019 06:26
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jlongster/d6580e1051d7505e8231edd79a29e185 to your computer and use it in GitHub Desktop.
Save jlongster/d6580e1051d7505e8231edd79a29e185 to your computer and use it in GitHub Desktop.
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;
}
function scrub(str) {
return str.replace(/[^a-zA-Z ]/g, '').toLowerCase();
}
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