Remove old fuzzy search

pull/483/head
Zef Hemel 2023-07-27 17:05:07 +02:00
parent 804d87436c
commit 9dd7a5310b
2 changed files with 0 additions and 85 deletions

View File

@ -1,40 +0,0 @@
import { FilterOption } from "../types.ts";
import { assertEquals } from "../../test_deps.ts";
import { fuzzySearchAndSort } from "./fuzzy_search.ts";
Deno.test("testFuzzyFilter", () => {
const array: FilterOption[] = [
{ name: "My Company/Hank", orderId: -5 },
{ name: "My Company/Steve Co", orderId: -5 },
{ name: "Other/Steve", orderId: -7 },
{ name: "Steve", orderId: -3 },
];
// Prioritize match in last path part
const result = fuzzySearchAndSort(array, "Co");
assertEquals(result.length, 2);
assertEquals(result[0].name, "My Company/Steve Co");
// Support slash matches
const result2 = fuzzySearchAndSort(array, "Co/St");
assertEquals(result2.length, 1);
assertEquals(result2[0].name, "My Company/Steve Co");
// Find "St" in both, but pioritize based on orderId
const result3 = fuzzySearchAndSort(array, "St");
assertEquals(result3.length, 3);
assertEquals(result3[0].name, "Other/Steve");
const result4 = fuzzySearchAndSort(array, "Steve");
assertEquals(result4[0].name, "Steve");
// const result2 = fuzzySearchAndSort(array, "");
// console.log("Result 2", result2);
// assertEquals(result.length, 3);
// assertEquals(result[0].orderId, 1);
// assertEquals(result[1].name, "Jack");
// assertEquals(result[1].orderId, 2);
// assertEquals(result[2].name, "Jill");
// assertEquals(result[2].orderId, 3);
});

View File

@ -1,45 +0,0 @@
import { FilterOption } from "../types.ts";
export const fuzzySearchAndSort = (
arr: FilterOption[],
searchPhrase: string,
): FilterOption[] => {
// Prepare regular expression: escape special characters, add '.*' around each character
const safePhrase = searchPhrase.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // escape special characters
const searchRegex = new RegExp(Array.from(safePhrase).join(".*"), "i"); // 'i' makes it case-insensitive
// Fuzzy matching on name using the regular expression
const filtered = arr.filter((item) => searchRegex.test(item.name));
// Sorting by exact match, whether match is in part after '/', then by orderId
filtered.sort((a, b) => {
const aNamePart = a.name.includes("/")
? a.name.split("/").pop() || ""
: a.name;
const bNamePart = b.name.includes("/")
? b.name.split("/").pop() || ""
: b.name;
const aMatchInPart = searchRegex.test(aNamePart);
const bMatchInPart = searchRegex.test(bNamePart);
// Check for exact match
const aExactMatch = a.name.toLowerCase() === searchPhrase.toLowerCase();
const bExactMatch = b.name.toLowerCase() === searchPhrase.toLowerCase();
if (aExactMatch !== bExactMatch) {
// If one is an exact match and the other is not, prioritize the exact match
return aExactMatch ? -1 : 1;
} else if (aMatchInPart !== bMatchInPart) {
// If one matches in the part after '/' and the other doesn't, prioritize the one that does
return aMatchInPart ? -1 : 1;
} else {
// If both match in the same part of name, prioritize by orderId
const aOrder = a.orderId !== undefined ? a.orderId : Infinity;
const bOrder = b.orderId !== undefined ? b.orderId : Infinity;
return aOrder - bOrder;
}
});
return filtered;
};