Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

vitest-report.xml
node_modules
dist
*.local
Expand All @@ -21,5 +22,3 @@ dist
*.njsproj
*.sln
*.sw?

vitest-report.xml
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ await commonUtil.sleep(1000); // Pauses execution for 1 second
const copied = await commonUtil.copyToClipboard("Hello, World!"); // true if successful
const encoded = commonUtil.encodeBase64("Hello 한글!"); // Base64 encoded string
const decoded = commonUtil.decodeBase64(encoded); // "Hello 한글!"
const debouncedFn = commonUtil.debounce(() => console.log("Called!"), 300); // Debounced function

// Search Query utilities
const queryParams = searchQueryUtil.getAllQuery(); // { key: ["value1", "value2"], id: "123" }
Expand Down Expand Up @@ -158,6 +159,7 @@ const cleaned = clearNullProperties({ a: 1, b: null, c: 3 });
- `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.
- `encodeBase64(str: string, options?: { convertSpecialChars?: boolean }): string` - Encodes a string to Base64 format with optional special character handling
- `decodeBase64(str: string, options?: { convertSpecialChars?: boolean }): string` - Decodes a Base64 string back to original text with optional special character handling
- `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

### SearchQueryUtil

Expand Down
41 changes: 41 additions & 0 deletions package/commonUtil/debounce/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe } from "vitest";
import { afterEach, expect, test, vi } from "vitest";
import debounce from "./index";

describe("debounce 유틸 함수 테스트", () => {
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});

test("함수가 지정된 시간 (300ms) 후에 실행되어야 한다.", () => {
const fn = vi.fn();
vi.useFakeTimers();
const debounceFn = debounce(fn, 300);
debounceFn();

expect(fn).not.toHaveBeenCalled();

vi.advanceTimersByTime(299);
expect(fn).not.toHaveBeenCalled();

vi.advanceTimersByTime(1);
expect(fn).toHaveBeenCalledTimes(1);
});

test("여러 번 호출했을 경우에는 일정 시간 후 하나의 이벤트만 실행되어야 한다.", () => {
const fn = vi.fn();
vi.useFakeTimers();
const debounced = debounce(fn, 300);

debounced({ name: "현우" });
vi.advanceTimersByTime(100);
debounced({ name: "승준" });
vi.advanceTimersByTime(100);
debounced({ name: "철수" });

vi.advanceTimersByTime(300);
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).toHaveBeenCalledWith({ name: "철수" });
});
});
10 changes: 10 additions & 0 deletions package/commonUtil/debounce/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default function debounce<T extends (...args: unknown[]) => void>(
fn: T,
delay: number = 300
) {
let timer: ReturnType<typeof setTimeout>;
return (...args: Parameters<T>) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}
1 change: 1 addition & 0 deletions package/commonUtil/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { default as sleep } from "./sleep";
export { default as copyToClipboard } from "./copyToClipboard";
export { default as encodeBase64 } from "./encodeBase64";
export { default as decodeBase64 } from "./decodeBase64";
export { default as debounce } from "./debounce";
Loading