Skip to content

Commit 02245f3

Browse files
committed
Add helper.ts
1 parent ef04b8d commit 02245f3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

helper.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export function formatString(input: string): string {
2+
return input.trim().toLowerCase().replace(/\s+/g, '-');
3+
}
4+
5+
export function calculateSum(numbers: number[]): number {
6+
return numbers.reduce((acc, num) => acc + num, 0);
7+
}
8+
9+
export function isValidEmail(email: string): boolean {
10+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
11+
return emailRegex.test(email);
12+
}
13+
14+
export function debounce<T extends (...args: any[]) => any>(
15+
func: T,
16+
delay: number
17+
): (...args: Parameters<T>) => void {
18+
let timeoutId: NodeJS.Timeout;
19+
return (...args: Parameters<T>) => {
20+
clearTimeout(timeoutId);
21+
timeoutId = setTimeout(() => func(...args), delay);
22+
};
23+
}
24+
25+
export function chunk<T>(array: T[], size: number): T[][] {
26+
const chunks: T[][] = [];
27+
for (let i = 0; i < array.length; i += size) {
28+
chunks.push(array.slice(i, i + size));
29+
}
30+
return chunks;
31+
}

0 commit comments

Comments
 (0)