-
Notifications
You must be signed in to change notification settings - Fork 5
Add helper.ts #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add helper.ts #75
Conversation
WalkthroughA new TypeScript helper module has been introduced, providing five utility functions: string formatting, array summation, email validation, function debouncing, and array chunking. All functions are exported for use in other modules. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant HelperModule
Caller->>HelperModule: formatString(input)
HelperModule-->>Caller: formatted string
Caller->>HelperModule: calculateSum(numbers)
HelperModule-->>Caller: sum
Caller->>HelperModule: isValidEmail(email)
HelperModule-->>Caller: boolean
Caller->>HelperModule: debounce(func, delay)
HelperModule-->>Caller: debounced function
Caller->>HelperModule: chunk(array, size)
HelperModule-->>Caller: array of chunks
Estimated code review effort🎯 2 (Simple) | ⏱️ ~7 minutes Poem
Pre-Merge Checks (3 warnings)❌ Failed Checks (3 warnings)
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (1)
helper.ts (1)
9-12
: Consider strengthening email validation.The current regex is basic and may not catch all edge cases. Consider using a more robust validation approach or a dedicated library for production use.
For a more comprehensive email validation:
export function isValidEmail(email: string): boolean { - const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; return emailRegex.test(email); }Or consider using a validation library like
validator.js
for more robust email validation.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
helper.ts
(1 hunks)
🧰 Additional context used
🔍 MCP Research (1 server)
Notion:
- The page titled "Add TypeScript helper functions" states that helper functions need to be added in a file called helper.ts, but it specifies that the file should NOT include the function calculateSum because this function already exists elsewhere in the codebase. (Source: https://www.notion.so/23496e76cda180cfac92c22f78edd65b)
🔇 Additional comments (1)
helper.ts (1)
1-3
: LGTM! Clean string formatting implementation.The function correctly handles trimming, case conversion, and whitespace normalization with a concise implementation.
export function calculateSum(numbers: number[]): number { | ||
return numbers.reduce((acc, num) => acc + num, 0); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove calculateSum function - organizational constraint violation.
According to the organizational documentation, the calculateSum
function should NOT be included in this file because it already exists elsewhere in the codebase.
-export function calculateSum(numbers: number[]): number {
- return numbers.reduce((acc, num) => acc + num, 0);
-}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
export function calculateSum(numbers: number[]): number { | |
return numbers.reduce((acc, num) => acc + num, 0); | |
} |
🤖 Prompt for AI Agents
In helper.ts around lines 5 to 7, remove the entire calculateSum function
because it violates organizational constraints by duplicating functionality that
already exists elsewhere in the codebase. Simply delete this function from the
file to comply with the guidelines.
export function debounce<T extends (...args: any[]) => any>( | ||
func: T, | ||
delay: number | ||
): (...args: Parameters<T>) => void { | ||
let timeoutId: NodeJS.Timeout; | ||
return (...args: Parameters<T>) => { | ||
clearTimeout(timeoutId); | ||
timeoutId = setTimeout(() => func(...args), delay); | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add input validation for debounce function.
The function should validate that delay
is a non-negative number to prevent unexpected behavior.
export function debounce<T extends (...args: any[]) => any>(
func: T,
delay: number
): (...args: Parameters<T>) => void {
+ if (delay < 0) {
+ throw new Error('Delay must be non-negative');
+ }
let timeoutId: NodeJS.Timeout;
return (...args: Parameters<T>) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => func(...args), delay);
};
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
export function debounce<T extends (...args: any[]) => any>( | |
func: T, | |
delay: number | |
): (...args: Parameters<T>) => void { | |
let timeoutId: NodeJS.Timeout; | |
return (...args: Parameters<T>) => { | |
clearTimeout(timeoutId); | |
timeoutId = setTimeout(() => func(...args), delay); | |
}; | |
} | |
export function debounce<T extends (...args: any[]) => any>( | |
func: T, | |
delay: number | |
): (...args: Parameters<T>) => void { | |
if (delay < 0) { | |
throw new Error('Delay must be non-negative'); | |
} | |
let timeoutId: NodeJS.Timeout; | |
return (...args: Parameters<T>) => { | |
clearTimeout(timeoutId); | |
timeoutId = setTimeout(() => func(...args), delay); | |
}; | |
} |
🤖 Prompt for AI Agents
In helper.ts around lines 14 to 23, the debounce function lacks validation for
the delay parameter. Add a check at the start of the function to ensure delay is
a non-negative number, and throw an error or handle invalid input appropriately
to prevent unexpected behavior.
export function chunk<T>(array: T[], size: number): T[][] { | ||
const chunks: T[][] = []; | ||
for (let i = 0; i < array.length; i += size) { | ||
chunks.push(array.slice(i, i + size)); | ||
} | ||
return chunks; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add input validation for chunk function.
The function should validate that size
is a positive integer to prevent infinite loops or unexpected behavior.
export function chunk<T>(array: T[], size: number): T[][] {
+ if (size <= 0 || !Number.isInteger(size)) {
+ throw new Error('Size must be a positive integer');
+ }
const chunks: T[][] = [];
for (let i = 0; i < array.length; i += size) {
chunks.push(array.slice(i, i + size));
}
return chunks;
}
🤖 Prompt for AI Agents
In helper.ts around lines 25 to 31, the chunk function lacks validation for the
size parameter, which can cause infinite loops or unexpected behavior if size is
not a positive integer. Add input validation at the start of the function to
check if size is a positive integer, and throw an error or handle invalid input
appropriately to prevent misuse.
Summary by CodeRabbit