Skip to content

Commit

Permalink
chore: remove arrayElement(s) js only error (#2958)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Jun 21, 2024
1 parent d6924f7 commit d31c635
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 32 deletions.
18 changes: 18 additions & 0 deletions docs/guide/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,13 +844,6 @@ export class SimpleHelpersModule extends SimpleModuleBase {
* @since 6.3.0
*/
arrayElement<const T>(array: ReadonlyArray<T>): 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.');
}
Expand Down Expand Up @@ -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 [];
}
Expand Down
18 changes: 0 additions & 18 deletions test/modules/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down Expand Up @@ -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(() =>
Expand Down

0 comments on commit d31c635

Please sign in to comment.