Skip to content

Commit f3c4e9b

Browse files
authored
Merge pull request #38 from klmhyeonwoo/feature/33
feat: Added getAllQuery utility function
2 parents 0576f59 + f6e0793 commit f3c4e9b

File tree

7 files changed

+367
-6
lines changed

7 files changed

+367
-6
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"@vitest/coverage-v8": "^3.2.4",
5050
"typescript": "~5.8.3",
5151
"vite": "^7.1.2",
52-
"vitest": "^3.2.4"
52+
"vitest": "^3.2.4",
53+
"jsdom": "^26.1.0"
5354
},
5455
"files": [
5556
"dist"

package/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * as cookieUtil from "./cookieUtil";
44
export * as numberUtil from "./numberUtil";
55
export * as validationUtil from "./validationUtil";
66
export * as commonUtil from "./commonUtil";
7+
export * as searchQueryUtil from "./searchQueryUtil";
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, test, vi, afterEach } from "vitest";
2+
import getAllQuery from ".";
3+
4+
describe("getAll 유틸 함수 테스트", () => {
5+
afterEach(() => {
6+
vi.unstubAllGlobals();
7+
});
8+
9+
test("여러 개의 동일한 키가 있는 쿼리 스트링을 올바르게 처리해야 함", () => {
10+
vi.stubGlobal("location", {
11+
search: "?key=value1&key=value2&key=value3",
12+
});
13+
14+
const result = getAllQuery();
15+
expect(result).toEqual({ key: ["value1", "value2", "value3"] });
16+
});
17+
18+
test("여러 키-값 쌍이 있는 쿼리 스트링을 올바르게 처리해야 함", () => {
19+
vi.stubGlobal("location", {
20+
search: "?name=John&age=30&city=NewYork",
21+
});
22+
23+
const result = getAllQuery();
24+
expect(result).toEqual({ name: "John", age: "30", city: "NewYork" });
25+
});
26+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default function getAllQuery(): Record<string, string | string[]> {
2+
if (window && typeof window.URLSearchParams === "undefined") {
3+
throw new Error("URLSearchParams is not supported in this environment.");
4+
}
5+
6+
const params = new URLSearchParams(window.location.search);
7+
const result: { [key: string]: string | string[] } = {};
8+
9+
for (const [key, value] of params.entries()) {
10+
if (result[key]) {
11+
if (Array.isArray(result[key])) {
12+
result[key].push(value);
13+
} else {
14+
result[key] = [result[key], value];
15+
}
16+
} else {
17+
result[key] = value;
18+
}
19+
}
20+
21+
return result;
22+
}

package/searchQueryUtil/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as getAllQuery } from "./getAllQuery";

0 commit comments

Comments
 (0)