Skip to content

Commit 5d2c01f

Browse files
authored
Merge pull request #58 from klmhyeonwoo/feature/util-workspace
feat: Added debounce utility functions
2 parents c05da01 + 446fec0 commit 5d2c01f

6 files changed

Lines changed: 55 additions & 331 deletions

File tree

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ yarn-error.log*
77
pnpm-debug.log*
88
lerna-debug.log*
99

10+
vitest-report.xml
1011
node_modules
1112
dist
1213
*.local
@@ -21,5 +22,3 @@ dist
2122
*.njsproj
2223
*.sln
2324
*.sw?
24-
25-
vitest-report.xml

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ await commonUtil.sleep(1000); // Pauses execution for 1 second
7474
const copied = await commonUtil.copyToClipboard("Hello, World!"); // true if successful
7575
const encoded = commonUtil.encodeBase64("Hello 한글!"); // Base64 encoded string
7676
const decoded = commonUtil.decodeBase64(encoded); // "Hello 한글!"
77+
const debouncedFn = commonUtil.debounce(() => console.log("Called!"), 300); // Debounced function
7778

7879
// Search Query utilities
7980
const queryParams = searchQueryUtil.getAllQuery(); // { key: ["value1", "value2"], id: "123" }
@@ -158,6 +159,7 @@ const cleaned = clearNullProperties({ a: 1, b: null, c: 3 });
158159
- `copyToClipboard(text: string): Promise<boolean>` - Copies text to the user's clipboard. Uses modern Clipboard API with fallback to legacy execCommand method. Returns true if successful, false if failed.
159160
- `encodeBase64(str: string, options?: { convertSpecialChars?: boolean }): string` - Encodes a string to Base64 format with optional special character handling
160161
- `decodeBase64(str: string, options?: { convertSpecialChars?: boolean }): string` - Decodes a Base64 string back to original text with optional special character handling
162+
- `debounce<T>(fn: T, delay?: number): (...args: Parameters<T>) => void` - Creates a debounced function that delays execution until after a specified delay (default 300ms) has passed since its last invocation
161163

162164
### SearchQueryUtil
163165

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe } from "vitest";
2+
import { afterEach, expect, test, vi } from "vitest";
3+
import debounce from "./index";
4+
5+
describe("debounce 유틸 함수 테스트", () => {
6+
afterEach(() => {
7+
vi.useRealTimers();
8+
vi.restoreAllMocks();
9+
});
10+
11+
test("함수가 지정된 시간 (300ms) 후에 실행되어야 한다.", () => {
12+
const fn = vi.fn();
13+
vi.useFakeTimers();
14+
const debounceFn = debounce(fn, 300);
15+
debounceFn();
16+
17+
expect(fn).not.toHaveBeenCalled();
18+
19+
vi.advanceTimersByTime(299);
20+
expect(fn).not.toHaveBeenCalled();
21+
22+
vi.advanceTimersByTime(1);
23+
expect(fn).toHaveBeenCalledTimes(1);
24+
});
25+
26+
test("여러 번 호출했을 경우에는 일정 시간 후 하나의 이벤트만 실행되어야 한다.", () => {
27+
const fn = vi.fn();
28+
vi.useFakeTimers();
29+
const debounced = debounce(fn, 300);
30+
31+
debounced({ name: "현우" });
32+
vi.advanceTimersByTime(100);
33+
debounced({ name: "승준" });
34+
vi.advanceTimersByTime(100);
35+
debounced({ name: "철수" });
36+
37+
vi.advanceTimersByTime(300);
38+
expect(fn).toHaveBeenCalledTimes(1);
39+
expect(fn).toHaveBeenCalledWith({ name: "철수" });
40+
});
41+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default function debounce<T extends (...args: unknown[]) => void>(
2+
fn: T,
3+
delay: number = 300
4+
) {
5+
let timer: ReturnType<typeof setTimeout>;
6+
return (...args: Parameters<T>) => {
7+
clearTimeout(timer);
8+
timer = setTimeout(() => fn(...args), delay);
9+
};
10+
}

package/commonUtil/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export { default as sleep } from "./sleep";
44
export { default as copyToClipboard } from "./copyToClipboard";
55
export { default as encodeBase64 } from "./encodeBase64";
66
export { default as decodeBase64 } from "./decodeBase64";
7+
export { default as debounce } from "./debounce";

0 commit comments

Comments
 (0)