From 4c6513e105f50db1911c3548c258537c35d1b83d Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Fri, 21 Jun 2024 11:49:51 +0200 Subject: [PATCH] chore: remove arrayElement(s) js only error (#2958) --- docs/guide/upgrading.md | 18 ++++++++++++++++++ src/modules/helpers/index.ts | 14 -------------- test/modules/helpers.spec.ts | 18 ------------------ 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/docs/guide/upgrading.md b/docs/guide/upgrading.md index 9797a48c314..beccc45719d 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, 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 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(() =>