Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle fractional ingredients (Fixes #2004) #2421

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changelog/current/2421-fix-fraction-ingrediants.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Fixes

- Fraction calculation keeps fractions if possible

42 changes: 34 additions & 8 deletions src/js/yieldCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
The ingredientFractionRegExp is used to identify fractions in the string.
This is used to exclude strings that contain fractions from being valid.
*/
const fractionRegExp = /^((\d+\s+)?(\d+)\s*\/\s*(\d+)).*/;
const fractionRegExp = /^((\d+\s+)?(?:\p{No}|(\d+)\s*\/\s*(\d+))).*/u;

function isValidIngredientSyntax(ingredient) {
/*
The ingredientSyntaxRegExp checks whether the ingredient string starts with a number,
possibly followed by a fractional part or a fraction. Then there should be a space
and then any sequence of characters.
The ingredientSyntaxRegExp checks whether the ingredient string starts with a fraction,
or a whole part and fraction, or a decimal.
It may optionally have a unit but must be proceeded by a single whitespace and further text.
*/
const ingredientSyntaxRegExp = /^(?:\d+(?:\.\d+)?(?:\/\d+)?)\s?.*$/;
const ingredientSyntaxRegExp =
/^(?:(?:\d+\s)?(?:\d+\/\d+|\p{No})|\d+(?:\.\d+)?)[a-zA-z]*\s.*$/;

/*
The ingredientMultipleSeperatorsRegExp is used to check whether the string contains
Expand Down Expand Up @@ -47,6 +48,7 @@ function recalculateIngredients(ingredients, currentYield, originalYield) {

const matches = ingredient.match(fractionRegExp);

// Fraction
if (matches) {
const [
,
Expand All @@ -58,17 +60,41 @@ function recalculateIngredients(ingredients, currentYield, originalYield) {
const wholeNumberPart = wholeNumberPartRaw
? parseInt(wholeNumberPartRaw, 10)
: 0;
const numerator = parseInt(numeratorRaw, 10);
const denominator = parseInt(denominatorRaw, 10);
let numerator = 0;
let denominator = 0;
// Unicode fraction
if (numeratorRaw == null) {
[numerator, denominator] = fractionMatch
.normalize('NFKD')
.split('\u2044')
.map((x) => parseInt(x, 10));
} else {
numerator = parseInt(numeratorRaw, 10);
denominator = parseInt(denominatorRaw, 10);
}

const decimalAmount = wholeNumberPart + numerator / denominator;
let newAmount = (decimalAmount / originalYield) * currentYield;
newAmount = newAmount.toFixed(2).replace(/[.]00$/, '');
const newWholeNumberPart = parseInt(newAmount, 10);
let newNumerator = (newAmount - newWholeNumberPart) * 16;
if (Number.isInteger(newNumerator)) {
const gcd = (a, b) => (b ? gcd(b, a % b) : a);
const div = gcd(newNumerator, 16);
newNumerator /= div;
const newDenominator = 16 / div;
const prefix = newWholeNumberPart
? `${newWholeNumberPart} `
: '';
newAmount = `${prefix}${newNumerator}/${newDenominator}`;
} else {
newAmount = newAmount.toFixed(2).replace(/[.]00$/, '');
}

const newIngredient = ingredient.replace(fractionMatch, newAmount);
return newIngredient;
}

// Decimal
if (isValidIngredientSyntax(ingredient)) {
const possibleUnit = ingredient
.split(' ')[0]
Expand Down
Loading