@tmlmt/cooklang-parser 3.0.0-alpha.11 → 3.0.0-alpha.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2290,7 +2290,7 @@ var _Recipe = class _Recipe {
2290
2290
  alternative.note = note;
2291
2291
  }
2292
2292
  if (itemQuantity) {
2293
- alternative.itemQuantity = itemQuantity;
2293
+ Object.assign(alternative, itemQuantity);
2294
2294
  }
2295
2295
  alternatives.push(alternative);
2296
2296
  testString = groups.ingredientAlternative || "";
@@ -2398,7 +2398,7 @@ var _Recipe = class _Recipe {
2398
2398
  displayName
2399
2399
  };
2400
2400
  if (itemQuantity) {
2401
- alternative.itemQuantity = itemQuantity;
2401
+ Object.assign(alternative, itemQuantity);
2402
2402
  }
2403
2403
  const existingAlternatives = this.choices.ingredientGroups.get(groupKey);
2404
2404
  function upsertAlternativeToIngredient(ingredients, ingredientIdx, newAlternativeIdx) {
@@ -2534,28 +2534,28 @@ var _Recipe = class _Recipe {
2534
2534
  for (const alt of allAlts) {
2535
2535
  referencedIndices.add(alt.index);
2536
2536
  }
2537
- if (!alternative.itemQuantity) continue;
2537
+ if (!alternative.quantity) continue;
2538
2538
  const baseQty = {
2539
- quantity: alternative.itemQuantity.quantity,
2540
- ...alternative.itemQuantity.unit && {
2541
- unit: alternative.itemQuantity.unit
2539
+ quantity: alternative.quantity,
2540
+ ...alternative.unit && {
2541
+ unit: alternative.unit
2542
2542
  }
2543
2543
  };
2544
- const quantityEntry = alternative.itemQuantity.equivalents?.length ? { or: [baseQty, ...alternative.itemQuantity.equivalents] } : baseQty;
2544
+ const quantityEntry = alternative.equivalents?.length ? { or: [baseQty, ...alternative.equivalents] } : baseQty;
2545
2545
  let alternativeRefs;
2546
2546
  if (!hasExplicitChoice && allAlts.length > 1) {
2547
2547
  alternativeRefs = allAlts.filter(
2548
2548
  (alt) => isGrouped ? alt.itemId !== item.id : alt.index !== alternative.index
2549
2549
  ).map((otherAlt) => {
2550
2550
  const ref = { index: otherAlt.index };
2551
- if (otherAlt.itemQuantity) {
2551
+ if (otherAlt.quantity) {
2552
2552
  const altQty = {
2553
- quantity: otherAlt.itemQuantity.quantity,
2554
- ...otherAlt.itemQuantity.unit && {
2555
- unit: otherAlt.itemQuantity.unit.name
2553
+ quantity: otherAlt.quantity,
2554
+ ...otherAlt.unit && {
2555
+ unit: otherAlt.unit.name
2556
2556
  },
2557
- ...otherAlt.itemQuantity.equivalents && {
2558
- equivalents: otherAlt.itemQuantity.equivalents.map(
2557
+ ...otherAlt.equivalents && {
2558
+ equivalents: otherAlt.equivalents.map(
2559
2559
  (eq) => toPlainUnit(eq)
2560
2560
  )
2561
2561
  }
@@ -2568,14 +2568,10 @@ var _Recipe = class _Recipe {
2568
2568
  const altIndices = getAlternativeSignature(alternativeRefs) ?? "";
2569
2569
  let signature;
2570
2570
  if (isGrouped) {
2571
- const resolvedUnit = resolveUnit(
2572
- alternative.itemQuantity.unit?.name
2573
- );
2571
+ const resolvedUnit = resolveUnit(alternative.unit?.name);
2574
2572
  signature = `group:${item.group}|${altIndices}|${resolvedUnit.type}`;
2575
2573
  } else if (altIndices) {
2576
- const resolvedUnit = resolveUnit(
2577
- alternative.itemQuantity.unit?.name
2578
- );
2574
+ const resolvedUnit = resolveUnit(alternative.unit?.name);
2579
2575
  signature = `${altIndices}|${resolvedUnit.type}}`;
2580
2576
  } else {
2581
2577
  signature = null;
@@ -2843,16 +2839,16 @@ var _Recipe = class _Recipe {
2843
2839
  const unitSystem = this.unitSystem;
2844
2840
  function scaleAlternativesBy(alternatives, factor2) {
2845
2841
  for (const alternative of alternatives) {
2846
- if (alternative.itemQuantity) {
2847
- const scaleFactor = alternative.itemQuantity.scalable ? (0, import_big4.default)(factor2) : 1;
2848
- if (alternative.itemQuantity.quantity.type !== "fixed" || alternative.itemQuantity.quantity.value.type !== "text") {
2849
- alternative.itemQuantity.quantity = multiplyQuantityValue(
2850
- alternative.itemQuantity.quantity,
2842
+ if (alternative.quantity) {
2843
+ const scaleFactor = alternative.scalable ? (0, import_big4.default)(factor2) : 1;
2844
+ if (alternative.quantity.type !== "fixed" || alternative.quantity.value.type !== "text") {
2845
+ alternative.quantity = multiplyQuantityValue(
2846
+ alternative.quantity,
2851
2847
  scaleFactor
2852
2848
  );
2853
2849
  }
2854
- if (alternative.itemQuantity.equivalents) {
2855
- alternative.itemQuantity.equivalents = alternative.itemQuantity.equivalents.map(
2850
+ if (alternative.equivalents) {
2851
+ alternative.equivalents = alternative.equivalents.map(
2856
2852
  (altQuantity) => {
2857
2853
  if (altQuantity.quantity.type === "fixed" && altQuantity.quantity.value.type === "text") {
2858
2854
  return altQuantity;
@@ -2870,15 +2866,15 @@ var _Recipe = class _Recipe {
2870
2866
  }
2871
2867
  const optimizedPrimary = applyBestUnit(
2872
2868
  {
2873
- quantity: alternative.itemQuantity.quantity,
2874
- unit: alternative.itemQuantity.unit
2869
+ quantity: alternative.quantity,
2870
+ unit: alternative.unit
2875
2871
  },
2876
2872
  unitSystem
2877
2873
  );
2878
- alternative.itemQuantity.quantity = optimizedPrimary.quantity;
2879
- alternative.itemQuantity.unit = optimizedPrimary.unit;
2880
- if (alternative.itemQuantity.equivalents) {
2881
- alternative.itemQuantity.equivalents = alternative.itemQuantity.equivalents.map(
2874
+ alternative.quantity = optimizedPrimary.quantity;
2875
+ alternative.unit = optimizedPrimary.unit;
2876
+ if (alternative.equivalents) {
2877
+ alternative.equivalents = alternative.equivalents.map(
2882
2878
  (eq) => applyBestUnit(eq, unitSystem)
2883
2879
  );
2884
2880
  }
@@ -2982,18 +2978,27 @@ var _Recipe = class _Recipe {
2982
2978
  }
2983
2979
  return newPrimary;
2984
2980
  }
2985
- function convertItemQuantity(itemQuantity) {
2986
- const primaryUnit = resolveUnit(itemQuantity.unit?.name);
2987
- const equivalents = itemQuantity.equivalents ?? [];
2981
+ function convertAlternativeQuantity(alternative) {
2982
+ const primaryUnit = resolveUnit(alternative.unit?.name);
2983
+ const equivalents = alternative.equivalents ?? [];
2988
2984
  const oldPrimary = {
2989
- quantity: itemQuantity.quantity,
2990
- unit: itemQuantity.unit
2985
+ quantity: alternative.quantity,
2986
+ unit: alternative.unit
2991
2987
  };
2992
2988
  if (primaryUnit.type !== "other" && isUnitCompatibleWithSystem(primaryUnit, system)) {
2993
2989
  if (method === "remove") {
2994
- return { ...itemQuantity, equivalents: void 0 };
2990
+ return {
2991
+ quantity: alternative.quantity,
2992
+ unit: alternative.unit,
2993
+ scalable: alternative.scalable
2994
+ };
2995
2995
  }
2996
- return itemQuantity;
2996
+ return {
2997
+ quantity: alternative.quantity,
2998
+ unit: alternative.unit,
2999
+ scalable: alternative.scalable,
3000
+ equivalents
3001
+ };
2997
3002
  }
2998
3003
  const targetEquivIndex = equivalents.findIndex((eq) => {
2999
3004
  const eqUnit = resolveUnit(eq.unit?.name);
@@ -3008,7 +3013,7 @@ var _Recipe = class _Recipe {
3008
3013
  targetEquiv,
3009
3014
  oldPrimary,
3010
3015
  remainingEquivalents,
3011
- itemQuantity.scalable,
3016
+ alternative.scalable,
3012
3017
  targetEquiv.unit?.integerProtected,
3013
3018
  "swapped"
3014
3019
  );
@@ -3019,8 +3024,8 @@ var _Recipe = class _Recipe {
3019
3024
  converted,
3020
3025
  oldPrimary,
3021
3026
  equivalents,
3022
- itemQuantity.scalable,
3023
- itemQuantity.unit?.integerProtected,
3027
+ alternative.scalable,
3028
+ alternative.unit?.integerProtected,
3024
3029
  "swapped"
3025
3030
  );
3026
3031
  }
@@ -3033,24 +3038,37 @@ var _Recipe = class _Recipe {
3033
3038
  convertedEquiv,
3034
3039
  oldPrimary,
3035
3040
  remainingEquivalents,
3036
- itemQuantity.scalable,
3041
+ alternative.scalable,
3037
3042
  equiv.unit?.integerProtected,
3038
3043
  "converted"
3039
3044
  );
3040
3045
  }
3041
3046
  }
3042
3047
  if (method === "remove") {
3043
- return { ...itemQuantity, equivalents: void 0 };
3048
+ return {
3049
+ quantity: alternative.quantity,
3050
+ unit: alternative.unit,
3051
+ scalable: alternative.scalable
3052
+ };
3044
3053
  } else {
3045
- return itemQuantity;
3054
+ return {
3055
+ quantity: alternative.quantity,
3056
+ unit: alternative.unit,
3057
+ scalable: alternative.scalable,
3058
+ equivalents
3059
+ };
3046
3060
  }
3047
3061
  }
3048
3062
  function convertAlternatives(alternatives) {
3049
3063
  for (const alternative of alternatives) {
3050
- if (alternative.itemQuantity) {
3051
- alternative.itemQuantity = convertItemQuantity(
3052
- alternative.itemQuantity
3064
+ if (alternative.quantity) {
3065
+ const converted = convertAlternativeQuantity(
3066
+ alternative
3053
3067
  );
3068
+ alternative.quantity = converted.quantity;
3069
+ alternative.unit = converted.unit;
3070
+ alternative.scalable = converted.scalable;
3071
+ alternative.equivalents = converted.equivalents;
3054
3072
  }
3055
3073
  }
3056
3074
  }