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(helpers): new method enumValue #1920

Merged
merged 19 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 28 additions & 0 deletions src/modules/datatype/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,34 @@ export class DatatypeModule {
return this.faker.number.float({ min, max, precision });
}

/**
* Returns a random value from an Enum object
ocodista marked this conversation as resolved.
Show resolved Hide resolved
ocodista marked this conversation as resolved.
Show resolved Hide resolved
*
* @param enumObject
*
* @example
* faker.datatype.enum(enum { A, B, C}) // 'C'
* faker.datatype.enum(enum { A, B, C}) // 'A'
ocodista marked this conversation as resolved.
Show resolved Hide resolved
* faker.datatype.enum(enum { A, B, C}) // 'B'
*/
enum<GenericEnumType extends Object, SpecificEnumType>(
ocodista marked this conversation as resolved.
Show resolved Hide resolved
enumObject: GenericEnumType
): SpecificEnumType {
if(!enumObject) return '0' as SpecificEnumType
const ignoreTypeScriptAddedKeys = (enumKeys: string[]) => {
return enumKeys.filter((key) => isNaN(Number(key)));
};
const keys = Object.keys(enumObject);
const enumKeys = ignoreTypeScriptAddedKeys(keys);
const { length } = enumKeys;
const validIndexes = length - 1;
const randomEnumIndex = this.faker.number.int(validIndexes);
const randomOption = (enumObject as any)[
ocodista marked this conversation as resolved.
Show resolved Hide resolved
randomEnumIndex
] as SpecificEnumType;
return randomOption;
}

/**
* Returns a single random floating-point number for the given precision or range and precision.
*
Expand Down
24 changes: 24 additions & 0 deletions test/__snapshots__/datatype.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ exports[`datatype > 42 > datetime > with min 1`] = `1801-04-11T15:13:06.330Z`;

exports[`datatype > 42 > datetime > with min and max 1`] = `1689-09-09T08:39:09.444Z`;

exports[`datatype > 42 > enum > with default enum 1`] = `"B"`;

exports[`datatype > 42 > enum > with enum starting from some index 1`] = `undefined`;

exports[`datatype > 42 > enum > with string enum 1`] = `undefined`;

exports[`datatype > 42 > enum > with valid enum 1`] = `"B"`;

exports[`datatype > 42 > float > noArgs 1`] = `37453.64`;

exports[`datatype > 42 > float > repeated 1`] = `37453.64`;
Expand Down Expand Up @@ -193,6 +201,14 @@ exports[`datatype > 1211 > datetime > with min 1`] = `2065-11-10T19:27:20.915Z`;

exports[`datatype > 1211 > datetime > with min and max 1`] = `1789-03-26T15:44:45.219Z`;

exports[`datatype > 1211 > enum > with default enum 1`] = `"C"`;

exports[`datatype > 1211 > enum > with enum starting from some index 1`] = `"A"`;

exports[`datatype > 1211 > enum > with string enum 1`] = `undefined`;
ocodista marked this conversation as resolved.
Show resolved Hide resolved

exports[`datatype > 1211 > enum > with valid enum 1`] = `"C"`;

exports[`datatype > 1211 > float > noArgs 1`] = `92851.09`;

exports[`datatype > 1211 > float > repeated 1`] = `92851.09`;
Expand Down Expand Up @@ -323,6 +339,14 @@ exports[`datatype > 1337 > datetime > with min 1`] = `1747-07-16T01:19:54.159Z`;

exports[`datatype > 1337 > datetime > with min and max 1`] = `1669-06-22T01:21:21.236Z`;

exports[`datatype > 1337 > enum > with default enum 1`] = `"A"`;

exports[`datatype > 1337 > enum > with enum starting from some index 1`] = `undefined`;
ocodista marked this conversation as resolved.
Show resolved Hide resolved

exports[`datatype > 1337 > enum > with string enum 1`] = `undefined`;

exports[`datatype > 1337 > enum > with valid enum 1`] = `"A"`;

exports[`datatype > 1337 > float > noArgs 1`] = `26202.2`;

exports[`datatype > 1337 > float > repeated 1`] = `26202.2`;
Expand Down
23 changes: 23 additions & 0 deletions test/datatype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ describe('datatype', () => {
});
});

t.describe('enum', (t) => {
ocodista marked this conversation as resolved.
Show resolved Hide resolved
enum defaultEnum {
A,
B,
C,
}

enum enumStartingFromSomeInt {
A = 2,
B,
C,
}

enum stringEnum {
A = 'A',
B = 'B',
C = 'C',
}
ocodista marked this conversation as resolved.
Show resolved Hide resolved
t.it('with default enum', defaultEnum)
.it('with enum starting from some index', enumStartingFromSomeInt)
.it('with string enum', stringEnum);
});
ocodista marked this conversation as resolved.
Show resolved Hide resolved

t.describe('string', (t) => {
t.it('noArgs')
.it('with number', 42)
Expand Down