From dc06cd692af64d32a196cf308faba5cb9bf76c3f Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Thu, 20 Jun 2024 13:13:38 +0200 Subject: [PATCH 1/3] chore: remove arrayElement(s) js only error --- src/modules/helpers/index.ts | 14 -------------- test/modules/helpers.spec.ts | 18 ------------------ 2 files changed, 32 deletions(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index f62d90868fb..69a9995c120 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -844,13 +844,6 @@ export class SimpleHelpersModule extends SimpleModuleBase { * @since 6.3.0 */ arrayElement(array: ReadonlyArray): T { - // TODO @xDivisionByZerox 2023-04-20: Remove in v9 - if (array == null) { - throw new FakerError( - 'Calling `faker.helpers.arrayElement()` without arguments is no longer supported.' - ); - } - if (array.length === 0) { throw new FakerError('Cannot get value from empty dataset.'); } @@ -954,13 +947,6 @@ export class SimpleHelpersModule extends SimpleModuleBase { max: number; } ): T[] { - // TODO @xDivisionByZerox 2023-04-20: Remove in v9 - if (array == null) { - throw new FakerError( - 'Calling `faker.helpers.arrayElements()` without arguments is no longer supported.' - ); - } - if (array.length === 0) { return []; } diff --git a/test/modules/helpers.spec.ts b/test/modules/helpers.spec.ts index 09f264679d3..3582799fff4 100644 --- a/test/modules/helpers.spec.ts +++ b/test/modules/helpers.spec.ts @@ -190,15 +190,6 @@ describe('helpers', () => { expect(actual).toBe('hello'); }); - it('should throw with no arguments', () => { - // @ts-expect-error: `arrayElement` without arguments is not supported in TypeScript - expect(() => faker.helpers.arrayElement()).toThrow( - new FakerError( - 'Calling `faker.helpers.arrayElement()` without arguments is no longer supported.' - ) - ); - }); - it('should throw on an empty array', () => { expect(() => faker.helpers.arrayElement([])).toThrow( new FakerError('Cannot get value from empty dataset.') @@ -468,15 +459,6 @@ describe('helpers', () => { } ); - it('should throw with no arguments', () => { - // @ts-expect-error: `arrayElements` without arguments is not supported in TypeScript - expect(() => faker.helpers.arrayElements()).toThrow( - new FakerError( - 'Calling `faker.helpers.arrayElements()` without arguments is no longer supported.' - ) - ); - }); - describe('should not throw on an array with nullish elements', () => { it.each(['', 0, undefined, null, false])('%s', (nullishValue) => { expect(() => From c161d8db5b7d36b71e5a3a35e346967f7176414a Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Thu, 20 Jun 2024 15:01:44 +0200 Subject: [PATCH 2/3] docs: write migration guide --- docs/guide/upgrading.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/guide/upgrading.md b/docs/guide/upgrading.md index b562cdb096e..a8b0dd2003d 100644 --- a/docs/guide/upgrading.md +++ b/docs/guide/upgrading.md @@ -349,6 +349,24 @@ const city = enforcer.enforce(faker.location.city, { `enforce-unique` does not directly support the `store` option previously available in `faker.helpers.unique`. If you were previously using this parameter, check the [documentation](https://www.npmjs.com/package/enforce-unique). If you need to reset the store, you can call the `reset()` method on the `UniqueEnforcer` instance. ::: +#### `faker.helpers.arrayElement` and `faker.helpers.arrayElements` + +The following only affects usage in Javascript, as in Typescript this usage would already throw a compile-time error. + +Previously, the `arrayElement` and `arrayElements` methods would throw a dedicated error, when called without arguments. + +```ts +faker.helpers.arrayElement(undefined); // FakerError: Calling `faker.helpers.arrayElement()` without arguments is no longer supported. +``` + +Now, now it throws a JS native error: + +```ts +faker.helpers.arrayElement(undefined); // TypeError: Cannot read properties of undefined (reading 'length') +``` + +Calling the methods with an empty array instead still behaves as before. + ### Image Module Removed deprecated image methods From 84447392620dcadd2b82490e4d7bb230c04393d2 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Thu, 20 Jun 2024 15:02:55 +0200 Subject: [PATCH 3/3] Update docs/guide/upgrading.md --- docs/guide/upgrading.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guide/upgrading.md b/docs/guide/upgrading.md index a8b0dd2003d..c89f2b4a5bc 100644 --- a/docs/guide/upgrading.md +++ b/docs/guide/upgrading.md @@ -359,7 +359,7 @@ Previously, the `arrayElement` and `arrayElements` methods would throw a dedicat faker.helpers.arrayElement(undefined); // FakerError: Calling `faker.helpers.arrayElement()` without arguments is no longer supported. ``` -Now, now it throws a JS native error: +Now, it throws a JS native error: ```ts faker.helpers.arrayElement(undefined); // TypeError: Cannot read properties of undefined (reading 'length')