diff options
| author | DivisionByZero <[email protected]> | 2024-02-11 11:29:51 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-02-11 10:29:51 +0000 |
| commit | 9aab7c2429b50c0f6e8bd1908344f9ff92912f5d (patch) | |
| tree | a30e8d5a6704c8c5011757e48dba4e1bba5ac638 /src/modules | |
| parent | 0e4b93fe42ed6c63970c629e9b5e3ff415e5711c (diff) | |
| download | faker-9aab7c2429b50c0f6e8bd1908344f9ff92912f5d.tar.xz faker-9aab7c2429b50c0f6e8bd1908344f9ff92912f5d.zip | |
feat(food): new module (#2484)
Co-authored-by: Matt Mayer <[email protected]>
Co-authored-by: Matt Mayer <[email protected]>
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/food/index.ts | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/src/modules/food/index.ts b/src/modules/food/index.ts new file mode 100644 index 00000000..0effaae6 --- /dev/null +++ b/src/modules/food/index.ts @@ -0,0 +1,141 @@ +import { ModuleBase } from '../../internal/module-base'; +/** + * Module for generating food-related data. + * + * ### Overview + * + * This module provides methods to generate various food-related information, such as items on a menu. + * To generate the name of a dish, use [`dish()`](https://fakerjs.dev/api/food.html#dish) and to generate a long description for a dish use [`description()`](https://fakerjs.dev/api/food.html#description). Note that these will not correspond with each other. + * You can also generate individual components of a dish such as [spices](https://fakerjs.dev/api/food.html#spice), [vegetables](https://fakerjs.dev/api/food.html#vegetable), [meats](https://fakerjs.dev/api/food.html#meat), [fruits](https://fakerjs.dev/api/food.html#fruit), or generic [ingredients](https://fakerjs.dev/api/food.html#ingredient). + */ +export class FoodModule extends ModuleBase { + /** + * Generates a random dish adjective. + * + * @example + * faker.food.adjective() // 'crispy' + * + * @since 9.0.0 + */ + adjective(): string { + return this.faker.helpers.fake(this.faker.definitions.food.adjective); + } + + /** + * Generates a random dish description. + * + * @example + * faker.food.description() // 'An exquisite ostrich roast, infused with the essence of longan, slow-roasted to bring out its natural flavors and served with a side of creamy red cabbage' + * + * @since 9.0.0 + */ + description(): string { + return this.faker.helpers.fake( + this.faker.definitions.food.description_pattern + ); + } + + /** + * Generates a random dish name. + * + * @example + * faker.food.dish() // 'Tagine-Rubbed Venison Salad' + * + * @since 9.0.0 + */ + dish(): string { + // A 50/50 mix of specific dishes and dish_patterns + const toTitleCase = (s: string) => + s + .split(' ') + .map((w) => w.charAt(0).toUpperCase() + w.slice(1)) + .join(' '); + if (this.faker.datatype.boolean()) { + return toTitleCase( + this.faker.helpers.fake(this.faker.definitions.food.dish_pattern) + ); + } + + return toTitleCase( + this.faker.helpers.arrayElement(this.faker.definitions.food.dish) + ); + } + + /** + * Generates a random food's ethnic category. + * + * @example + * faker.food.ethnicCategory() // 'Italian' + * + * @since 9.0.0 + */ + ethnicCategory(): string { + return this.faker.helpers.arrayElement( + this.faker.definitions.food.ethnic_category + ); + } + + /** + * Generates a random fruit name. + * + * @example + * faker.food.fruit() // 'lemon' + * + * @since 9.0.0 + */ + fruit(): string { + return this.faker.helpers.arrayElement(this.faker.definitions.food.fruit); + } + + /** + * Generates a random ingredient name. + * + * @example + * faker.food.ingredient() // 'butter' + * + * @since 9.0.0 + */ + ingredient(): string { + return this.faker.helpers.arrayElement( + this.faker.definitions.food.ingredient + ); + } + + /** + * Generates a random meat + * + * @example + * faker.food.meat() // 'venison' + * + * @since 9.0.0 + */ + meat(): string { + return this.faker.helpers.arrayElement(this.faker.definitions.food.meat); + } + + /** + * Generates a random spice name. + * + * @example + * faker.food.spice() // 'chilli' + * + * @since 9.0.0 + */ + spice(): string { + return this.faker.helpers.arrayElement(this.faker.definitions.food.spice); + } + + /** + * Generates a random vegetable name. + * + * @example + * faker.food.vegetable() // 'broccoli' + * + * @since 9.0.0 + */ + vegetable(): string { + return this.faker.helpers.arrayElement( + this.faker.definitions.food.vegetable + ); + } +} |
