Skip to content

Commit

Permalink
feat: move to helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Apr 26, 2022
1 parent f3dab67 commit 349136c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 56 deletions.
23 changes: 23 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,4 +691,27 @@ export class Helpers {
account: this.faker.finance.account(),
};
}

/**
* Returns the result of the callback if the probability check was successful, otherwise `undefined`.
*
* @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;
}
}
23 changes: 0 additions & 23 deletions src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,27 +608,4 @@ export class Random {

return this.faker.datatype.hexadecimal(count);
}

/**
* Returns the result of the callback if the probability check was successful, otherwise `undefined`.
*
* @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;
}
}
9 changes: 0 additions & 9 deletions test/all_functional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ const BROKEN_LOCALE_METHODS = {
prefix: ['az', 'id_ID', 'ru'],
suffix: ['az', 'it', 'mk', 'pt_PT', 'ru'],
},
random: {
maybe: [...Object.keys(faker.locales)],
},
};

function isWorkingLocaleForMethod(
Expand Down Expand Up @@ -107,12 +104,6 @@ describe('faker.fake functional tests', () => {
Object.keys(modules).forEach((module) => {
describe(module, () => {
modules[module].forEach((meth) => {
// Skip `faker.random.maybe()` because the first param is a callback and not supported by `fake`
if (module === 'random' && meth === 'maybe') {
it.skip('maybe()');
return;
}

it(`${meth}()`, () => {
faker.locale = locale;
// TODO ST-DDT 2022-03-28: Use random seed once there are no more failures
Expand Down
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
24 changes: 0 additions & 24 deletions test/random.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,30 +483,6 @@ describe('random', () => {
});
});

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

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

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

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

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

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

describe('deprecation warnings', () => {
it.each([
['number', 'datatype.number'],
Expand Down

0 comments on commit 349136c

Please sign in to comment.