Skip to content

Commit ce0d978

Browse files
committed
feat: #33 Added getAllQuery utility function
1 parent 0b4df1a commit ce0d978

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

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(): object {
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)