-
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 #2 from LaunchPlatform/fuzzy-match
Fuzzy match
- Loading branch information
Showing
13 changed files
with
509 additions
and
44 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,93 @@ | ||
import { it, expect, describe } from "@jest/globals"; | ||
import { fuzzyMatch } from "../src/TransactionForm/fuzzyMatch"; | ||
import { MatchedText } from "../src/TransactionForm/PostingCandidateList"; | ||
|
||
describe.each([ | ||
[ | ||
"ABC", | ||
"abc", | ||
[ | ||
{ | ||
text: "ABC", | ||
matched: true, | ||
}, | ||
] as Array<MatchedText>, | ||
], | ||
[ | ||
"abc", | ||
"ABC", | ||
[ | ||
{ | ||
text: "abc", | ||
matched: true, | ||
}, | ||
] as Array<MatchedText>, | ||
], | ||
[ | ||
"aBcDeF", | ||
"ABcE", | ||
[ | ||
{ | ||
text: "aBc", | ||
matched: true, | ||
}, | ||
{ | ||
text: "D", | ||
matched: false, | ||
}, | ||
{ | ||
text: "e", | ||
matched: true, | ||
}, | ||
{ | ||
text: "F", | ||
matched: false, | ||
}, | ||
] as Array<MatchedText>, | ||
], | ||
[ | ||
"Assets:Bank", | ||
"ab", | ||
[ | ||
{ | ||
text: "A", | ||
matched: true, | ||
}, | ||
{ | ||
text: "ssets:", | ||
matched: false, | ||
}, | ||
{ | ||
text: "B", | ||
matched: true, | ||
}, | ||
{ | ||
text: "ank", | ||
matched: false, | ||
}, | ||
] as Array<MatchedText>, | ||
], | ||
[ | ||
"Assets:Bank", | ||
"a", | ||
[ | ||
{ | ||
text: "A", | ||
matched: true, | ||
}, | ||
{ | ||
text: "ssets:Bank", | ||
matched: false, | ||
}, | ||
] as Array<MatchedText>, | ||
], | ||
["Assets:Bank", "", null], | ||
["Assets:Bank", "xyz", null], | ||
["Assets:Bank", "aaa", null], | ||
])(".fuzzyMatch(%s, %s, %s)", (text, keyword, expected) => { | ||
it(`fuzzyMatch(${JSON.stringify(text)}, ${JSON.stringify( | ||
keyword | ||
)}, ${JSON.stringify(expected)})`, () => { | ||
expect(fuzzyMatch(text, keyword)).toEqual(expected); | ||
}); | ||
}); |
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,7 @@ | ||
module.exports = { | ||
presets: [ | ||
['@babel/preset-env', {targets: {node: 'current'}}], | ||
'@babel/preset-typescript', | ||
], | ||
}; | ||
|
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
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
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
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,43 @@ | ||
import { MatchedText } from "./PostingCandidateList"; | ||
|
||
export const fuzzyMatch = ( | ||
value: string, | ||
keyword: string | ||
): Array<MatchedText> | null => { | ||
const pieces: Array<MatchedText> = []; | ||
const chars = value.split(""); | ||
let i = 0; | ||
let j = 0; | ||
let chunks = []; | ||
let matched = false; | ||
let firstMatch = false; | ||
for (; i < chars.length; ++i) { | ||
const char = chars.at(i); | ||
if (char?.toLowerCase() === keyword.substring(j, j + 1).toLowerCase()) { | ||
j += 1; | ||
if (chunks.length > 0 && !matched) { | ||
pieces.push({ text: chunks.join(""), matched }); | ||
chunks = []; | ||
} | ||
matched = true; | ||
firstMatch = true; | ||
} else { | ||
if (chunks.length > 0 && matched) { | ||
pieces.push({ text: chunks.join(""), matched }); | ||
chunks = []; | ||
} | ||
matched = false; | ||
} | ||
chunks.push(char); | ||
} | ||
if (!firstMatch) { | ||
return null; | ||
} | ||
if (chunks.length > 0) { | ||
pieces.push({ text: chunks.join(""), matched }); | ||
} | ||
if (j < keyword.length) { | ||
return null; | ||
} | ||
return pieces; | ||
}; |
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.