@mark1russell7/market 0.3.3 → 0.4.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.
@@ -1,5 +1,5 @@
1
- import { a as moneyOf, i as moneyAbove, o as monthSchema, r as latestAverages } from "./average-B29bynFb.js";
2
- import { convertible, kindOf, quantity, rationals, valueIn, whyNotConvertible } from "@mark1russell7/quantity";
1
+ import { a as monthSchema, i as moneyOf, r as latestAverages } from "./average-BXyKRj8R.js";
2
+ import { UnitError, atomAbove, canonicalUnit, convertible, divide, kindOf, multiply, quantity, rationals, valueIn, whyNotConvertible } from "@mark1russell7/quantity";
3
3
  import { checkSources, checkUnits } from "@mark1russell7/guidebook";
4
4
  // src/supplies.ts
5
5
  /** The format of a catalog of supplies. */
@@ -27,8 +27,21 @@ const SUPPLY_CODES = [
27
27
  "supplies-shelf",
28
28
  "supplies-unused",
29
29
  "supplies-no-read",
30
- "supplies-no-reason"
30
+ "supplies-no-reason",
31
+ "supplies-energy"
31
32
  ];
33
+ /** This function gives an amount in a unit in any known form. `quantity` takes the canonical form only. */
34
+ function amountIn(value, unit) {
35
+ return quantity(value, canonicalUnit(unit));
36
+ }
37
+ /** This function tells if a value from JSON is a finite number. `quantity` throws for another value. */
38
+ function finite(value) {
39
+ return typeof value === "number" && Number.isFinite(value);
40
+ }
41
+ /** This function tells if two amounts are equal, to a relative error of 1e-9 for the rounding of a conversion. A value that is not finite, for example a conversion that overflows, is not equal to a value. */
42
+ function same(a, b) {
43
+ return Number.isFinite(a) && Number.isFinite(b) && Math.abs(a - b) <= 1e-9 * Math.max(Math.abs(a), Math.abs(b));
44
+ }
32
45
  /** This function tells if a text is a unit of `@mark1russell7/quantity`. `convertible` throws for an unknown unit. */
