diff options
| author | DivisionByZero <[email protected]> | 2023-04-21 22:53:44 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-04-21 22:53:44 +0200 |
| commit | 05644468208601f842081d8ce4545f9d37b55461 (patch) | |
| tree | b7d9063049067dea031b880e4f17de901d88fe9b /src/modules | |
| parent | 9161a069d06d17f62b6b6d904251fd2102299064 (diff) | |
| download | faker-05644468208601f842081d8ce4545f9d37b55461.tar.xz faker-05644468208601f842081d8ce4545f9d37b55461.zip | |
refactor(helpers)!: remove default value from arrayElement (#2045)
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/helpers/index.ts | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index d90bcbda..48c8cea0 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -794,18 +794,27 @@ export class HelpersModule { * * @template T The type of the elements to pick from. * - * @param array Array to pick the value from. + * @param array The array to pick the value from. + * + * @throws If the given array is empty. * * @example * faker.helpers.arrayElement(['cat', 'dog', 'mouse']) // 'dog' * * @since 6.3.0 */ - arrayElement<T = string>( - // TODO @Shinigami92 2022-04-30: We want to remove this default value, but currently it's not possible because some definitions could be empty - // See https://github.com/faker-js/faker/issues/893 - array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T> - ): T { + arrayElement<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.'); + } + const index = array.length > 1 ? this.faker.number.int({ max: array.length - 1 }) : 0; @@ -891,9 +900,7 @@ export class HelpersModule { * @since 6.3.0 */ arrayElements<T>( - // TODO @Shinigami92 2022-04-30: We want to remove this default value, but currently it's not possible because some definitions could be empty - // See https://github.com/faker-js/faker/issues/893 - array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T>, + array: ReadonlyArray<T>, count?: | number | { @@ -907,6 +914,13 @@ export class HelpersModule { 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 []; } @@ -1117,10 +1131,6 @@ export class HelpersModule { fake(pattern: string | string[]): string { if (Array.isArray(pattern)) { pattern = this.arrayElement(pattern); - // TODO @ST-DDT 2022-10-15: Remove this check after we fail in `arrayElement` when the array is empty - if (pattern == null) { - throw new FakerError('Array of pattern strings cannot be empty.'); - } } // find first matching {{ and }} |
