Skip to content

Commit f2abf8b

Browse files
ocodistaST-DDT
andauthored
feat(helpers): new method enumValue (#1920)
Co-authored-by: ST-DDT <ST-DDT@gmx.de>
1 parent e5442d2 commit f2abf8b

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

src/modules/helpers/index.ts

+31
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,37 @@ export class HelpersModule {
622622
return arrayCopy.slice(min);
623623
}
624624

625+
/**
626+
* Returns a random value from an Enum object.
627+
*
628+
* This does the same as `objectValue` except that it ignores (the values assigned to) the numeric keys added for TypeScript enums.
629+
*
630+
* @template EnumType Type of generic enums, automatically inferred by TypeScript.
631+
* @param enumObject Enum to pick the value from.
632+
*
633+
* @example
634+
* enum Color { Red, Green, Blue }
635+
* faker.helpers.enumValue(Color) // 1 (Green)
636+
*
637+
* enum Direction { North = 'North', South = 'South'}
638+
* faker.helpers.enumValue(Direction) // 'South'
639+
*
640+
* enum HttpStatus { Ok = 200, Created = 201, BadRequest = 400, Unauthorized = 401 }
641+
* faker.helpers.enumValue(HttpStatus) // 200 (Ok)
642+
*
643+
* @since 8.0.0
644+
*/
645+
enumValue<EnumType extends Record<string | number, string | number>>(
646+
enumObject: EnumType
647+
): EnumType[keyof EnumType] {
648+
// ignore numeric keys added by TypeScript
649+
const keys: Array<keyof EnumType> = Object.keys(enumObject).filter((key) =>
650+
isNaN(Number(key))
651+
);
652+
const randomKey = this.arrayElement(keys);
653+
return enumObject[randomKey];
654+
}
655+
625656
/**
626657
* Generator for combining faker methods based on a static string input.
627658
*

test/__snapshots__/helpers.spec.ts.snap

+24
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ exports[`helpers > 42 > arrayElements > with array and count range 1`] = `
3636
]
3737
`;
3838

39+
exports[`helpers > 42 > enumValue > with default enum 1`] = `1`;
40+
41+
exports[`helpers > 42 > enumValue > with enum starting from some index 1`] = `400`;
42+
43+
exports[`helpers > 42 > enumValue > with mixed enum 1`] = `1`;
44+
45+
exports[`helpers > 42 > enumValue > with string enum 1`] = `"Brazil"`;
46+
3947
exports[`helpers > 42 > fake > with a dynamic template 1`] = `"my string: Cky2eiXX/J"`;
4048

4149
exports[`helpers > 42 > fake > with a static template 1`] = `"my test string"`;
@@ -235,6 +243,14 @@ exports[`helpers > 1211 > arrayElements > with array and count range 1`] = `
235243
]
236244
`;
237245

246+
exports[`helpers > 1211 > enumValue > with default enum 1`] = `2`;
247+
248+
exports[`helpers > 1211 > enumValue > with enum starting from some index 1`] = `401`;
249+
250+
exports[`helpers > 1211 > enumValue > with mixed enum 1`] = `"Bar"`;
251+
252+
exports[`helpers > 1211 > enumValue > with string enum 1`] = `"United States of America"`;
253+
238254
exports[`helpers > 1211 > fake > with a dynamic template 1`] = `"my string: wKti5-}$_/"`;
239255

240256
exports[`helpers > 1211 > fake > with a static template 1`] = `"my test string"`;
@@ -427,6 +443,14 @@ exports[`helpers > 1337 > arrayElements > with array and count range 1`] = `
427443
]
428444
`;
429445

446+
exports[`helpers > 1337 > enumValue > with default enum 1`] = `0`;
447+
448+
exports[`helpers > 1337 > enumValue > with enum starting from some index 1`] = `200`;
449+
450+
exports[`helpers > 1337 > enumValue > with mixed enum 1`] = `1`;
451+
452+
exports[`helpers > 1337 > enumValue > with string enum 1`] = `"Brazil"`;
453+
430454
exports[`helpers > 1337 > fake > with a dynamic template 1`] = `"my string: 9U/4:SK$>6"`;
431455

432456
exports[`helpers > 1337 > fake > with a static template 1`] = `"my test string"`;

test/helpers.spec.ts

+75
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,37 @@ describe('helpers', () => {
5555
t.it('noArgs').it('with array', 'Hello World!'.split(''));
5656
});
5757

58+
t.describe('enumValue', (t) => {
59+
enum Color {
60+
Red,
61+
Green,
62+
Blue,
63+
}
64+
65+
enum HttpStatus {
66+
Ok = 200,
67+
BadRequest = 400,
68+
Unauthorized = 401,
69+
}
70+
71+
enum Country {
72+
BR = 'Brazil',
73+
USA = 'United States of America',
74+
}
75+
76+
enum MixedFoo {
77+
Foo = 0,
78+
Bar = 1,
79+
FooName = 'Foo',
80+
BarName = 'Bar',
81+
}
82+
83+
t.it('with default enum', Color)
84+
.it('with enum starting from some index', HttpStatus)
85+
.it('with string enum', Country)
86+
.it('with mixed enum', MixedFoo);
87+
});
88+
5889
t.describe('weightedArrayElement', (t) => {
5990
t.it('with array', [
6091
{ weight: 5, value: 'sunny' },
@@ -159,6 +190,50 @@ describe('helpers', () => {
159190
});
160191
});
161192

193+
describe('enumValue', () => {
194+
enum ColorValueEnum {
195+
Red,
196+
Green,
197+
Blue,
198+
}
199+
enum ColorValueWithStartIndexEnum {
200+
Red = 3,
201+
Green,
202+
Blue,
203+
}
204+
enum ColorStringEnum {
205+
Red = 'RED',
206+
Green = 'GREEN',
207+
Blue = 'BLUE',
208+
}
209+
enum FooMixedEnum {
210+
Foo = 0,
211+
Bar = 1,
212+
StrFoo = 'FOO',
213+
StrBar = 'BAR',
214+
}
215+
216+
it('should return a value from a numeric enum', () => {
217+
const actual = faker.helpers.enumValue(ColorValueEnum);
218+
expect([0, 1, 2]).toContain(actual);
219+
});
220+
221+
it('should return a value from a numeric enum that first value is not 0', () => {
222+
const actual = faker.helpers.enumValue(ColorValueWithStartIndexEnum);
223+
expect([3, 4, 5]).toContain(actual);
224+
});
225+
226+
it('should return a value from a string enum', () => {
227+
const actual = faker.helpers.enumValue(ColorStringEnum);
228+
expect(['RED', 'GREEN', 'BLUE']).toContain(actual);
229+
});
230+
231+
it('should return a value from a mixed enum', () => {
232+
const actual = faker.helpers.enumValue(FooMixedEnum);
233+
expect([0, 1, 'FOO', 'BAR']).toContain(actual);
234+
});
235+
});
236+
162237
describe('weightedArrayElement', () => {
163238
it('should return a weighted random element in the array', () => {
164239
const testArray = [

0 commit comments

Comments
 (0)