Skip to content

Commit 13efe7a

Browse files
committed
fix(scaling): default servings 1 if no servings defined
1 parent cbd4f5e commit 13efe7a

File tree

3 files changed

+36
-51
lines changed

3 files changed

+36
-51
lines changed

docs/examples-scaling-recipes.md

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

77
## Pre-requisite
88

9-
To be able to scale, a `Recipe` must have its [`servings`](/api/classes/Recipe.html#servings) property set, which is done by the parser when it encounters
9+
To be able to scale, `Recipe` uses its [`servings`](/api/classes/Recipe.html#servings) property, which is done by the parser when it encounters
1010
one of the following tags in the recipe's frontmatter: [`servings`](/api/interfaces/Metadata.html#servings), [`serves`](/api/interfaces/Metadata.html#serves) or [`yield`](/api/interfaces/Metadata.html#yield):
1111

1212
```json
@@ -17,7 +17,7 @@ servings: 2
1717
---
1818
```
1919

20-
The rest of this guide assumes that you have set one of the above, and will assume a `servings` value of `2`
20+
If none of those are found and the `servings` property is undefined, it will default to 1 when scaling. The rest of this guide assumes that you have set one of the above, and will assume a `servings` value of `2`
2121

2222
## Scaling by a factor
2323

src/classes/recipe.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,10 +1120,11 @@ export class Recipe {
11201120
* @throws `Error` if the recipe does not contains an initial {@link Recipe.servings | servings} value
11211121
*/
11221122
scaleTo(newServings: number): Recipe {
1123-
const originalServings = this.getServings();
1123+
let originalServings = this.getServings();
11241124

1125+
// Default to 1 if no servings defined
11251126
if (originalServings === undefined || originalServings === 0) {
1126-
throw new Error("Error scaling recipe: no initial servings value set");
1127+
originalServings = 1;
11271128
}
11281129

11291130
const factor = Big(newServings).div(originalServings);
@@ -1139,10 +1140,11 @@ export class Recipe {
11391140
scaleBy(factor: number | Big): Recipe {
11401141
const newRecipe = this.clone();
11411142

1142-
const originalServings = newRecipe.getServings();
1143+
let originalServings = newRecipe.getServings();
11431144

1145+
// Default to 1 if no servings defined
11441146
if (originalServings === undefined || originalServings === 0) {
1145-
throw new Error("Error scaling recipe: no initial servings value set");
1147+
originalServings = 1;
11461148
}
11471149

11481150
function scaleAlternativesBy(

test/recipe_scaling.test.ts

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -125,29 +125,21 @@ describe("scaleTo", () => {
125125
});
126126
});
127127

128-
it("should throw an error if no initial servings information", () => {
129-
const recipeWithoutServings = new Recipe();
130-
recipeWithoutServings.ingredients = [
131-
{
132-
name: "water",
133-
quantities: [
134-
{
135-
groupQuantity: {
136-
quantity: {
137-
type: "fixed",
138-
value: { type: "decimal", decimal: 1 },
139-
},
140-
unit: "l",
141-
},
142-
},
143-
],
144-
flags: [],
128+
it("should default servings to 1 if no initial servings information", () => {
129+
const recipeWithoutServings = new Recipe("@water{1%L}");
130+
const scaledRecipe = recipeWithoutServings.scaleTo(4);
131+
expect(scaledRecipe.servings).toBe(4);
132+
const water = scaledRecipe.ingredients[0]!.quantities;
133+
if (!water) throw new Error("No quantities found for water ingredient");
134+
expect(water[0]).toEqual({
135+
groupQuantity: {
136+
quantity: {
137+
type: "fixed",
138+
value: { type: "decimal", decimal: 4 },
139+
},
140+
unit: "L",
145141
},
146-
];
147-
148-
expect(() => recipeWithoutServings.scaleTo(4)).toThrowError(
149-
"Error scaling recipe: no initial servings value set",
150-
);
142+
});
151143
});
152144

153145
it("should not modify the original recipe", () => {
@@ -292,30 +284,21 @@ describe("scaleBy", () => {
292284
expect(scaledRecipe.metadata.yield).toBe("4");
293285
});
294286

295-
it("should throw an error if no initial serving information", () => {
296-
const recipeWithoutServings = new Recipe();
297-
recipeWithoutServings.metadata = {};
298-
recipeWithoutServings.ingredients = [
299-
{
300-
name: "water",
301-
quantities: [
302-
{
303-
groupQuantity: {
304-
quantity: {
305-
type: "fixed",
306-
value: { type: "decimal", decimal: 1 },
307-
},
308-
unit: "l",
309-
},
310-
},
311-
],
312-
flags: [],
287+
it("should default servings to 1 if no initial serving information", () => {
288+
const recipeWithoutServings = new Recipe("@water{1%L}");
289+
const scaledRecipe = recipeWithoutServings.scaleBy(2);
290+
expect(scaledRecipe.servings).toBe(2);
291+
const water = scaledRecipe.ingredients[0]!.quantities;
292+
if (!water) throw new Error("No quantities found for water ingredient");
293+
expect(water[0]).toEqual({
294+
groupQuantity: {
295+
quantity: {
296+
type: "fixed",
297+
value: { type: "decimal", decimal: 2 },
298+
},
299+
unit: "L",
313300
},
314-
];
315-
316-
expect(() => recipeWithoutServings.scaleBy(2)).toThrowError(
317-
"Error scaling recipe: no initial servings value set",
318-
);
301+
});
319302
});
320303

321304
it("should not modify the original recipe", () => {

0 commit comments

Comments
 (0)