Skip to content
Closed
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
24 changes: 24 additions & 0 deletions src/lib/array-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Array helper utilities
*/

export function chunk<T>(array: T[], size: number): T[][] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 CRITICAL

The chunk function will create an infinite loop if size is 0 or negative. Add a guard clause: if (size <= 0) throw new Error('Size must be positive') or return [array] to prevent this.

const chunks: T[][] = [];
for (let i = 0; i < array.length; i += size) {
chunks.push(array.slice(i, i + size));
}
return chunks;
}

export function unique<T>(array: T[]): T[] {
return [...new Set(array)];
}

export function shuffle<T>(array: T[]): T[] {
const result = [...array];
for (let i = result.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[result[i], result[j]] = [result[j], result[i]];
}
return result;
}
Loading