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

chore: remove arrayElement(s) js only error #2958

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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