How do I use faker.js to pick a random enum value?
Asked Answered
E

2

15

Given an enumeration such as:

enum DistributionType {
    Calculated = "Calculated",
    Custom = "Custom",
    None = "None",
}

How do I use Faker.JS to pick a random enum value from it?

const randomValue = faker.???

Reference: https://fakerjs.dev/

Everara answered 30/10, 2022 at 14:50 Comment(0)
L
24

For @faker-js/faker > 8.0.0 you can now also use the following helper:

const randomValue = faker.helpers.enumValue(DistributionType);
Lagting answered 14/7, 2023 at 14:21 Comment(0)
E
15

I ended up doing this, as I didn't see any specific helpers for enums.

const randomValue = faker.helpers.arrayElement(Object.values(DistributionType));
Everara answered 30/10, 2022 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.