Skip to content

Commit af96231

Browse files
authored
Merge pull request #18 from author-more/feat/improve-search-wildcard
feat: add wildcard search
2 parents 76b1798 + af14ac9 commit af96231

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/App.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import LinkTag from "./LinkTag";
1818
import { toSortedBy } from "./sort";
1919
import { Bug, ChevronDown, ChevronRight, Lightbulb } from "lucide-react";
2020
import { sendMessage } from "./window";
21+
import { filterByPhrase } from "./search";
2122

2223
function App() {
2324
const url = new URL(window.location.href);
@@ -103,9 +104,7 @@ function App() {
103104
const { library } = metadata;
104105

105106
return Object.entries(icons)
106-
.filter(([name]) => {
107-
return name.toLowerCase().includes(searchPhrase.toLowerCase());
108-
})
107+
.filter(([name]) => filterByPhrase(searchPhrase, name))
109108
.map(
110109
([
111110
name,

src/search.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export function filterByPhrase(phrase: string, content: string) {
2+
const phraseNormalised = phrase?.toLowerCase();
3+
const contentNormalised = content?.toLowerCase();
4+
5+
const pattern = phraseNormalised
6+
.split(" ")
7+
.map(
8+
(subPhrase) =>
9+
`\\b(${subPhrase
10+
.split("")
11+
// Escape RegExp's special characters
12+
.map((char) => char.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&"))
13+
.join("\\w*")}\\w*)\\b`,
14+
)
15+
.join(".*");
16+
const regexp = new RegExp(pattern, "gi");
17+
18+
return regexp.test(contentNormalised);
19+
}

tests/search.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test.describe("search", () => {
1010
await expect(input).toBeVisible();
1111
});
1212

13-
test("filters icons by phrase", async ({ page }) => {
13+
test("filters icons by phrase - complete", async ({ page }) => {
1414
const iconSetToggleButton = page.getByRole("button", {
1515
name: /Show Lucide icon set/,
1616
});
@@ -28,4 +28,23 @@ test.describe("search", () => {
2828
expect(await iconButtons.count()).toEqual(1);
2929
expect(await iconButtons.textContent()).toContain("Insert icon: pickaxe");
3030
});
31+
32+
test("filters icons by phrase - wildcard", async ({ page }) => {
33+
const iconSetToggleButton = page.getByRole("button", {
34+
name: /Show Lucide icon set/,
35+
});
36+
await iconSetToggleButton.click();
37+
38+
const iconButtons = page.getByRole("button", { name: /(Insert icon:).*/ });
39+
await iconButtons.first().waitFor({ state: "visible" });
40+
41+
expect(await iconButtons.count()).toBeGreaterThan(1);
42+
43+
const input = page.getByLabel("Search icon");
44+
await input.fill("pkxe");
45+
46+
await iconButtons.first().waitFor({ state: "visible" });
47+
expect(await iconButtons.count()).toEqual(1);
48+
expect(await iconButtons.textContent()).toContain("Insert icon: pickaxe");
49+
});
3150
});

0 commit comments

Comments
 (0)