@mark1russell7/stack-needs 0.7.0 → 0.8.1
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/README.md +84 -11
- package/build/index.d.ts +224 -19
- package/build/index.d.ts.map +1 -1
- package/build/index.js +418 -45
- package/build/index.js.map +1 -1
- package/package.json +4 -4
package/build/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { quantity, valueIn } from "@mark1russell7/quantity";
|
|
2
2
|
import { checkSources } from "@mark1russell7/guidebook";
|
|
3
|
-
import { buyListEnergy, giftsOf } from "@mark1russell7/market/supplies";
|
|
3
|
+
import { buyListEnergy, cookWater, fuelEnergyPerMass, giftsOf } from "@mark1russell7/market/supplies";
|
|
4
4
|
// src/calorie.ts
|
|
5
5
|
/** The activity levels of the table, as the source names them: sedentary, moderately active and active. */
|
|
6
6
|
const ACTIVITY_LEVELS = [
|
|
@@ -1027,10 +1027,17 @@ function hasNeedsError(findings) {
|
|
|
1027
1027
|
// src/uses.ts
|
|
1028
1028
|
/** The code of the need of the food draw: food energy, in `kcal` for each day. */
|
|
1029
1029
|
const FOOD_NEED = "food.energy";
|
|
1030
|
+
/**
|
|
1031
|
+
* The code of the need of the infants on formula below `FORMULA_DRAW_MONTHS`, in `kcal` for each day.
|
|
1032
|
+
* It is a need of its own. Thus the food of the adults does not feed the infants, and the formula does not feed the adults.
|
|
1033
|
+
*/
|
|
1034
|
+
const FORMULA_NEED = "food.infant-formula";
|
|
1030
1035
|
/** The code of the need of drinking water, in `L` for each day. */
|
|
1031
1036
|
const WATER_NEED = "hydration.drink";
|
|
1032
1037
|
/** The resource of the food to eat with no water and no fuel, in `kcal`. It has a direct use for the food draw. */
|
|
1033
1038
|
const NO_COOK = "food.no-cook";
|
|
1039
|
+
/** 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. */
|
|
1040
|
+
const FORMULA_READY = "food.infant-formula-ready";
|
|
1034
1041
|
/** The resource of the stored drinking water, in `L`. It has a direct use for the water need. */
|
|
1035
1042
|
const STORED_WATER = "water.stored";
|
|
1036
1043
|
/**
|
|
@@ -1040,25 +1047,35 @@ const STORED_WATER = "water.stored";
|
|
|
1040
1047
|
*/
|
|
1041
1048
|
const NEED_SUPPLIES = {
|
|
1042
1049
|
"hydration.drink": "water.main",
|
|
1043
|
-
"food.energy": "food.retail"
|
|
1050
|
+
"food.energy": "food.retail",
|
|
1051
|
+
"food.infant-formula": "food.retail"
|
|
1044
1052
|
};
|
|
1045
1053
|
/**
|
|
1046
1054
|
* The direct uses of the resources of the buy list. Each use gives 1 unit of its need for each unit of its resource.
|
|
1047
1055
|
*
|
|
1048
1056
|
* - 1 L of stored drinking water gives 1 L of drink. This is a definition, not a read.
|
|
1049
1057
|
* - 1 kcal of food to eat with no water and no fuel gives 1 kcal of food energy.
|
|
1058
|
+
* - 1 kcal of ready-to-feed infant formula gives 1 kcal of the formula need.
|
|
1050
1059
|
*
|
|
1051
1060
|
* These records have the form of `DirectUse` of `@mark1russell7/stack-endurance`.
|
|
1052
1061
|
*/
|
|
1053
|
-
const DIRECT_USES = [
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
+
const DIRECT_USES = [
|
|
1063
|
+
{
|
|
1064
|
+
resource: STORED_WATER,
|
|
1065
|
+
need: WATER_NEED,
|
|
1066
|
+
amount: 1
|
|
1067
|
+
},
|
|
1068
|
+
{
|
|
1069
|
+
resource: NO_COOK,
|
|
1070
|
+
need: FOOD_NEED,
|
|
1071
|
+
amount: 1
|
|
1072
|
+
},
|
|
1073
|
+
{
|
|
1074
|
+
resource: FORMULA_READY,
|
|
1075
|
+
need: FORMULA_NEED,
|
|
1076
|
+
amount: 1
|
|
1077
|
+
}
|
|
1078
|
+
];
|
|
1062
1079
|
/**
|
|
1063
1080
|
* These are the codes of the join gate. Each code is an error.
|
|
1064
1081
|
*
|
|
@@ -1143,12 +1160,15 @@ const INFANT_MONTHS = 12;
|
|
|
1143
1160
|
* From 6 months, the infant table gives more than the change of a stage of lactation, so a part of the need is in no draw. Give `food` from this age.
|
|
1144
1161
|
*/
|
|
1145
1162
|
const BREAST_MILK_MONTHS = 6;
|
|
1163
|
+
/**
|
|
1164
|
+
* The age in months below which an infant on formula is in the formula draw with the whole need of its row: 6.
|
|
1165
|
+
* Below this age, an infant has milk only, as for `BREAST_MILK_MONTHS`. From this age, no cited source gives the part of formula.
|
|
1166
|
+
*/
|
|
1167
|
+
const FORMULA_DRAW_MONTHS = 6;
|
|
1146
1168
|
/** This function gives the stores of the buy list, its resources and its gifts with no read. */
|
|
1147
|
-
function buyListStores(input
|
|
1169
|
+
function buyListStores(input) {
|
|
1148
1170
|
const { catalog, units } = input;
|
|
1149
1171
|
const energy = buyListEnergy(catalog, units);
|
|
1150
|
-
const unitOf = new Map(catalog.resources.map((resource) => [resource.code, resource.unit]));
|
|
1151
|
-
for (const use of foodUses) if (unitOf.get(use.resource) !== "kcal") throw new RangeError(`The catalog has no resource "${use.resource}" in kcal for the food use.`);
|
|
1152
1172
|
const prefix = input.prefix ?? "buy";
|
|
1153
1173
|
const stores = [];
|
|
1154
1174
|
const noReads = [];
|
|
@@ -1164,10 +1184,6 @@ function buyListStores(input, foodUses) {
|
|
|
1164
1184
|
});
|
|
1165
1185
|
return {
|
|
1166
1186
|
stores,
|
|
1167
|
-
resources: [.../* @__PURE__ */ new Set([...stores.map((store) => store.resource), ...foodUses.map((use) => use.resource)])].map((code) => ({
|
|
1168
|
-
code,
|
|
1169
|
-
unit: unitOf.get(code) ?? ""
|
|
1170
|
-
})),
|
|
1171
1187
|
parts: {
|
|
1172
1188
|
energy,
|
|
1173
1189
|
noReads,
|
|
@@ -1176,14 +1192,30 @@ function buyListStores(input, foodUses) {
|
|
|
1176
1192
|
};
|
|
1177
1193
|
}
|
|
1178
1194
|
/**
|
|
1195
|
+
* This function gives the resources of the stores and of the uses, with the units of the catalog.
|
|
1196
|
+
*
|
|
1197
|
+
* @throws RangeError when the resource of a use is not in the catalog in kcal
|
|
1198
|
+
*/
|
|
1199
|
+
function layerResources(catalog, stores, uses) {
|
|
1200
|
+
const unitOf = new Map(catalog.resources.map((resource) => [resource.code, resource.unit]));
|
|
1201
|
+
for (const use of uses) if (unitOf.get(use.resource) !== "kcal") throw new RangeError(`The catalog has no resource "${use.resource}" in kcal for the use of ${use.need}.`);
|
|
1202
|
+
return [.../* @__PURE__ */ new Set([...stores.map((store) => store.resource), ...uses.map((use) => use.resource)])].map((code) => ({
|
|
1203
|
+
code,
|
|
1204
|
+
unit: unitOf.get(code) ?? ""
|
|
1205
|
+
}));
|
|
1206
|
+
}
|
|
1207
|
+
/**
|
|
1179
1208
|
* This function joins a household and a buy list into a fragment of an endurance layer.
|
|
1180
1209
|
*
|
|
1181
|
-
* - The
|
|
1210
|
+
* - The draw of food energy has the rate of the need of its persons for one day, and its users are the persons.
|
|
1211
|
+
* - The draw of formula has the infants on formula below `FORMULA_DRAW_MONTHS`, with the whole need of their rows.
|
|
1212
|
+
* - The fragment has each draw with one or more users. The draw of food energy is first.
|
|
1182
1213
|
* - Each gift of the buy list with a read is one store, with the `id` `<prefix>:<product>:<resource>`. A gift with no read has no store.
|
|
1183
|
-
* - The direct uses are
|
|
1184
|
-
*
|
|
1185
|
-
* - The
|
|
1186
|
-
* - The
|
|
1214
|
+
* - The direct uses are each use of `uses` for the need of a draw. The default `DIRECT_USES` gives 1 kcal for each kcal.
|
|
1215
|
+
* `food.no-cook` feeds the food draw, and `food.infant-formula-ready` feeds the formula draw.
|
|
1216
|
+
* - The resources are the resources of the stores and of the direct uses, with the units of the catalog.
|
|
1217
|
+
* - The supply part of each draw is `supply`. Its default is the part of `NEED_SUPPLIES` for the need of the draw.
|
|
1218
|
+
* - The fragment has no converter. `cookFragment` gives the converters for the dry food, with the water and the fuel of each pot.
|
|
1187
1219
|
*
|
|
1188
1220
|
* A person with no row in the tables gives no layer, because a low rate gives too many days. The default tables give a row for each age.
|
|
1189
1221
|
* The option `noRow: "leave-out"` leaves those persons out of the draw. A draw of 0 kcal gives no layer.
|
|
@@ -1194,22 +1226,22 @@ function buyListStores(input, foodUses) {
|
|
|
1194
1226
|
* - `breast-milk`: the infant has a line, and no part of the draw. It is only for an infant below `BREAST_MILK_MONTHS`.
|
|
1195
1227
|
* A person of the household must have a stage of lactation. Each infant on breast milk needs one person of the draw with a stage of lactation.
|
|
1196
1228
|
* When the infants are more, the function gives no layer: the change of a stage is the milk of one infant.
|
|
1197
|
-
* - `formula`:
|
|
1198
|
-
*
|
|
1229
|
+
* - `formula`: below `FORMULA_DRAW_MONTHS`, the whole need of the infant is in the draw of formula, because the infant has milk only.
|
|
1230
|
+
* From `FORMULA_DRAW_MONTHS`, no cited source gives the part of formula, so the infant gives no layer, as a person with no row.
|
|
1231
|
+
* The option `leave-out` also applies. The feeding is only for an infant below `INFANT_MONTHS`.
|
|
1199
1232
|
*
|
|
1200
1233
|
* The function is pure.
|
|
1201
1234
|
*
|
|
1202
1235
|
* @param input - The persons, the activity level, the catalog and the buy list, the supply part and the level of the draw
|
|
1203
1236
|
* @returns The fragment or a no read of the draw, with the energy of the list and the gifts with no read
|
|
1204
1237
|
* @throws RangeError for the errors of `calorieNeed`, `giftsOf` and `buyListEnergy`, except a household with no person
|
|
1205
|
-
* @throws RangeError when the catalog has no resource in `kcal` for
|
|
1238
|
+
* @throws RangeError when the catalog has no resource in `kcal` for the direct use of a draw
|
|
1206
1239
|
* @throws RangeError when a record below `INFANT_MONTHS` has no `feeding`, or when a `feeding` is not a way of `INFANT_FEEDINGS`
|
|
1207
1240
|
* @throws RangeError for `breast-milk` from `BREAST_MILK_MONTHS`, and for `formula` from `INFANT_MONTHS`
|
|
1208
1241
|
* @throws RangeError when an infant drinks only breast milk and no person of the household has a stage of lactation
|
|
1209
1242
|
*/
|
|
1210
1243
|
function householdFragment(input) {
|
|
1211
|
-
const
|
|
1212
|
-
const { stores, resources, parts } = buyListStores(input, foodUses);
|
|
1244
|
+
const { stores, parts } = buyListStores(input);
|
|
1213
1245
|
checkFeedings(input.persons);
|
|
1214
1246
|
if (input.persons.every((person) => person.count === 0)) return {
|
|
1215
1247
|
...parts,
|
|
@@ -1225,9 +1257,12 @@ function householdFragment(input) {
|
|
|
1225
1257
|
const need = calorieNeed(tables, input.persons, input.activity, 1);
|
|
1226
1258
|
const noRow = [];
|
|
1227
1259
|
const formula = [];
|
|
1260
|
+
const formulaLater = [];
|
|
1228
1261
|
const breastMilk = [];
|
|
1229
1262
|
let users = 0;
|
|
1230
1263
|
let perDay = 0;
|
|
1264
|
+
let formulaUsers = 0;
|
|
1265
|
+
let formulaPerDay = 0;
|
|
1231
1266
|
let mothers = 0;
|
|
1232
1267
|
let nursed = 0;
|
|
1233
1268
|
for (const line of need.lines) {
|
|
@@ -1236,8 +1271,13 @@ function householdFragment(input) {
|
|
|
1236
1271
|
if (person.feeding === "breast-milk") {
|
|
1237
1272
|
breastMilk.push(person);
|
|
1238
1273
|
nursed += person.count;
|
|
1239
|
-
} else if (person.feeding === "formula")
|
|
1240
|
-
|
|
1274
|
+
} else if (person.feeding === "formula") {
|
|
1275
|
+
formula.push(person);
|
|
1276
|
+
if (line.read && person.ageMonths < 6) {
|
|
1277
|
+
formulaUsers += person.count;
|
|
1278
|
+
formulaPerDay += line.need.value;
|
|
1279
|
+
} else formulaLater.push(person);
|
|
1280
|
+
} else if (!line.read) noRow.push(person);
|
|
1241
1281
|
else {
|
|
1242
1282
|
users += person.count;
|
|
1243
1283
|
perDay += line.need.value;
|
|
@@ -1248,7 +1288,7 @@ function householdFragment(input) {
|
|
|
1248
1288
|
const reasons = [
|
|
1249
1289
|
...nursed > mothers ? [`${String(nursed)} infants drink only breast milk, but the draw has ${String(mothers)} persons with a stage of lactation. The change of a stage is the milk of one infant. Give feeding: food to the other infants.`] : [],
|
|
1250
1290
|
...noRow.length > 0 ? [`The table has no row for ${String(noRow.length)} person records.`] : [],
|
|
1251
|
-
...
|
|
1291
|
+
...formulaLater.length > 0 ? [`${String(formulaLater.length)} infant records of ${String(6)} months or more are on formula. No cited source gives the part of their need that formula gives.`] : []
|
|
1252
1292
|
];
|
|
1253
1293
|
if (nursed > mothers) return {
|
|
1254
1294
|
...parts,
|
|
@@ -1270,7 +1310,22 @@ function householdFragment(input) {
|
|
|
1270
1310
|
breastMilk,
|
|
1271
1311
|
why: `${reasons.join(" ")} The option noRow: "leave-out" leaves them out of the draw.`
|
|
1272
1312
|
};
|
|
1273
|
-
|
|
1313
|
+
const draws = [];
|
|
1314
|
+
if (users > 0 && perDay > 0) draws.push({
|
|
1315
|
+
need: FOOD_NEED,
|
|
1316
|
+
level: input.level,
|
|
1317
|
+
perDay,
|
|
1318
|
+
users,
|
|
1319
|
+
supply: input.supply ?? NEED_SUPPLIES["food.energy"]
|
|
1320
|
+
});
|
|
1321
|
+
if (formulaUsers > 0 && formulaPerDay > 0) draws.push({
|
|
1322
|
+
need: FORMULA_NEED,
|
|
1323
|
+
level: input.level,
|
|
1324
|
+
perDay: formulaPerDay,
|
|
1325
|
+
users: formulaUsers,
|
|
1326
|
+
supply: input.supply ?? NEED_SUPPLIES["food.infant-formula"]
|
|
1327
|
+
});
|
|
1328
|
+
if (draws.length === 0) return {
|
|
1274
1329
|
...parts,
|
|
1275
1330
|
layer: void 0,
|
|
1276
1331
|
need,
|
|
@@ -1278,28 +1333,23 @@ function householdFragment(input) {
|
|
|
1278
1333
|
noRow,
|
|
1279
1334
|
formula,
|
|
1280
1335
|
breastMilk,
|
|
1281
|
-
why: "No person of the
|
|
1282
|
-
};
|
|
1283
|
-
const draw = {
|
|
1284
|
-
need: FOOD_NEED,
|
|
1285
|
-
level: input.level,
|
|
1286
|
-
perDay,
|
|
1287
|
-
users,
|
|
1288
|
-
supply: input.supply ?? NEED_SUPPLIES["food.energy"]
|
|
1336
|
+
why: "No person of the draws has a need of food in the draws, so the draws are 0 kcal."
|
|
1289
1337
|
};
|
|
1338
|
+
const needs = new Set(draws.map((draw) => draw.need));
|
|
1339
|
+
const uses = (input.uses ?? DIRECT_USES).filter((use) => needs.has(use.need));
|
|
1290
1340
|
return {
|
|
1291
1341
|
...parts,
|
|
1292
1342
|
layer: {
|
|
1293
1343
|
schema: "endurance/1",
|
|
1294
1344
|
levels: [...input.levels ?? []],
|
|
1295
|
-
resources,
|
|
1345
|
+
resources: layerResources(input.catalog, stores, uses),
|
|
1296
1346
|
stores,
|
|
1297
|
-
draws
|
|
1298
|
-
direct:
|
|
1347
|
+
draws,
|
|
1348
|
+
direct: uses.map((use) => ({ ...use }))
|
|
1299
1349
|
},
|
|
1300
1350
|
need,
|
|
1301
1351
|
needRead: true,
|
|
1302
|
-
leftOut: [...noRow, ...
|
|
1352
|
+
leftOut: [...noRow, ...formulaLater],
|
|
1303
1353
|
formula,
|
|
1304
1354
|
breastMilk
|
|
1305
1355
|
};
|
|
@@ -1328,6 +1378,329 @@ function makesMilk(tables, person) {
|
|
|
1328
1378
|
if (stage === void 0) return false;
|
|
1329
1379
|
return ("rows" in tables ? [tables] : tables).some((table) => calorieChange(table, person.sex, stage)?.milk === true);
|
|
1330
1380
|
}
|
|
1331
|
-
|
|
1381
|
+
// src/cook.ts
|
|
1382
|
+
/** The source of the specific heat of water: the NIST Guide to the SI (Special Publication 811). It is a work of the United States federal government. */
|
|
1383
|
+
const NIST_SI_GUIDE_SOURCE = {
|
|
1384
|
+
id: "nist-sp-811",
|
|
1385
|
+
title: "NIST Guide to the SI (Special Publication 811), Appendix B.8 and footnotes",
|
|
1386
|
+
url: "https://www.nist.gov/pml/special-publication-811/nist-guide-si-appendix-b-conversion-factors/nist-guide-si-appendix-b8",
|
|
1387
|
+
license: "us-public-domain",
|
|
1388
|
+
checked: "2026-11-24",
|
|
1389
|
+
by: "National Institute of Standards and Technology",
|
|
1390
|
+
terms: "https://www.nist.gov/oism/copyrights",
|
|
1391
|
+
note: "The footnotes are at https://www.nist.gov/pml/special-publication-811/nist-guide-si-footnotes."
|
|
1392
|
+
};
|
|
1393
|
+
/** The source of the density of water: the page Water Density of the USGS Water Science School. It is a work of the United States federal government. */
|
|
1394
|
+
const USGS_WATER_DENSITY_SOURCE = {
|
|
1395
|
+
id: "usgs-water-density",
|
|
1396
|
+
title: "Water Density, USGS Water Science School",
|
|
1397
|
+
url: "https://www.usgs.gov/special-topics/water-science-school/science/water-density",
|
|
1398
|
+
license: "us-public-domain",
|
|
1399
|
+
checked: "2026-11-24",
|
|
1400
|
+
by: "U.S. Geological Survey",
|
|
1401
|
+
terms: "https://www.usgs.gov/information-policies-and-instructions/copyrights-and-credits"
|
|
1402
|
+
};
|
|
1403
|
+
/**
|
|
1404
|
+
* The mean specific heat of liquid water from 0 °C to 100 °C: 4.19002 kJ/(kg.K).
|
|
1405
|
+
* The NIST Guide to the SI gives 4.190 02 J for the calorie (mean). The common definition of that calorie is the mean over this range.
|
|
1406
|
+
* SP 811 does not state the range. Its footnote 10 tells that the calorie is the energy to raise 1 g of water
|
|
1407
|
+
* by 1 kelvin. For the default start (0 °C), the value is the mean of the rise. For a warmer start, the mean of the
|
|
1408
|
+
* rise can be a little higher. The model also heats the dry food with this value, above the specific heat of a dry food.
|
|
1409
|
+
* That term holds the heat of the pot above the true value.
|
|
1410
|
+
*/
|
|
1411
|
+
const WATER_SPECIFIC_HEAT = {
|
|
1412
|
+
value: 4.19002,
|
|
1413
|
+
unit: "kJ/(kg.K)",
|
|
1414
|
+
source: NIST_SI_GUIDE_SOURCE,
|
|
1415
|
+
locator: "Appendix B.8, row calorie (mean): 4.190 02 E+00 J. Footnotes, footnote 10: the energy to raise 1 gram of water by 1 °C.",
|
|
1416
|
+
note: "The calorie (mean) in joules, for 1 g of water and 1 K. By its common definition, the mean is over the range from 0 °C to 100 °C. SP 811 does not state the range."
|
|
1417
|
+
};
|
|
1418
|
+
/**
|
|
1419
|
+
* The largest density of liquid water: 1 kg/L. The USGS table gives 1.00000 g/cm3 at 4.0 °C, its largest value.
|
|
1420
|
+
* Thus 1 L of cook water is 1 kg or less.
|
|
1421
|
+
*/
|
|
1422
|
+
const WATER_DENSITY_MAX = {
|
|
1423
|
+
value: 1,
|
|
1424
|
+
unit: "kg/L",
|
|
1425
|
+
source: USGS_WATER_DENSITY_SOURCE,
|
|
1426
|
+
locator: "Table of the density and the weight of water by temperature, row 39.2 °F/4.0 °C: 1.00000 g/cm3",
|
|
1427
|
+
note: "The largest density of the table, from 0 °C to 100 °C. Each other row is less."
|
|
1428
|
+
};
|
|
1429
|
+
/**
|
|
1430
|
+
* The temperature at the end of the heat to boil, in °C.
|
|
1431
|
+
* Water boils at a lower temperature at a high place. Then the heat is less.
|
|
1432
|
+
* But the cook time is longer there, and the model does not give that time. Thus at a high place the fuel is not an upper limit.
|
|
1433
|
+
*/
|
|
1434
|
+
const BOIL_CELSIUS = 100;
|
|
1435
|
+
/**
|
|
1436
|
+
* The default start temperature of the cook water, in °C: 0, the freezing point. Liquid water at the pressure of the sea is
|
|
1437
|
+
* not colder. Thus the default gives the most heat. A caller gives a warmer start with its citation.
|
|
1438
|
+
*/
|
|
1439
|
+
const COLD_START_CELSIUS = 0;
|
|
1440
|
+
/** This function gives the value of an amount in a unit, or `undefined` when the units do not convert. */
|
|
1441
|
+
function inUnit(value, from, to) {
|
|
1442
|
+
try {
|
|
1443
|
+
const out = valueIn({
|
|
1444
|
+
value,
|
|
1445
|
+
unit: from
|
|
1446
|
+
}, to);
|
|
1447
|
+
return Number.isFinite(out) ? out : void 0;
|
|
1448
|
+
} catch {
|
|
1449
|
+
return;
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
/** 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. */
|
|
1453
|
+
function stoveRate(rate) {
|
|
1454
|
+
const minutes = inUnit(rate.per.amount, rate.per.unit, "min");
|
|
1455
|
+
if (minutes === void 0 || !(minutes > 0)) return void 0;
|
|
1456
|
+
const energy = inUnit(rate.amount, rate.unit, "kJ");
|
|
1457
|
+
if (energy !== void 0) return {
|
|
1458
|
+
kind: "energy",
|
|
1459
|
+
perMin: energy / minutes
|
|
1460
|
+
};
|
|
1461
|
+
const mass = inUnit(rate.amount, rate.unit, "kg");
|
|
1462
|
+
if (mass !== void 0) return {
|
|
1463
|
+
kind: "mass",
|
|
1464
|
+
perMin: mass / minutes
|
|
1465
|
+
};
|
|
1466
|
+
}
|
|
1467
|
+
const read = (value, unit) => ({
|
|
1468
|
+
read: true,
|
|
1469
|
+
value,
|
|
1470
|
+
unit
|
|
1471
|
+
});
|
|
1472
|
+
const noRead = (why) => ({
|
|
1473
|
+
read: false,
|
|
1474
|
+
why
|
|
1475
|
+
});
|
|
1476
|
+
/**
|
|
1477
|
+
* This function gives the cook line of one dry food. Each missing fact gives a no read with its reason.
|
|
1478
|
+
* The fuel is a safe upper limit: the stove burns at its full rate for the boil time and the cook time.
|
|
1479
|
+
* 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.
|
|
1480
|
+
*/
|
|
1481
|
+
function cookLine(product, resource, perUnitKg, foodGifts, context) {
|
|
1482
|
+
const { stove, batchKg, share, startC, fuelEnergy } = context;
|
|
1483
|
+
let energy;
|
|
1484
|
+
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.`);
|
|
1485
|
+
else if (product.energy === void 0) energy = noRead(`The product ${product.id} has no food energy in the catalog.`);
|
|
1486
|
+
else if (!product.energy.read) energy = noRead(`The food energy of the product ${product.id} is a no read: ${product.energy.why}`);
|
|
1487
|
+
else {
|
|
1488
|
+
const kcal = inUnit(product.energy.amount, product.energy.unit, "kcal");
|
|
1489
|
+
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.`);
|
|
1490
|
+
else if (perUnitKg === void 0 || !(perUnitKg > 0)) energy = noRead(`The gift of ${resource} of the product ${product.id} has no read in kg.`);
|
|
1491
|
+
else energy = read(kcal / perUnitKg, "kcal/kg");
|
|
1492
|
+
}
|
|
1493
|
+
const litres = cookWater(product);
|
|
1494
|
+
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");
|
|
1495
|
+
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}`);
|
|
1496
|
+
const why = [];
|
|
1497
|
+
if (!heat.read) why.push(heat.why);
|
|
1498
|
+
const time = product.cook?.time;
|
|
1499
|
+
const cookMin = time === void 0 ? void 0 : inUnit(time.high, time.unit, "min");
|
|
1500
|
+
if (time === void 0) why.push(`The catalog cites no cook time for the product ${product.id}.`);
|
|
1501
|
+
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.`);
|
|
1502
|
+
const rate = stove.rate === void 0 ? void 0 : stoveRate(stove.rate);
|
|
1503
|
+
if (stove.rate === void 0) why.push(`The stove of ${stove.fuel} has no cited fuel rate.`);
|
|
1504
|
+
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.`);
|
|
1505
|
+
let potPerMin;
|
|
1506
|
+
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.");
|
|
1507
|
+
else if (rate !== void 0) {
|
|
1508
|
+
if (rate.kind === "energy") {
|
|
1509
|
+
if (stove.heating !== share.heating) why.push(`The stove share is for the ${share.heating} heating value, and the stove rate is for the ${String(stove.heating ?? "unknown")} heating value.`);
|
|
1510
|
+
else potPerMin = rate.perMin * share.value;
|
|
1511
|
+
} else if (fuelEnergy === void 0) why.push(`The fuel ${stove.fuel} has no cited energy, so its mass rate gives no heat for the boil.`);
|
|
1512
|
+
else if (fuelEnergy.heating !== share.heating) why.push(`The stove share is for the ${share.heating} heating value, and the energy of the fuel ${stove.fuel} is the ${fuelEnergy.heating} value.`);
|
|
1513
|
+
else potPerMin = rate.perMin * fuelEnergy.perKg * share.value;
|
|
1514
|
+
}
|
|
1515
|
+
let fuelPerMin;
|
|
1516
|
+
if (rate?.kind === "mass") fuelPerMin = rate.perMin;
|
|
1517
|
+
else if (rate?.kind === "energy") {
|
|
1518
|
+
if (fuelEnergy === void 0) why.push(`The fuel ${stove.fuel} has no cited energy, so the energy rate of the stove gives no fuel mass.`);
|
|
1519
|
+
else if (stove.heating === void 0) why.push("The energy rate of the stove has no heating value.");
|
|
1520
|
+
else if (stove.heating !== fuelEnergy.heating) why.push(`The stove rate is for the ${stove.heating} heating value, and the energy of the fuel ${stove.fuel} is the ${fuelEnergy.heating} value.`);
|
|
1521
|
+
else fuelPerMin = rate.perMin / fuelEnergy.perKg;
|
|
1522
|
+
}
|
|
1523
|
+
if (why.length > 0 || !heat.read || cookMin === void 0 || potPerMin === void 0 || fuelPerMin === void 0 || !(potPerMin > 0)) {
|
|
1524
|
+
const text = why.join(" ");
|
|
1525
|
+
return {
|
|
1526
|
+
product: product.id,
|
|
1527
|
+
resource,
|
|
1528
|
+
energy,
|
|
1529
|
+
water,
|
|
1530
|
+
heat,
|
|
1531
|
+
minutes: noRead(text),
|
|
1532
|
+
fuel: noRead(text)
|
|
1533
|
+
};
|
|
1534
|
+
}
|
|
1535
|
+
const minutes = heat.value / potPerMin + cookMin;
|
|
1536
|
+
return {
|
|
1537
|
+
product: product.id,
|
|
1538
|
+
resource,
|
|
1539
|
+
energy,
|
|
1540
|
+
water,
|
|
1541
|
+
heat,
|
|
1542
|
+
minutes: read(minutes, "min"),
|
|
1543
|
+
fuel: read(minutes * fuelPerMin, "kg")
|
|
1544
|
+
};
|
|
1545
|
+
}
|
|
1546
|
+
/** This function gives the stove facts of the input: the facts of the caller, or the facts of a product of the catalog. */
|
|
1547
|
+
function stoveOf(catalog, stove) {
|
|
1548
|
+
if (typeof stove !== "string") return stove;
|
|
1549
|
+
const product = catalog.products.find((item) => item.id === stove);
|
|
1550
|
+
if (product === void 0) throw new RangeError(`The catalog has no product "${stove}".`);
|
|
1551
|
+
if (product.stove === void 0) throw new RangeError(`The product "${stove}" has no stove facts.`);
|
|
1552
|
+
return product.stove;
|
|
1553
|
+
}
|
|
1554
|
+
/** This function tells if a resource of the catalog is a food. */
|
|
1555
|
+
function isFood(catalog, code) {
|
|
1556
|
+
return catalog.resources.find((item) => item.code === code)?.kind === "food";
|
|
1557
|
+
}
|
|
1558
|
+
/** This function tells if a resource of the catalog is a dry food: a food resource in a unit of mass. */
|
|
1559
|
+
function isDryFood(catalog, code) {
|
|
1560
|
+
const resource = catalog.resources.find((item) => item.code === code);
|
|
1561
|
+
return resource !== void 0 && resource.kind === "food" && inUnit(1, resource.unit, "kg") !== void 0;
|
|
1562
|
+
}
|
|
1563
|
+
/**
|
|
1564
|
+
* This function gives the converters that cook the dry food of a buy list, with the water and the fuel of each pot.
|
|
1565
|
+
*
|
|
1566
|
+
* - One run of a converter is one pot of `batch`. It takes the dry food, the cook water from `water.stored` and the fuel of the stove.
|
|
1567
|
+
* It gives the food energy of the pot to the need `food.energy`. Thus a dry food with no fuel or no water gives no meal.
|
|
1568
|
+
* - The cook water is `cookWater` of market times the dry mass. It is the resource of the drinking water.
|
|
1569
|
+
* - The heat to boil is the mass of the water and the dry food times `WATER_SPECIFIC_HEAT` times the rise from the start to `BOIL_CELSIUS`.
|
|
1570
|
+
* The water mass takes `WATER_DENSITY_MAX`. The dry food takes the specific heat of water, which is higher than its own.
|
|
1571
|
+
* - 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.
|
|
1572
|
+
* - 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.
|
|
1573
|
+
* The model does not give that time, so there the fuel is not an upper limit.
|
|
1574
|
+
* - 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.
|
|
1575
|
+
*
|
|
1576
|
+
* 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.
|
|
1577
|
+
* 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:
|
|
1578
|
+
* 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.
|
|
1579
|
+
* A store of the resource from a different fragment gets the converter of this list.
|
|
1580
|
+
*
|
|
1581
|
+
* The catalog of market gives no soak water. A food that needs a soak, for example dry beans, has a second water draw that this function does not give.
|
|
1582
|
+
* A product that gives more than one food has no energy for each food, so its energy is a no read.
|
|
1583
|
+
*
|
|
1584
|
+
* The cook water is `water.stored`, the store of the drinking water. A drinking draw at a higher level of the same store takes
|
|
1585
|
+
* its water first, by the take order of `enduranceOf`. The drinking draw keeps back its days times its demand, which is all of the store.
|
|
1586
|
+
* Thus, when a case cuts the water supply, the dry food adds 0 days at each amount of stored water.
|
|
1587
|
+
*
|
|
1588
|
+
* The function is pure.
|
|
1589
|
+
*
|
|
1590
|
+
* @param input - The catalog and the buy list, the stove, the dry mass of one pot, the share and the start temperature
|
|
1591
|
+
* @returns The fragment, the line of each dry food and the dry resources with no converter
|
|
1592
|
+
* @throws RangeError when the batch is not a mass above 0, or when the share is not above 0 and up to 1
|
|
1593
|
+
* @throws RangeError when the start is not from 0 up to less than 100 °C
|
|
1594
|
+
* @throws RangeError when the stove is a product with no stove facts
|
|
1595
|
+
* @throws RangeError when the catalog has no product of an `id` of the buy list
|
|
1596
|
+
* @throws RangeError when the catalog has no resource of the fuel in a unit of mass
|
|
1597
|
+
* @throws RangeError when a converter has a read and the catalog has no resource `water.stored` in a unit of volume
|
|
1598
|
+
* @throws RangeError for the errors of `giftsOf`
|
|
1599
|
+
*/
|
|
1600
|
+
function cookFragment(input) {
|
|
1601
|
+
const { catalog, units } = input;
|
|
1602
|
+
const batchKg = inUnit(input.batch.value, input.batch.unit, "kg");
|
|
1603
|
+
if (batchKg === void 0 || !(batchKg > 0)) throw new RangeError(`The batch ${String(input.batch.value)} ${input.batch.unit} is not a mass above 0.`);
|
|
1604
|
+
const { share } = input;
|
|
1605
|
+
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.`);
|
|
1606
|
+
const startC = input.start?.celsius ?? 0;
|
|
1607
|
+
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.`);
|
|
1608
|
+
const stove = stoveOf(catalog, input.stove);
|
|
1609
|
+
const fuel = catalog.resources.find((resource) => resource.code === stove.fuel);
|
|
1610
|
+
const fuelUnitPerKg = fuel === void 0 ? void 0 : inUnit(1, "kg", fuel.unit);
|
|
1611
|
+
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.`);
|
|
1612
|
+
const perKg = fuelEnergyPerMass(fuel);
|
|
1613
|
+
const context = {
|
|
1614
|
+
stove,
|
|
1615
|
+
batchKg,
|
|
1616
|
+
share,
|
|
1617
|
+
startC,
|
|
1618
|
+
fuelEnergy: perKg === void 0 || fuel.fuelEnergy === void 0 ? void 0 : {
|
|
1619
|
+
perKg: valueIn(perKg, "kJ/kg"),
|
|
1620
|
+
heating: fuel.fuelEnergy.heating
|
|
1621
|
+
}
|
|
1622
|
+
};
|
|
1623
|
+
const unitOf = new Map(catalog.resources.map((resource) => [resource.code, resource.unit]));
|
|
1624
|
+
const lines = [];
|
|
1625
|
+
for (const [id, count] of Object.entries(units)) {
|
|
1626
|
+
if (!(count > 0)) continue;
|
|
1627
|
+
const product = catalog.products.find((item) => item.id === id);
|
|
1628
|
+
if (product === void 0) throw new RangeError(`The catalog has no product "${id}".`);
|
|
1629
|
+
if (product.kind !== "food") continue;
|
|
1630
|
+
const gifts = giftsOf(catalog, id, 1);
|
|
1631
|
+
const foodGifts = gifts.filter((gift) => isFood(catalog, gift.resource)).length;
|
|
1632
|
+
for (const gift of gifts) {
|
|
1633
|
+
if (!isDryFood(catalog, gift.resource)) continue;
|
|
1634
|
+
const perUnitKg = gift.read ? inUnit(gift.amount, gift.unit, "kg") : void 0;
|
|
1635
|
+
lines.push(cookLine(product, gift.resource, perUnitKg, foodGifts, context));
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
const prefix = input.prefix ?? "cook";
|
|
1639
|
+
const converters = [];
|
|
1640
|
+
const noReads = [];
|
|
1641
|
+
for (const resource of [...new Set(lines.map((line) => line.resource))]) {
|
|
1642
|
+
const own = lines.filter((line) => line.resource === resource);
|
|
1643
|
+
const why = own.flatMap((line) => [
|
|
1644
|
+
line.energy,
|
|
1645
|
+
line.water,
|
|
1646
|
+
line.fuel
|
|
1647
|
+
].flatMap((value) => value.read ? [] : [`${line.product}: ${value.why}`]));
|
|
1648
|
+
if (why.length > 0) {
|
|
1649
|
+
noReads.push({
|
|
1650
|
+
resource,
|
|
1651
|
+
why: [...new Set(why)].join(" ")
|
|
1652
|
+
});
|
|
1653
|
+
continue;
|
|
1654
|
+
}
|
|
1655
|
+
const most = (pick) => Math.max(...own.map((line) => valueOf(pick(line))));
|
|
1656
|
+
const least = (pick) => Math.min(...own.map((line) => valueOf(pick(line))));
|
|
1657
|
+
const dryPerKg = inUnit(1, "kg", unitOf.get(resource) ?? "") ?? 1;
|
|
1658
|
+
const waterPerL = inUnit(1, "L", unitOf.get("water.stored") ?? "");
|
|
1659
|
+
if (waterPerL === void 0) throw new RangeError(`The catalog has no resource "${STORED_WATER}" in a unit of volume for the cook water.`);
|
|
1660
|
+
converters.push({
|
|
1661
|
+
id: `${prefix}:${resource}`,
|
|
1662
|
+
...input.device === void 0 ? {} : { device: input.device },
|
|
1663
|
+
takes: [
|
|
1664
|
+
{
|
|
1665
|
+
resource,
|
|
1666
|
+
amount: batchKg * dryPerKg
|
|
1667
|
+
},
|
|
1668
|
+
{
|
|
1669
|
+
resource: STORED_WATER,
|
|
1670
|
+
amount: most((line) => line.water) * batchKg * waterPerL
|
|
1671
|
+
},
|
|
1672
|
+
{
|
|
1673
|
+
resource: stove.fuel,
|
|
1674
|
+
amount: most((line) => line.fuel) * fuelUnitPerKg
|
|
1675
|
+
}
|
|
1676
|
+
],
|
|
1677
|
+
gives: {
|
|
1678
|
+
need: FOOD_NEED,
|
|
1679
|
+
amount: least((line) => line.energy) * batchKg
|
|
1680
|
+
}
|
|
1681
|
+
});
|
|
1682
|
+
}
|
|
1683
|
+
return {
|
|
1684
|
+
layer: {
|
|
1685
|
+
schema: "endurance/1",
|
|
1686
|
+
levels: [],
|
|
1687
|
+
resources: [...new Set(converters.flatMap((converter) => converter.takes.map((take) => take.resource)))].map((code) => ({
|
|
1688
|
+
code,
|
|
1689
|
+
unit: unitOf.get(code) ?? ""
|
|
1690
|
+
})),
|
|
1691
|
+
stores: [],
|
|
1692
|
+
draws: [],
|
|
1693
|
+
converters
|
|
1694
|
+
},
|
|
1695
|
+
lines,
|
|
1696
|
+
noReads,
|
|
1697
|
+
read: noReads.length === 0
|
|
1698
|
+
};
|
|
1699
|
+
}
|
|
1700
|
+
/** This function gives the value of a read. The caller tests the read first. */
|
|
1701
|
+
function valueOf(value) {
|
|
1702
|
+
return value.read ? value.value : NaN;
|
|
1703
|
+
}
|
|
1704
|
+
export { ACTIVITY_LEVELS, BOIL_CELSIUS, BREAST_MILK_MONTHS, CALORIE_CHAIN, CALORIE_SEXES, CALORIE_TABLES, COLD_START_CELSIUS, DGA_CALORIES, DGA_SOURCE, DIRECT_USES, FOOD_NEED, FORMULA_DRAW_MONTHS, FORMULA_NEED, FORMULA_READY, INFANT_FEEDINGS, INFANT_MONTHS, JOIN_CODES, NASEM_ENERGY_SOURCE, NASEM_INFANT_CALORIES, NEEDS_CODES, NEED_SUPPLIES, NIST_SI_GUIDE_SOURCE, NO_COOK, STORED_WATER, USGS_WATER_DENSITY_SOURCE, WATER_DENSITY_MAX, WATER_NEED, WATER_SPECIFIC_HEAT, calorieChange, calorieNeed, calorieRow, calorieRowOf, checkCalorieChain, checkCalorieTable, checkJoins, cookFragment, hasNeedsError, householdFragment, isActivityLevel };
|
|
1332
1705
|
|
|
1333
1706
|
//# sourceMappingURL=index.js.map
|