1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-07-24 15:49:42 +02:00
mealie/dev/ingredientScaler/index.js
Hayden 284df44209
feature/editor-improvements (#289)
* pin editor buttons on scroll

* scaler scratch

* fix langauge assignment 1st pass

* set lang on navigate

* refactor/breakup router

* unify style for language selectro

* refactor/code-cleanup

* refactor/page specific components to page folder

* Fix time card layout issue

* fix timecard display

* update mobile cards / fix overflow errors

Co-authored-by: hay-kot <hay-kot@pm.me>
2021-04-21 21:52:12 -08:00

42 lines
1.3 KiB
JavaScript

import { recipeIngredient } from "./recipeIngredient";
import { recipeNumber } from "./recipeNumber";
export const ingredientScaler = {
process(ingredientArray, scale) {
console.log(scale);
let workingArray = ingredientArray.map(x =>
ingredientScaler.markIngredient(x)
);
return workingArray.map(x => ingredientScaler.adjustIngredients(x, scale));
},
adjustIngredients(ingredient, scale) {
var scaledQuantity = new recipeNumber(ingredient.quantity).multiply(scale);
const newText = ingredient.text.replace(
ingredient.quantity,
scaledQuantity
);
return { ...ingredient, quantity: scaledQuantity, text: newText };
},
markIngredient(ingredient) {
console.log(ingredient);
const returnVar = ingredient.replace(
/^([\d/?[^\s&]*)(?:&nbsp;|\s)(\w*)/g,
(match, quantity, unit) => {
return `${unit}${quantity},${match}`;
}
);
const split = returnVar.split(",");
const [unit, quantity, match] = split;
console.log("Split", unit, quantity, match);
const n = new recipeNumber(quantity);
const i = new recipeIngredient(n, unit);
const serializedQuantity = n.isFraction() ? n.toImproperFraction() : n;
return {
unit: i,
quantity: serializedQuantity.toString(),
text: match,
};
},
};