Skip to content

Commit

Permalink
Merge pull request #18 from author-more/feat/improve-search-wildcard
Browse files Browse the repository at this point in the history
feat: add wildcard search
  • Loading branch information
Belar authored Sep 29, 2024
2 parents 76b1798 + af14ac9 commit af96231
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import LinkTag from "./LinkTag";
import { toSortedBy } from "./sort";
import { Bug, ChevronDown, ChevronRight, Lightbulb } from "lucide-react";
import { sendMessage } from "./window";
import { filterByPhrase } from "./search";

function App() {
const url = new URL(window.location.href);
Expand Down Expand Up @@ -103,9 +104,7 @@ function App() {
const { library } = metadata;

return Object.entries(icons)
.filter(([name]) => {
return name.toLowerCase().includes(searchPhrase.toLowerCase());
})
.filter(([name]) => filterByPhrase(searchPhrase, name))
.map(
([
name,
Expand Down
19 changes: 19 additions & 0 deletions src/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function filterByPhrase(phrase: string, content: string) {
const phraseNormalised = phrase?.toLowerCase();
const contentNormalised = content?.toLowerCase();

const pattern = phraseNormalised
.split(" ")
.map(
(subPhrase) =>
`\\b(${subPhrase
.split("")
// Escape RegExp's special characters
.map((char) => char.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&"))
.join("\\w*")}\\w*)\\b`,
)
.join(".*");
const regexp = new RegExp(pattern, "gi");

return regexp.test(contentNormalised);
}
21 changes: 20 additions & 1 deletion tests/search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test.describe("search", () => {
await expect(input).toBeVisible();
});

test("filters icons by phrase", async ({ page }) => {
test("filters icons by phrase - complete", async ({ page }) => {
const iconSetToggleButton = page.getByRole("button", {
name: /Show Lucide icon set/,
});
Expand All @@ -28,4 +28,23 @@ test.describe("search", () => {
expect(await iconButtons.count()).toEqual(1);
expect(await iconButtons.textContent()).toContain("Insert icon: pickaxe");
});

test("filters icons by phrase - wildcard", async ({ page }) => {
const iconSetToggleButton = page.getByRole("button", {
name: /Show Lucide icon set/,
});
await iconSetToggleButton.click();

const iconButtons = page.getByRole("button", { name: /(Insert icon:).*/ });
await iconButtons.first().waitFor({ state: "visible" });

expect(await iconButtons.count()).toBeGreaterThan(1);

const input = page.getByLabel("Search icon");
await input.fill("pkxe");

await iconButtons.first().waitFor({ state: "visible" });
expect(await iconButtons.count()).toEqual(1);
expect(await iconButtons.textContent()).toContain("Insert icon: pickaxe");
});
});

0 comments on commit af96231

Please sign in to comment.