mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 11:03:30 +01:00
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>
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import { enableValidators, disableValidators, isValidationEnabled } from '../src/index.js';
|
import { enableValidators, disableValidators, isValidationEnabled, normalizeArray } from '../src/index.js';
|
||||||
|
|
||||||
describe('validation', () => {
|
describe('validation', () => {
|
||||||
test('enables validation', () => {
|
test('enables validation', () => {
|
||||||
@@ -12,3 +12,19 @@ describe('validation', () => {
|
|||||||
expect(isValidationEnabled()).toBeFalsy();
|
expect(isValidationEnabled()).toBeFalsy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('normalizeArray', () => {
|
||||||
|
test('normalizes an array or array (when input is an array)', () => {
|
||||||
|
expect(normalizeArray([[1, 2, 3]])).toEqual([1, 2, 3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('normalizes an array (when input is rest parameter)', () => {
|
||||||
|
expect(normalizeArray([1, 2, 3])).toEqual([1, 2, 3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('always returns a clone', () => {
|
||||||
|
const arr = [1, 2, 3];
|
||||||
|
expect(normalizeArray([arr])).toEqual(arr);
|
||||||
|
expect(normalizeArray([arr])).not.toBe(arr);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ export class MentionableSelectMenuBuilder extends BaseSelectMenuBuilder<APIMenti
|
|||||||
) {
|
) {
|
||||||
const normalizedValues = normalizeArray(values);
|
const normalizedValues = normalizeArray(values);
|
||||||
optionsLengthValidator.parse(normalizedValues.length);
|
optionsLengthValidator.parse(normalizedValues.length);
|
||||||
this.data.default_values = normalizedValues.slice();
|
this.data.default_values = normalizedValues;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
* @param arr - The (possibly variadic) data to normalize
|
* @param arr - The (possibly variadic) data to normalize
|
||||||
*/
|
*/
|
||||||
export function normalizeArray<ItemType>(arr: RestOrArray<ItemType>): ItemType[] {
|
export function normalizeArray<ItemType>(arr: RestOrArray<ItemType>): ItemType[] {
|
||||||
if (Array.isArray(arr[0])) return arr[0];
|
if (Array.isArray(arr[0])) return [...arr[0]];
|
||||||
return arr as ItemType[];
|
return arr as ItemType[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user