Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add wildcard search #18

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});
});