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

feat: faker.helpers.maybe #874

Merged
merged 7 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
24 changes: 24 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,4 +691,28 @@ export class Helpers {
account: this.faker.finance.account(),
};
}

/**
* Returns the result of the callback if the probability check was successful, otherwise `undefined`.
*
* @template T The type of result of the given callback.
* @param callback The callback to that will be invoked if the probability check was successful.
* @param options The options to use. Defaults to `{}`.
* @param options.probability The probability (`[0.00, 1.00]`) of the callback being invoked. Defaults to `0.5`.
*
* @example
* faker.random.maybe(() => 'Hello World!') // 'Hello World!'
* faker.random.maybe(() => 'Hello World!', { probability: 0.1 }) // undefined
* faker.random.maybe(() => 'Hello World!', { probability: 0.9 }) // 'Hello World!'
*/
maybe<T>(
callback: () => T,
options: { probability?: number } = {}
): T | undefined {
const { probability = 0.5 } = options;
if (this.faker.datatype.float({ min: 0, max: 1 }) < probability) {
return callback();
}
return undefined;
}
}
26 changes: 26 additions & 0 deletions test/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ describe('helpers', () => {
expect(card).toBeTypeOf('object');
});
});

describe('userCard()', () => {
it('returns an object', () => {
const card = faker.helpers.userCard();
Expand All @@ -806,6 +807,30 @@ describe('helpers', () => {
});
});

describe('maybe', () => {
it('should always return the callback result when probability is 1', () => {
const actual = faker.helpers.maybe(() => 'foo', { probability: 1 });

expect(actual).toBe('foo');
});

it('should never return the callback result when probability is 0', () => {
const actual = faker.helpers.maybe(() => expect.fail(), {
probability: 0,
});

expect(actual).toBeUndefined();
});

it('should not mutate the input object', () => {
const input = Object.freeze({
probability: 0.4,
});

expect(() => faker.helpers.maybe(() => 'foo', input)).not.toThrow();
});
});

describe('deprecation warnings', () => {
it.each([['randomize', 'random.arrayElement']])(
'should warn user that function helpers.%s is deprecated',
Expand All @@ -823,6 +848,7 @@ describe('helpers', () => {
});
}
});

describe('deprecation warnings', () => {
it.each(['createCard', 'contextualCard', 'userCard'])(
'should warn user that function random.%s is deprecated',
Expand Down