aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2024-06-21 11:49:51 +0200
committerGitHub <[email protected]>2024-06-21 09:49:51 +0000
commitd31c635e6efc4013ea44a6676f832091c1763050 (patch)
treeb648d4ab8b9d8e16f18cfd7b5c01c8fd89d90ffd
parentd6924f7fbb9403d106022f58a1bda3b159e88ae6 (diff)
downloadfaker-d31c635e6efc4013ea44a6676f832091c1763050.tar.xz
faker-d31c635e6efc4013ea44a6676f832091c1763050.zip
chore: remove arrayElement(s) js only error (#2958)
-rw-r--r--docs/guide/upgrading.md18
-rw-r--r--src/modules/helpers/index.ts14
-rw-r--r--test/modules/helpers.spec.ts18
3 files changed, 18 insertions, 32 deletions
diff --git a/docs/guide/upgrading.md b/docs/guide/upgrading.md
index 9797a48c..beccc457 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 f62d9086..69a9995c 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<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.');
}
@@ -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 09f26467..3582799f 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(() =>