@mark1russell7/stack-needs 0.0.0-bootstrap.0 → 0.1.0
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/LICENSE +21 -0
- package/README.md +97 -1
- package/build/index.d.ts +277 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +787 -0
- package/build/index.js.map +1 -0
- package/package.json +61 -6
package/build/index.js
ADDED
|
@@ -0,0 +1,787 @@
|
|
|
1
|
+
import { quantity, valueIn } from "@mark1russell7/quantity";
|
|
2
|
+
import { checkSources } from "@mark1russell7/guidebook";
|
|
3
|
+
import { buyListEnergy, giftsOf } from "@mark1russell7/market/supplies";
|
|
4
|
+
// src/calorie.ts
|
|
5
|
+
/** The activity levels of the table, as the source names them: sedentary, moderately active and active. */
|
|
6
|
+
const ACTIVITY_LEVELS = [
|
|
7
|
+
"sedentary",
|
|
8
|
+
"moderately-active",
|
|
9
|
+
"active"
|
|
10
|
+
];
|
|
11
|
+
/** The sexes of the table. */
|
|
12
|
+
const CALORIE_SEXES = ["female", "male"];
|
|
13
|
+
/** The unit of each food energy. */
|
|
14
|
+
const ENERGY_UNIT = "kcal";
|
|
15
|
+
/** The unit of each daily need. */
|
|
16
|
+
const DAILY_UNIT = "kcal/d";
|
|
17
|
+
/** This function tells if a value is an activity level of the table. */
|
|
18
|
+
function isActivityLevel(level) {
|
|
19
|
+
return ACTIVITY_LEVELS.includes(level);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* This function gives the row of a table for a sex and an age in months, or `undefined` when the table has no row.
|
|
23
|
+
*
|
|
24
|
+
* @param table - The table of calorie needs
|
|
25
|
+
* @param sex - The sex
|
|
26
|
+
* @param ageMonths - The age, in whole months
|
|
27
|
+
*/
|
|
28
|
+
function calorieRow(table, sex, ageMonths) {
|
|
29
|
+
return table.rows.find((row) => row.sex === sex && ageMonths >= row.from && (row.to === void 0 || ageMonths <= row.to));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* This function gives the energy need of the persons of a household for a count of days, in `kcal`.
|
|
33
|
+
* It adds the need of each person at the activity level of that person. The table finds the row of each person from the age.
|
|
34
|
+
*
|
|
35
|
+
* - An age with no row in the table gives a line with no read, for example 6 months. Then the total is a lower limit.
|
|
36
|
+
* - The table of the toddlers gives one need for each age and sex. Thus the level does not change the need of a toddler.
|
|
37
|
+
*
|
|
38
|
+
* The activity level is an explicit input. This package has no default level. The planner selects it, also for the worst case of a long power cut.
|
|
39
|
+
* In that case, work can make the level higher, for example the carry of water. The function is pure.
|
|
40
|
+
*
|
|
41
|
+
* @param table - The table of calorie needs
|
|
42
|
+
* @param persons - The person records of the household: a sex, an age in months, a count and, as an option, a level
|
|
43
|
+
* @param level - The activity level of each record with no level of its own
|
|
44
|
+
* @param days - The count of days, a finite number of 0 or more
|
|
45
|
+
* @throws RangeError when a count of persons or an age is not a whole number of 0 or more, or when the household has no person
|
|
46
|
+
* @throws RangeError when a level is not a level of the table, or when a sex is not a sex of the table
|
|
47
|
+
* @throws RangeError when the count of days is not a finite number of 0 or more
|
|
48
|
+
*/
|
|
49
|
+
function calorieNeed(table, persons, level, days) {
|
|
50
|
+
if (!(Number.isFinite(days) && days >= 0)) throw new RangeError(`The count of days is ${String(days)}. It must be a finite number of 0 or more.`);
|
|
51
|
+
if (!isActivityLevel(level)) throw new RangeError(`The activity level ${String(level)} is not a level of the table.`);
|
|
52
|
+
let count = 0;
|
|
53
|
+
const lines = persons.map((person) => {
|
|
54
|
+
const name = `${String(person.sex)} of ${String(person.ageMonths)} months`;
|
|
55
|
+
if (!(Number.isSafeInteger(person.count) && person.count >= 0)) throw new RangeError(`The count of the persons ${name} is ${String(person.count)}. It must be a whole number of 0 or more.`);
|
|
56
|
+
if (!(Number.isSafeInteger(person.ageMonths) && person.ageMonths >= 0)) throw new RangeError(`The age of the persons ${name} is not a whole number of months of 0 or more.`);
|
|
57
|
+
if (!CALORIE_SEXES.includes(person.sex)) throw new RangeError(`The sex ${String(person.sex)} is not a sex of the table.`);
|
|
58
|
+
const own = person.level ?? level;
|
|
59
|
+
if (!isActivityLevel(own)) throw new RangeError(`The activity level ${String(own)} of the persons ${name} is not a level of the table.`);
|
|
60
|
+
count += person.count;
|
|
61
|
+
const row = calorieRow(table, person.sex, person.ageMonths);
|
|
62
|
+
if (row === void 0) return {
|
|
63
|
+
person,
|
|
64
|
+
read: false,
|
|
65
|
+
why: `The table ${table.id} has no row for a ${name}.`
|
|
66
|
+
};
|
|
67
|
+
const daily = row.need.levels === void 0 ? row.need.each : row.need.levels[own];
|
|
68
|
+
return {
|
|
69
|
+
person,
|
|
70
|
+
read: true,
|
|
71
|
+
level: own,
|
|
72
|
+
row: row.age,
|
|
73
|
+
need: quantity(person.count * valueIn(daily, DAILY_UNIT) * days, ENERGY_UNIT)
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
if (count === 0) throw new RangeError("The household has no person.");
|
|
77
|
+
const total = lines.reduce((sum, line) => sum + (line.read ? line.need.value : 0), 0);
|
|
78
|
+
return {
|
|
79
|
+
lines,
|
|
80
|
+
read: lines.every((line) => line.read),
|
|
81
|
+
need: quantity(total, ENERGY_UNIT),
|
|
82
|
+
persons: count
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// src/data.ts
|
|
86
|
+
/** The source of the calorie table: the PDF of the Dietary Guidelines for Americans, 2020-2025, 9th Edition. It is a work of the United States federal government. */
|
|
87
|
+
const DGA_SOURCE = {
|
|
88
|
+
id: "dga-2020-2025",
|
|
89
|
+
title: "Dietary Guidelines for Americans, 2020-2025, 9th Edition",
|
|
90
|
+
url: "https://www.dietaryguidelines.gov/sites/default/files/2020-12/Dietary_Guidelines_for_Americans_2020-2025.pdf",
|
|
91
|
+
license: "us-public-domain",
|
|
92
|
+
checked: "2026-11-22",
|
|
93
|
+
by: "U.S. Department of Agriculture and U.S. Department of Health and Human Services",
|
|
94
|
+
sha256: "ceea04ce1a7c13a95a170f3548ed3926ad6dd49599bd730fb58bc9c02711d16e"
|
|
95
|
+
};
|
|
96
|
+
const day = (value) => ({
|
|
97
|
+
value,
|
|
98
|
+
unit: "kcal/d"
|
|
99
|
+
});
|
|
100
|
+
const levels = (sedentary, moderate, active) => ({ levels: {
|
|
101
|
+
sedentary: day(sedentary),
|
|
102
|
+
"moderately-active": day(moderate),
|
|
103
|
+
active: day(active)
|
|
104
|
+
} });
|
|
105
|
+
const each = (value) => ({ each: day(value) });
|
|
106
|
+
/** The table of estimated calorie needs of the Dietary Guidelines for Americans, 2020-2025: 68 rows, 34 for each sex. */
|
|
107
|
+
const DGA_CALORIES = {
|
|
108
|
+
id: "dga.2020-2025:calorie-needs",
|
|
109
|
+
title: "Dietary Guidelines for Americans, 2020-2025: Estimated Calorie Needs",
|
|
110
|
+
source: DGA_SOURCE,
|
|
111
|
+
locator: "Appendix 2: Table A2-1 (ages 12 through 23 months, page 139) and Table A2-2 (ages 2 and older, pages 140 and 141)",
|
|
112
|
+
rows: [
|
|
113
|
+
{
|
|
114
|
+
sex: "female",
|
|
115
|
+
age: "12 months",
|
|
116
|
+
from: 12,
|
|
117
|
+
to: 14,
|
|
118
|
+
need: each(800)
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
sex: "female",
|
|
122
|
+
age: "15 months",
|
|
123
|
+
from: 15,
|
|
124
|
+
to: 17,
|
|
125
|
+
need: each(800)
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
sex: "female",
|
|
129
|
+
age: "18 months",
|
|
130
|
+
from: 18,
|
|
131
|
+
to: 20,
|
|
132
|
+
need: each(900)
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
sex: "female",
|
|
136
|
+
age: "21 through 23 months",
|
|
137
|
+
from: 21,
|
|
138
|
+
to: 23,
|
|
139
|
+
need: each(1e3)
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
sex: "female",
|
|
143
|
+
age: "2",
|
|
144
|
+
from: 24,
|
|
145
|
+
to: 35,
|
|
146
|
+
need: levels(1e3, 1e3, 1e3)
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
sex: "female",
|
|
150
|
+
age: "3",
|
|
151
|
+
from: 36,
|
|
152
|
+
to: 47,
|
|
153
|
+
need: levels(1e3, 1200, 1400)
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
sex: "female",
|
|
157
|
+
age: "4",
|
|
158
|
+
from: 48,
|
|
159
|
+
to: 59,
|
|
160
|
+
need: levels(1200, 1400, 1400)
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
sex: "female",
|
|
164
|
+
age: "5",
|
|
165
|
+
from: 60,
|
|
166
|
+
to: 71,
|
|
167
|
+
need: levels(1200, 1400, 1600)
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
sex: "female",
|
|
171
|
+
age: "6",
|
|
172
|
+
from: 72,
|
|
173
|
+
to: 83,
|
|
174
|
+
need: levels(1200, 1400, 1600)
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
sex: "female",
|
|
178
|
+
age: "7",
|
|
179
|
+
from: 84,
|
|
180
|
+
to: 95,
|
|
181
|
+
need: levels(1200, 1600, 1800)
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
sex: "female",
|
|
185
|
+
age: "8",
|
|
186
|
+
from: 96,
|
|
187
|
+
to: 107,
|
|
188
|
+
need: levels(1400, 1600, 1800)
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
sex: "female",
|
|
192
|
+
age: "9",
|
|
193
|
+
from: 108,
|
|
194
|
+
to: 119,
|
|
195
|
+
need: levels(1400, 1600, 1800)
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
sex: "female",
|
|
199
|
+
age: "10",
|
|
200
|
+
from: 120,
|
|
201
|
+
to: 131,
|
|
202
|
+
need: levels(1400, 1800, 2e3)
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
sex: "female",
|
|
206
|
+
age: "11",
|
|
207
|
+
from: 132,
|
|
208
|
+
to: 143,
|
|
209
|
+
need: levels(1600, 1800, 2e3)
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
sex: "female",
|
|
213
|
+
age: "12",
|
|
214
|
+
from: 144,
|
|
215
|
+
to: 155,
|
|
216
|
+
need: levels(1600, 2e3, 2200)
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
sex: "female",
|
|
220
|
+
age: "13",
|
|
221
|
+
from: 156,
|
|
222
|
+
to: 167,
|
|
223
|
+
need: levels(1600, 2e3, 2200)
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
sex: "female",
|
|
227
|
+
age: "14",
|
|
228
|
+
from: 168,
|
|
229
|
+
to: 179,
|
|
230
|
+
need: levels(1800, 2e3, 2400)
|
|
231
|
+
},
|
|
232
|
+
{
|
|
233
|
+
sex: "female",
|
|
234
|
+
age: "15",
|
|
235
|
+
from: 180,
|
|
236
|
+
to: 191,
|
|
237
|
+
need: levels(1800, 2e3, 2400)
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
sex: "female",
|
|
241
|
+
age: "16",
|
|
242
|
+
from: 192,
|
|
243
|
+
to: 203,
|
|
244
|
+
need: levels(1800, 2e3, 2400)
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
sex: "female",
|
|
248
|
+
age: "17",
|
|
249
|
+
from: 204,
|
|
250
|
+
to: 215,
|
|
251
|
+
need: levels(1800, 2e3, 2400)
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
sex: "female",
|
|
255
|
+
age: "18",
|
|
256
|
+
from: 216,
|
|
257
|
+
to: 227,
|
|
258
|
+
need: levels(1800, 2e3, 2400)
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
sex: "female",
|
|
262
|
+
age: "19-20",
|
|
263
|
+
from: 228,
|
|
264
|
+
to: 251,
|
|
265
|
+
need: levels(2e3, 2200, 2400)
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
sex: "female",
|
|
269
|
+
age: "21-25",
|
|
270
|
+
from: 252,
|
|
271
|
+
to: 311,
|
|
272
|
+
need: levels(2e3, 2200, 2400)
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
sex: "female",
|
|
276
|
+
age: "26-30",
|
|
277
|
+
from: 312,
|
|
278
|
+
to: 371,
|
|
279
|
+
need: levels(1800, 2e3, 2400)
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
sex: "female",
|
|
283
|
+
age: "31-35",
|
|
284
|
+
from: 372,
|
|
285
|
+
to: 431,
|
|
286
|
+
need: levels(1800, 2e3, 2200)
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
sex: "female",
|
|
290
|
+
age: "36-40",
|
|
291
|
+
from: 432,
|
|
292
|
+
to: 491,
|
|
293
|
+
need: levels(1800, 2e3, 2200)
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
sex: "female",
|
|
297
|
+
age: "41-45",
|
|
298
|
+
from: 492,
|
|
299
|
+
to: 551,
|
|
300
|
+
need: levels(1800, 2e3, 2200)
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
sex: "female",
|
|
304
|
+
age: "46-50",
|
|
305
|
+
from: 552,
|
|
306
|
+
to: 611,
|
|
307
|
+
need: levels(1800, 2e3, 2200)
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
sex: "female",
|
|
311
|
+
age: "51-55",
|
|
312
|
+
from: 612,
|
|
313
|
+
to: 671,
|
|
314
|
+
need: levels(1600, 1800, 2200)
|
|
315
|
+
},
|
|
316
|
+
{
|
|
317
|
+
sex: "female",
|
|
318
|
+
age: "56-60",
|
|
319
|
+
from: 672,
|
|
320
|
+
to: 731,
|
|
321
|
+
need: levels(1600, 1800, 2200)
|
|
322
|
+
},
|
|
323
|
+
{
|
|
324
|
+
sex: "female",
|
|
325
|
+
age: "61-65",
|
|
326
|
+
from: 732,
|
|
327
|
+
to: 791,
|
|
328
|
+
need: levels(1600, 1800, 2e3)
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
sex: "female",
|
|
332
|
+
age: "66-70",
|
|
333
|
+
from: 792,
|
|
334
|
+
to: 851,
|
|
335
|
+
need: levels(1600, 1800, 2e3)
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
sex: "female",
|
|
339
|
+
age: "71-75",
|
|
340
|
+
from: 852,
|
|
341
|
+
to: 911,
|
|
342
|
+
need: levels(1600, 1800, 2e3)
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
sex: "female",
|
|
346
|
+
age: "76 and up",
|
|
347
|
+
from: 912,
|
|
348
|
+
need: levels(1600, 1800, 2e3)
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
sex: "male",
|
|
352
|
+
age: "12 months",
|
|
353
|
+
from: 12,
|
|
354
|
+
to: 14,
|
|
355
|
+
need: each(800)
|
|
356
|
+
},
|
|
357
|
+
{
|
|
358
|
+
sex: "male",
|
|
359
|
+
age: "15 months",
|
|
360
|
+
from: 15,
|
|
361
|
+
to: 17,
|
|
362
|
+
need: each(900)
|
|
363
|
+
},
|
|
364
|
+
{
|
|
365
|
+
sex: "male",
|
|
366
|
+
age: "18 months",
|
|
367
|
+
from: 18,
|
|
368
|
+
to: 20,
|
|
369
|
+
need: each(1e3)
|
|
370
|
+
},
|
|
371
|
+
{
|
|
372
|
+
sex: "male",
|
|
373
|
+
age: "21 through 23 months",
|
|
374
|
+
from: 21,
|
|
375
|
+
to: 23,
|
|
376
|
+
need: each(1e3)
|
|
377
|
+
},
|
|
378
|
+
{
|
|
379
|
+
sex: "male",
|
|
380
|
+
age: "2",
|
|
381
|
+
from: 24,
|
|
382
|
+
to: 35,
|
|
383
|
+
need: levels(1e3, 1e3, 1e3)
|
|
384
|
+
},
|
|
385
|
+
{
|
|
386
|
+
sex: "male",
|
|
387
|
+
age: "3",
|
|
388
|
+
from: 36,
|
|
389
|
+
to: 47,
|
|
390
|
+
need: levels(1e3, 1400, 1400)
|
|
391
|
+
},
|
|
392
|
+
{
|
|
393
|
+
sex: "male",
|
|
394
|
+
age: "4",
|
|
395
|
+
from: 48,
|
|
396
|
+
to: 59,
|
|
397
|
+
need: levels(1200, 1400, 1600)
|
|
398
|
+
},
|
|
399
|
+
{
|
|
400
|
+
sex: "male",
|
|
401
|
+
age: "5",
|
|
402
|
+
from: 60,
|
|
403
|
+
to: 71,
|
|
404
|
+
need: levels(1200, 1400, 1600)
|
|
405
|
+
},
|
|
406
|
+
{
|
|
407
|
+
sex: "male",
|
|
408
|
+
age: "6",
|
|
409
|
+
from: 72,
|
|
410
|
+
to: 83,
|
|
411
|
+
need: levels(1400, 1600, 1800)
|
|
412
|
+
},
|
|
413
|
+
{
|
|
414
|
+
sex: "male",
|
|
415
|
+
age: "7",
|
|
416
|
+
from: 84,
|
|
417
|
+
to: 95,
|
|
418
|
+
need: levels(1400, 1600, 1800)
|
|
419
|
+
},
|
|
420
|
+
{
|
|
421
|
+
sex: "male",
|
|
422
|
+
age: "8",
|
|
423
|
+
from: 96,
|
|
424
|
+
to: 107,
|
|
425
|
+
need: levels(1400, 1600, 2e3)
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
sex: "male",
|
|
429
|
+
age: "9",
|
|
430
|
+
from: 108,
|
|
431
|
+
to: 119,
|
|
432
|
+
need: levels(1600, 1800, 2e3)
|
|
433
|
+
},
|
|
434
|
+
{
|
|
435
|
+
sex: "male",
|
|
436
|
+
age: "10",
|
|
437
|
+
from: 120,
|
|
438
|
+
to: 131,
|
|
439
|
+
need: levels(1600, 1800, 2200)
|
|
440
|
+
},
|
|
441
|
+
{
|
|
442
|
+
sex: "male",
|
|
443
|
+
age: "11",
|
|
444
|
+
from: 132,
|
|
445
|
+
to: 143,
|
|
446
|
+
need: levels(1800, 2e3, 2200)
|
|
447
|
+
},
|
|
448
|
+
{
|
|
449
|
+
sex: "male",
|
|
450
|
+
age: "12",
|
|
451
|
+
from: 144,
|
|
452
|
+
to: 155,
|
|
453
|
+
need: levels(1800, 2200, 2400)
|
|
454
|
+
},
|
|
455
|
+
{
|
|
456
|
+
sex: "male",
|
|
457
|
+
age: "13",
|
|
458
|
+
from: 156,
|
|
459
|
+
to: 167,
|
|
460
|
+
need: levels(2e3, 2200, 2600)
|
|
461
|
+
},
|
|
462
|
+
{
|
|
463
|
+
sex: "male",
|
|
464
|
+
age: "14",
|
|
465
|
+
from: 168,
|
|
466
|
+
to: 179,
|
|
467
|
+
need: levels(2e3, 2400, 2800)
|
|
468
|
+
},
|
|
469
|
+
{
|
|
470
|
+
sex: "male",
|
|
471
|
+
age: "15",
|
|
472
|
+
from: 180,
|
|
473
|
+
to: 191,
|
|
474
|
+
need: levels(2200, 2600, 3e3)
|
|
475
|
+
},
|
|
476
|
+
{
|
|
477
|
+
sex: "male",
|
|
478
|
+
age: "16",
|
|
479
|
+
from: 192,
|
|
480
|
+
to: 203,
|
|
481
|
+
need: levels(2400, 2800, 3200)
|
|
482
|
+
},
|
|
483
|
+
{
|
|
484
|
+
sex: "male",
|
|
485
|
+
age: "17",
|
|
486
|
+
from: 204,
|
|
487
|
+
to: 215,
|
|
488
|
+
need: levels(2400, 2800, 3200)
|
|
489
|
+
},
|
|
490
|
+
{
|
|
491
|
+
sex: "male",
|
|
492
|
+
age: "18",
|
|
493
|
+
from: 216,
|
|
494
|
+
to: 227,
|
|
495
|
+
need: levels(2400, 2800, 3200)
|
|
496
|
+
},
|
|
497
|
+
{
|
|
498
|
+
sex: "male",
|
|
499
|
+
age: "19-20",
|
|
500
|
+
from: 228,
|
|
501
|
+
to: 251,
|
|
502
|
+
need: levels(2600, 2800, 3e3)
|
|
503
|
+
},
|
|
504
|
+
{
|
|
505
|
+
sex: "male",
|
|
506
|
+
age: "21-25",
|
|
507
|
+
from: 252,
|
|
508
|
+
to: 311,
|
|
509
|
+
need: levels(2400, 2800, 3e3)
|
|
510
|
+
},
|
|
511
|
+
{
|
|
512
|
+
sex: "male",
|
|
513
|
+
age: "26-30",
|
|
514
|
+
from: 312,
|
|
515
|
+
to: 371,
|
|
516
|
+
need: levels(2400, 2600, 3e3)
|
|
517
|
+
},
|
|
518
|
+
{
|
|
519
|
+
sex: "male",
|
|
520
|
+
age: "31-35",
|
|
521
|
+
from: 372,
|
|
522
|
+
to: 431,
|
|
523
|
+
need: levels(2400, 2600, 3e3)
|
|
524
|
+
},
|
|
525
|
+
{
|
|
526
|
+
sex: "male",
|
|
527
|
+
age: "36-40",
|
|
528
|
+
from: 432,
|
|
529
|
+
to: 491,
|
|
530
|
+
need: levels(2400, 2600, 2800)
|
|
531
|
+
},
|
|
532
|
+
{
|
|
533
|
+
sex: "male",
|
|
534
|
+
age: "41-45",
|
|
535
|
+
from: 492,
|
|
536
|
+
to: 551,
|
|
537
|
+
need: levels(2200, 2600, 2800)
|
|
538
|
+
},
|
|
539
|
+
{
|
|
540
|
+
sex: "male",
|
|
541
|
+
age: "46-50",
|
|
542
|
+
from: 552,
|
|
543
|
+
to: 611,
|
|
544
|
+
need: levels(2200, 2400, 2800)
|
|
545
|
+
},
|
|
546
|
+
{
|
|
547
|
+
sex: "male",
|
|
548
|
+
age: "51-55",
|
|
549
|
+
from: 612,
|
|
550
|
+
to: 671,
|
|
551
|
+
need: levels(2200, 2400, 2800)
|
|
552
|
+
},
|
|
553
|
+
{
|
|
554
|
+
sex: "male",
|
|
555
|
+
age: "56-60",
|
|
556
|
+
from: 672,
|
|
557
|
+
to: 731,
|
|
558
|
+
need: levels(2200, 2400, 2600)
|
|
559
|
+
},
|
|
560
|
+
{
|
|
561
|
+
sex: "male",
|
|
562
|
+
age: "61-65",
|
|
563
|
+
from: 732,
|
|
564
|
+
to: 791,
|
|
565
|
+
need: levels(2e3, 2400, 2600)
|
|
566
|
+
},
|
|
567
|
+
{
|
|
568
|
+
sex: "male",
|
|
569
|
+
age: "66-70",
|
|
570
|
+
from: 792,
|
|
571
|
+
to: 851,
|
|
572
|
+
need: levels(2e3, 2200, 2600)
|
|
573
|
+
},
|
|
574
|
+
{
|
|
575
|
+
sex: "male",
|
|
576
|
+
age: "71-75",
|
|
577
|
+
from: 852,
|
|
578
|
+
to: 911,
|
|
579
|
+
need: levels(2e3, 2200, 2600)
|
|
580
|
+
},
|
|
581
|
+
{
|
|
582
|
+
sex: "male",
|
|
583
|
+
age: "76 and up",
|
|
584
|
+
from: 912,
|
|
585
|
+
need: levels(2e3, 2200, 2400)
|
|
586
|
+
}
|
|
587
|
+
]
|
|
588
|
+
};
|
|
589
|
+
/** The tables of estimated calorie needs, one record for each source. */
|
|
590
|
+
const CALORIE_TABLES = [DGA_CALORIES];
|
|
591
|
+
// src/check.ts
|
|
592
|
+
/**
|
|
593
|
+
* These are the codes of the gate of the rows and the hash. Each code is an error. The gate of the sources of `@mark1russell7/guidebook` gives its own codes.
|
|
594
|
+
*
|
|
595
|
+
* - `needs-hash`: the hash of the source is not 64 lowercase hexadecimal digits.
|
|
596
|
+
* - `needs-row`: a row has no age or a wrong sex. Or its range of months is not a range of whole numbers of 0 or more.
|
|
597
|
+
* - `needs-overlap`: two rows of one sex have ages in common.
|
|
598
|
+
* - `needs-gap`: the rows of one sex do not cover each month with no gap, from the first row to an open last row.
|
|
599
|
+
* - `needs-value`: a need is not a finite number above 0 in a unit of energy for each day.
|
|
600
|
+
*/
|
|
601
|
+
const NEEDS_CODES = [
|
|
602
|
+
"needs-hash",
|
|
603
|
+
"needs-row",
|
|
604
|
+
"needs-overlap",
|
|
605
|
+
"needs-gap",
|
|
606
|
+
"needs-value"
|
|
607
|
+
];
|
|
608
|
+
/** This function tells if a need is a finite number above 0 in a unit of energy for each day. */
|
|
609
|
+
function goodNeed(need) {
|
|
610
|
+
try {
|
|
611
|
+
const value = valueIn(need, "kcal/d");
|
|
612
|
+
return Number.isFinite(value) && value > 0;
|
|
613
|
+
} catch {
|
|
614
|
+
return false;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* This function gives each finding of a table of calorie needs. A list with no error tells that the gate passes.
|
|
619
|
+
*
|
|
620
|
+
* - The findings of the source come from `checkSources` of `@mark1russell7/guidebook`, with the options of the call.
|
|
621
|
+
* - The form of the hash and the rows have the codes of `NEEDS_CODES`.
|
|
622
|
+
*
|
|
623
|
+
* The findings are in the order of the table. The function is pure.
|
|
624
|
+
*
|
|
625
|
+
* @param table - The table of calorie needs
|
|
626
|
+
* @param options - The options of the gate of the sources, for example the date of the gate
|
|
627
|
+
*/
|
|
628
|
+
function checkCalorieTable(table, options) {
|
|
629
|
+
const findings = [...checkSources([table.source], options)];
|
|
630
|
+
const add = (code, record, id, message) => {
|
|
631
|
+
findings.push({
|
|
632
|
+
level: "error",
|
|
633
|
+
code,
|
|
634
|
+
record,
|
|
635
|
+
id,
|
|
636
|
+
message
|
|
637
|
+
});
|
|
638
|
+
};
|
|
639
|
+
const { sha256 } = table.source;
|
|
640
|
+
if (sha256 !== void 0 && !/^[0-9a-f]{64}$/u.test(sha256)) add("needs-hash", "source", table.source.id, `The hash of the source "${table.source.id}" is not 64 lowercase hexadecimal digits.`);
|
|
641
|
+
const whole = (value) => value === void 0 || Number.isSafeInteger(value) && value >= 0;
|
|
642
|
+
const range = (row) => whole(row.from) && whole(row.to) && (row.to === void 0 || row.to >= row.from);
|
|
643
|
+
const idOf = (index) => `${table.id}.rows[${String(index)}]`;
|
|
644
|
+
table.rows.forEach((row, index) => {
|
|
645
|
+
const id = idOf(index);
|
|
646
|
+
if (row.age.trim() === "" || !CALORIE_SEXES.includes(row.sex)) add("needs-row", "row", id, `The row "${id}" has no age or a sex that is not a sex of the table.`);
|
|
647
|
+
if (!range(row)) add("needs-row", "row", id, `The row "${id}" has a range of months that is not a range of whole numbers of 0 or more.`);
|
|
648
|
+
if (!(row.need.levels === void 0 ? [row.need.each] : ACTIVITY_LEVELS.map((level) => row.need.levels?.[level])).every((need) => need !== void 0 && goodNeed(need))) add("needs-value", "row", id, `The row "${id}" has a need that is not a number above 0 in kcal/d.`);
|
|
649
|
+
for (const other of table.rows.slice(0, index)) {
|
|
650
|
+
if (other.sex !== row.sex) continue;
|
|
651
|
+
if (row.from <= (other.to ?? 1e400) && other.from <= (row.to ?? 1e400)) add("needs-overlap", "row", id, `The row "${id}" (${row.sex}, ${row.age}) has ages in common with the row ${other.age}.`);
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
for (const sex of CALORIE_SEXES) {
|
|
655
|
+
const rows = table.rows.map((row, index) => ({
|
|
656
|
+
row,
|
|
657
|
+
index
|
|
658
|
+
})).filter(({ row }) => row.sex === sex && range(row));
|
|
659
|
+
rows.sort((a, b) => a.row.from - b.row.from);
|
|
660
|
+
rows.slice(1).forEach(({ row, index }, at) => {
|
|
661
|
+
const before = rows[at]?.row.to;
|
|
662
|
+
if (before !== void 0 && row.from > before + 1) add("needs-gap", "row", idOf(index), `The table has no row of a ${sex} for the months ${String(before + 1)} to ${String(row.from - 1)}.`);
|
|
663
|
+
});
|
|
664
|
+
const last = rows.at(-1);
|
|
665
|
+
if (last !== void 0 && last.row.to !== void 0) add("needs-gap", "row", idOf(last.index), `The last row of a ${sex} ends at ${String(last.row.to)} months. It is not an open row.`);
|
|
666
|
+
}
|
|
667
|
+
return findings;
|
|
668
|
+
}
|
|
669
|
+
/** This function tells if a list of findings has an error. */
|
|
670
|
+
function hasNeedsError(findings) {
|
|
671
|
+
return findings.some((finding) => finding.level === "error");
|
|
672
|
+
}
|
|
673
|
+
// src/household.ts
|
|
674
|
+
/** The code of the need of the food draw: food energy, in `kcal` for each day. */
|
|
675
|
+
const FOOD_NEED = "food.energy";
|
|
676
|
+
/** The resource of the food to eat with no water and no fuel, in `kcal`. It has a direct use for the food draw. */
|
|
677
|
+
const NO_COOK = "food.no-cook";
|
|
678
|
+
/** This function gives the stores of the buy list, its resources and its gifts with no read. */
|
|
679
|
+
function buyListStores(input) {
|
|
680
|
+
const { catalog, units } = input;
|
|
681
|
+
const energy = buyListEnergy(catalog, units);
|
|
682
|
+
const unitOf = new Map(catalog.resources.map((resource) => [resource.code, resource.unit]));
|
|
683
|
+
if (unitOf.get("food.no-cook") !== "kcal") throw new RangeError(`The catalog has no resource "${NO_COOK}" in kcal.`);
|
|
684
|
+
const prefix = input.prefix ?? "buy";
|
|
685
|
+
const stores = [];
|
|
686
|
+
const noReads = [];
|
|
687
|
+
for (const [product, count] of Object.entries(units)) for (const gift of giftsOf(catalog, product, count)) if (!gift.read) noReads.push({
|
|
688
|
+
product,
|
|
689
|
+
resource: gift.resource,
|
|
690
|
+
why: gift.why
|
|
691
|
+
});
|
|
692
|
+
else if (gift.amount > 0) stores.push({
|
|
693
|
+
id: `${prefix}:${product}:${gift.resource}`,
|
|
694
|
+
resource: gift.resource,
|
|
695
|
+
amount: gift.amount
|
|
696
|
+
});
|
|
697
|
+
return {
|
|
698
|
+
stores,
|
|
699
|
+
resources: [.../* @__PURE__ */ new Set([...stores.map((store) => store.resource), NO_COOK])].map((code) => ({
|
|
700
|
+
code,
|
|
701
|
+
unit: unitOf.get(code) ?? ""
|
|
702
|
+
})),
|
|
703
|
+
parts: {
|
|
704
|
+
energy,
|
|
705
|
+
noReads,
|
|
706
|
+
storesRead: noReads.length === 0
|
|
707
|
+
}
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* This function joins a household and a buy list into a fragment of an endurance layer.
|
|
712
|
+
*
|
|
713
|
+
* - The fragment has one draw of food energy. Its rate is the need of the persons for one day, and its users are the persons.
|
|
714
|
+
* - 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.
|
|
715
|
+
* - The resources are the resources of the stores and `food.no-cook`, with the units of the catalog.
|
|
716
|
+
* - The direct use of `food.no-cook` gives 1 kcal of the need for each kcal.
|
|
717
|
+
* - The fragment has no converter. A caller adds converters for the dry food, with the water and the fuel of each run.
|
|
718
|
+
*
|
|
719
|
+
* A person with no row in the table gives no layer, because a low rate gives too many days.
|
|
720
|
+
* The option `noRow: "leave-out"` leaves those persons out of the draw. A draw of 0 kcal gives no layer.
|
|
721
|
+
* The function is pure.
|
|
722
|
+
*
|
|
723
|
+
* @param input - The persons, the activity level, the catalog and the buy list, the supply part and the level of the draw
|
|
724
|
+
* @returns The fragment or a no read of the draw, with the energy of the list and the gifts with no read
|
|
725
|
+
* @throws RangeError for the errors of `calorieNeed`, `giftsOf` and `buyListEnergy`, except a household with no person
|
|
726
|
+
* @throws RangeError when the catalog has no resource `food.no-cook` in `kcal`
|
|
727
|
+
*/
|
|
728
|
+
function householdFragment(input) {
|
|
729
|
+
const { stores, resources, parts } = buyListStores(input);
|
|
730
|
+
if (input.persons.every((person) => person.count === 0)) return {
|
|
731
|
+
...parts,
|
|
732
|
+
layer: void 0,
|
|
733
|
+
need: void 0,
|
|
734
|
+
needRead: false,
|
|
735
|
+
noRow: [],
|
|
736
|
+
why: "The household has no person, so the draw is 0 kcal."
|
|
737
|
+
};
|
|
738
|
+
const need = calorieNeed(input.table ?? DGA_CALORIES, input.persons, input.activity, 1);
|
|
739
|
+
const noRow = [];
|
|
740
|
+
let users = 0;
|
|
741
|
+
for (const line of need.lines) if (line.read) users += line.person.count;
|
|
742
|
+
else if (line.person.count > 0) noRow.push(line.person);
|
|
743
|
+
if (noRow.length > 0 && input.noRow !== "leave-out") return {
|
|
744
|
+
...parts,
|
|
745
|
+
layer: void 0,
|
|
746
|
+
need,
|
|
747
|
+
needRead: false,
|
|
748
|
+
noRow,
|
|
749
|
+
why: `The table has no row for ${String(noRow.length)} person records. The option noRow: "leave-out" leaves them out of the draw.`
|
|
750
|
+
};
|
|
751
|
+
if (users === 0 || need.need.value <= 0) return {
|
|
752
|
+
...parts,
|
|
753
|
+
layer: void 0,
|
|
754
|
+
need,
|
|
755
|
+
needRead: false,
|
|
756
|
+
noRow,
|
|
757
|
+
why: "No person of the draw has a row in the table, so the draw is 0 kcal."
|
|
758
|
+
};
|
|
759
|
+
const draw = {
|
|
760
|
+
need: FOOD_NEED,
|
|
761
|
+
level: input.level,
|
|
762
|
+
perDay: need.need.value,
|
|
763
|
+
users,
|
|
764
|
+
supply: input.supply
|
|
765
|
+
};
|
|
766
|
+
return {
|
|
767
|
+
...parts,
|
|
768
|
+
layer: {
|
|
769
|
+
schema: "endurance/1",
|
|
770
|
+
levels: [...input.levels ?? []],
|
|
771
|
+
resources,
|
|
772
|
+
stores,
|
|
773
|
+
draws: [draw],
|
|
774
|
+
direct: [{
|
|
775
|
+
resource: NO_COOK,
|
|
776
|
+
need: FOOD_NEED,
|
|
777
|
+
amount: 1
|
|
778
|
+
}]
|
|
779
|
+
},
|
|
780
|
+
need,
|
|
781
|
+
needRead: true,
|
|
782
|
+
leftOut: noRow
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
export { ACTIVITY_LEVELS, CALORIE_SEXES, CALORIE_TABLES, DGA_CALORIES, DGA_SOURCE, FOOD_NEED, NEEDS_CODES, NO_COOK, calorieNeed, calorieRow, checkCalorieTable, hasNeedsError, householdFragment, isActivityLevel };
|
|
786
|
+
|
|
787
|
+
//# sourceMappingURL=index.js.map
|