Generate Utility Function
Description: $ARGUMENTS
- Check if a similar utility already exists:
grep -rn "export.*function\|export const" src/utils/ src/lib/ src/helpers/ 2>/dev/null | head -20
- If a similar one exists, consider extending it instead of creating a new one.
- Implement the function:
- Pure function (no side effects) where possible
- Full TypeScript generics/types
- Handle edge cases: null, undefined, empty arrays/strings, negative numbers
- Performance-conscious for likely large inputs
- Write unit tests covering:
- Normal usage
- Edge cases
- Type narrowing behavior
- Place in the appropriate utils file or create
src/utils/<name>.ts.
- Add a usage example in JSDoc.
Example output:
/**
* Chunks an array into smaller arrays of the specified size.
* @example chunk([1,2,3,4,5], 2) // [[1,2],[3,4],[5]]
*/
export function chunk<T>(arr: T[], size: number): T[][] {
if (size <= 0) return []
return Array.from({ length: Math.ceil(arr.length / size) }, (_, i) =>
arr.slice(i * size, i * size + size)
)
}