@tmlmt/cooklang-parser 3.0.0-alpha.20 → 3.0.0-alpha.22

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
@@ -2092,7 +2092,7 @@ function unionOfSets(s1, s2) {
2092
2092
  }
2093
2093
  function getAlternativeSignature(alternatives) {
2094
2094
  if (!alternatives || alternatives.length === 0) return null;
2095
- return alternatives.map((a2) => a2.index).sort((a2, b) => a2 - b).join(",");
2095
+ return alternatives.flat().map((a2) => a2.index).sort((a2, b) => a2 - b).join(",");
2096
2096
  }
2097
2097
 
2098
2098
  // src/classes/pantry.ts
@@ -2487,7 +2487,7 @@ var Section = class {
2487
2487
  };
2488
2488
 
2489
2489
  // src/quantities/alternatives.ts
2490
- var import_big3 = __toESM(require("big.js"), 1);
2490
+ var import_big4 = __toESM(require("big.js"), 1);
2491
2491
 
2492
2492
  // src/units/lookup.ts
2493
2493
  function findListWithCompatibleQuantity(list, quantity) {
@@ -2510,7 +2510,40 @@ function findCompatibleQuantityWithinList(list, quantity) {
2510
2510
  }
2511
2511
 
2512
2512
  // src/utils/general.ts
2513
- var deepClone = (v) => structuredClone(v);
2513
+ var import_big3 = __toESM(require("big.js"), 1);
2514
+ var legacyDeepClone = (v) => {
2515
+ if (v === null || typeof v !== "object") {
2516
+ return v;
2517
+ }
2518
+ if (v instanceof import_big3.default) {
2519
+ return new import_big3.default(v);
2520
+ }
2521
+ if (v instanceof Map) {
2522
+ return new Map(
2523
+ Array.from(v.entries()).map(([k, val]) => [
2524
+ legacyDeepClone(k),
2525
+ legacyDeepClone(val)
2526
+ ])
2527
+ );
2528
+ }
2529
+ if (v instanceof Set) {
2530
+ return new Set(
2531
+ Array.from(v).map((val) => legacyDeepClone(val))
2532
+ );
2533
+ }
2534
+ if (v instanceof Date) {
2535
+ return new Date(v.getTime());
2536
+ }
2537
+ if (Array.isArray(v)) {
2538
+ return v.map((item) => legacyDeepClone(item));
2539
+ }
2540
+ const cloned = {};
2541
+ for (const key of Object.keys(v)) {
2542
+ cloned[key] = legacyDeepClone(v[key]);
2543
+ }
2544
+ return cloned;
2545
+ };
2546
+ var deepClone = (v) => legacyDeepClone(v);
2514
2547
 
2515
2548
  // src/quantities/alternatives.ts
2516
2549
  function getEquivalentUnitsLists(...quantities) {
@@ -2736,7 +2769,7 @@ function regroupQuantitiesAndExpandEquivalents(sum, unitsLists, system) {
2736
2769
  return main.reduce((acc, v) => {
2737
2770
  const mainInList = findCompatibleQuantityWithinList(list, v);
2738
2771
  const conversionRatio = getBaseUnitRatio(v, mainInList);
2739
- const valueInOriginalUnit = (0, import_big3.default)(getAverageValue(v.quantity)).times(
2772
+ const valueInOriginalUnit = (0, import_big4.default)(getAverageValue(v.quantity)).times(
2740
2773
  conversionRatio
2741
2774
  );
2742
2775
  const newValue = {
@@ -2748,7 +2781,7 @@ function regroupQuantitiesAndExpandEquivalents(sum, unitsLists, system) {
2748
2781
  decimal: valueInOriginalUnit.toNumber()
2749
2782
  }
2750
2783
  },
2751
- (0, import_big3.default)(getAverageValue(equiv.quantity)).div(
2784
+ (0, import_big4.default)(getAverageValue(equiv.quantity)).div(
2752
2785
  getAverageValue(mainInList.quantity)
2753
2786
  )
2754
2787
  )
@@ -2832,7 +2865,7 @@ function recomputeEquivalents(primaries, ratioMap, equivUnits) {
2832
2865
  }
2833
2866
 
2834
2867
  // src/classes/recipe.ts
2835
- var import_big4 = __toESM(require("big.js"), 1);
2868
+ var import_big5 = __toESM(require("big.js"), 1);
2836
2869
  var _Recipe = class _Recipe {
2837
2870
  /**
2838
2871
  * Creates a new Recipe instance.
@@ -3372,7 +3405,7 @@ var _Recipe = class _Recipe {
3372
3405
  const currentSubgroupIdx = groupSubgroups.findIndex(
3373
3406
  (sg) => sg.some((alt) => alt.itemId === item.id)
3374
3407
  );
3375
- alternativeRefs = groupSubgroups.filter((_, idx) => idx !== currentSubgroupIdx).flatMap(
3408
+ alternativeRefs = groupSubgroups.filter((_, idx) => idx !== currentSubgroupIdx).map(
3376
3409
  (subgroup) => subgroup.map((otherAlt) => {
3377
3410
  const ref = {
3378
3411
  index: otherAlt.index
@@ -3411,7 +3444,7 @@ var _Recipe = class _Recipe {
3411
3444
  };
3412
3445
  ref.quantities = [altQty];
3413
3446
  }
3414
- return ref;
3447
+ return [ref];
3415
3448
  });
3416
3449
  }
3417
3450
  const altIndices = getAlternativeSignature(alternativeRefs) ?? "";
@@ -3432,28 +3465,36 @@ var _Recipe = class _Recipe {
3432
3465
  if (!groupsForIng.has(signature)) {
3433
3466
  groupsForIng.set(signature, {
3434
3467
  quantities: [],
3435
- alternativeQuantities: /* @__PURE__ */ new Map()
3468
+ alternativeQuantities: /* @__PURE__ */ new Map(),
3469
+ alternativeSubgroups: []
3436
3470
  });
3437
3471
  }
3438
3472
  const group = groupsForIng.get(signature);
3439
3473
  group.quantities.push(quantityEntry);
3440
- for (const ref of alternativeRefs ?? []) {
3441
- if (!group.alternativeQuantities.has(ref.index)) {
3442
- group.alternativeQuantities.set(ref.index, []);
3443
- }
3444
- for (const altQty of ref.quantities ?? []) {
3445
- const extended = toExtendedUnit({
3446
- quantity: altQty.quantity,
3447
- unit: altQty.unit
3448
- });
3449
- if (altQty.equivalents?.length) {
3450
- const eqEntries = [
3451
- extended,
3452
- ...altQty.equivalents.map((eq) => toExtendedUnit(eq))
3453
- ];
3454
- group.alternativeQuantities.get(ref.index).push({ or: eqEntries });
3455
- } else {
3456
- group.alternativeQuantities.get(ref.index).push(extended);
3474
+ if (alternativeRefs && alternativeRefs.length > 0 && group.alternativeSubgroups.length === 0) {
3475
+ group.alternativeSubgroups = alternativeRefs.map(
3476
+ (subgroup) => subgroup.map((ref) => ref.index)
3477
+ );
3478
+ }
3479
+ for (const subgroup of alternativeRefs ?? []) {
3480
+ for (const ref of subgroup) {
3481
+ if (!group.alternativeQuantities.has(ref.index)) {
3482
+ group.alternativeQuantities.set(ref.index, []);
3483
+ }
3484
+ for (const altQty of ref.quantities ?? []) {
3485
+ const extended = toExtendedUnit({
3486
+ quantity: altQty.quantity,
3487
+ unit: altQty.unit
3488
+ });
3489
+ if (altQty.equivalents?.length) {
3490
+ const eqEntries = [
3491
+ extended,
3492
+ ...altQty.equivalents.map((eq) => toExtendedUnit(eq))
3493
+ ];
3494
+ group.alternativeQuantities.get(ref.index).push({ or: eqEntries });
3495
+ } else {
3496
+ group.alternativeQuantities.get(ref.index).push(extended);
3497
+ }
3457
3498
  }
3458
3499
  }
3459
3500
  }
@@ -3576,17 +3617,25 @@ var _Recipe = class _Recipe {
3576
3617
  this.unitSystem
3577
3618
  );
3578
3619
  const flattened = flattenPlainUnitGroup(summed);
3579
- const alternatives = group.alternativeQuantities.size > 0 ? [...group.alternativeQuantities].map(([altIdx, altQtys]) => ({
3580
- index: altIdx,
3581
- ...altQtys.length > 0 && {
3582
- quantities: flattenPlainUnitGroup(
3583
- addEquivalentsAndSimplify(altQtys, this.unitSystem)
3584
- ).flatMap(
3585
- /* v8 ignore next -- item.and branch requires complex nested AND-with-equivalents structure */
3586
- (item) => "quantity" in item ? [item] : item.and
3587
- )
3588
- }
3589
- })) : void 0;
3620
+ let alternatives;
3621
+ if (group.alternativeSubgroups.length > 0) {
3622
+ alternatives = group.alternativeSubgroups.map(
3623
+ (subgroupIndices) => subgroupIndices.map((altIdx) => {
3624
+ const altQtys = group.alternativeQuantities.get(altIdx);
3625
+ return {
3626
+ index: altIdx,
3627
+ ...altQtys.length > 0 && {
3628
+ quantities: flattenPlainUnitGroup(
3629
+ addEquivalentsAndSimplify(altQtys, this.unitSystem)
3630
+ ).flatMap(
3631
+ /* v8 ignore next -- item.and branch requires complex nested AND-with-equivalents structure */
3632
+ (item) => "quantity" in item ? [item] : item.and
3633
+ )
3634
+ }
3635
+ };
3636
+ })
3637
+ );
3638
+ }
3590
3639
  for (const gq of flattened) {
3591
3640
  if ("and" in gq) {
3592
3641
  quantityGroups.push({
@@ -3864,7 +3913,7 @@ var _Recipe = class _Recipe {
3864
3913
  if (originalServings === void 0 || originalServings === 0) {
3865
3914
  originalServings = 1;
3866
3915
  }
3867
- const factor = (0, import_big4.default)(newServings).div(originalServings);
3916
+ const factor = (0, import_big5.default)(newServings).div(originalServings);
3868
3917
  return this.scaleBy(factor);
3869
3918
  }
3870
3919
  /**
@@ -3883,7 +3932,7 @@ var _Recipe = class _Recipe {
3883
3932
  function scaleAlternativesBy(alternatives, factor2) {
3884
3933
  for (const alternative of alternatives) {
3885
3934
  if (alternative.quantity) {
3886
- const scaleFactor = alternative.scalable ? (0, import_big4.default)(factor2) : 1;
3935
+ const scaleFactor = alternative.scalable ? (0, import_big5.default)(factor2) : 1;
3887
3936
  if (alternative.quantity.type !== "fixed" || alternative.quantity.value.type !== "text") {
3888
3937
  alternative.quantity = multiplyQuantityValue(
3889
3938
  alternative.quantity,
@@ -3956,10 +4005,10 @@ var _Recipe = class _Recipe {
3956
4005
  arbitrary.unit = optimized.unit;
3957
4006
  }
3958
4007
  newRecipe._populateIngredientQuantities();
3959
- newRecipe.servings = (0, import_big4.default)(originalServings).times(factor).toNumber();
4008
+ newRecipe.servings = (0, import_big5.default)(originalServings).times(factor).toNumber();
3960
4009
  for (const metaVar of ["servings", "serves"]) {
3961
4010
  if (typeof newRecipe.metadata[metaVar] === "number") {
3962
- newRecipe.metadata[metaVar] = (0, import_big4.default)(newRecipe.metadata[metaVar]).times(factor).toNumber();
4011
+ newRecipe.metadata[metaVar] = (0, import_big5.default)(newRecipe.metadata[metaVar]).times(factor).toNumber();
3963
4012
  }
3964
4013
  }
3965
4014
  if (newRecipe.metadata.yield && this.metadata.yield) {