@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.d.cts CHANGED
@@ -509,7 +509,7 @@ interface IngredientExtras {
509
509
  * Used if: the ingredient is a recipe
510
510
  *
511
511
  * @example
512
- * ```cooklang
512
+ * ```yaml
513
513
  * Take @./essentials/doughs/pizza dough{1} out of the freezer and let it unfreeze overnight
514
514
  * ```
515
515
  * Would lead to:
@@ -541,9 +541,12 @@ interface AlternativeIngredientRef {
541
541
  interface IngredientQuantityGroup extends QuantityWithPlainUnit {
542
542
  /**
543
543
  * References to alternative ingredients for this quantity group.
544
+ * Each inner array represents one alternative choice option (subgroup).
545
+ * Items within the same inner array are combined with "+" (AND),
546
+ * while different inner arrays represent "or" alternatives.
544
547
  * If undefined, this group has no alternatives.
545
548
  */
546
- alternatives?: AlternativeIngredientRef[];
549
+ alternatives?: AlternativeIngredientRef[][];
547
550
  }
548
551
  /**
549
552
  * Represents an AND group of quantities when primary units are incompatible but equivalents can be summed.
@@ -557,9 +560,12 @@ interface IngredientQuantityAndGroup extends FlatAndGroup<QuantityWithPlainUnit>
557
560
  equivalents?: QuantityWithPlainUnit[];
558
561
  /**
559
562
  * References to alternative ingredients for this quantity group.
563
+ * Each inner array represents one alternative choice option (subgroup).
564
+ * Items within the same inner array are combined with "+" (AND),
565
+ * while different inner arrays represent "or" alternatives.
560
566
  * If undefined, this group has no alternatives.
561
567
  */
562
- alternatives?: AlternativeIngredientRef[];
568
+ alternatives?: AlternativeIngredientRef[][];
563
569
  }
564
570
  /**
565
571
  * Represents an ingredient in a recipe.
@@ -1760,15 +1766,15 @@ declare function renderFractionAsVulgar(num: number, den: number): string;
1760
1766
  * Format a numeric value (decimal or fraction) to a string.
1761
1767
  *
1762
1768
  * @param value - The decimal or fraction value to format
1763
- * @param useVulgar - Whether to use Unicode vulgar fraction characters (default: false)
1769
+ * @param useVulgar - Whether to use Unicode vulgar fraction characters (default: true)
1764
1770
  * @returns The formatted string representation
1765
1771
  * @category Helpers
1766
1772
  *
1767
1773
  * @example
1768
1774
  * ```typescript
1769
1775
  * formatNumericValue({ type: "decimal", decimal: 1.5 }); // "1.5"
1770
- * formatNumericValue({ type: "fraction", num: 1, den: 2 }); // "1/2"
1771
- * formatNumericValue({ type: "fraction", num: 1, den: 2 }, true); // "½"
1776
+ * formatNumericValue({ type: "fraction", num: 1, den: 2 }); // "½"
1777
+ * formatNumericValue({ type: "fraction", num: 1, den: 2 }, false); // "1/2"
1772
1778
  * formatNumericValue({ type: "fraction", num: 5, den: 4 }, true); // "1¼"
1773
1779
  * ```
1774
1780
  */
@@ -1899,7 +1905,7 @@ declare function isGroupedItem(item: IngredientItem): boolean;
1899
1905
  * for (const item of step.items) {
1900
1906
  * if (item.type === 'ingredient') {
1901
1907
  * item.alternatives.forEach((alt, idx) => {
1902
- * const isSelected = isAlternativeSelected(item, idx, recipe, choices);
1908
+ * const isSelected = isAlternativeSelected(recipe, choices, item, idx);
1903
1909
  * // Render differently based on isSelected
1904
1910
  * });
1905
1911
  * }
@@ -2033,15 +2039,18 @@ declare function isSimpleGroup(entry: IngredientQuantityGroup | IngredientQuanti
2033
2039
  * for (const entry of ingredient.quantities) {
2034
2040
  * if (hasAlternatives(entry)) {
2035
2041
  * // entry.alternatives is available and non-empty
2036
- * for (const alt of entry.alternatives) {
2037
- * console.log(`Alternative ingredient index: ${alt.index}`);
2042
+ * for (const subgroup of entry.alternatives) {
2043
+ * // Each subgroup is one "or" choice; items within are combined with "+"
2044
+ * for (const alt of subgroup) {
2045
+ * console.log(`Alternative ingredient index: ${alt.index}`);
2046
+ * }
2038
2047
  * }
2039
2048
  * }
2040
2049
  * }
2041
2050
  * ```
2042
2051
  */
2043
2052
  declare function hasAlternatives(entry: IngredientQuantityGroup | IngredientQuantityAndGroup): entry is (IngredientQuantityGroup | IngredientQuantityAndGroup) & {
2044
- alternatives: AlternativeIngredientRef[];
2053
+ alternatives: AlternativeIngredientRef[][];
2045
2054
  };
2046
2055
 
2047
2056
  /**
package/dist/index.d.ts CHANGED
@@ -509,7 +509,7 @@ interface IngredientExtras {
509
509
  * Used if: the ingredient is a recipe
510
510
  *
511
511
  * @example
512
- * ```cooklang
512
+ * ```yaml
513
513
  * Take @./essentials/doughs/pizza dough{1} out of the freezer and let it unfreeze overnight
514
514
  * ```
515
515
  * Would lead to:
@@ -541,9 +541,12 @@ interface AlternativeIngredientRef {
541
541
  interface IngredientQuantityGroup extends QuantityWithPlainUnit {
542
542
  /**
543
543
  * References to alternative ingredients for this quantity group.
544
+ * Each inner array represents one alternative choice option (subgroup).
545
+ * Items within the same inner array are combined with "+" (AND),
546
+ * while different inner arrays represent "or" alternatives.
544
547
  * If undefined, this group has no alternatives.
545
548
  */
546
- alternatives?: AlternativeIngredientRef[];
549
+ alternatives?: AlternativeIngredientRef[][];
547
550
  }
