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

refactor(random.arrayElement): remove default parameter #589

Closed
wants to merge 6 commits into from
Closed
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
28 changes: 13 additions & 15 deletions src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,41 +99,39 @@ export class Random {
* Returns random element from the given array.
*
* @template T The type of the entries to pick from.
* @param array Array to pick the value from. Defaults to `['a', 'b', 'c']`.
* @param array Array to pick the value from.
*
* @example
* faker.random.arrayElement() // 'b'
* faker.random.arrayElement(['cat', 'dog', 'mouse']) // 'dog'
* faker.random.arrayElement([0, 1, 2, 3]) // 3
*/
arrayElement<T = string>(
array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T>
): T {
const index =
array.length > 1
? this.faker.datatype.number({ max: array.length - 1 })
: 0;
arrayElement<T>(array: ReadonlyArray<T>): T {
if (array == null || array.length === 0) {
throw new FakerError('Cannot get random element from empty array.');
}

const index = this.faker.datatype.number({ max: array.length - 1 });
return array[index];
}

/**
* Returns a subset with random elements of the given array in random order.
*
* @template T The type of the entries to pick from.
* @param array Array to pick the value from. Defaults to `['a', 'b', 'c']`.
* @param array Array to pick the value from.
* @param count Number of elements to pick.
* When not provided, random number of elements will be picked.
* When value exceeds array boundaries, it will be limited to stay inside.
*
* @example
* faker.random.arrayElements() // ['b', 'c']
* faker.random.arrayElements(['cat', 'dog', 'mouse']) // ['mouse', 'cat']
* faker.random.arrayElements([1, 2, 3, 4, 5], 2) // [4, 2]
*/
arrayElements<T>(
array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T>,
count?: number
): T[] {
arrayElements<T>(array: ReadonlyArray<T>, count?: number): T[] {
if (array == null || array.length === 0) {
throw new FakerError('Cannot get random element from empty array.');
}

if (typeof count !== 'number') {
count = this.faker.datatype.number({ min: 1, max: array.length });
} else if (count > array.length) {
Expand Down
110 changes: 26 additions & 84 deletions src/word.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import type { Faker } from '.';

function selectWords(length?: number, words: string[] = []): string[] {
let wordList = words;
if (length) {
wordList = words.filter((word) => word.length === length);
if (wordList.length === 0) {
wordList = words;
}
}

return wordList;
}

/**
* Module to return various types of words.
*/
Expand All @@ -25,18 +37,8 @@ export class Word {
* faker.word.adjective(100) // 'complete'
*/
adjective(length?: number): string {
let wordList = this.faker.definitions.word.adjective;
if (length) {
wordList = this.faker.definitions.word.adjective.filter(
(word) => word.length === length
);
}

// If result of filtered word list is undefined, return an element
// from the unfiltered list.
return (
this.faker.random.arrayElement(wordList) ||
this.faker.random.arrayElement(this.faker.definitions.word.adjective)
return this.faker.random.arrayElement(
selectWords(length, this.faker.definitions.word.adjective)
);
}

Expand All @@ -51,18 +53,8 @@ export class Word {
* faker.word.adverb(100) // 'sadly'
*/
adverb(length?: number): string {
let wordList = this.faker.definitions.word.adverb;
if (length) {
wordList = this.faker.definitions.word.adverb.filter(
(word: string) => word.length === length
);
}

// If result of filtered word list is undefined, return an element
// from the unfiltered list.
return (
this.faker.random.arrayElement(wordList) ||
this.faker.random.arrayElement(this.faker.definitions.word.adverb)
return this.faker.random.arrayElement(
selectWords(length, this.faker.definitions.word.adverb)
);
}

Expand All @@ -77,18 +69,8 @@ export class Word {
* faker.word.conjunction(100) // 'as long as'
*/
conjunction(length?: number): string {
let wordList = this.faker.definitions.word.conjunction;
if (length) {
wordList = this.faker.definitions.word.conjunction.filter(
(word: string) => word.length === length
);
}

// If result of filtered word list is undefined, return an element
// from the unfiltered list.
return (
this.faker.random.arrayElement(wordList) ||
this.faker.random.arrayElement(this.faker.definitions.word.conjunction)
return this.faker.random.arrayElement(
selectWords(length, this.faker.definitions.word.conjunction)
);
}

Expand All @@ -103,18 +85,8 @@ export class Word {
* faker.word.interjection(100) // 'yowza'
*/
interjection(length?: number): string {
let wordList = this.faker.definitions.word.interjection;
if (length) {
wordList = this.faker.definitions.word.interjection.filter(
(word: string) => word.length === length
);
}

// If result of filtered word list is undefined, return an element
// from the unfiltered list.
return (
this.faker.random.arrayElement(wordList) ||
this.faker.random.arrayElement(this.faker.definitions.word.interjection)
return this.faker.random.arrayElement(
selectWords(length, this.faker.definitions.word.interjection)
);
}

Expand All @@ -129,18 +101,8 @@ export class Word {
* faker.word.noun(100) // 'care'
*/
noun(length?: number): string {
let wordList = this.faker.definitions.word.noun;
if (length) {
wordList = this.faker.definitions.word.noun.filter(
(word: string) => word.length === length
);
}

// If result of filtered word list is undefined, return an element
// from the unfiltered list.
return (
this.faker.random.arrayElement(wordList) ||
this.faker.random.arrayElement(this.faker.definitions.word.noun)
return this.faker.random.arrayElement(
selectWords(length, this.faker.definitions.word.noun)
);
}

Expand All @@ -155,18 +117,8 @@ export class Word {
* faker.word.preposition(100) // 'an'
*/
preposition(length?: number): string {
let wordList = this.faker.definitions.word.preposition;
if (length) {
wordList = this.faker.definitions.word.preposition.filter(
(word: string) => word.length === length
);
}

// If result of filtered word list is undefined, return an element
// from the unfiltered list.
return (
this.faker.random.arrayElement(wordList) ||
this.faker.random.arrayElement(this.faker.definitions.word.preposition)
return this.faker.random.arrayElement(
selectWords(length, this.faker.definitions.word.preposition)
);
}

Expand All @@ -181,18 +133,8 @@ export class Word {
* faker.word.verb(100) // 'mess'
*/
verb(length?: number): string {
let wordList = this.faker.definitions.word.verb;
if (length) {
wordList = this.faker.definitions.word.verb.filter(
(word: string) => word.length === length
);
}

// If result of filtered word list is undefined, return an element
// from the unfiltered list.
return (
this.faker.random.arrayElement(wordList) ||
this.faker.random.arrayElement(this.faker.definitions.word.verb)
return this.faker.random.arrayElement(
selectWords(length, this.faker.definitions.word.verb)
);
}
}
6 changes: 3 additions & 3 deletions test/address.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, it } from 'vitest';
import { faker } from '../src';
import { faker, FakerError } from '../src';

const seededRuns = [
{
Expand Down Expand Up @@ -164,8 +164,8 @@ describe('address', () => {
it('streetPrefix()', () => {
faker.seed(seed);

const streetPrefix = faker.address.streetPrefix();
expect(streetPrefix).toEqual(expectations.streetPrefix);
// expect an element when `en` locales provide `street_prefix`
expect(() => faker.address.streetPrefix()).toThrowError(FakerError);
});

it('streetSuffix()', () => {
Expand Down
Loading