Skip to content
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

fix: don't mutate user provided array #10014

Merged
merged 6 commits into from Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/builders/__tests__/util.test.ts
@@ -1,5 +1,5 @@
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', () => {
test('enables validation', () => {
Expand All @@ -12,3 +12,19 @@ describe('validation', () => {
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', () => {
vladfrangu marked this conversation as resolved.
Show resolved Hide resolved
const arr = [1, 2, 3];
expect(normalizeArray([arr])).toEqual(arr);
expect(normalizeArray([arr])).not.toBe(arr);
Jiralite marked this conversation as resolved.
Show resolved Hide resolved
});
});
2 changes: 1 addition & 1 deletion packages/builders/src/util/normalizeArray.ts
Expand Up @@ -5,7 +5,7 @@
* @param arr - The (possibly variadic) data to normalize
*/
export function normalizeArray<ItemType>(arr: RestOrArray<ItemType>): ItemType[] {
if (Array.isArray(arr[0])) return arr[0];
if (Array.isArray(arr[0])) return [...arr[0]];
vladfrangu marked this conversation as resolved.
Show resolved Hide resolved
return arr as ItemType[];
}

Expand Down