@mark1russell7/stack-needs 0.5.0 → 0.7.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/README.md +43 -7
- package/build/index.d.ts +114 -16
- package/build/index.d.ts.map +1 -1
- package/build/index.js +409 -30
- package/build/index.js.map +1 -1
- package/package.json +3 -3
package/build/index.js
CHANGED
|
@@ -10,6 +10,18 @@ const ACTIVITY_LEVELS = [
|
|
|
10
10
|
];
|
|
11
11
|
/** The sexes of the table. */
|
|
12
12
|
const CALORIE_SEXES = ["female", "male"];
|
|
13
|
+
/**
|
|
14
|
+
* The ways to feed an infant.
|
|
15
|
+
*
|
|
16
|
+
* - `breast-milk`: only the milk of a person of the household.
|
|
17
|
+
* - `formula`: infant formula.
|
|
18
|
+
* - `food`: the food of the household, also with milk.
|
|
19
|
+
*/
|
|
20
|
+
const INFANT_FEEDINGS = [
|
|
21
|
+
"breast-milk",
|
|
22
|
+
"formula",
|
|
23
|
+
"food"
|
|
24
|
+
];
|
|
13
25
|
/** The unit of each food energy. */
|
|
14
26
|
const ENERGY_UNIT = "kcal";
|
|
15
27
|
/** The unit of each daily need. */
|
|
@@ -29,6 +41,27 @@ function calorieRow(table, sex, ageMonths) {
|
|
|
29
41
|
return table.rows.find((row) => row.sex === sex && ageMonths >= row.from && (row.to === void 0 || ageMonths <= row.to));
|
|
30
42
|
}
|
|
31
43
|
/**
|
|
44
|
+
* This function gives the first table with a row for a sex and an age in months, and its row.
|
|
45
|
+
* It gives `undefined` when no table has a row.
|
|
46
|
+
*
|
|
47
|
+
* @param tables - One table, or tables in order
|
|
48
|
+
* @param sex - The sex
|
|
49
|
+
* @param ageMonths - The age, in whole months
|
|
50
|
+
*/
|
|
51
|
+
function calorieRowOf(tables, sex, ageMonths) {
|
|
52
|
+
for (const table of tableList(tables)) {
|
|
53
|
+
const row = calorieRow(table, sex, ageMonths);
|
|
54
|
+
if (row !== void 0) return {
|
|
55
|
+
table,
|
|
56
|
+
row
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** This function gives the tables of the input as a list. */
|
|
61
|
+
function tableList(tables) {
|
|
62
|
+
return "rows" in tables ? [tables] : tables;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
32
65
|
* This function gives the change of a table for a sex and a stage, or `undefined` when the table has no such change.
|
|
33
66
|
*
|
|
34
67
|
* @param table - The table of calorie needs
|
|
@@ -40,29 +73,34 @@ function calorieChange(table, sex, stage) {
|
|
|
40
73
|
}
|
|
41
74
|
/**
|
|
42
75
|
* This function gives the energy need of the persons of a household for a count of days, in `kcal`.
|
|
43
|
-
* It adds the need of each person at the activity level of that person. The table
|
|
76
|
+
* It adds the need of each person at the activity level of that person. The first table with a row for the age of a person gives the row.
|
|
44
77
|
*
|
|
45
|
-
* - An age with no row in the
|
|
78
|
+
* - An age with no row in the tables gives a line with no read, for example 6 months in `DGA_CALORIES` alone. Then the total is a lower limit.
|
|
79
|
+
* - Each line with a read has the `id` of the table of its row.
|
|
46
80
|
* - The table of the toddlers gives one need for each age and sex. Thus the level does not change the need of a toddler.
|
|
47
|
-
* - A record with a stage adds the change of the stage to the need of its row, for example 340 kcal/d in the second trimester.
|
|
81
|
+
* - A record with a stage adds the change of the stage to the need of its row, for example 340 kcal/d in the second trimester. The change comes from the table of the row.
|
|
48
82
|
* - One stage holds for each day of the call. For a period that crosses stages, the planner selects the stage, for example the stage with the largest change.
|
|
49
83
|
*
|
|
50
84
|
* 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.
|
|
51
85
|
* In that case, work can make the level higher, for example the carry of water. The function is pure.
|
|
52
86
|
*
|
|
53
|
-
* @param
|
|
87
|
+
* @param tables - The table of calorie needs, or tables in order
|
|
54
88
|
* @param persons - The person records of the household: a sex, an age in months, a count and, as an option, a level
|
|
55
89
|
* @param level - The activity level of each record with no level of its own
|
|
56
90
|
* @param days - The count of days, a finite number of 0 or more
|
|
57
91
|
* @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
|
|
58
92
|
* @throws RangeError when a level is not a level of the table, or when a sex is not a sex of the table
|
|
59
|
-
* @throws RangeError when the count of days is not a finite number of 0 or more
|
|
60
|
-
* @throws RangeError when
|
|
93
|
+
* @throws RangeError when the count of days is not a finite number of 0 or more, or when the list of tables is empty
|
|
94
|
+
* @throws RangeError when no table has the change for the stage and the sex of a record
|
|
95
|
+
* @throws RangeError when the table of the row of a record has no change for the stage and the sex of the record
|
|
61
96
|
* @throws RangeError when the daily need of a record, with the change of its stage, is not above 0
|
|
62
97
|
*/
|
|
63
|
-
function calorieNeed(
|
|
98
|
+
function calorieNeed(tables, persons, level, days) {
|
|
64
99
|
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.`);
|
|
65
100
|
if (!isActivityLevel(level)) throw new RangeError(`The activity level ${String(level)} is not a level of the table.`);
|
|
101
|
+
const list = tableList(tables);
|
|
102
|
+
if (list.length === 0) throw new RangeError("The list of tables is empty.");
|
|
103
|
+
const ids = list.map((table) => table.id).join(", ");
|
|
66
104
|
let count = 0;
|
|
67
105
|
const lines = persons.map((person) => {
|
|
68
106
|
const name = `${String(person.sex)} of ${String(person.ageMonths)} months`;
|
|
@@ -71,21 +109,25 @@ function calorieNeed(table, persons, level, days) {
|
|
|
71
109
|
if (!CALORIE_SEXES.includes(person.sex)) throw new RangeError(`The sex ${String(person.sex)} is not a sex of the table.`);
|
|
72
110
|
const own = person.level ?? level;
|
|
73
111
|
if (!isActivityLevel(own)) throw new RangeError(`The activity level ${String(own)} of the persons ${name} is not a level of the table.`);
|
|
74
|
-
const
|
|
75
|
-
if (
|
|
112
|
+
const { stage } = person;
|
|
113
|
+
if (stage !== void 0 && list.every((table) => calorieChange(table, person.sex, stage) === void 0)) throw new RangeError(`The tables ${ids} have no change for the stage ${String(stage)} of the persons ${name}.`);
|
|
76
114
|
count += person.count;
|
|
77
|
-
const
|
|
78
|
-
if (
|
|
115
|
+
const found = calorieRowOf(list, person.sex, person.ageMonths);
|
|
116
|
+
if (found === void 0) return {
|
|
79
117
|
person,
|
|
80
118
|
read: false,
|
|
81
|
-
why: `The
|
|
119
|
+
why: `The tables ${ids} have no row for a ${name}.`
|
|
82
120
|
};
|
|
121
|
+
const { table, row } = found;
|
|
122
|
+
const change = stage === void 0 ? void 0 : calorieChange(table, person.sex, stage);
|
|
123
|
+
if (stage !== void 0 && change === void 0) throw new RangeError(`The table ${table.id} of the row of the persons ${name} has no change for the stage ${String(stage)}.`);
|
|
83
124
|
const daily = valueIn(row.need.levels === void 0 ? row.need.each : row.need.levels[own], DAILY_UNIT) + (change === void 0 ? 0 : valueIn(change.change, DAILY_UNIT));
|
|
84
125
|
if (!(daily > 0)) throw new RangeError(`The daily need of the persons ${name} at the stage ${String(person.stage)} is ${String(daily)} kcal/d. It must be above 0.`);
|
|
85
126
|
const line = {
|
|
86
127
|
person,
|
|
87
128
|
read: true,
|
|
88
129
|
level: own,
|
|
130
|
+
table: table.id,
|
|
89
131
|
row: row.age,
|
|
90
132
|
need: quantity(person.count * daily * days, ENERGY_UNIT)
|
|
91
133
|
};
|
|
@@ -133,6 +175,10 @@ const change = (stage, title, value) => ({
|
|
|
133
175
|
change: day(value),
|
|
134
176
|
condition: HEALTHY
|
|
135
177
|
});
|
|
178
|
+
const milk = (stage, title, value) => ({
|
|
179
|
+
...change(stage, title, value),
|
|
180
|
+
milk: true
|
|
181
|
+
});
|
|
136
182
|
/** The table of estimated calorie needs of the Dietary Guidelines for Americans, 2020-2025: 68 rows, 34 for each sex, and 5 changes of pregnancy and lactation. */
|
|
137
183
|
const DGA_CALORIES = {
|
|
138
184
|
id: "dga.2020-2025:calorie-needs",
|
|
@@ -143,8 +189,8 @@ const DGA_CALORIES = {
|
|
|
143
189
|
change("pregnancy-1", "Pregnancy: 1st trimester", 0),
|
|
144
190
|
change("pregnancy-2", "Pregnancy: 2nd trimester", 340),
|
|
145
191
|
change("pregnancy-3", "Pregnancy: 3rd trimester", 452),
|
|
146
|
-
|
|
147
|
-
|
|
192
|
+
milk("lactation-1", "Lactation: 1st 6 months", 330),
|
|
193
|
+
milk("lactation-2", "Lactation: 2nd 6 months", 400)
|
|
148
194
|
],
|
|
149
195
|
rows: [
|
|
150
196
|
{
|
|
@@ -623,26 +669,227 @@ const DGA_CALORIES = {
|
|
|
623
669
|
}
|
|
624
670
|
]
|
|
625
671
|
};
|
|
672
|
+
/** The source of the infant table: the PDF of the report Dietary Reference Intakes for Energy (2023) of the National Academies. */
|
|
673
|
+
const NASEM_ENERGY_SOURCE = {
|
|
674
|
+
id: "nasem-dri-energy-2023",
|
|
675
|
+
title: "Dietary Reference Intakes for Energy (2023)",
|
|
676
|
+
url: "https://nap.nationalacademies.org/26818",
|
|
677
|
+
license: "all-rights-reserved",
|
|
678
|
+
checked: "2026-11-24",
|
|
679
|
+
by: "National Academies of Sciences, Engineering, and Medicine",
|
|
680
|
+
terms: "https://nap.nationalacademies.org/26818",
|
|
681
|
+
note: "DOI 10.17226/26818. The terms are on the first page of the PDF. The table cites its values as facts and copies no text.",
|
|
682
|
+
sha256: "e6a7d9c9104053d4925c621b659b06de4c8dd6c64ed4534e6f5bc5cf0dd41b27"
|
|
683
|
+
};
|
|
684
|
+
/**
|
|
685
|
+
* The table of the Estimated Energy Requirements of the ages of 0 to 35 months, from the report Dietary Reference Intakes
|
|
686
|
+
* for Energy (2023): 24 rows, 12 for each sex. Each row gives one need for each age group and sex. The last row ends at
|
|
687
|
+
* 35 months, so the table is for a chain of tables, for example `CALORIE_CHAIN`.
|
|
688
|
+
*/
|
|
689
|
+
const NASEM_INFANT_CALORIES = {
|
|
690
|
+
id: "nasem.2023:eer-0-35-months",
|
|
691
|
+
title: "Dietary Reference Intakes for Energy (2023): Estimated Energy Requirements of the ages of 0 to 35 months",
|
|
692
|
+
source: NASEM_ENERGY_SOURCE,
|
|
693
|
+
locator: "Chapter 7: Table 7-5 (boys of 0 to 35 months, page 160) and Table 7-6 (girls of 0 to 35 months, page 161)",
|
|
694
|
+
rows: [
|
|
695
|
+
{
|
|
696
|
+
sex: "female",
|
|
697
|
+
age: "Birth-2",
|
|
698
|
+
from: 0,
|
|
699
|
+
to: 2,
|
|
700
|
+
need: each(531)
|
|
701
|
+
},
|
|
702
|
+
{
|
|
703
|
+
sex: "female",
|
|
704
|
+
age: "3-5",
|
|
705
|
+
from: 3,
|
|
706
|
+
to: 5,
|
|
707
|
+
need: each(546)
|
|
708
|
+
},
|
|
709
|
+
{
|
|
710
|
+
sex: "female",
|
|
711
|
+
age: "6-8",
|
|
712
|
+
from: 6,
|
|
713
|
+
to: 8,
|
|
714
|
+
need: each(604)
|
|
715
|
+
},
|
|
716
|
+
{
|
|
717
|
+
sex: "female",
|
|
718
|
+
age: "9-11",
|
|
719
|
+
from: 9,
|
|
720
|
+
to: 11,
|
|
721
|
+
need: each(689)
|
|
722
|
+
},
|
|
723
|
+
{
|
|
724
|
+
sex: "female",
|
|
725
|
+
age: "12-14",
|
|
726
|
+
from: 12,
|
|
727
|
+
to: 14,
|
|
728
|
+
need: each(780)
|
|
729
|
+
},
|
|
730
|
+
{
|
|
731
|
+
sex: "female",
|
|
732
|
+
age: "15-17",
|
|
733
|
+
from: 15,
|
|
734
|
+
to: 17,
|
|
735
|
+
need: each(817)
|
|
736
|
+
},
|
|
737
|
+
{
|
|
738
|
+
sex: "female",
|
|
739
|
+
age: "18-20",
|
|
740
|
+
from: 18,
|
|
741
|
+
to: 20,
|
|
742
|
+
need: each(881)
|
|
743
|
+
},
|
|
744
|
+
{
|
|
745
|
+
sex: "female",
|
|
746
|
+
age: "21-23",
|
|
747
|
+
from: 21,
|
|
748
|
+
to: 23,
|
|
749
|
+
need: each(956)
|
|
750
|
+
},
|
|
751
|
+
{
|
|
752
|
+
sex: "female",
|
|
753
|
+
age: "24-26",
|
|
754
|
+
from: 24,
|
|
755
|
+
to: 26,
|
|
756
|
+
need: each(1011)
|
|
757
|
+
},
|
|
758
|
+
{
|
|
759
|
+
sex: "female",
|
|
760
|
+
age: "27-29",
|
|
761
|
+
from: 27,
|
|
762
|
+
to: 29,
|
|
763
|
+
need: each(1051)
|
|
764
|
+
},
|
|
765
|
+
{
|
|
766
|
+
sex: "female",
|
|
767
|
+
age: "30-32",
|
|
768
|
+
from: 30,
|
|
769
|
+
to: 32,
|
|
770
|
+
need: each(1122)
|
|
771
|
+
},
|
|
772
|
+
{
|
|
773
|
+
sex: "female",
|
|
774
|
+
age: "33-35",
|
|
775
|
+
from: 33,
|
|
776
|
+
to: 35,
|
|
777
|
+
need: each(1192)
|
|
778
|
+
},
|
|
779
|
+
{
|
|
780
|
+
sex: "male",
|
|
781
|
+
age: "Birth-2",
|
|
782
|
+
from: 0,
|
|
783
|
+
to: 2,
|
|
784
|
+
need: each(566)
|
|
785
|
+
},
|
|
786
|
+
{
|
|
787
|
+
sex: "male",
|
|
788
|
+
age: "3-5",
|
|
789
|
+
from: 3,
|
|
790
|
+
to: 5,
|
|
791
|
+
need: each(589)
|
|
792
|
+
},
|
|
793
|
+
{
|
|
794
|
+
sex: "male",
|
|
795
|
+
age: "6-8",
|
|
796
|
+
from: 6,
|
|
797
|
+
to: 8,
|
|
798
|
+
need: each(675)
|
|
799
|
+
},
|
|
800
|
+
{
|
|
801
|
+
sex: "male",
|
|
802
|
+
age: "9-11",
|
|
803
|
+
from: 9,
|
|
804
|
+
to: 11,
|
|
805
|
+
need: each(745)
|
|
806
|
+
},
|
|
807
|
+
{
|
|
808
|
+
sex: "male",
|
|
809
|
+
age: "12-14",
|
|
810
|
+
from: 12,
|
|
811
|
+
to: 14,
|
|
812
|
+
need: each(821)
|
|
813
|
+
},
|
|
814
|
+
{
|
|
815
|
+
sex: "male",
|
|
816
|
+
age: "15-17",
|
|
817
|
+
from: 15,
|
|
818
|
+
to: 17,
|
|
819
|
+
need: each(881)
|
|
820
|
+
},
|
|
821
|
+
{
|
|
822
|
+
sex: "male",
|
|
823
|
+
age: "18-20",
|
|
824
|
+
from: 18,
|
|
825
|
+
to: 20,
|
|
826
|
+
need: each(952)
|
|
827
|
+
},
|
|
828
|
+
{
|
|
829
|
+
sex: "male",
|
|
830
|
+
age: "21-23",
|
|
831
|
+
from: 21,
|
|
832
|
+
to: 23,
|
|
833
|
+
need: each(1027)
|
|
834
|
+
},
|
|
835
|
+
{
|
|
836
|
+
sex: "male",
|
|
837
|
+
age: "24-26",
|
|
838
|
+
from: 24,
|
|
839
|
+
to: 26,
|
|
840
|
+
need: each(1058)
|
|
841
|
+
},
|
|
842
|
+
{
|
|
843
|
+
sex: "male",
|
|
844
|
+
age: "27-29",
|
|
845
|
+
from: 27,
|
|
846
|
+
to: 29,
|
|
847
|
+
need: each(1136)
|
|
848
|
+
},
|
|
849
|
+
{
|
|
850
|
+
sex: "male",
|
|
851
|
+
age: "30-32",
|
|
852
|
+
from: 30,
|
|
853
|
+
to: 32,
|
|
854
|
+
need: each(1183)
|
|
855
|
+
},
|
|
856
|
+
{
|
|
857
|
+
sex: "male",
|
|
858
|
+
age: "33-35",
|
|
859
|
+
from: 33,
|
|
860
|
+
to: 35,
|
|
861
|
+
need: each(1213)
|
|
862
|
+
}
|
|
863
|
+
]
|
|
864
|
+
};
|
|
626
865
|
/** The tables of estimated calorie needs, one record for each source. */
|
|
627
|
-
const CALORIE_TABLES = [DGA_CALORIES];
|
|
866
|
+
const CALORIE_TABLES = [DGA_CALORIES, NASEM_INFANT_CALORIES];
|
|
867
|
+
/**
|
|
868
|
+
* The default tables of `householdFragment`, in order: the guidelines first, then the infant table. The guidelines give
|
|
869
|
+
* each age from 12 months. The infant table gives the ages below 12 months. Together they give a row for each age.
|
|
870
|
+
*/
|
|
871
|
+
const CALORIE_CHAIN = [DGA_CALORIES, NASEM_INFANT_CALORIES];
|
|
628
872
|
// src/check.ts
|
|
629
873
|
/**
|
|
630
874
|
* These are the codes of the gate of the rows. Each code is an error. The gate of the sources of `@mark1russell7/guidebook` gives its own codes.
|
|
631
875
|
*
|
|
632
876
|
* - `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.
|
|
633
877
|
* - `needs-overlap`: two rows of one sex have ages in common.
|
|
634
|
-
* - `needs-gap`: the rows of one sex
|
|
878
|
+
* - `needs-gap`: the rows of one sex in a table have a gap between two rows. Or the rows of one sex in a chain of tables
|
|
879
|
+
* do not cover each month from 0 to an open last row.
|
|
635
880
|
* - `needs-value`: a need is not a finite number above 0 in a unit of energy for each day.
|
|
636
881
|
* - `needs-change`: a change has no stage, no title or a wrong sex, or a stage with a space at an end. Or a different
|
|
637
882
|
* change has its stage and sex. Or its value is not a finite number in a unit of energy for each day. Or its sum with
|
|
638
883
|
* the smallest need of its sex is not above 0.
|
|
884
|
+
* - `needs-id`: two tables of a chain have the same `id`. Each line of a need names its table by the `id`.
|
|
639
885
|
*/
|
|
640
886
|
const NEEDS_CODES = [
|
|
641
887
|
"needs-row",
|
|
642
888
|
"needs-overlap",
|
|
643
889
|
"needs-gap",
|
|
644
890
|
"needs-value",
|
|
645
|
-
"needs-change"
|
|
891
|
+
"needs-change",
|
|
892
|
+
"needs-id"
|
|
646
893
|
];
|
|
647
894
|
/** This function tells if a need is a finite number in a unit of energy for each day, and above 0 when `positive` is true. */
|
|
648
895
|
function goodNeed(need, positive = true) {
|
|
@@ -711,8 +958,6 @@ function checkCalorieTable(table, options) {
|
|
|
711
958
|
const before = rows[at]?.row.to;
|
|
712
959
|
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)}.`);
|
|
713
960
|
});
|
|
714
|
-
const last = rows.at(-1);
|
|
715
|
-
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.`);
|
|
716
961
|
}
|
|
717
962
|
const smallest = (sex) => Math.min(...table.rows.filter((row) => row.sex === sex).flatMap((row) => row.need.levels === void 0 ? [row.need.each] : ACTIVITY_LEVELS.map((level) => row.need.levels?.[level])).filter((need) => need !== void 0 && goodNeed(need)).map((need) => valueIn(need, "kcal/d")));
|
|
718
963
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -728,6 +973,53 @@ function checkCalorieTable(table, options) {
|
|
|
728
973
|
});
|
|
729
974
|
return findings;
|
|
730
975
|
}
|
|
976
|
+
/**
|
|
977
|
+
* This function gives each finding of a chain of tables. First come the findings of `checkCalorieTable` for each table,
|
|
978
|
+
* in the order of the chain. Then come the findings of the cover. For each sex, the rows of all the tables cover each month from 0 to an
|
|
979
|
+
* open last row (`needs-gap`, record `chain`). A row of a later table can overlap a row of an earlier table: the earlier
|
|
980
|
+
* table gives the need. Two tables of the chain with the same `id` give `needs-id`.
|
|
981
|
+
*
|
|
982
|
+
* The limit of the first match: when an earlier table loses a row at its start, a later table can give those months.
|
|
983
|
+
* Then the gate gives no finding. A caller that needs each month from one table tests the `id` of the table of each month.
|
|
984
|
+
*
|
|
985
|
+
* A list with no error tells that the gate passes. The function is pure.
|
|
986
|
+
*
|
|
987
|
+
* @param tables - One table, or tables in order
|
|
988
|
+
* @param options - The options of the gate of the sources, for example the date of the gate
|
|
989
|
+
*/
|
|
990
|
+
function checkCalorieChain(tables, options) {
|
|
991
|
+
const list = "rows" in tables ? [tables] : tables;
|
|
992
|
+
const findings = list.flatMap((table) => checkCalorieTable(table, options));
|
|
993
|
+
const id = list.map((table) => table.id).join(" + ");
|
|
994
|
+
const add = (message, code = "needs-gap") => {
|
|
995
|
+
findings.push({
|
|
996
|
+
level: "error",
|
|
997
|
+
code,
|
|
998
|
+
record: "chain",
|
|
999
|
+
id,
|
|
1000
|
+
message
|
|
1001
|
+
});
|
|
1002
|
+
};
|
|
1003
|
+
if (list.length === 0) add("The chain has no table.");
|
|
1004
|
+
list.forEach((table, index) => {
|
|
1005
|
+
if (list.findIndex((other) => other.id === table.id) < index) add(`The chain has the table ${table.id} two times or more.`, "needs-id");
|
|
1006
|
+
});
|
|
1007
|
+
for (const sex of CALORIE_SEXES) {
|
|
1008
|
+
const rows = list.flatMap((table) => table.rows).filter((row) => row.sex === sex && Number.isSafeInteger(row.from) && row.from >= 0);
|
|
1009
|
+
if (rows.length === 0) {
|
|
1010
|
+
add(`The tables have no row of a ${sex}.`);
|
|
1011
|
+
continue;
|
|
1012
|
+
}
|
|
1013
|
+
rows.sort((a, b) => a.from - b.from);
|
|
1014
|
+
let next = 0;
|
|
1015
|
+
for (const row of rows) {
|
|
1016
|
+
if (row.from > next) add(`The tables have no row of a ${sex} for the months ${String(next)} to ${String(row.from - 1)}.`);
|
|
1017
|
+
next = Math.max(next, row.to === void 0 ? 1e400 : row.to + 1);
|
|
1018
|
+
}
|
|
1019
|
+
if (next !== 1e400) add(`The rows of a ${sex} end at ${String(next - 1)} months. The chain has no open last row.`);
|
|
1020
|
+
}
|
|
1021
|
+
return findings;
|
|
1022
|
+
}
|
|
731
1023
|
/** This function tells if a list of findings has an error. */
|
|
732
1024
|
function hasNeedsError(findings) {
|
|
733
1025
|
return findings.some((finding) => finding.level === "error");
|
|
@@ -844,6 +1136,13 @@ function checkJoins(catalog, uses, supplies, references) {
|
|
|
844
1136
|
return findings;
|
|
845
1137
|
}
|
|
846
1138
|
// src/household.ts
|
|
1139
|
+
/** The age in months below which a person record must have a `feeding`: 12, the first month of the guidelines. */
|
|
1140
|
+
const INFANT_MONTHS = 12;
|
|
1141
|
+
/**
|
|
1142
|
+
* The age in months from which an infant cannot have `feeding: "breast-milk"`: 6.
|
|
1143
|
+
* 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
|
+
*/
|
|
1145
|
+
const BREAST_MILK_MONTHS = 6;
|
|
847
1146
|
/** This function gives the stores of the buy list, its resources and its gifts with no read. */
|
|
848
1147
|
function buyListStores(input, foodUses) {
|
|
849
1148
|
const { catalog, units } = input;
|
|
@@ -886,51 +1185,105 @@ function buyListStores(input, foodUses) {
|
|
|
886
1185
|
* - The supply part of the draw is `supply`. Its default is the part of `NEED_SUPPLIES` for `food.energy`.
|
|
887
1186
|
* - The fragment has no converter. A caller adds converters for the dry food, with the water and the fuel of each run.
|
|
888
1187
|
*
|
|
889
|
-
* A person with no row in the
|
|
1188
|
+
* 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.
|
|
890
1189
|
* The option `noRow: "leave-out"` leaves those persons out of the draw. A draw of 0 kcal gives no layer.
|
|
1190
|
+
*
|
|
1191
|
+
* Each person record below `INFANT_MONTHS` must have a `feeding`. It has no default.
|
|
1192
|
+
*
|
|
1193
|
+
* - `food`: the need of the infant is in the draw of food energy.
|
|
1194
|
+
* - `breast-milk`: the infant has a line, and no part of the draw. It is only for an infant below `BREAST_MILK_MONTHS`.
|
|
1195
|
+
* 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
|
+
* When the infants are more, the function gives no layer: the change of a stage is the milk of one infant.
|
|
1197
|
+
* - `formula`: the fragment has no draw of formula, so the infant gives no layer, as a person with no row. The option `leave-out` also applies.
|
|
1198
|
+
* It is only for an infant below `INFANT_MONTHS`.
|
|
1199
|
+
*
|
|
891
1200
|
* The function is pure.
|
|
892
1201
|
*
|
|
893
1202
|
* @param input - The persons, the activity level, the catalog and the buy list, the supply part and the level of the draw
|
|
894
1203
|
* @returns The fragment or a no read of the draw, with the energy of the list and the gifts with no read
|
|
895
1204
|
* @throws RangeError for the errors of `calorieNeed`, `giftsOf` and `buyListEnergy`, except a household with no person
|
|
896
1205
|
* @throws RangeError when the catalog has no resource in `kcal` for a food use
|
|
1206
|
+
* @throws RangeError when a record below `INFANT_MONTHS` has no `feeding`, or when a `feeding` is not a way of `INFANT_FEEDINGS`
|
|
1207
|
+
* @throws RangeError for `breast-milk` from `BREAST_MILK_MONTHS`, and for `formula` from `INFANT_MONTHS`
|
|
1208
|
+
* @throws RangeError when an infant drinks only breast milk and no person of the household has a stage of lactation
|
|
897
1209
|
*/
|
|
898
1210
|
function householdFragment(input) {
|
|
899
1211
|
const foodUses = (input.uses ?? DIRECT_USES).filter((use) => use.need === FOOD_NEED);
|
|
900
1212
|
const { stores, resources, parts } = buyListStores(input, foodUses);
|
|
1213
|
+
checkFeedings(input.persons);
|
|
901
1214
|
if (input.persons.every((person) => person.count === 0)) return {
|
|
902
1215
|
...parts,
|
|
903
1216
|
layer: void 0,
|
|
904
1217
|
need: void 0,
|
|
905
1218
|
needRead: false,
|
|
906
1219
|
noRow: [],
|
|
1220
|
+
formula: [],
|
|
1221
|
+
breastMilk: [],
|
|
907
1222
|
why: "The household has no person, so the draw is 0 kcal."
|
|
908
1223
|
};
|
|
909
|
-
const
|
|
1224
|
+
const tables = input.table ?? CALORIE_CHAIN;
|
|
1225
|
+
const need = calorieNeed(tables, input.persons, input.activity, 1);
|
|
910
1226
|
const noRow = [];
|
|
1227
|
+
const formula = [];
|
|
1228
|
+
const breastMilk = [];
|
|
911
1229
|
let users = 0;
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
1230
|
+
let perDay = 0;
|
|
1231
|
+
let mothers = 0;
|
|
1232
|
+
let nursed = 0;
|
|
1233
|
+
for (const line of need.lines) {
|
|
1234
|
+
const { person } = line;
|
|
1235
|
+
if (person.count === 0) continue;
|
|
1236
|
+
if (person.feeding === "breast-milk") {
|
|
1237
|
+
breastMilk.push(person);
|
|
1238
|
+
nursed += person.count;
|
|
1239
|
+
} else if (person.feeding === "formula") formula.push(person);
|
|
1240
|
+
else if (!line.read) noRow.push(person);
|
|
1241
|
+
else {
|
|
1242
|
+
users += person.count;
|
|
1243
|
+
perDay += line.need.value;
|
|
1244
|
+
if (makesMilk(tables, person)) mothers += person.count;
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
if (breastMilk.length > 0 && !input.persons.some((person) => person.count > 0 && makesMilk(tables, person))) throw new RangeError(`The infants ${breastMilk.map(nameOf).join(", ")} drink only breast milk, but no person of the household has a stage of lactation. The milk comes from the food of the mother, so give the mother a stage of lactation, for example lactation-1.`);
|
|
1248
|
+
const reasons = [
|
|
1249
|
+
...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
|
+
...noRow.length > 0 ? [`The table has no row for ${String(noRow.length)} person records.`] : [],
|
|
1251
|
+
...formula.length > 0 ? [`${String(formula.length)} infant records are on formula, and the fragment has no draw of formula.`] : []
|
|
1252
|
+
];
|
|
1253
|
+
if (nursed > mothers) return {
|
|
915
1254
|
...parts,
|
|
916
1255
|
layer: void 0,
|
|
917
1256
|
need,
|
|
918
1257
|
needRead: false,
|
|
919
1258
|
noRow,
|
|
920
|
-
|
|
1259
|
+
formula,
|
|
1260
|
+
breastMilk,
|
|
1261
|
+
why: reasons.join(" ")
|
|
921
1262
|
};
|
|
922
|
-
if (
|
|
1263
|
+
if (reasons.length > 0 && input.noRow !== "leave-out") return {
|
|
923
1264
|
...parts,
|
|
924
1265
|
layer: void 0,
|
|
925
1266
|
need,
|
|
926
1267
|
needRead: false,
|
|
927
1268
|
noRow,
|
|
928
|
-
|
|
1269
|
+
formula,
|
|
1270
|
+
breastMilk,
|
|
1271
|
+
why: `${reasons.join(" ")} The option noRow: "leave-out" leaves them out of the draw.`
|
|
1272
|
+
};
|
|
1273
|
+
if (users === 0 || perDay <= 0) return {
|
|
1274
|
+
...parts,
|
|
1275
|
+
layer: void 0,
|
|
1276
|
+
need,
|
|
1277
|
+
needRead: false,
|
|
1278
|
+
noRow,
|
|
1279
|
+
formula,
|
|
1280
|
+
breastMilk,
|
|
1281
|
+
why: "No person of the draw has a need of food in the draw, so the draw is 0 kcal."
|
|
929
1282
|
};
|
|
930
1283
|
const draw = {
|
|
931
1284
|
need: FOOD_NEED,
|
|
932
1285
|
level: input.level,
|
|
933
|
-
perDay
|
|
1286
|
+
perDay,
|
|
934
1287
|
users,
|
|
935
1288
|
supply: input.supply ?? NEED_SUPPLIES["food.energy"]
|
|
936
1289
|
};
|
|
@@ -946,9 +1299,35 @@ function householdFragment(input) {
|
|
|
946
1299
|
},
|
|
947
1300
|
need,
|
|
948
1301
|
needRead: true,
|
|
949
|
-
leftOut: noRow
|
|
1302
|
+
leftOut: [...noRow, ...formula],
|
|
1303
|
+
formula,
|
|
1304
|
+
breastMilk
|
|
950
1305
|
};
|
|
951
1306
|
}
|
|
952
|
-
|
|
1307
|
+
/** This function gives the name of a person record in a message. */
|
|
1308
|
+
function nameOf(person) {
|
|
1309
|
+
return `${String(person.sex)} of ${String(person.ageMonths)} months`;
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* This function examines the feeding of each record. A record below `INFANT_MONTHS` must have one.
|
|
1313
|
+
* Each feeding is a way of `INFANT_FEEDINGS`, and each way holds at the age of the record.
|
|
1314
|
+
*/
|
|
1315
|
+
function checkFeedings(persons) {
|
|
1316
|
+
for (const person of persons) {
|
|
1317
|
+
const { feeding } = person;
|
|
1318
|
+
if (feeding === void 0) {
|
|
1319
|
+
if (person.count > 0 && person.ageMonths < 12) throw new RangeError(`The persons ${nameOf(person)} are below ${String(12)} months and have no feeding. Give feeding: ${INFANT_FEEDINGS.join(", ")}.`);
|
|
1320
|
+
} else if (!INFANT_FEEDINGS.includes(feeding)) throw new RangeError(`The feeding ${String(feeding)} of the persons ${nameOf(person)} is not a way of ${INFANT_FEEDINGS.join(", ")}.`);
|
|
1321
|
+
else if (feeding === "breast-milk" && !(person.ageMonths < 6)) throw new RangeError(`The persons ${nameOf(person)} have feeding: breast-milk, which is only for an infant below ${String(6)} months. From that age, the need is more than the change of a stage of lactation. Give feeding: food.`);
|
|
1322
|
+
else if (feeding === "formula" && !(person.ageMonths < 12)) throw new RangeError(`The persons ${nameOf(person)} have feeding: formula, which is only for an infant below ${String(12)} months. Give feeding: food.`);
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
/** This function tells if the stage of a person record is a stage that makes milk, in one of the tables. */
|
|
1326
|
+
function makesMilk(tables, person) {
|
|
1327
|
+
const { stage } = person;
|
|
1328
|
+
if (stage === void 0) return false;
|
|
1329
|
+
return ("rows" in tables ? [tables] : tables).some((table) => calorieChange(table, person.sex, stage)?.milk === true);
|
|
1330
|
+
}
|
|
1331
|
+
export { ACTIVITY_LEVELS, BREAST_MILK_MONTHS, CALORIE_CHAIN, CALORIE_SEXES, CALORIE_TABLES, DGA_CALORIES, DGA_SOURCE, DIRECT_USES, FOOD_NEED, INFANT_FEEDINGS, INFANT_MONTHS, JOIN_CODES, NASEM_ENERGY_SOURCE, NASEM_INFANT_CALORIES, NEEDS_CODES, NEED_SUPPLIES, NO_COOK, STORED_WATER, WATER_NEED, calorieChange, calorieNeed, calorieRow, calorieRowOf, checkCalorieChain, checkCalorieTable, checkJoins, hasNeedsError, householdFragment, isActivityLevel };
|
|
953
1332
|
|
|
954
1333
|
//# sourceMappingURL=index.js.map
|