33
46
  function known(unit) {
34
47
  if (typeof unit !== "string") return false;
@@ -64,6 +77,8 @@ function countUnit(unit) {
64
77
  * - An error for a fact that is not a fact of the catalog, and for an amount that is not more than 0. The basis of a
65
78
  * product is also a fact.
66
79
  * - An error for an amount with no reason, with two reasons, or with a blank definition. A catalog from JSON can have them.
80
+ * - An error for a scaled amount that is not the rate times the amount of its reason, or that has a bad factor.
81
+ * - An error for a food energy that is not equal to a gift of the product in a unit of energy.
67
82
  * - An error for a unit that is not a unit of `@mark1russell7/quantity`.
68
83
  * - An error for a unit that does not convert to the unit of its resource.
69
84
  * - An error for a size in a unit that is not a mass, a volume or a count, for example `h` or `kcal`.
@@ -110,6 +125,71 @@ function checkSupplies(catalog, options = {}) {
110
125
  }
111
126
  const facts = new Map(catalog.facts.map((fact) => [fact.id, fact]));
112
127
  const used = /* @__PURE__ */ new Set();
128
+ /** This function examines the id of a fact that a reason cites. */
129
+ const cite = (product, what, id) => {
130
+ const fact = typeof id === "string" ? facts.get(id) : void 0;
131
+ if (fact === void 0 || fact.use !== "fact") error("supplies-fact", "product", product, `${what} of the product ${product} cites ${String(id)}, and the catalog has no fact with that id.`);
132
+ if (typeof id === "string") used.add(id);
133
+ };
134
+ /** This function examines the text of a definition. */
135
+ const definition = (product, what, text) => {
136
+ if (typeof text !== "string" || text.trim() === "") error("supplies-fact", "product", product, `The definition of the ${what} of the product ${product} is blank.`);
137
+ };
138
+ /** This function examines a scaled reason: its factors, and the amount against the rate times the amount of the reason. */
139
+ const scaled = (product, what, reason, value, unit) => {
140
+ const { rate, of } = typeof reason === "object" && reason !== null ? reason : {};
141
+ if (typeof rate !== "object" || rate === null || typeof rate.per !== "object" || rate.per === null || typeof of !== "object" || of === null) {
142
+ error("supplies-fact", "product", product, `The scaled ${what} of the product ${product} must have a rate with its amount (\`per\`), and the amount that the rate applies to (\`of\`).`);
143
+ return;
144
+ }
145
+ cite(product, `The rate of the ${what}`, rate.fact);
146
+ const ofReasons = (of.fact === void 0 ? 0 : 1) + (of.definition === void 0 ? 0 : 1);
147
+ if (ofReasons !== 1) error("supplies-fact", "product", product, `The amount of the scaled ${what} of the product ${product} has ${String(ofReasons)} reasons. It must have a fact or a definition, not the two.`);
148
+ else if (of.fact !== void 0) cite(product, `The amount of the scaled ${what}`, of.fact);
149
+ else definition(product, `amount of the scaled ${what}`, of.definition);
150
+ const factors = [
151
+ [
152
+ "rate",
153
+ rate.amount,
154
+ rate.unit
155
+ ],
156
+ [
157
+ "amount of the rate",
158
+ rate.per.amount,
159
+ rate.per.unit
160
+ ],
161
+ [
162
+ "amount",
163
+ of.amount,
164
+ of.unit
165
+ ]
166
+ ];
167
+ let good = Number.isFinite(value) && value > 0 && known(unit);
168
+ for (const [name, factor, factorUnit] of factors) {
169
+ if (!known(factorUnit)) {
170
+ error("supplies-unit", "product", product, `The unit ${String(factorUnit)} of the ${name} of the scaled ${what} of the product ${product} is not a known unit.`);
171
+ good = false;
172
+ }
173
+ if (typeof factor !== "number" || !Number.isFinite(factor) || factor <= 0) {
174
+ error("supplies-amount", "product", product, `The ${name} of the scaled ${what} of the product ${product} is ${String(factor)}. It must be a finite number more than 0.`);
175
+ good = false;
176
+ }
177
+ }
178
+ if (!good) return;
179
+ try {
180
+ const total = multiply(divide(amountIn(rate.amount, rate.unit), amountIn(rate.per.amount, rate.per.unit)), amountIn(of.amount, of.unit));
181
+ const why = whyNotConvertible(total.unit, unit);
182
+ if (why !== void 0) {
183
+ error("supplies-unit", "product", product, `The rate times the amount of the scaled ${what} of the product ${product} is in ${total.unit}, and it does not convert to ${unit}: ${why}.`);
184
+ return;
185
+ }
186
+ const expected = valueIn(total, canonicalUnit(unit));
187
+ if (!same(expected, value)) error("supplies-amount", "product", product, `The ${what} of the product ${product} is ${String(value)} ${unit}, and its rate times its amount gives ${String(expected)} ${unit}.`);
188
+ } catch (cause) {
189
+ const code = cause instanceof UnitError ? "supplies-unit" : "supplies-amount";
190
+ error(code, "product", product, `The rate times the amount of the scaled ${what} of the product ${product} has no value: ${cause instanceof Error ? cause.message : String(cause)}.`);
191
+ }
192
+ };
113
193
  /** This function examines one cited amount, and gives the unit check to `units`. */
114
194
  const amount = (product, what, value, unit) => {
115
195
  if (!value.read) {
@@ -117,14 +197,11 @@ function checkSupplies(catalog, options = {}) {
117
197
  if (typeof value.why !== "string" || value.why.trim() === "") warning("supplies-no-reason", "product", product, `The no read of the ${what} of the product ${product} has no reason.`);
118
198
  return;
119
199
  }
120
- const reasons = (value.fact === void 0 ? 0 : 1) + (value.definition === void 0 ? 0 : 1);
121
- if (reasons !== 1) error("supplies-fact", "product", product, `The ${what} of the product ${product} has ${String(reasons)} reasons. It must have a fact or a definition, not the two.`);
122
- else if (value.definition !== void 0 && (typeof value.definition !== "string" || value.definition.trim() === "")) error("supplies-fact", "product", product, `The definition of the ${what} of the product ${product} is blank.`);
123
- if (value.fact !== void 0) {
124
- const fact = facts.get(value.fact);
125
- if (fact === void 0 || fact.use !== "fact") error("supplies-fact", "product", product, `The ${what} of the product ${product} cites ${value.fact}, and the catalog has no fact with that id.`);
126
- used.add(value.fact);
127
- }
200
+ const reasons = (value.fact === void 0 ? 0 : 1) + (value.definition === void 0 ? 0 : 1) + (value.scaled === void 0 ? 0 : 1);
201
+ if (reasons !== 1) error("supplies-fact", "product", product, `The ${what} of the product ${product} has ${String(reasons)} reasons. It must have one: a fact, a definition or a scaled reason.`);
202
+ else if (value.definition !== void 0) definition(product, what, value.definition);
203
+ if (value.fact !== void 0) cite(product, `The ${what}`, value.fact);
204
+ if (value.scaled !== void 0 && reasons === 1) scaled(product, what, value.scaled, value.amount, value.unit);
128
205
  if (!(Number.isFinite(value.amount) && value.amount > 0)) error("supplies-amount", "product", product, `The ${what} of the product ${product} is ${String(value.amount)}. It must be a finite number more than 0.`);
129
206
  if (!known(value.unit)) error("supplies-unit", "product", product, `The unit ${String(value.unit)} of the ${what} of the product ${product} is not a known unit.`);
130
207
  else if (known(unit) && !convertible(value.unit, unit)) error("supplies-unit", "product", product, `The ${what} of the product ${product} is in ${value.unit}, and ${value.unit} does not convert to ${unit}.`);
@@ -150,6 +227,16 @@ function checkSupplies(catalog, options = {}) {
150
227
  }
151
228
  if (product.energy !== void 0 && product.kind !== "food" && SUPPLY_KINDS.includes(product.kind)) error("supplies-kind", "product", product.id, `The product ${product.id} has a food energy, and its kind is ${String(product.kind)}. Only a product of the kind food has a food energy.`);
152
229
  if (product.energy !== void 0) amount(product.id, "food energy", product.energy, ENERGY_UNIT$1);
230
+ if (product.energy?.read === true && finite(product.energy.amount) && known(product.energy.unit) && convertible(product.energy.unit, ENERGY_UNIT$1)) {
231
+ const energy = valueIn(amountIn(product.energy.amount, product.energy.unit), ENERGY_UNIT$1);
232
+ for (const gift of product.gives) {
233
+ const unit = resources.get(gift.resource)?.unit;
234
+ if (!gift.amount.read || unit === void 0 || !known(unit) || !convertible(unit, ENERGY_UNIT$1)) continue;
235
+ if (!finite(gift.amount.amount) || !known(gift.amount.unit) || !convertible(gift.amount.unit, ENERGY_UNIT$1)) continue;
236
+ const given = valueIn(amountIn(gift.amount.amount, gift.amount.unit), ENERGY_UNIT$1);
237
+ if (!same(given, energy)) error("supplies-energy", "product", product.id, `The product ${product.id} gives ${String(given)} kcal of ${gift.resource}, and its food energy is ${String(energy)} kcal. The two must be equal.`);
238
+ }
239
+ }
153
240
  if (product.size !== void 0) {
154
241
  amount(product.id, "size", product.size, "");
155
242
  if (product.size.read && known(product.size.unit) && !sizeUnit(product.size.unit)) error("supplies-unit", "product", product.id, `The size of the product ${product.id} is in ${product.size.unit}. It must be a mass, a volume or a count.`);
@@ -318,49 +405,49 @@ const SUPPLY_CATALOG = {
318
405
  {
319
406
  id: "fdc.rice-white",
320
407
  use: "fact",
321
- text: "The item 168877 (white long-grain rice, raw, enriched) gives 365 kcal in 100 g. Thus 1 kg gives 3650 kcal.",
408
+ text: "The item 168877 (white long-grain rice, raw, enriched) gives 365 kcal in 100 g.",
322
409
  source: "fdc.sr-legacy",
323
410
  locator: "FDC ID 168877"
324
411
  },
325
412
  {
326
413
  id: "fdc.oats",
327
414
  use: "fact",
328
- text: "The item 173904 (oats, regular and quick, not fortified, dry) gives 379 kcal in 100 g. Thus 1 kg gives 3790 kcal.",
415
+ text: "The item 173904 (oats, regular and quick, not fortified, dry) gives 379 kcal in 100 g.",
329
416
  source: "fdc.sr-legacy",
330
417
  locator: "FDC ID 173904"
331
418
  },
332
419
  {
333
420
  id: "fdc.beans-black",
334
421
  use: "fact",
335
- text: "The item 173734 (black beans, mature seeds, raw) gives 341 kcal in 100 g. Thus 1 kg gives 3410 kcal.",
422
+ text: "The item 173734 (black beans, mature seeds, raw) gives 341 kcal in 100 g.",
336
423
  source: "fdc.sr-legacy",
337
424
  locator: "FDC ID 173734"
338
425
  },
339
426
  {
340
427
  id: "fdc.peanut-butter",
341
428
  use: "fact",
342
- text: "The item 174266 (peanut butter, smooth style, with salt) gives 598 kcal in 100 g. Thus 1 kg gives 5980 kcal.",
429
+ text: "The item 174266 (peanut butter, smooth style, with salt) gives 598 kcal in 100 g.",
343
430
  source: "fdc.sr-legacy",
344
431
  locator: "FDC ID 174266"
345
432
  },
346
433
  {
347
434
  id: "fdc.pasta",
348
435
  use: "fact",
349
- text: "The item 168927 (pasta, dry, unenriched) gives 371 kcal in 100 g. Thus 1 kg gives 3710 kcal.",
436
+ text: "The item 168927 (pasta, dry, unenriched) gives 371 kcal in 100 g.",
350
437
  source: "fdc.sr-legacy",
351
438
  locator: "FDC ID 168927"
352
439
  },
353
440
  {
354
441
  id: "fdc.flour-white",
355
442
  use: "fact",
356
- text: "The item 169761 (wheat flour, white, all-purpose, unenriched) gives 364 kcal in 100 g. Thus 1 kg gives 3640 kcal.",
443
+ text: "The item 169761 (wheat flour, white, all-purpose, unenriched) gives 364 kcal in 100 g.",
357
444
  source: "fdc.sr-legacy",
358
445
  locator: "FDC ID 169761"
359
446
  },
360
447
  {
361
448
  id: "fdc.soup-chicken-noodle",
362
449
  use: "fact",
363
- text: "The item 171148 (chunky chicken noodle soup, canned, ready-to-serve) gives 41 kcal in 100 g. Its portion of 1 can is 530 g. Thus 1 can gives 217.3 kcal.",
450
+ text: "The item 171148 (chunky chicken noodle soup, canned, ready-to-serve) gives 41 kcal in 100 g. Its portion of 1 can is 530 g.",
364
451
  source: "fdc.sr-legacy",
365
452
  locator: "FDC ID 171148, portion 1 can (530 g)"
366
453
  },
@@ -498,7 +585,22 @@ const SUPPLY_CATALOG = {
498
585
  read: true,
499
586
  amount: 3650,
500
587
  unit: "kcal",
501
- fact: "fdc.rice-white"
588
+ scaled: {
589
+ rate: {
590
+ amount: 365,
591
+ unit: "kcal",
592
+ per: {
593
+ amount: 100,
594
+ unit: "g"
595
+ },
596
+ fact: "fdc.rice-white"
597
+ },
598
+ of: {
599
+ amount: 1,
600
+ unit: "kg",
601
+ definition: "The product is 1 kg of dry rice."
602
+ }
603
+ }
502
604
  },
503
605
  shelf: {
504
606
  read: true,
@@ -539,7 +641,22 @@ const SUPPLY_CATALOG = {
539
641
  read: true,
540
642
  amount: 3790,
541
643
  unit: "kcal",
542
- fact: "fdc.oats"
644
+ scaled: {
645
+ rate: {
646
+ amount: 379,
647
+ unit: "kcal",
648
+ per: {
649
+ amount: 100,
650
+ unit: "g"
651
+ },
652
+ fact: "fdc.oats"
653
+ },
654
+ of: {
655
+ amount: 1,
656
+ unit: "kg",
657
+ definition: "The product is 1 kg of dry oats."
658
+ }
659
+ }
543
660
  },
544
661
  shelf: {
545
662
  read: true,
@@ -573,7 +690,22 @@ const SUPPLY_CATALOG = {
573
690
  read: true,
574
691
  amount: 3410,
575
692
  unit: "kcal",
576
- fact: "fdc.beans-black"
693
+ scaled: {
694
+ rate: {
695
+ amount: 341,
696
+ unit: "kcal",
697
+ per: {
698
+ amount: 100,
699
+ unit: "g"
700
+ },
701
+ fact: "fdc.beans-black"
702
+ },
703
+ of: {
704
+ amount: 1,
705
+ unit: "kg",
706
+ definition: "The product is 1 kg of dry beans."
707
+ }
708
+ }
577
709
  },
578
710
  shelf: {
579
711
  read: true,
@@ -601,7 +733,22 @@ const SUPPLY_CATALOG = {
601
733
  read: true,
602
734
  amount: 5980,
603
735
  unit: "kcal",
604
- fact: "fdc.peanut-butter"
736
+ scaled: {
737
+ rate: {
738
+ amount: 598,
739
+ unit: "kcal",
740
+ per: {
741
+ amount: 100,
742
+ unit: "g"
743
+ },
744
+ fact: "fdc.peanut-butter"
745
+ },
746
+ of: {
747
+ amount: 1,
748
+ unit: "kg",
749
+ definition: "The product is 1 kg of peanut butter."
750
+ }
751
+ }
605
752
  }
606
753
  }],
607
754
  size: {
@@ -614,7 +761,22 @@ const SUPPLY_CATALOG = {
614
761
  read: true,
615
762
  amount: 5980,
616
763
  unit: "kcal",
617
- fact: "fdc.peanut-butter"
764
+ scaled: {
765
+ rate: {
766
+ amount: 598,
767
+ unit: "kcal",
768
+ per: {
769
+ amount: 100,
770
+ unit: "g"
771
+ },
772
+ fact: "fdc.peanut-butter"
773
+ },
774
+ of: {
775
+ amount: 1,
776
+ unit: "kg",
777
+ definition: "The product is 1 kg of peanut butter."
778
+ }
779
+ }
618
780
  },
619
781
  shelf: {
620
782
  read: true,
@@ -648,7 +810,22 @@ const SUPPLY_CATALOG = {
648
810
  read: true,
649
811
  amount: 3710,
650
812
  unit: "kcal",
651
- fact: "fdc.pasta"
813
+ scaled: {
814
+ rate: {
815
+ amount: 371,
816
+ unit: "kcal",
817
+ per: {
818
+ amount: 100,
819
+ unit: "g"
820
+ },
821
+ fact: "fdc.pasta"
822
+ },
823
+ of: {
824
+ amount: 1,
825
+ unit: "kg",
826
+ definition: "The product is 1 kg of dry pasta."
827
+ }
828
+ }
652
829
  },
653
830
  shelf: {
654
831
  read: true,
@@ -689,7 +866,22 @@ const SUPPLY_CATALOG = {
689
866
  read: true,
690
867
  amount: 3640,
691
868
  unit: "kcal",
692
- fact: "fdc.flour-white"
869
+ scaled: {
870
+ rate: {
871
+ amount: 364,
872
+ unit: "kcal",
873
+ per: {
874
+ amount: 100,
875
+ unit: "g"
876
+ },
877
+ fact: "fdc.flour-white"
878
+ },
879
+ of: {
880
+ amount: 1,
881
+ unit: "kg",
882
+ definition: "The product is 1 kg of white all-purpose flour."
883
+ }
884
+ }
693
885
  },
694
886
  shelf: {
695
887
  read: true,
@@ -717,7 +909,22 @@ const SUPPLY_CATALOG = {
717
909
  read: true,
718
910
  amount: 217.3,
719
911
  unit: "kcal",
720
- fact: "fdc.soup-chicken-noodle"
912
+ scaled: {
913
+ rate: {
914
+ amount: 41,
915
+ unit: "kcal",
916
+ per: {
917
+ amount: 100,
918
+ unit: "g"
919
+ },
920
+ fact: "fdc.soup-chicken-noodle"
921
+ },
922
+ of: {
923
+ amount: 530,
924
+ unit: "g",
925
+ fact: "fdc.soup-chicken-noodle"
926
+ }
927
+ }
721
928
  }
722
929
  }],
723
930
  size: {
@@ -730,7 +937,22 @@ const SUPPLY_CATALOG = {
730
937
  read: true,
731
938
  amount: 217.3,
732
939
  unit: "kcal",
733
- fact: "fdc.soup-chicken-noodle"
940
+ scaled: {
941
+ rate: {
942
+ amount: 41,
943
+ unit: "kcal",
944
+ per: {
945
+ amount: 100,
946
+ unit: "g"
947
+ },
948
+ fact: "fdc.soup-chicken-noodle"
949
+ },
950
+ of: {
951
+ amount: 530,
952
+ unit: "g",
953
+ fact: "fdc.soup-chicken-noodle"
954
+ }
955
+ }
734
956
  },
735
957
  shelf: {
736
958
  read: true,
@@ -816,7 +1038,7 @@ const SUPPLY_CATALOG = {
816
1038
  // src/food-plan.ts
817
1039
  /** This function gives the money of a cost for each unit of time, for example `USD` for `USD/wk`. */
818
1040
  function moneyOfRate(unit) {
819
- const money = moneyAbove(unit);
1041
+ const money = atomAbove(unit, "money");
820
1042
  return money !== void 0 && convertible(unit, `${money}/d`) ? money : void 0;
821
1043
  }
822
1044
  /** The age and sex groups of a food plan of USDA. */