Files
discord.js/packages/builders/src/util/normalizeArray.ts
Parbez 7ea3638dbc fix: don't mutate user provided array (#10014)
* fix(builders): don't mutate user provided array

* test: add normalize array tests

* chore: revert vscode autochange

* Update util.test.ts

* refactor: remove unnecessary clone

---------

Co-authored-by: Vlad Frangu <me@vladfrangu.dev>
Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
Co-authored-by: Almeida <github@almeidx.dev>
2024-04-30 20:46:21 +00:00

20 lines
687 B
TypeScript

/**
* Normalizes data that is a rest parameter or an array into an array with a depth of 1.
*
* @typeParam ItemType - The data that must satisfy {@link RestOrArray}.
* @param arr - The (possibly variadic) data to normalize
*/
export function normalizeArray<ItemType>(arr: RestOrArray<ItemType>): ItemType[] {
if (Array.isArray(arr[0])) return [...arr[0]];
return arr as ItemType[];
}
/**
* Represents data that may be an array or came from a rest parameter.
*
* @remarks
* This type is used throughout builders to ensure both an array and variadic arguments
* may be used. It is normalized with {@link normalizeArray}.
*/
export type RestOrArray<Type> = Type[] | [Type[]];