Skip to content

Commit

Permalink
Fix code styling
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Wolf <github@christianwolf.email>
  • Loading branch information
christianlupus committed Jun 30, 2023
1 parent 4511252 commit 45166e0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
9 changes: 5 additions & 4 deletions src/components/RecipeView.vue
Expand Up @@ -524,15 +524,16 @@ export default {
return yieldCalculator.recalculateIngredients(
this.parsedIngredients,
this.recipeYield,
this.$store.state.recipe.recipeYield,
this.$store.state.recipe.recipeYield
)
},
ingredientsWithValidSyntax() {
return this.parsedIngredients.map(yieldCalculator.isValidIngredientSyntax)
return this.parsedIngredients.map(
yieldCalculator.isValidIngredientSyntax
)
},
ingredientsSyntaxCorrect() {
return this.ingredientsWithValidSyntax.every(x => x)
// return yieldCalculator.isIngredientsArrayValid(this.parsedIngredients)
return this.ingredientsWithValidSyntax.every((x) => x)
},
},
watch: {
Expand Down
57 changes: 25 additions & 32 deletions src/js/yieldCalculator.js
Expand Up @@ -28,44 +28,37 @@ function isValidIngredientSyntax(ingredient) {
}

function isIngredientsArrayValid(ingredients) {
return ingredients.every(isValidIngredientSyntax)
return ingredients.every(isValidIngredientSyntax)
}

function recalculateIngredients(ingredients, currentYield, originalYield) {
return ingredients.map(
(ingredient, index) => {
if (isValidIngredientSyntax(ingredient)) {
// For some cases, where the unit is not separated from the amount: 100g cheese
const possibleUnit = ingredient
.split(" ")[0]
.replace(/[^a-zA-Z]/g, "")
const amount = parseFloat(
ingredients[index].split(" ")[0]
)
const unitAndIngredient = ingredient
.split(" ")
.slice(1)
.join(" ")
let newAmount = (amount / originalYield) * currentYield
newAmount = newAmount.toFixed(2).replace(/[.]00$/, "")
return ingredients.map((ingredient, index) => {
if (isValidIngredientSyntax(ingredient)) {
// For some cases, where the unit is not separated from the amount: 100g cheese
const possibleUnit = ingredient
.split(" ")[0]
.replace(/[^a-zA-Z]/g, "")
const amount = parseFloat(ingredients[index].split(" ")[0])
const unitAndIngredient = ingredient.split(" ").slice(1).join(" ")
let newAmount = (amount / originalYield) * currentYield
newAmount = newAmount.toFixed(2).replace(/[.]00$/, "")

return `${newAmount}${possibleUnit} ${unitAndIngredient}`
}
return `${newAmount}${possibleUnit} ${unitAndIngredient}`
}

const factor = (currentYield/originalYield)
const prefix= ((f) => {
if(f === 1){
return ''
}
return `${f.toFixed(2)}x `
})(factor)
return `${prefix}${ingredient}`
}
)
const factor = currentYield / originalYield
const prefix = ((f) => {
if (f === 1) {
return ""
}
return `${f.toFixed(2)}x `
})(factor)
return `${prefix}${ingredient}`
})
}

export default {
isValidIngredientSyntax,
isIngredientsArrayValid,
recalculateIngredients
isValidIngredientSyntax,
isIngredientsArrayValid,
recalculateIngredients,
}

0 comments on commit 45166e0

Please sign in to comment.