@tmlmt/cooklang-parser 3.0.0-alpha.21 → 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
@@ -3405,7 +3405,7 @@ var _Recipe = class _Recipe {
3405
3405
  const currentSubgroupIdx = groupSubgroups.findIndex(
3406
3406
  (sg) => sg.some((alt) => alt.itemId === item.id)
3407
3407
  );
3408
- alternativeRefs = groupSubgroups.filter((_, idx) => idx !== currentSubgroupIdx).flatMap(
3408
+ alternativeRefs = groupSubgroups.filter((_, idx) => idx !== currentSubgroupIdx).map(
3409
3409
  (subgroup) => subgroup.map((otherAlt) => {
3410
3410
  const ref = {
3411
3411
  index: otherAlt.index
@@ -3444,7 +3444,7 @@ var _Recipe = class _Recipe {
3444
3444
  };
3445
3445
  ref.quantities = [altQty];
3446
3446
  }
3447
- return ref;
3447
+ return [ref];
3448
3448
  });
3449
3449
  }
3450
3450
  const altIndices = getAlternativeSignature(alternativeRefs) ?? "";
@@ -3465,28 +3465,36 @@ var _Recipe = class _Recipe {
3465
3465
  if (!groupsForIng.has(signature)) {
3466
3466
  groupsForIng.set(signature, {
3467
3467
  quantities: [],
3468
- alternativeQuantities: /* @__PURE__ */ new Map()
3468
+ alternativeQuantities: /* @__PURE__ */ new Map(),
3469
+ alternativeSubgroups: []
3469
3470
  });
3470
3471
  }
3471
3472
  const group = groupsForIng.get(signature);
3472
3473
  group.quantities.push(quantityEntry);
3473
- for (const ref of alternativeRefs ?? []) {
3474
- if (!group.alternativeQuantities.has(ref.index)) {
3475
- group.alternativeQuantities.set(ref.index, []);
3476
- }
3477
- for (const altQty of ref.quantities ?? []) {
3478
- const extended = toExtendedUnit({
3479
- quantity: altQty.quantity,
3480
- unit: altQty.unit
3481
- });
3482
- if (altQty.equivalents?.length) {
3483
- const eqEntries = [
3484
- extended,
3485
- ...altQty.equivalents.map((eq) => toExtendedUnit(eq))
3486
- ];
3487
- group.alternativeQuantities.get(ref.index).push({ or: eqEntries });
3488
- } else {
3489
- 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
+ }
3490
3498
  }
3491
3499
  }
3492
3500
  }
@@ -3609,17 +3617,25 @@ var _Recipe = class _Recipe {
3609
3617
  this.unitSystem
3610
3618
  );
3611
3619
  const flattened = flattenPlainUnitGroup(summed);
3612
- const alternatives = group.alternativeQuantities.size > 0 ? [...group.alternativeQuantities].map(([altIdx, altQtys]) => ({
3613
- index: altIdx,
3614
- ...altQtys.length > 0 && {
3615
- quantities: flattenPlainUnitGroup(
3616
- addEquivalentsAndSimplify(altQtys, this.unitSystem)
3617
- ).flatMap(
3618
- /* v8 ignore next -- item.and branch requires complex nested AND-with-equivalents structure */
3619
- (item) => "quantity" in item ? [item] : item.and
3620
- )
3621
- }
3622
- })) : 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
+ }
3623
3639
  for (const gq of flattened) {
3624
3640
  if ("and" in gq) {
3625
3641
  quantityGroups.push({