548
551
  /**
549
552
  * Represents an AND group of quantities when primary units are incompatible but equivalents can be summed.
@@ -557,9 +560,12 @@ interface IngredientQuantityAndGroup extends FlatAndGroup<QuantityWithPlainUnit>
557
560
  equivalents?: QuantityWithPlainUnit[];
558
561
  /**
559
562
  * References to alternative ingredients for this quantity group.
563
+ * Each inner array represents one alternative choice option (subgroup).
564
+ * Items within the same inner array are combined with "+" (AND),
565
+ * while different inner arrays represent "or" alternatives.
560
566
  * If undefined, this group has no alternatives.
561
567
  */
562
- alternatives?: AlternativeIngredientRef[];
568
+ alternatives?: AlternativeIngredientRef[][];
563
569
  }
564
570
  /**
565
571
  * Represents an ingredient in a recipe.
@@ -1760,15 +1766,15 @@ declare function renderFractionAsVulgar(num: number, den: number): string;
1760
1766
  * Format a numeric value (decimal or fraction) to a string.
1761
1767
  *
1762
1768
  * @param value - The decimal or fraction value to format
1763
- * @param useVulgar - Whether to use Unicode vulgar fraction characters (default: false)
1769
+ * @param useVulgar - Whether to use Unicode vulgar fraction characters (default: true)
1764
1770
  * @returns The formatted string representation
1765
1771
  * @category Helpers
1766
1772
  *
1767
1773
  * @example
1768
1774
  * ```typescript
1769
1775
  * formatNumericValue({ type: "decimal", decimal: 1.5 }); // "1.5"
1770
- * formatNumericValue({ type: "fraction", num: 1, den: 2 }); // "1/2"
1771
- * formatNumericValue({ type: "fraction", num: 1, den: 2 }, true); // "½"
1776
+ * formatNumericValue({ type: "fraction", num: 1, den: 2 }); // "½"
1777
+ * formatNumericValue({ type: "fraction", num: 1, den: 2 }, false); // "1/2"
1772
1778
  * formatNumericValue({ type: "fraction", num: 5, den: 4 }, true); // "1¼"
1773
1779
  * ```
1774
1780
  */
