Skip to content

Commit b7555f0

Browse files
thavelickclaude
andcommitted
Refactor search URL handling and fix edge cases
- Extract hardcoded fallback URL to FALLBACK_SEARCH_URL constant - Add buildSearchUrl() helper function to eliminate duplication - Fix processBang() to return fallback URL instead of null for incomplete bangs - Add test coverage for buildSearchUrl() and edge case scenarios - Add todo.md with remaining code review recommendations 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent b67defc commit b7555f0

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

public_html/search.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const FALLBACK_SEARCH_URL = "https://lite.duckduckgo.com/lite?q={{{s}}}&kl=us-en";
2+
13
const bangs = [
24
{
35
t: "al",
@@ -69,21 +71,25 @@ const bangs = [
6971
},
7072
];
7173

74+
function buildSearchUrl(urlTemplate, searchTerm) {
75+
return urlTemplate.replace(/{{{s}}}/g, encodeURIComponent(searchTerm));
76+
}
77+
7278
function processBang(query) {
7379
const trimmed = query.trim();
7480

7581
if (trimmed.startsWith("!")) {
7682
const spaceIndex = trimmed.indexOf(" ");
7783
if (spaceIndex === -1) {
78-
return null;
84+
return buildSearchUrl(FALLBACK_SEARCH_URL, trimmed);
7985
}
8086

8187
const bangTag = trimmed.substring(1, spaceIndex);
8288
const searchTerm = trimmed.substring(spaceIndex + 1).trim();
8389

8490
const bang = bangs.find((b) => b.t === bangTag);
8591
if (bang) {
86-
return bang.u.replace(/{{{s}}}/g, encodeURIComponent(searchTerm));
92+
return buildSearchUrl(bang.u, searchTerm);
8793
}
8894
}
8995
const bangMatch = trimmed.match(/^(.+)\s+(\w+)!$/);
@@ -93,10 +99,10 @@ function processBang(query) {
9399

94100
const bang = bangs.find((b) => b.t === bangTag);
95101
if (bang) {
96-
return bang.u.replace(/{{{s}}}/g, encodeURIComponent(searchTerm));
102+
return buildSearchUrl(bang.u, searchTerm);
97103
}
98104
}
99-
return `https://lite.duckduckgo.com/lite?q=${encodeURIComponent(trimmed)}&kl=us-en`;
105+
return FALLBACK_SEARCH_URL.replace(/{{{s}}}/g, encodeURIComponent(trimmed));
100106
}
101107

102108
function getQueryParam(key, windowObj = window) {
@@ -165,5 +171,6 @@ if (typeof module !== "undefined" && module.exports) {
165171
performSearch,
166172
setupUI,
167173
initialize,
174+
buildSearchUrl,
168175
};
169176
}

tests/search.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@ const {
44
performSearch,
55
setupUI,
66
initialize,
7+
buildSearchUrl,
78
} = require("../public_html/search.js");
89

10+
describe("buildSearchUrl", () => {
11+
test("replaces template placeholder with encoded search term", () => {
12+
const template = "https://example.com/search?q={{{s}}}";
13+
const result = buildSearchUrl(template, "hello world");
14+
expect(result).toBe("https://example.com/search?q=hello%20world");
15+
});
16+
});
17+
918
describe("getQueryParam", () => {
1019
const originalLocation = global.window?.location;
1120

@@ -76,6 +85,13 @@ describe("processBang", () => {
7685
"https://lite.duckduckgo.com/lite?q=!unknown%20search%20term&kl=us-en",
7786
);
7887
});
88+
89+
test("falls back to DuckDuckGo for bang without search term", () => {
90+
const result = processBang("!gh");
91+
expect(result).toBe(
92+
"https://lite.duckduckgo.com/lite?q=!gh&kl=us-en",
93+
);
94+
});
7995
});
8096

8197
describe("performSearch", () => {

todo.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Code Review Recommendations
2+
3+
## Code Quality Improvements
4+
3. Add error handling for missing DOM elements in `setupUI()`
5+
4. Use descriptive property names instead of `t` and `u` in bangs array
6+
5. Add bounds checking before accessing DOM elements
7+
8+
## Testing Enhancements
9+
7. Add test for DOM element not found scenarios in `setupUI()`
10+
11+
## Documentation Updates
12+
11. Document bang property names (`t` = trigger, `u` = URL) in code comments. Note: bangs should use only a-z characters.
13+
14+
## Optional Enhancements
15+
13. Consider adding input validation for extremely long queries

0 commit comments

Comments
 (0)