@mark1russell7/stack-needs 0.9.3 → 0.9.5

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/build/index.js CHANGED
@@ -689,7 +689,7 @@ const FORMULA_FEEDS = {
689
689
  source: WIC_INFANT_SOURCE,
690
690
  locator: "Table 5.2, Guidelines for Feeding Healthy Infants, Birth to 12 Months Old, page 137"
691
691
  };
692
- /** The source of the energy of one fluid ounce of ready-to-feed formula: the SR Legacy file of FoodData Central. */
692
+ /** The source of the energy and the water of the infant formula items: the SR Legacy file of FoodData Central. */
693
693
  const FDC_SR_LEGACY_SOURCE = {
694
694
  id: "fdc-sr-legacy-2018-04",
695
695
  title: "FoodData Central, SR Legacy (April 2018)",
@@ -713,6 +713,21 @@ const FORMULA_LATER_LEAST = {
713
713
  kcalSource: FDC_SR_LEGACY_SOURCE,
714
714
  kcalLocator: "FDC ID 172319: 63 kcal in 100 g (food_nutrient.csv), and 30.4 g in 1 fl oz (food_portion.csv)"
715
715
  };
716
+ /**
717
+ * The facts of the mix rule: the FDC items 173554 (store brand, powder) and 172319 (store brand, ready-to-feed).
718
+ * The powder gives 524 kcal and 2 g of water in 100 g. The ready-to-feed item gives 63 kcal and 88.1 g of water in 100 g.
719
+ * The WIC guide tells to mix powder one bottle at a time, and to use bottled water after a disaster or in a power cut.
720
+ */
721
+ const FORMULA_MIX = {
722
+ powderKcal: 524,
723
+ powderWater: 2,
724
+ readyKcal: 63,
725
+ readyWater: 88.1,
726
+ source: FDC_SR_LEGACY_SOURCE,
727
+ locator: "FDC ID 173554: 524 kcal (nutrient 1008) and 2 g of water (nutrient 1051) in 100 g. FDC ID 172319: 63 kcal and 88.1 g of water in 100 g (food_nutrient.csv)",
728
+ waterSource: WIC_INFANT_SOURCE,
729
+ waterLocator: "Page 106, Guidelines for Using Infant Formula When Access to Common Kitchen Appliances Is Limited, and Natural Disaster or Power Outage"
730
+ };
716
731
  /** The source of the infant table: the PDF of the report Dietary Reference Intakes for Energy (2023) of the National Academies. */
717
732
  const NASEM_ENERGY_SOURCE = {
718
733
  id: "nasem-dri-energy-2023",
@@ -1092,6 +1107,11 @@ const WATER_NEED = "hydration.drink";
1092
1107
  const NO_COOK = "food.no-cook";
1093
1108
  /** The resource of the ready-to-feed infant formula, in `kcal`. It has a direct use for the formula need. It needs no water and no fuel. */
1094
1109
  const FORMULA_READY = "food.infant-formula-ready";
1110
+ /**
1111
+ * The resource of the infant formula powder, in `kg`. It has no direct use. `formulaMixFragment` mixes it with
1112
+ * `STORED_WATER` into `FORMULA_READY`. `cookFragment` does not cook it.
1113
+ */
1114
+ const FORMULA_POWDER = "food.infant-formula-powder";
1095
1115
  /** The resource of the stored drinking water, in `L`. It has a direct use for the water need. */
1096
1116
  const STORED_WATER = "water.stored";
1097
1117
  /**
@@ -1330,6 +1350,7 @@ function openedStores(input, stores, noReads, feed) {
1330
1350
  * - The resources are the resources of the stores and of the direct uses, with the units of the catalog.
1331
1351
  * - The supply part of each draw is `supply`. Its default is the part of `NEED_SUPPLIES` for the need of the draw.
1332
1352
  * - The fragment has no converter. `cookFragment` gives the converters for the dry food, with the water and the fuel of each pot.
1353
+ * `formulaMixFragment` gives the converter of `food.infant-formula-powder`. The powder has a store, and no direct use.
1333
1354
  * - With no cold storage (the default), an opened container of formula serves one feed. The store has only the energy of the feeds,
1334
1355
  * and `waste` gives the rest. The smallest feed is the smallest need of an infant of the draw, divided by `FORMULA_FEEDS`.
1335
1356
  * A container of no read gives no store and a gift with no read, so the stores are a lower limit.
@@ -1581,7 +1602,7 @@ const BOIL_CELSIUS = 100;
1581
1602
  */
1582
1603
  const COLD_START_CELSIUS = 0;
1583
1604
  /** This function gives the value of an amount in a unit, or `undefined` when the units do not convert. */
1584
- function inUnit(value, from, to) {
1605
+ function inUnit$1(value, from, to) {
1585
1606
  try {
1586
1607
  const out = valueIn({
1587
1608
  value,
@@ -1594,56 +1615,59 @@ function inUnit(value, from, to) {
1594
1615
  }
1595
1616
  /** This function gives the fuel rate of a stove in `kJ/min` or `kg/min`. It gives `undefined` for a rate that is not an energy or a mass in a time. */
1596
1617
  function stoveRate(rate) {
1597
- const minutes = inUnit(rate.per.amount, rate.per.unit, "min");
1618
+ const minutes = inUnit$1(rate.per.amount, rate.per.unit, "min");
1598
1619
  if (minutes === void 0 || !(minutes > 0)) return void 0;
1599
- const energy = inUnit(rate.amount, rate.unit, "kJ");
1620
+ const energy = inUnit$1(rate.amount, rate.unit, "kJ");
1600
1621
  if (energy !== void 0) return {
1601
1622
  kind: "energy",
1602
1623
  perMin: energy / minutes
1603
1624
  };
1604
- const mass = inUnit(rate.amount, rate.unit, "kg");
1625
+ const mass = inUnit$1(rate.amount, rate.unit, "kg");
1605
1626
  if (mass !== void 0) return {
1606
1627
  kind: "mass",
1607
1628
  perMin: mass / minutes
1608
1629
  };
1609
1630
  }
1610
- const read = (value, unit) => ({
1631
+ const read$1 = (value, unit) => ({
1611
1632
  read: true,
1612
1633
  value,
1613
1634
  unit
1614
1635
  });
1615
- const noRead = (why) => ({
1636
+ const noRead$1 = (why) => ({
1616
1637
  read: false,
1617
1638
  why
1618
1639
  });
1619
1640
  /**
1620
1641
  * This function gives the cook line of one dry food. Each missing fact gives a no read with its reason.
1621
1642
  * The fuel is a safe upper limit: the stove burns at its full rate for the boil time and the cook time.
1643
+ * Only a stove rate with no bound gives the fuel. A rate with a bound, for example `bound: "least"`, gives a no read.
1622
1644
  * At a high place the cook time is longer. The model does not give that time, so there the fuel is not an upper limit.
1623
1645
  */
1624
1646
  function cookLine(product, resource, perUnitKg, foodGifts, context) {
1625
1647
  const { stove, batchKg, share, startC, fuelEnergy } = context;
1626
1648
  let energy;
1627
- if (foodGifts > 1) energy = noRead(`The product ${product.id} gives ${String(foodGifts)} foods. The catalog gives one energy for the whole product, not the energy of each food.`);
1628
- else if (product.energy === void 0) energy = noRead(`The product ${product.id} has no food energy in the catalog.`);
1629
- else if (!product.energy.read) energy = noRead(`The food energy of the product ${product.id} is a no read: ${product.energy.why}`);
1649
+ if (foodGifts > 1) energy = noRead$1(`The product ${product.id} gives ${String(foodGifts)} foods. The catalog gives one energy for the whole product, not the energy of each food.`);
1650
+ else if (product.energy === void 0) energy = noRead$1(`The product ${product.id} has no food energy in the catalog.`);
1651
+ else if (!product.energy.read) energy = noRead$1(`The food energy of the product ${product.id} is a no read: ${product.energy.why}`);
1630
1652
  else {
1631
- const kcal = inUnit(product.energy.amount, product.energy.unit, "kcal");
1632
- if (kcal === void 0) energy = noRead(`The food energy of the product ${product.id} is in ${product.energy.unit}, which does not convert to kcal.`);
1633
- else if (perUnitKg === void 0 || !(perUnitKg > 0)) energy = noRead(`The gift of ${resource} of the product ${product.id} has no read in kg.`);
1634
- else energy = read(kcal / perUnitKg, "kcal/kg");
1653
+ const kcal = inUnit$1(product.energy.amount, product.energy.unit, "kcal");
1654
+ if (kcal === void 0) energy = noRead$1(`The food energy of the product ${product.id} is in ${product.energy.unit}, which does not convert to kcal.`);
1655
+ else if (perUnitKg === void 0 || !(perUnitKg > 0)) energy = noRead$1(`The gift of ${resource} of the product ${product.id} has no read in kg.`);
1656
+ else energy = read$1(kcal / perUnitKg, "kcal/kg");
1635
1657
  }
1636
1658
  const litres = cookWater(product);
1637
- const water = litres === void 0 ? noRead(`The catalog cites no cook water for the product ${product.id}, or no bulk mass for its water rate.`) : read(valueIn(litres, "L/kg"), "L/kg");
1638
- const heat = water.read ? read((water.value * WATER_DENSITY_MAX.value + 1) * batchKg * WATER_SPECIFIC_HEAT.value * (100 - startC), "kJ") : noRead(`The heat to boil is a no read: ${water.why}`);
1659
+ const water = litres === void 0 ? noRead$1(`The catalog cites no cook water for the product ${product.id}, or no bulk mass for its water rate.`) : read$1(valueIn(litres, "L/kg"), "L/kg");
1660
+ const heat = water.read ? read$1((water.value * WATER_DENSITY_MAX.value + 1) * batchKg * WATER_SPECIFIC_HEAT.value * (100 - startC), "kJ") : noRead$1(`The heat to boil is a no read: ${water.why}`);
1639
1661
  const why = [];
1640
1662
  if (!heat.read) why.push(heat.why);
1641
1663
  const time = product.cook?.time;
1642
- const cookMin = time === void 0 ? void 0 : inUnit(time.high, time.unit, "min");
1664
+ const cookMin = time === void 0 ? void 0 : inUnit$1(time.high, time.unit, "min");
1643
1665
  if (time === void 0) why.push(`The catalog cites no cook time for the product ${product.id}.`);
1644
1666
  else if (cookMin === void 0) why.push(`The cook time of the product ${product.id} is in ${time.unit}, which does not convert to min.`);
1645
- const rate = stove.rate === void 0 ? void 0 : stoveRate(stove.rate);
1667
+ const bound = stove.rate !== void 0 && "bound" in stove.rate ? stove.rate.bound : void 0;
1668
+ const rate = stove.rate === void 0 || bound !== void 0 ? void 0 : stoveRate(stove.rate);
1646
1669
  if (stove.rate === void 0) why.push(`The stove of ${stove.fuel} has no cited fuel rate.`);
1670
+ else if (bound !== void 0) why.push(`The fuel rate of the stove has the bound ${String(bound)}. Only a rate with no bound is the value.${bound === "least" ? " A least rate gives too little fuel for the cook time." : ""}`);
1647
1671
  else if (rate === void 0) why.push(`The fuel rate of the stove is in ${stove.rate.unit} for each ${stove.rate.per.unit}, which is not an energy or a mass in a time.`);
1648
1672
  let potPerMin;
1649
1673
  if (share === void 0) why.push("No cited stove share gives the part of the stove heat that goes into the pot. Thus the fuel of the boil is a no read.");
@@ -1671,8 +1695,8 @@ function cookLine(product, resource, perUnitKg, foodGifts, context) {
1671
1695
  energy,
1672
1696
  water,
1673
1697
  heat,
1674
- minutes: noRead(text),
1675
- fuel: noRead(text)
1698
+ minutes: noRead$1(text),
1699
+ fuel: noRead$1(text)
1676
1700
  };
1677
1701
  }
1678
1702
  const minutes = heat.value / potPerMin + cookMin;
@@ -1682,8 +1706,8 @@ function cookLine(product, resource, perUnitKg, foodGifts, context) {
1682
1706
  energy,
1683
1707
  water,
1684
1708
  heat,
1685
- minutes: read(minutes, "min"),
1686
- fuel: read(minutes * fuelPerMin, "kg")
1709
+ minutes: read$1(minutes, "min"),
1710
+ fuel: read$1(minutes * fuelPerMin, "kg")
1687
1711
  };
1688
1712
  }
1689
1713
  /** This function gives the stove facts of the input: the facts of the caller, or the facts of a product of the catalog. */
@@ -1698,10 +1722,14 @@ function stoveOf(catalog, stove) {
1698
1722
  function isFood(catalog, code) {
1699
1723
  return catalog.resources.find((item) => item.code === code)?.kind === "food";
1700
1724
  }
1701
- /** This function tells if a resource of the catalog is a dry food: a food resource in a unit of mass. */
1725
+ /**
1726
+ * This function tells if a resource of the catalog is a dry food to cook: a food resource in a unit of mass.
1727
+ * `FORMULA_POWDER` is not a food to cook: `formulaMixFragment` mixes it with water, with no fuel.
1728
+ */
1702
1729
  function isDryFood(catalog, code) {
1730
+ if (code === "food.infant-formula-powder") return false;
1703
1731
  const resource = catalog.resources.find((item) => item.code === code);
1704
- return resource !== void 0 && resource.kind === "food" && inUnit(1, resource.unit, "kg") !== void 0;
1732
+ return resource !== void 0 && resource.kind === "food" && inUnit$1(1, resource.unit, "kg") !== void 0;
1705
1733
  }
1706
1734
  /**
1707
1735
  * This function gives the converters that cook the dry food of a buy list, with the water and the fuel of each pot.
@@ -1714,9 +1742,12 @@ function isDryFood(catalog, code) {
1714
1742
  * - The boil time is the heat divided by the stove rate times the share. The time of the stove is the boil time plus the high end of the cook time.
1715
1743
  * - The fuel is a safe upper limit: the stove burns at its full rate for all of that time. At a high place the cook time is longer.
1716
1744
  * The model does not give that time, so there the fuel is not an upper limit.
1745
+ * - Only a stove rate with no bound gives the fuel. A rate with a bound, for example `bound: "least"`, gives a no read. The fuel of the cook time is the rate times the time, so a least rate gives too little fuel.
1717
1746
  * - A rate in a mass gives the fuel directly. A rate in an energy gives a mass only through the cited energy of the fuel, with the same heating value.
1718
1747
  *
1719
- * A dry food is a gift of a food resource in a unit of mass. Each missing fact gives a line with no read and its reasons, not a figure.
1748
+ * A dry food is a gift of a food resource in a unit of mass, except `FORMULA_POWDER`. `formulaMixFragment` gives its converter.
1749
+ * Each missing fact gives a line with no read and its reasons, not a figure.
1750
+ *
1720
1751
  * Two products of the list can give one dry resource, for example rice and oats. The converter then takes the worst case of their lines:
1721
1752
  * the most water, the most fuel and the least food energy. When a line of the resource has no read, the resource has no converter.
1722
1753
  * A store of the resource from a different fragment gets the converter of this list.
@@ -1742,7 +1773,7 @@ function isDryFood(catalog, code) {
1742
1773
  */
1743
1774
  function cookFragment(input) {
1744
1775
  const { catalog, units } = input;
1745
- const batchKg = inUnit(input.batch.value, input.batch.unit, "kg");
1776
+ const batchKg = inUnit$1(input.batch.value, input.batch.unit, "kg");
1746
1777
  if (batchKg === void 0 || !(batchKg > 0)) throw new RangeError(`The batch ${String(input.batch.value)} ${input.batch.unit} is not a mass above 0.`);
1747
1778
  const { share } = input;
1748
1779
  if (share !== void 0 && !(share.value > 0 && share.value <= 1)) throw new RangeError(`The stove share is ${String(share.value)}. It must be above 0 and up to 1.`);
@@ -1750,7 +1781,7 @@ function cookFragment(input) {
1750
1781
  if (!(startC >= 0 && startC < 100)) throw new RangeError(`The start temperature is ${String(startC)} °C. It must be from 0 up to less than ${String(100)} °C.`);
1751
1782
  const stove = stoveOf(catalog, input.stove);
1752
1783
  const fuel = catalog.resources.find((resource) => resource.code === stove.fuel);
1753
- const fuelUnitPerKg = fuel === void 0 ? void 0 : inUnit(1, "kg", fuel.unit);
1784
+ const fuelUnitPerKg = fuel === void 0 ? void 0 : inUnit$1(1, "kg", fuel.unit);
1754
1785
  if (fuel === void 0 || fuelUnitPerKg === void 0) throw new RangeError(`The catalog has no resource "${stove.fuel}" in a unit of mass for the fuel of the stove.`);
1755
1786
  const perKg = fuelEnergyPerMass(fuel);
1756
1787
  const context = {
@@ -1774,7 +1805,7 @@ function cookFragment(input) {
1774
1805
  const foodGifts = gifts.filter((gift) => isFood(catalog, gift.resource)).length;
1775
1806
  for (const gift of gifts) {
1776
1807
  if (!isDryFood(catalog, gift.resource)) continue;
1777
- const perUnitKg = gift.read ? inUnit(gift.amount, gift.unit, "kg") : void 0;
1808
+ const perUnitKg = gift.read ? inUnit$1(gift.amount, gift.unit, "kg") : void 0;
1778
1809
  lines.push(cookLine(product, gift.resource, perUnitKg, foodGifts, context));
1779
1810
  }
1780
1811
  }
@@ -1797,8 +1828,8 @@ function cookFragment(input) {
1797
1828
  }
1798
1829
  const most = (pick) => Math.max(...own.map((line) => valueOf(pick(line))));
1799
1830
  const least = (pick) => Math.min(...own.map((line) => valueOf(pick(line))));
1800
- const dryPerKg = inUnit(1, "kg", unitOf.get(resource) ?? "") ?? 1;
1801
- const waterPerL = inUnit(1, "L", unitOf.get("water.stored") ?? "");
1831
+ const dryPerKg = inUnit$1(1, "kg", unitOf.get(resource) ?? "") ?? 1;
1832
+ const waterPerL = inUnit$1(1, "L", unitOf.get("water.stored") ?? "");
1802
1833
  if (waterPerL === void 0) throw new RangeError(`The catalog has no resource "${STORED_WATER}" in a unit of volume for the cook water.`);
1803
1834
  converters.push({
1804
1835
  id: `${prefix}:${resource}`,
@@ -1844,6 +1875,196 @@ function cookFragment(input) {
1844
1875
  function valueOf(value) {
1845
1876
  return value.read ? value.value : NaN;
1846
1877
  }
1878
+ // src/mix.ts
1879
+ /**
1880
+ * This function gives the mix water of infant formula powder, in `kg` for each `kg` of powder.
1881
+ *
1882
+ * The rule is a mass balance. It assumes that the prepared powder has the energy in 100 g of the ready-to-feed item of the
1883
+ * same store brand. The reason: the FDC file gives these two items of one brand, and the catalog cites no mix ratio of a label.
1884
+ * The assumption ends when a label-cited mix ratio replaces it.
1885
+ *
1886
+ * Thus 1 kg of powder gives `powder / ready` kg of prepared formula, and the water is that mass minus 1 kg.
1887
+ * For `FORMULA_MIX`, 5240 kcal/kg divided by 630 kcal/kg gives 8.3175 kg, so the water is 7.3175 kg. That is 1.39646 g for each kcal.
1888
+ * `formulaMixCheck` compares the water of the prepared formula with the cited water of the ready-to-feed item.
1889
+ *
1890
+ * @param powderKcalPerKg - The energy of the powder, in `kcal/kg`, for example 5240
1891
+ * @param mix - The facts of the rule. The default is `FORMULA_MIX`. Only its energy of the ready-to-feed item applies.
1892
+ * @returns The mass of the mix water, in kg for each kg of powder
1893
+ * @throws RangeError when the energy of the powder is not above the energy of the ready-to-feed item
1894
+ */
1895
+ function formulaMixWater(powderKcalPerKg, mix = FORMULA_MIX) {
1896
+ const readyKcalPerKg = mix.readyKcal * 10;
1897
+ if (!(powderKcalPerKg > readyKcalPerKg) || !Number.isFinite(powderKcalPerKg)) throw new RangeError(`The powder gives ${String(powderKcalPerKg)} kcal/kg. The mix rule needs a finite energy above the ${String(readyKcalPerKg)} kcal/kg of the ready-to-feed item.`);
1898
+ return powderKcalPerKg / readyKcalPerKg - 1;
1899
+ }
1900
+ /**
1901
+ * This function gives the check of the mix rule with the water of the two items. The rule does not use their water, so the
1902
+ * check is independent of it. For `FORMULA_MIX`, 100 g of prepared formula has 12.023 g of powder and 87.977 g of mix water.
1903
+ * The powder holds 0.240 g of water, so the prediction is 88.218 g. The FDC item gives 88.1 g, 0.13% less.
1904
+ *
1905
+ * @param mix - The facts of the rule. The default is `FORMULA_MIX`.
1906
+ * @returns The predicted water, the cited water and their relative difference
1907
+ * @throws RangeError for the errors of `formulaMixWater`
1908
+ */
1909
+ function formulaMixCheck(mix = FORMULA_MIX) {
1910
+ const powder = 100 / (1 + formulaMixWater(mix.powderKcal * 10, mix));
1911
+ const added = 100 - powder;
1912
+ const predicted = added + powder * mix.powderWater / 100;
1913
+ return {
1914
+ powder,
1915
+ added,
1916
+ predicted,
1917
+ cited: mix.readyWater,
1918
+ relative: (predicted - mix.readyWater) / mix.readyWater
1919
+ };
1920
+ }
1921
+ /** This function gives the value of an amount in a unit, or `undefined` when the units do not convert. */
1922
+ function inUnit(value, from, to) {
1923
+ try {
1924
+ const out = valueIn({
1925
+ value,
1926
+ unit: from
1927
+ }, to);
1928
+ return Number.isFinite(out) ? out : void 0;
1929
+ } catch {
1930
+ return;
1931
+ }
1932
+ }
1933
+ const read = (value, unit) => ({
1934
+ read: true,
1935
+ value,
1936
+ unit
1937
+ });
1938
+ const noRead = (why) => ({
1939
+ read: false,
1940
+ why
1941
+ });
1942
+ /** This function gives the mix line of one product. Each missing fact gives a no read with its reason. */
1943
+ function mixLine(product, perUnitKg, foodGifts, mix) {
1944
+ let energy;
1945
+ if (foodGifts > 1) energy = noRead(`The product ${product.id} gives ${String(foodGifts)} foods. The catalog gives one energy for the whole product, not the energy of each food.`);
1946
+ else if (product.energy === void 0) energy = noRead(`The product ${product.id} has no food energy in the catalog.`);
1947
+ else if (!product.energy.read) energy = noRead(`The food energy of the product ${product.id} is a no read: ${product.energy.why}`);
1948
+ else {
1949
+ const kcal = inUnit(product.energy.amount, product.energy.unit, "kcal");
1950
+ if (kcal === void 0) energy = noRead(`The food energy of the product ${product.id} is in ${product.energy.unit}, which does not convert to kcal.`);
1951
+ else if (perUnitKg === void 0 || !(perUnitKg > 0)) energy = noRead(`The gift of ${FORMULA_POWDER} of the product ${product.id} has no read in kg.`);
1952
+ else energy = read(kcal / perUnitKg, "kcal/kg");
1953
+ }
1954
+ let water;
1955
+ if (!energy.read) water = noRead(`The mix water is a no read: ${energy.why}`);
1956
+ else if (!(energy.value > mix.readyKcal * 10)) water = noRead(`The powder of the product ${product.id} gives ${String(energy.value)} kcal/kg, not more than the ${String(mix.readyKcal * 10)} kcal/kg of the ready-to-feed item.`);
1957
+ else water = read(formulaMixWater(energy.value, mix) / WATER_DENSITY_MAX.value, "L/kg");
1958
+ return {
1959
+ product: product.id,
1960
+ energy,
1961
+ water
1962
+ };
1963
+ }
1964
+ /**
1965
+ * This function gives the converter that mixes the infant formula powder of a buy list with water, with no fuel.
1966
+ *
1967
+ * - One run of the converter is 1 kg of powder. It takes the powder and the mix water from `water.stored`.
1968
+ * It gives the energy of the powder as `food.infant-formula-ready`. The direct use of that resource feeds the formula need.
1969
+ * - The mix water is `formulaMixWater`: a mass balance with the energy of the ready-to-feed item of the same store brand.
1970
+ * This is an assumption. A label-cited mix ratio replaces it.
1971
+ * - The mass of the water converts to a volume at `WATER_DENSITY_MAX`, 1 kg/L. Water above 4 °C is less dense, so the true volume is a little more.
1972
+ * - The water is `water.stored`, the store of the drinking water. The WIC guide tells to use bottled water after a disaster
1973
+ * or in a power cut. A store of water from the tap is safe water only when its source is safe.
1974
+ * - The WIC guide tells to prepare powder one bottle at a time, for one feed. Thus the mix has no waste of an opened container.
1975
+ * - The powder of each product comes from its food energy in the catalog. Two products of the list can give powder.
1976
+ * The converter then takes the worst case of their lines: the most water and the least energy. A line with no read gives no converter.
1977
+ * - A drinking draw at a higher level of the same store takes its water first, by the take order of `enduranceOf`.
1978
+ * Thus, when a case cuts the water supply, the powder can add 0 days.
1979
+ *
1980
+ * The function is pure.
1981
+ *
1982
+ * @param input - The catalog and the buy list, and the facts of the mix rule
1983
+ * @returns The fragment, the line of each powder product and the reasons for no converter
1984
+ * @throws RangeError when the catalog has no product of an `id` of the buy list
1985
+ * @throws RangeError when the powder has a converter and the catalog has no `food.infant-formula-powder` in a unit of mass
1986
+ * @throws RangeError when the powder has a converter and the catalog has no `food.infant-formula-ready` in a unit of energy
1987
+ * @throws RangeError when the powder has a converter and the catalog has no `water.stored` in a unit of volume
1988
+ * @throws RangeError for the errors of `giftsOf`
1989
+ */
1990
+ function formulaMixFragment(input) {
1991
+ const { catalog, units } = input;
1992
+ const mix = input.mix ?? FORMULA_MIX;
1993
+ const isFood = (code) => catalog.resources.find((item) => item.code === code)?.kind === "food";
1994
+ const lines = [];
1995
+ for (const [id, count] of Object.entries(units)) {
1996
+ if (!(count > 0)) continue;
1997
+ const product = catalog.products.find((item) => item.id === id);
1998
+ if (product === void 0) throw new RangeError(`The catalog has no product "${id}".`);
1999
+ const gifts = giftsOf(catalog, id, 1);
2000
+ const foodGifts = gifts.filter((gift) => isFood(gift.resource)).length;
2001
+ for (const gift of gifts) {
2002
+ if (gift.resource !== "food.infant-formula-powder") continue;
2003
+ lines.push(mixLine(product, gift.read ? inUnit(gift.amount, gift.unit, "kg") : void 0, foodGifts, mix));
2004
+ }
2005
+ }
2006
+ const empty = (why) => ({
2007
+ layer: {
2008
+ schema: "endurance/1",
2009
+ levels: [],
2010
+ resources: [],
2011
+ stores: [],
2012
+ draws: [],
2013
+ converters: []
2014
+ },
2015
+ lines,
2016
+ why,
2017
+ read: why === void 0
2018
+ });
2019
+ if (lines.length === 0) return empty(void 0);
2020
+ const why = lines.flatMap((line) => [line.energy, line.water].flatMap((value) => value.read ? [] : [`${line.product}: ${value.why}`]));
2021
+ if (why.length > 0) return empty([...new Set(why)].join(" "));
2022
+ const unitOf = new Map(catalog.resources.map((resource) => [resource.code, resource.unit]));
2023
+ const powderPerKg = inUnit(1, "kg", unitOf.get("food.infant-formula-powder") ?? "");
2024
+ if (powderPerKg === void 0) throw new RangeError(`The catalog has no resource "${FORMULA_POWDER}" in a unit of mass for the powder.`);
2025
+ const readyPerKcal = inUnit(1, "kcal", unitOf.get("food.infant-formula-ready") ?? "");
2026
+ if (readyPerKcal === void 0) throw new RangeError(`The catalog has no resource "${FORMULA_READY}" in a unit of energy for the prepared formula.`);
2027
+ const waterPerL = inUnit(1, "L", unitOf.get("water.stored") ?? "");
2028
+ if (waterPerL === void 0) throw new RangeError(`The catalog has no resource "${STORED_WATER}" in a unit of volume for the mix water.`);
2029
+ const values = (pick) => lines.map((line) => {
2030
+ const value = pick(line);
2031
+ return value.read ? value.value : NaN;
2032
+ });
2033
+ const converter = {
2034
+ id: `${input.prefix ?? "mix"}:${FORMULA_POWDER}`,
2035
+ takes: [{
2036
+ resource: FORMULA_POWDER,
2037
+ amount: powderPerKg
2038
+ }, {
2039
+ resource: STORED_WATER,
2040
+ amount: Math.max(...values((line) => line.water)) * waterPerL
2041
+ }],
2042
+ gives: {
2043
+ resource: FORMULA_READY,
2044
+ amount: Math.min(...values((line) => line.energy)) * readyPerKcal
2045
+ }
2046
+ };
2047
+ return {
2048
+ layer: {
2049
+ schema: "endurance/1",
2050
+ levels: [],
2051
+ resources: [
2052
+ FORMULA_POWDER,
2053
+ STORED_WATER,
2054
+ FORMULA_READY
2055
+ ].map((code) => ({
2056
+ code,
2057
+ unit: unitOf.get(code) ?? ""
2058
+ })),
2059
+ stores: [],
2060
+ draws: [],
2061
+ converters: [converter]
2062
+ },
2063
+ lines,
2064
+ why: void 0,
2065
+ read: true
2066
+ };
2067
+ }
1847
2068
  // src/daily.ts
1848
2069
  /** The code of the need of light to see in the dark in a home, in hours. */
1849
2070
  const LIGHT_NEED = "light.see";
@@ -1991,6 +2212,6 @@ function checkDailyNeeds(needs, options) {
1991
2212
  });
1992
2213
  return findings;
1993
2214
  }
1994
- export { ACTIVITY_LEVELS, ALERT_WATCH_NEED, BOIL_CELSIUS, BREAST_MILK_MONTHS, CALORIE_CHAIN, CALORIE_SEXES, CALORIE_TABLES, COLD_START_CELSIUS, DAILY_NEEDS, DAILY_NEED_CODES, DGA_CALORIES, DGA_SOURCE, DIRECT_USES, FDC_SR_LEGACY_SOURCE, FOOD_NEED, FORMULA_DRAW_MONTHS, FORMULA_FEEDS, FORMULA_LATER_LEAST, FORMULA_NEED, FORMULA_READY, INFANT_FEEDINGS, INFANT_MONTHS, JOIN_CODES, LIGHT_NEED, MILK_LATER, NASEM_ENERGY_SOURCE, NASEM_INFANT_CALORIES, NEEDS_CODES, NEED_SUPPLIES, NIST_SI_GUIDE_SOURCE, NO_COOK, NWS_ALARMS_SOURCE, NWS_RECEIVERS_SOURCE, STORED_WATER, USGS_WATER_DENSITY_SOURCE, WARMTH_NEED, WATER_DENSITY_MAX, WATER_NEED, WATER_SPECIFIC_HEAT, WIC_INFANT_SOURCE, calorieChange, calorieNeed, calorieRow, calorieRowOf, checkCalorieChain, checkCalorieTable, checkDailyNeeds, checkJoins, cookFragment, hasNeedsError, householdFragment, isActivityLevel };
2215
+ export { ACTIVITY_LEVELS, ALERT_WATCH_NEED, BOIL_CELSIUS, BREAST_MILK_MONTHS, CALORIE_CHAIN, CALORIE_SEXES, CALORIE_TABLES, COLD_START_CELSIUS, DAILY_NEEDS, DAILY_NEED_CODES, DGA_CALORIES, DGA_SOURCE, DIRECT_USES, FDC_SR_LEGACY_SOURCE, FOOD_NEED, FORMULA_DRAW_MONTHS, FORMULA_FEEDS, FORMULA_LATER_LEAST, FORMULA_MIX, FORMULA_NEED, FORMULA_POWDER, FORMULA_READY, INFANT_FEEDINGS, INFANT_MONTHS, JOIN_CODES, LIGHT_NEED, MILK_LATER, NASEM_ENERGY_SOURCE, NASEM_INFANT_CALORIES, NEEDS_CODES, NEED_SUPPLIES, NIST_SI_GUIDE_SOURCE, NO_COOK, NWS_ALARMS_SOURCE, NWS_RECEIVERS_SOURCE, STORED_WATER, USGS_WATER_DENSITY_SOURCE, WARMTH_NEED, WATER_DENSITY_MAX, WATER_NEED, WATER_SPECIFIC_HEAT, WIC_INFANT_SOURCE, calorieChange, calorieNeed, calorieRow, calorieRowOf, checkCalorieChain, checkCalorieTable, checkDailyNeeds, checkJoins, cookFragment, formulaMixCheck, formulaMixFragment, formulaMixWater, hasNeedsError, householdFragment, isActivityLevel };
1995
2216
 
1996
2217
  //# sourceMappingURL=index.js.map