@@ -1899,7 +1905,7 @@ declare function isGroupedItem(item: IngredientItem): boolean;
1899
1905
  * for (const item of step.items) {
1900
1906
  * if (item.type === 'ingredient') {
1901
1907
  * item.alternatives.forEach((alt, idx) => {
1902
- * const isSelected = isAlternativeSelected(item, idx, recipe, choices);
1908
+ * const isSelected = isAlternativeSelected(recipe, choices, item, idx);
1903
1909
  * // Render differently based on isSelected
1904
1910
  * });
1905
1911
  * }
@@ -2033,15 +2039,18 @@ declare function isSimpleGroup(entry: IngredientQuantityGroup | IngredientQuanti
2033
2039
  * for (const entry of ingredient.quantities) {
2034
2040
  * if (hasAlternatives(entry)) {
2035
2041
  * // entry.alternatives is available and non-empty
2036
- * for (const alt of entry.alternatives) {
2037
- * console.log(`Alternative ingredient index: ${alt.index}`);
2042
+ * for (const subgroup of entry.alternatives) {
2043
+ * // Each subgroup is one "or" choice; items within are combined with "+"
2044
+ * for (const alt of subgroup) {
2045
+ * console.log(`Alternative ingredient index: ${alt.index}`);
2046
+ * }
2038
2047
  * }
2039
2048
  * }
2040
2049
  * }
2041
2050
  * ```
2042
2051
  */
2043
2052
  declare function hasAlternatives(entry: IngredientQuantityGroup | IngredientQuantityAndGroup): entry is (IngredientQuantityGroup | IngredientQuantityAndGroup) & {
2044
- alternatives: AlternativeIngredientRef[];
2053
+ alternatives: AlternativeIngredientRef[][];
2045
2054
  };
2046
2055
 
2047
2056
  /**
package/dist/index.js CHANGED
@@ -2033,7 +2033,7 @@ function unionOfSets(s1, s2) {
2033
2033
  }
2034
2034
  function getAlternativeSignature(alternatives) {
2035
2035
  if (!alternatives || alternatives.length === 0) return null;
2036
- return alternatives.map((a2) => a2.index).sort((a2, b) => a2 - b).join(",");
2036
+ return alternatives.flat().map((a2) => a2.index).sort((a2, b) => a2 - b).join(",");
2037
2037
  }
2038
2038
 
2039
2039
  // src/classes/pantry.ts
@@ -2428,7 +2428,7 @@ var Section = class {
2428
2428
  };
2429
2429
 
2430
2430
  // src/quantities/alternatives.ts
2431
- import Big3 from "big.js";
2431
+ import Big4 from "big.js";
2432
2432
 
2433
2433
  // src/units/lookup.ts
2434
2434
  function findListWithCompatibleQuantity(list, quantity) {
@@ -2451,7 +2451,40 @@ function findCompatibleQuantityWithinList(list, quantity) {
2451
2451
  }
2452
2452
 
2453
2453
  // src/utils/general.ts
2454
- var deepClone = (v) => structuredClone(v);
2454
+ import Big3 from "big.js";
2455
+ var legacyDeepClone = (v) => {
2456
+ if (v === null || typeof v !== "object") {
2457
+ return v;
2458
+ }
2459
+ if (v instanceof Big3) {
2460
+ return new Big3(v);
2461
+ }
2462
+ if (v instanceof Map) {
2463
+ return new Map(
2464
+ Array.from(v.entries()).map(([k, val]) => [
2465
+ legacyDeepClone(k),
2466
+ legacyDeepClone(val)
2467
+ ])
2468
+ );
2469
+ }
2470
+ if (v instanceof Set) {
2471
+ return new Set(
2472
+ Array.from(v).map((val) => legacyDeepClone(val))
2473
+ );
2474
+ }
2475
+ if (v instanceof Date) {
2476
+ return new Date(v.getTime());
2477
+ }
2478
+ if (Array.isArray(v)) {
2479
+ return v.map((item) => legacyDeepClone(item));
2480
+ }
2481
+ const cloned = {};
2482
+ for (const key of Object.keys(v)) {
2483
+ cloned[key] = legacyDeepClone(v[key]);
2484
+ }
2485
+ return cloned;
2486
+ };
2487
+ var deepClone = (v) => legacyDeepClone(v);
2455
2488
 
2456
2489
  // src/quantities/alternatives.ts
2457
2490
  function getEquivalentUnitsLists(...quantities) {
@@ -2677,7 +2710,7 @@ function regroupQuantitiesAndExpandEquivalents(sum, unitsLists, system) {
2677
2710
  return main.reduce((acc, v) => {
2678
2711
  const mainInList = findCompatibleQuantityWithinList(list, v);
2679
2712
  const conversionRatio = getBaseUnitRatio(v, mainInList);
2680
- const valueInOriginalUnit = Big3(getAverageValue(v.quantity)).times(
2713
+ const valueInOriginalUnit = Big4(getAverageValue(v.quantity)).times(
2681
2714
  conversionRatio
2682
2715
  );
2683
2716
  const newValue = {
@@ -2689,7 +2722,7 @@ function regroupQuantitiesAndExpandEquivalents(sum, unitsLists, system) {
2689
2722
  decimal: valueInOriginalUnit.toNumber()
2690
2723
  }
2691
2724
  },
2692
- Big3(getAverageValue(equiv.quantity)).div(
2725
+ Big4(getAverageValue(equiv.quantity)).div(
2693
2726
  getAverageValue(mainInList.quantity)
2694
2727
  )
2695
2728
  )
@@ -2773,7 +2806,7 @@ function recomputeEquivalents(primaries, ratioMap, equivUnits) {
2773
2806
  }
2774
2807
 
2775
2808
  // src/classes/recipe.ts
2776
- import Big4 from "big.js";
2809
+ import Big5 from "big.js";
2777
2810
  var _Recipe = class _Recipe {
2778
2811
  /**
2779
2812
  * Creates a new Recipe instance.
@@ -3313,7 +3346,7 @@ var _Recipe = class _Recipe {
3313
3346
  const currentSubgroupIdx = groupSubgroups.findIndex(
3314
3347
  (sg) => sg.some((alt) => alt.itemId === item.id)
3315
3348
  );
3316
- alternativeRefs = groupSubgroups.filter((_, idx) => idx !== currentSubgroupIdx).flatMap(
3349
+ alternativeRefs = groupSubgroups.filter((_, idx) => idx !== currentSubgroupIdx).map(
3317
3350
  (subgroup) => subgroup.map((otherAlt) => {
3318
3351
  const ref = {
3319
3352
  index: otherAlt.index
@@ -3352,7 +3385,7 @@ var _Recipe = class _Recipe {
3352
3385
  };
3353
3386
  ref.quantities = [altQty];
3354
3387
  }
3355
- return ref;
3388
+ return [ref];
3356
3389
  });
3357
3390
  }
3358
3391
  const altIndices = getAlternativeSignature(alternativeRefs) ?? "";
@@ -3373,28 +3406,36 @@ var _Recipe = class _Recipe {
3373
3406
  if (!groupsForIng.has(signature)) {
3374
3407
  groupsForIng.set(signature, {
3375
3408
  quantities: [],
3376
- alternativeQuantities: /* @__PURE__ */ new Map()
3409
+ alternativeQuantities: /* @__PURE__ */ new Map(),
3410
+ alternativeSubgroups: []
3377
3411
  });
3378
3412
  }
3379
3413
  const group = groupsForIng.get(signature);
3380
3414
  group.quantities.push(quantityEntry);
3381
- for (const ref of alternativeRefs ?? []) {
3382
- if (!group.alternativeQuantities.has(ref.index)) {
3383
- group.alternativeQuantities.set(ref.index, []);
3384
- }
3385
- for (const altQty of ref.quantities ?? []) {
3386
- const extended = toExtendedUnit({
3387
- quantity: altQty.quantity,
3388
- unit: altQty.unit
3389
- });
3390
- if (altQty.equivalents?.length) {
3391
- const eqEntries = [
3392
- extended,
3393
- ...altQty.equivalents.map((eq) => toExtendedUnit(eq))
3394
- ];
3395
- group.alternativeQuantities.get(ref.index).push({ or: eqEntries });
3396
- } else {
3397
- group.alternativeQuantities.get(ref.index).push(extended);
3415
+ if (alternativeRefs && alternativeRefs.length > 0 && group.alternativeSubgroups.length === 0) {
3416
+ group.alternativeSubgroups = alternativeRefs.map(
3417
+ (subgroup) => subgroup.map((ref) => ref.index)
3418
+ );
3419
+ }
3420
+ for (const subgroup of alternativeRefs ?? []) {
3421
+ for (const ref of subgroup) {
3422
+ if (!group.alternativeQuantities.has(ref.index)) {
3423
+ group.alternativeQuantities.set(ref.index, []);
3424
+ }
3425
+ for (const altQty of ref.quantities ?? []) {
3426
+ const extended = toExtendedUnit({
3427
+ quantity: altQty.quantity,
3428
+ unit: altQty.unit
3429
+ });
3430
+ if (altQty.equivalents?.length) {
3431
+ const eqEntries = [
3432
+ extended,
3433
+ ...altQty.equivalents.map((eq) => toExtendedUnit(eq))
3434
+ ];
3435
+ group.alternativeQuantities.get(ref.index).push({ or: eqEntries });
3436
+ } else {
3437
+ group.alternativeQuantities.get(ref.index).push(extended);
3438
+ }
3398
3439
  }
3399
3440
  }
3400
3441
  }
@@ -3517,17 +3558,25 @@ var _Recipe = class _Recipe {
3517
3558
  this.unitSystem
3518
3559
  );
3519
3560
  const flattened = flattenPlainUnitGroup(summed);
3520
- const alternatives = group.alternativeQuantities.size > 0 ? [...group.alternativeQuantities].map(([altIdx, altQtys]) => ({
3521
- index: altIdx,
3522
- ...altQtys.length > 0 && {
3523
- quantities: flattenPlainUnitGroup(
3524
- addEquivalentsAndSimplify(altQtys, this.unitSystem)
3525
- ).flatMap(
3526
- /* v8 ignore next -- item.and branch requires complex nested AND-with-equivalents structure */
3527
- (item) => "quantity" in item ? [item] : item.and
3528
- )
3529
- }
3530
- })) : void 0;
3561
+ let alternatives;
3562
+ if (group.alternativeSubgroups.length > 0) {
3563
+ alternatives = group.alternativeSubgroups.map(
3564
+ (subgroupIndices) => subgroupIndices.map((altIdx) => {
3565
+ const altQtys = group.alternativeQuantities.get(altIdx);
3566
+ return {
3567
+ index: altIdx,
3568
+ ...altQtys.length > 0 && {
3569
+ quantities: flattenPlainUnitGroup(
3570
+ addEquivalentsAndSimplify(altQtys, this.unitSystem)
3571
+ ).flatMap(
3572
+ /* v8 ignore next -- item.and branch requires complex nested AND-with-equivalents structure */
3573
+ (item) => "quantity" in item ? [item] : item.and
3574
+ )
3575
+ }
3576
+ };
3577
+ })
3578
+ );
3579
+ }
3531
3580
  for (const gq of flattened) {
3532
3581
  if ("and" in gq) {
3533
3582
  quantityGroups.push({
@@ -3805,7 +3854,7 @@ var _Recipe = class _Recipe {
3805
3854
  if (originalServings === void 0 || originalServings === 0) {
3806
3855
  originalServings = 1;
3807
3856
  }
3808
- const factor = Big4(newServings).div(originalServings);
3857
+ const factor = Big5(newServings).div(originalServings);
3809
3858
  return this.scaleBy(factor);
3810
3859
  }
3811
3860
  /**
@@ -3824,7 +3873,7 @@ var _Recipe = class _Recipe {
3824
3873
  function scaleAlternativesBy(alternatives, factor2) {
3825
3874
  for (const alternative of alternatives) {
3826
3875
  if (alternative.quantity) {
3827
- const scaleFactor = alternative.scalable ? Big4(factor2) : 1;
3876
+ const scaleFactor = alternative.scalable ? Big5(factor2) : 1;
3828
3877
  if (alternative.quantity.type !== "fixed" || alternative.quantity.value.type !== "text") {
3829
3878
  alternative.quantity = multiplyQuantityValue(
3830
3879
  alternative.quantity,
@@ -3897,10 +3946,10 @@ var _Recipe = class _Recipe {
3897
3946
  arbitrary.unit = optimized.unit;
3898
3947
  }
3899
3948
  newRecipe._populateIngredientQuantities();
3900
- newRecipe.servings = Big4(originalServings).times(factor).toNumber();
3949
+ newRecipe.servings = Big5(originalServings).times(factor).toNumber();
3901
3950
  for (const metaVar of ["servings", "serves"]) {
3902
3951
  if (typeof newRecipe.metadata[metaVar] === "number") {
3903
- newRecipe.metadata[metaVar] = Big4(newRecipe.metadata[metaVar]).times(factor).toNumber();
3952
+ newRecipe.metadata[metaVar] = Big5(newRecipe.metadata[metaVar]).times(factor).toNumber();
3904
3953
  }
3905
3954
  }
3906
3955
  if (newRecipe.metadata.yield && this.metadata.yield) {