Skip to content

Commit 327a2c2

Browse files
committed
refactor!: harmonize method naming to camelCase
Rename public API methods and properties in ShoppingList: - category_config → categoryConfig - set_category_config() → setCategoryConfig() - add_recipe() → addRecipe() - remove_recipe() → removeRecipe() Rename private methods for consistency: - ShoppingList.calculate_ingredients() → calculateIngredients() - Recipe._populate_ingredient_quantities() → _populateIngredientQuantities() Update documentation and tests accordingly. BREAKING CHANGE: ShoppingList public API renamed to camelCase. Migrate by updating method calls: - .add_recipe() → .addRecipe() - .remove_recipe() → .removeRecipe() - .set_category_config() → .setCategoryConfig() - .category_config → .categoryConfig
1 parent 0a79b7b commit 327a2c2

File tree

6 files changed

+85
-86
lines changed

6 files changed

+85
-86
lines changed

docs/examples-shopping-lists.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ outline: deep
66

77
## Basics
88

9-
A quick start example is provided in the corresponding [API page](/api/classes/ShoppingList#example). Note that it is `Recipe` objects that are passed to the `add_recipe()` method.
9+
A quick start example is provided in the corresponding [API page](/api/classes/ShoppingList#example). Note that it is `Recipe` objects that are passed to the `addRecipe()` method.
1010

1111
## Adding scaled recipes
1212

13-
You can specify a number of servings for which the recipe should be scaled before being added with [`add_recipe()`](/api/classes/ShoppingList.html#add-recipe). Example:
13+
You can specify a number of servings for which the recipe should be scaled before being added with [`addRecipe()`](/api/classes/ShoppingList.html#addRecipe). Example:
1414

1515
```typescript
16-
shoppingList.add_recipe(myRecipe, 4)
16+
shoppingList.addRecipe(myRecipe, 4)
1717
```
1818

1919
When adding a recipe, the combined list of ingredients of the shopping list is automatically calculated, and the ingredients categorized if a `CategoryConfig` has been set.
2020

2121
## Removing recipes
2222

23-
You can remove the recipe by passing the index of the recipe to remove to the [`remove_recipe()`](/api/classes/ShoppingList.html#remove-recipe). The list of indexes is accessible via the [`recipes`](/api/classes/ShoppingList.html#recipes) property of the `ShoppingList`. Example:
23+
You can remove the recipe by passing the index of the recipe to remove to the [`removeRecipe()`](/api/classes/ShoppingList.html#removeRecipe). The list of indexes is accessible via the [`recipes`](/api/classes/ShoppingList.html#recipes) property of the `ShoppingList`. Example:
2424

2525
```typescript
26-
shoppingList.remove_recipe(0)
26+
shoppingList.removeRecipe(0)
2727
```
2828

2929
## Optional: Category Configuration
@@ -43,7 +43,7 @@ const myConfig = new CategoryConfig(`...`)
4343
const shoppingList = new ShoppingList(myConfig)
4444
// or
4545
const shoppingList = new ShoppingList()
46-
shoppingList.set_category_config(myConfig)
46+
shoppingList.setCategoryConfig(myConfig)
4747
```
4848

4949
### Categorizing according to the category configuration

src/classes/recipe.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ export class Recipe {
603603
* Quantities are grouped by their alternative signature and summed using addEquivalentsAndSimplify.
604604
* @internal
605605
*/
606-
private _populate_ingredient_quantities(): void {
606+
private _populateIngredientQuantities(): void {
607607
// Reset quantities and usedAsPrimary flag
608608
for (const ing of this.ingredients) {
609609
delete ing.quantities;
@@ -1118,7 +1118,7 @@ export class Recipe {
11181118
this.sections.push(section);
11191119
}
11201120

1121-
this._populate_ingredient_quantities();
1121+
this._populateIngredientQuantities();
11221122
}
11231123

11241124
/**
@@ -1250,7 +1250,7 @@ export class Recipe {
12501250
) as FixedNumericValue;
12511251
}
12521252

1253-
newRecipe._populate_ingredient_quantities();
1253+
newRecipe._populateIngredientQuantities();
12541254

12551255
newRecipe.servings = Big(originalServings).times(factor).toNumber();
12561256

@@ -1517,7 +1517,7 @@ export class Recipe {
15171517
}
15181518

15191519
// Re-aggregate ingredient quantities
1520-
newRecipe._populate_ingredient_quantities();
1520+
newRecipe._populateIngredientQuantities();
15211521

15221522
// Setting the unit system in 'keep' mode will convert all equivalents to that system
15231523
// which will lead to duplicates

src/classes/shopping_cart.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export interface ShoppingCartSummary {
6565
* ```ts
6666
* const shoppingList = new ShoppingList();
6767
* const recipe = new Recipe("@flour{600%g}");
68-
* shoppingList.add_recipe(recipe);
68+
* shoppingList.addRecipe(recipe);
6969
*
7070
* const catalog = new ProductCatalog();
7171
* catalog.products = [
@@ -138,7 +138,6 @@ export class ShoppingCart {
138138
this.productCatalog = catalog;
139139
}
140140

141-
// TODO: harmonize recipe name to use underscores
142141
/**
143142
* Sets the shopping list to build the cart from.
144143
* To use if a shopping list was not provided at the creation of the instance

src/classes/shopping_list.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { isAndGroup } from "../utils/type_guards";
2020
* ## Usage
2121
*
2222
* - Create a new ShoppingList instance with an optional category configuration (see {@link ShoppingList."constructor" | constructor})
23-
* - Add recipes, scaling them as needed (see {@link ShoppingList.add_recipe | add_recipe()})
23+
* - Add recipes, scaling them as needed (see {@link ShoppingList.addRecipe | addRecipe()})
2424
* - Categorize the ingredients (see {@link ShoppingList.categorize | categorize()})
2525
*
2626
* @example
@@ -32,10 +32,10 @@ import { isAndGroup } from "../utils/type_guards";
3232
* const categoryConfig = fs.readFileSync("./myconfig.txt", "utf-8")
3333
* const recipe1 = new Recipe(fs.readFileSync("./myrecipe.cook", "utf-8"));
3434
* const shoppingList = new ShoppingList();
35-
* shoppingList.set_category_config(categoryConfig);
35+
* shoppingList.setCategoryConfig(categoryConfig);
3636
* // Quantities are automatically calculated and ingredients categorized
3737
* // when adding a recipe
38-
* shoppingList.add_recipe(recipe1);
38+
* shoppingList.addRecipe(recipe1);
3939
* ```
4040
*
4141
* @category Classes
@@ -53,23 +53,23 @@ export class ShoppingList {
5353
/**
5454
* The category configuration for the shopping list.
5555
*/
56-
category_config?: CategoryConfig;
56+
categoryConfig?: CategoryConfig;
5757
/**
5858
* The categorized ingredients in the shopping list.
5959
*/
6060
categories?: CategorizedIngredients;
6161

6262
/**
6363
* Creates a new ShoppingList instance
64-
* @param category_config_str - The category configuration to parse.
64+
* @param categoryConfigStr - The category configuration to parse.
6565
*/
66-
constructor(category_config_str?: string | CategoryConfig) {
67-
if (category_config_str) {
68-
this.set_category_config(category_config_str);
66+
constructor(categoryConfigStr?: string | CategoryConfig) {
67+
if (categoryConfigStr) {
68+
this.setCategoryConfig(categoryConfigStr);
6969
}
7070
}
7171

72-
private calculate_ingredients() {
72+
private calculateIngredients() {
7373
this.ingredients = [];
7474

7575
const addIngredientQuantity = (
@@ -199,7 +199,7 @@ export class ShoppingList {
199199
* @param options - Options for adding the recipe.
200200
* @throws Error if the recipe has alternatives without corresponding choices.
201201
*/
202-
add_recipe(recipe: Recipe, options: AddedRecipeOptions = {}): void {
202+
addRecipe(recipe: Recipe, options: AddedRecipeOptions = {}): void {
203203
// Validate that choices are provided for all alternatives
204204
const errorMessage = this.getUnresolvedAlternativesError(
205205
recipe,
@@ -230,7 +230,7 @@ export class ShoppingList {
230230
});
231231
}
232232
}
233-
this.calculate_ingredients();
233+
this.calculateIngredients();
234234
this.categorize();
235235
}
236236

@@ -281,15 +281,15 @@ export class ShoppingList {
281281

282282
/**
283283
* Removes a recipe from the shopping list, then automatically
284-
* recalculates the quantities and recategorize the ingredients.s
284+
* recalculates the quantities and recategorize the ingredients.
285285
* @param index - The index of the recipe to remove.
286286
*/
287-
remove_recipe(index: number) {
287+
removeRecipe(index: number) {
288288
if (index < 0 || index >= this.recipes.length) {
289289
throw new Error("Index out of bounds");
290290
}
291291
this.recipes.splice(index, 1);
292-
this.calculate_ingredients();
292+
this.calculateIngredients();
293293
this.categorize();
294294
}
295295

@@ -298,10 +298,10 @@ export class ShoppingList {
298298
* and automatically categorize current ingredients from the list.
299299
* @param config - The category configuration to parse.
300300
*/
301-
set_category_config(config: string | CategoryConfig) {
301+
setCategoryConfig(config: string | CategoryConfig) {
302302
if (typeof config === "string")
303-
this.category_config = new CategoryConfig(config);
304-
else if (config instanceof CategoryConfig) this.category_config = config;
303+
this.categoryConfig = new CategoryConfig(config);
304+
else if (config instanceof CategoryConfig) this.categoryConfig = config;
305305
else throw new Error("Invalid category configuration");
306306
this.categorize();
307307
}
@@ -311,19 +311,19 @@ export class ShoppingList {
311311
* Will use the category config if any, otherwise all ingredients will be placed in the "other" category
312312
*/
313313
categorize() {
314-
if (!this.category_config) {
314+
if (!this.categoryConfig) {
315315
this.categories = { other: this.ingredients };
316316
return;
317317
}
318318

319319
const categories: CategorizedIngredients = { other: [] };
320-
for (const category of this.category_config.categories) {
320+
for (const category of this.categoryConfig.categories) {
321321
categories[category.name] = [];
322322
}
323323

324324
for (const ingredient of this.ingredients) {
325325
let found = false;
326-
for (const category of this.category_config.categories) {
326+
for (const category of this.categoryConfig.categories) {
327327
for (const categoryIngredient of category.ingredients) {
328328
if (categoryIngredient.aliases.includes(ingredient.name)) {
329329
categories[category.name]!.push(ingredient);

test/shopping_cart.test.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ productCatalog.products = [
101101
describe("initialisation", () => {
102102
it("should be initialized directly with the class constructor", () => {
103103
const shoppingList = new ShoppingList();
104-
shoppingList.add_recipe(new Recipe(recipeForShoppingList1));
104+
shoppingList.addRecipe(new Recipe(recipeForShoppingList1));
105105
const shoppingCart = new ShoppingCart({
106106
catalog: productCatalog,
107107
list: shoppingList,
@@ -118,7 +118,7 @@ describe("initialisation", () => {
118118

119119
it("should throw an error if no product catalog is set", () => {
120120
const shoppingList = new ShoppingList();
121-
shoppingList.add_recipe(new Recipe(recipeForShoppingList1));
121+
shoppingList.addRecipe(new Recipe(recipeForShoppingList1));
122122
const shoppingCart = new ShoppingCart();
123123
shoppingCart.setShoppingList(shoppingList);
124124
expect(() => shoppingCart.buildCart()).toThrow(
@@ -132,7 +132,7 @@ describe("buildCart", () => {
132132
const shoppingCart = new ShoppingCart();
133133
const shoppingList = new ShoppingList();
134134
const recipe = new Recipe("@unknown-ingredient{1}");
135-
shoppingList.add_recipe(recipe);
135+
shoppingList.addRecipe(recipe);
136136
shoppingCart.setShoppingList(shoppingList);
137137
shoppingCart.setProductCatalog(productCatalog);
138138
shoppingCart.buildCart();
@@ -144,7 +144,7 @@ describe("buildCart", () => {
144144
const shoppingCart = new ShoppingCart();
145145
const shoppingList = new ShoppingList();
146146
const recipe = new Recipe("@flour");
147-
shoppingList.add_recipe(recipe);
147+
shoppingList.addRecipe(recipe);
148148
shoppingCart.setShoppingList(shoppingList);
149149
shoppingCart.setProductCatalog(productCatalog);
150150
shoppingCart.buildCart();
@@ -156,7 +156,7 @@ describe("buildCart", () => {
156156
const shoppingCart = new ShoppingCart();
157157
const shoppingList = new ShoppingList();
158158
const recipe = new Recipe("@flour{a bit}");
159-
shoppingList.add_recipe(recipe);
159+
shoppingList.addRecipe(recipe);
160160
shoppingCart.setShoppingList(shoppingList);
161161
shoppingCart.setProductCatalog(productCatalog);
162162
shoppingCart.buildCart();
@@ -169,7 +169,7 @@ describe("buildCart", () => {
169169
const shoppingCart = new ShoppingCart();
170170
const shoppingList = new ShoppingList();
171171
const recipe = new Recipe("@flour{1%l}");
172-
shoppingList.add_recipe(recipe);
172+
shoppingList.addRecipe(recipe);
173173
shoppingCart.setShoppingList(shoppingList);
174174
shoppingCart.setProductCatalog(productCatalog);
175175
shoppingCart.buildCart();
@@ -194,7 +194,7 @@ describe("buildCart", () => {
194194
},
195195
],
196196
});
197-
shoppingList2.add_recipe(recipe2);
197+
shoppingList2.addRecipe(recipe2);
198198
shoppingCart2.setShoppingList(shoppingList2);
199199
shoppingCart2.setProductCatalog(productCatalog2);
200200
shoppingCart2.buildCart();
@@ -216,7 +216,7 @@ describe("buildCart", () => {
216216
{ size: { type: "fixed", value: { type: "decimal", decimal: 1 } } },
217217
],
218218
});
219-
shoppingList3.add_recipe(recipe3);
219+
shoppingList3.addRecipe(recipe3);
220220
shoppingCart3.setShoppingList(shoppingList3);
221221
shoppingCart3.setProductCatalog(productCatalog3);
222222
shoppingCart3.buildCart();
@@ -241,7 +241,7 @@ describe("buildCart", () => {
241241
},
242242
],
243243
});
244-
shoppingList4.add_recipe(recipe4);
244+
shoppingList4.addRecipe(recipe4);
245245
shoppingCart4.setShoppingList(shoppingList4);
246246
shoppingCart4.setProductCatalog(productCatalog4);
247247
shoppingCart4.buildCart();
@@ -256,7 +256,7 @@ describe("buildCart", () => {
256256
const shoppingCart = new ShoppingCart();
257257
const shoppingList = new ShoppingList();
258258
const recipe = new Recipe("@eggs{2%dozen}");
259-
shoppingList.add_recipe(recipe);
259+
shoppingList.addRecipe(recipe);
260260

261261
const catalog = new ProductCatalog();
262262
catalog.products = [
@@ -291,7 +291,7 @@ describe("buildCart", () => {
291291
const shoppingCart = new ShoppingCart();
292292
const shoppingList = new ShoppingList();
293293
const recipe = new Recipe("@eggs{24}"); // 24 eggs = 2 packs of 12
294-
shoppingList.add_recipe(recipe);
294+
shoppingList.addRecipe(recipe);
295295

296296
const catalog = new ProductCatalog();
297297
catalog.products = [
@@ -325,7 +325,7 @@ describe("buildCart", () => {
325325
const shoppingCart = new ShoppingCart();
326326
const shoppingList = new ShoppingList();
327327
const recipe = new Recipe("@flour{600%g}");
328-
shoppingList.add_recipe(recipe);
328+
shoppingList.addRecipe(recipe);
329329
shoppingCart.setShoppingList(shoppingList);
330330
const catalog = new ProductCatalog();
331331
catalog.products = [
@@ -367,7 +367,7 @@ describe("buildCart", () => {
367367
const shoppingCart = new ShoppingCart();
368368
const shoppingList = new ShoppingList();
369369
const recipe = new Recipe("Mix @flour{30-90%g} with @milk{80-120%cL}");
370-
shoppingList.add_recipe(recipe);
370+
shoppingList.addRecipe(recipe);
371371
shoppingCart.setShoppingList(shoppingList);
372372
shoppingCart.setProductCatalog(productCatalog);
373373
shoppingCart.buildCart();
@@ -383,7 +383,7 @@ describe("buildCart", () => {
383383
it("should build a cart with one recipe", () => {
384384
const shoppingList = new ShoppingList();
385385
const shoppingCart = new ShoppingCart();
386-
shoppingList.add_recipe(new Recipe(recipeForShoppingList1));
386+
shoppingList.addRecipe(new Recipe(recipeForShoppingList1));
387387
shoppingCart.setShoppingList(shoppingList);
388388
shoppingCart.setProductCatalog(productCatalog);
389389
shoppingCart.buildCart();
@@ -401,8 +401,8 @@ describe("buildCart", () => {
401401
it("should build a cart with multiple recipes", () => {
402402
const shoppingCart = new ShoppingCart();
403403
const shoppingList = new ShoppingList();
404-
shoppingList.add_recipe(new Recipe(recipeForShoppingList1));
405-
shoppingList.add_recipe(new Recipe(recipeForShoppingList2));
404+
shoppingList.addRecipe(new Recipe(recipeForShoppingList1));
405+
shoppingList.addRecipe(new Recipe(recipeForShoppingList2));
406406
shoppingCart.setShoppingList(shoppingList);
407407
shoppingCart.setProductCatalog(productCatalog);
408408
shoppingCart.buildCart();
@@ -432,7 +432,7 @@ describe("alternative quantities", () => {
432432
it("should handle ingredients with multiple quantities", () => {
433433
const shoppingCart = new ShoppingCart();
434434
const shoppingList = new ShoppingList();
435-
shoppingList.add_recipe(new Recipe(recipeForShoppingList4));
435+
shoppingList.addRecipe(new Recipe(recipeForShoppingList4));
436436
shoppingCart.setShoppingList(shoppingList);
437437
shoppingCart.setProductCatalog(productCatalog);
438438
shoppingCart.buildCart();
@@ -445,7 +445,7 @@ describe("alternative quantities", () => {
445445
it("should handle ingredients with alternative units", () => {
446446
const shoppingCart = new ShoppingCart();
447447
const shoppingList = new ShoppingList();
448-
shoppingList.add_recipe(
448+
shoppingList.addRecipe(
449449
new Recipe(`
450450
---
451451
servings: 1
@@ -464,7 +464,7 @@ Cook @carrots{10%large|1%kg}
464464
it("should add to misMatch when no alternatives can be matched", () => {
465465
const shoppingCart = new ShoppingCart();
466466
const shoppingList = new ShoppingList();
467-
shoppingList.add_recipe(
467+
shoppingList.addRecipe(
468468
new Recipe(`
469469
---
470470
servings: 1

0 commit comments

Comments
 (0)