Skip to content

Commit

Permalink
Move new code to separate module and interlink correctly
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 d21260f commit 4511252
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 40 deletions.
59 changes: 19 additions & 40 deletions src/components/RecipeView.vue
Expand Up @@ -138,16 +138,17 @@
</template>
{{ t("cookbook", "Copy ingredients") }}
</NcButton>
<h3 v-if="parsedIngredients.length">
<h3 v-if="scaledIngredients.length">
{{ t("cookbook", "Ingredients") }}
</h3>
<ul v-if="parsedIngredients.length">
<ul v-if="scaledIngredients.length">
<RecipeIngredient
v-for="(ingredient, idx) in parsedIngredients"
v-for="(ingredient, idx) in scaledIngredients"
:key="'ingr' + idx"
:ingredient="ingredient"
:ingredient-has-correct-syntax="
validateIngredientSyntax(ingredient)
/* yieldCalculator.isValidIngredientSyntax(ingredient) */
ingredientsWithValidSyntax[idx]
"
:recipe-ingredients-have-subgroups="
recipeIngredientsHaveSubgroups
Expand Down Expand Up @@ -318,6 +319,7 @@ import api from "cookbook/js/api-interface"
import helpers from "cookbook/js/helper"
import normalizeMarkdown from "cookbook/js/title-rename"
import { showSimpleAlertModal } from "cookbook/js/modals"
import yieldCalculator from "cookbook/js/yieldCalculator"
import ContentCopyIcon from "icons/ContentCopy.vue"
Expand Down Expand Up @@ -518,8 +520,19 @@ export default {
visibleInfoBlocks() {
return this.$store.state.config?.visibleInfoBlocks ?? {}
},
scaledIngredients() {
return yieldCalculator.recalculateIngredients(
this.parsedIngredients,
this.recipeYield,
this.$store.state.recipe.recipeYield,
)
},
ingredientsWithValidSyntax() {
return this.parsedIngredients.map(yieldCalculator.isValidIngredientSyntax)
},
ingredientsSyntaxCorrect() {
return this.parsedIngredients.every(this.validateIngredientSyntax)
return this.ingredientsWithValidSyntax.every(x => x)
// return yieldCalculator.isIngredientsArrayValid(this.parsedIngredients)
},
},
watch: {
Expand Down Expand Up @@ -593,8 +606,6 @@ export default {
if (this.recipeYield < 0) {
this.restoreOriginalRecipeYield()
}
this.recalculateIngredients()
},
},
mounted() {
Expand Down Expand Up @@ -682,43 +693,11 @@ export default {
this.recipeYield = this.$store.state.recipe.recipeYield
},
validateIngredientSyntax(ingredient) {
return helpers.validateIngredientSyntax(ingredient)
},
changeRecipeYield(increase = true) {
this.recipeYield = +this.recipeYield + (increase ? 1 : -1)
},
recalculateIngredients() {
this.parsedIngredients = this.parsedIngredients.map(
(ingredient, index) => {
if (this.validateIngredientSyntax(ingredient)) {
// For some cases, where the unit is not seperated from the amount: 100g cheese
const possibleUnit = ingredient
.split(" ")[0]
.replace(/[^a-zA-Z]/g, "")
const amount = parseFloat(
this.$store.state.recipe.recipeIngredient[
index
].split(" ")[0]
)
const unitAndIngredient = ingredient
.split(" ")
.slice(1)
.join(" ")
let newAmount =
(amount / this.$store.state.recipe.recipeYield) *
this.recipeYield
newAmount = newAmount.toFixed(2).replace(/[.]00$/, "")
return `${newAmount}${possibleUnit} ${unitAndIngredient}`
}
return ingredient
}
)
},
copyIngredientsToClipboard() {
const ingredientsToCopy = this.parsedIngredients.join("\n")
const ingredientsToCopy = this.scaledIngredients.join("\n")
if (navigator.clipboard) {
navigator.clipboard
Expand Down
71 changes: 71 additions & 0 deletions src/js/yieldCalculator.js
@@ -0,0 +1,71 @@
function isValidIngredientSyntax(ingredient) {
/*
Explanation of ingredientSyntaxRegExp:
^: Start of string
(?:\d+(?:\.\d+)?|\.\d+): Non-capturing group that matches either a positive float value or a positive integer value. The first alternative matches one or more digits, followed by an optional decimal part consisting of a dot and one or more digits. The second alternative matches a decimal point followed by one or more digits.
(?:\s.+$|\s\S+$): Non-capturing group that matches a whitespace character followed by any character with unlimited length or any special character with unlimited length. The first alternative matches a whitespace character followed by any character(s) until the end of the string. The second alternative matches a whitespace character followed by any non-whitespace character(s) until the end of the string.
$: End of string
*/
const ingredientSyntaxRegExp = /^(?:\d+(?:\.\d+)?(?:\/\d+)?)\s?.*$/
// Regular expression to match all possible fractions within a string
const ingredientFractionRegExp = /\b\d+\/\d+\b/g
/*
Explanation of ingredientMultipleSeperatorsRegExp:
/^ - Start of the string
-? - Matches an optional minus sign
\d+ - Matches one or more digits
(?:[.,]\d+){2,} - Non-capturing group that matches a separator (.,) followed by one or more digits.
The {2,} quantifier ensures that there are at least two occurrences of this pattern.
.* - Matches any characters (except newline) zero or more times.
*/
const ingredientMultipleSeperatorsRegExp = /^-?\d+(?:[.,]\d+){2,}.*/

return (
ingredientSyntaxRegExp.test(ingredient) &&
!ingredientFractionRegExp.test(ingredient) &&
!ingredientMultipleSeperatorsRegExp.test(ingredient)
)
}

function isIngredientsArrayValid(ingredients) {
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 `${newAmount}${possibleUnit} ${unitAndIngredient}`
}

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
}

0 comments on commit 4511252

Please sign in to comment.