Skip to content

About

US food and drink label rules as code: Nutrition Facts rounding (21 CFR 101.9), allergen statements, the alcohol health warning. Every result cites its regulation.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

label-rules: US food and drink label rules as code, every result cited to its regulation

Nutrition Facts  ·  Allergens  ·  Alcohol warning  ·  Coverage  ·  Every rule

CI npm MIT licence


label-rules turns the rules for US packaged-food and drink labels into small, typed functions with no dependencies. Every value it prints and every problem it flags carries the regulation paragraph it comes from and a link to the text, and says whether that source is a statute, a regulation or FDA guidance.

Used in TrulyFinal.

npm install label-rules
module what it does source
label-rules/nutrition rounds every nutrient for the Nutrition Facts panel, works out % Daily Value, checks a panel you already have 21 CFR 101.9
label-rules/allergens finds the nine major allergens in an ingredient list and checks the "Contains" statement 21 U.S.C. 343(w), FASTER Act, FDA Q&A
label-rules/alcohol compares the government health warning word for word and checks its type size 27 CFR part 16

ESM, Node 20 or later, zero runtime dependencies, tree-shakeable. Tested just below, at and just above every threshold.

Nutrition Facts (21 CFR 101.9)

import { declare, percentDailyValue, formatNutritionFacts } from "label-rules/nutrition";

declare("totalFat", 7.8);
// {
//   id: "totalFat", amount: 8, lessThan: false, value: "8", unit: "g", text: "8g",
//   rule: "NF_ROUND_FAT", cite: "21 CFR 101.9(c)(2)",
//   url: "https://www.ecfr.gov/current/title-21/section-101.9#p-101.9(c)(2)",
//   authority: "regulation"
// }

declare("cholesterol", 3).text;      // "<5mg"
declare("sodium", 158).text;         // "160mg"
percentDailyValue("totalFat", 7.8);  // 10

const panel = formatNutritionFacts({
  nutrients: { calories: 232, totalFat: 7.8, saturatedFat: 1.05, totalSugars: 12.2, addedSugars: 9.8 },
});
panel.calories;                                           // "230"
panel.lines.map((l) => [l.text, l.percentDailyValue]);
// [ ["Total Fat 8g", "10%"], ["Saturated Fat 1g", "5%"],
//   ["Total Sugars 12g", null], ["Includes 10g Added Sugars", "20%"] ]
panel.findings.length;                                    // 10: the mandatory nutrients not given

Also here: checkNutrients (parts larger than their total, missing mandatory nutrients), checkPrintedPanel (read an existing label back: is each amount on its increment, does each % Daily Value follow from it), roundServingsPerContainer, roundServingMetric, simplifiedFormat, percentDailyValueProtein, and the DRV, RDI and NUTRIENTS tables.

Allergen statement (21 U.S.C. 343(w))

import { checkAllergenLabel } from "label-rules/allergens";

const r = checkAllergenLabel({
  ingredients: "Rolled oats, honey, almonds, whey, tahini, sea salt",
  contains: "Contains: nuts",
});
r.findings.map((f) => [f.id, f.term, f.cite]);
// [ ["ALLERGEN_MISSING_FROM_CONTAINS", "almonds", "21 U.S.C. 343(w)(1)(A)"],
//   ["ALLERGEN_MISSING_FROM_CONTAINS", "whey",    "21 U.S.C. 343(w)(1)(A)"],
//   ["ALLERGEN_MISSING_FROM_CONTAINS", "tahini",  "21 U.S.C. 343(w)(1)(A)"],
//   ["ALLERGEN_GROUP_NOT_SPECIFIC",    "nuts",    "21 U.S.C. 343(w)(2)"] ]
r.findings[3].message;
// "\"nuts\" names a group. Name the specific tree nut (for example almond, pecan, walnut)."
r.suggestion.text;
// "Contains: Milk, Almond, Sesame."

detectAllergens and parseContainsStatement are exported too. Every hit says whether the word comes from the statute, from FDA guidance, or from this library's own synonym table (basis), and whether it is certain or only likely (cheese may be vegan; an oil may be highly refined and exempt).

Alcohol health warning (27 CFR part 16)

import { checkHealthWarning, GOVERNMENT_WARNING, warningTypeRequirements } from "label-rules/alcohol";

warningTypeRequirements(750);
// {
//   minTypeSizeMm: 2, maxCharactersPerInch: 25,
//   band: "more than 237 milliliters (8 fl. oz.) up to 3 liters (101 fl. oz.)",
//   cite: "27 CFR 16.22(b)(2)",
//   url: "https://www.ecfr.gov/current/title-27/section-16.22#p-16.22(b)(2)"
// }

const w = checkHealthWarning({
  text: GOVERNMENT_WARNING.replace("should not drink", "should not consume"),
  containerMl: 750,
  typeSizeMm: 1.5,
});
w.comparison.differences;
// [ { kind: "changed", expected: "drink", found: "consume", position: 15 } ]
w.findings.map((f) => [f.id, f.cite, f.message]);
// [ ["ALC_WARNING_WORDING", "27 CFR 16.21",
//    "The wording differs from 27 CFR 16.21: \"consume\" should be \"drink\"."],
//   ["ALC_TYPE_TOO_SMALL", "27 CFR 16.22(b)(2)",
//    "Type of 1.5 mm is smaller than the 2 mm minimum for containers of more than 237 milliliters (8 fl. oz.) up to 3 liters (101 fl. oz.)."] ]

What a result looks like

Anything that flags something is a Finding:

interface Finding {
  id: string;                          // stable; all of them are in docs/rules.md
  severity: "error" | "warning" | "info";
  message: string;
  cite: string;                        // "21 CFR 101.9(c)(2)"
  url: string;                         // the cited text; eCFR for regulations
  authority: "statute" | "regulation" | "guidance" | "derived" | "heuristic";
}

authority matters. guidance is an FDA guidance document: FDA's current thinking, not binding. derived follows from the cited definitions but is not stated there as a rule. heuristic is this library's own sanity check and no agency's rule.

Coverage

Status: done = implemented, tested at every threshold, and checked against the primary text. docs/rules.md lists every rule id.

Rule Citation Status
Calories rounding 21 CFR 101.9(c)(1) done
Total, saturated, trans, poly-, monounsaturated fat rounding 21 CFR 101.9(c)(2), (c)(2)(i)-(iv) done
Cholesterol rounding, "less than 5 mg" 21 CFR 101.9(c)(3) done
Sodium rounding 21 CFR 101.9(c)(4) done
Fluoride rounding 21 CFR 101.9(c)(5) done
Total carbohydrate, fiber, sugars, added sugars, sugar alcohol rounding, "less than 1 g" 21 CFR 101.9(c)(6), (c)(6)(i)-(iv) done
Protein rounding; % DV from the PDCAAS-corrected amount 21 CFR 101.9(c)(7), (c)(7)(i)-(iii) done (you supply the PDCAAS)
Vitamin and mineral amounts: levels of significance of the RDI table 21 CFR 101.9(c)(8)(iii) done
Vitamin and mineral amounts: FDA's recommended increments FDA Q&A guidance, Dec. 2019 done, opt-in, guidance
% Daily Value, vitamins and minerals (2 / 5 / 10% steps) 21 CFR 101.9(c)(8)(iii) done
RDIs, adults and children 4 years and older 21 CFR 101.9(c)(8)(iv) done
DRVs, adults and children 4 years and older 21 CFR 101.9(c)(9) done
% Daily Value, DRV nutrients (nearest whole percent, declared or actual basis) 21 CFR 101.9(d)(7)(ii) done
Halfway values round up FDA Food Labeling Guide, Appendix H done, guidance
Servings per container, "about" 21 CFR 101.9(b)(8)(i) done (not "varied" or "usually")
Metric equivalent of the serving 21 CFR 101.9(b)(7)(ii) done
Simplified format: eight of fifteen insignificant 21 CFR 101.9(f), (f)(1) eligibility test only
Mandatory nutrients present; poly- and monounsaturated together 21 CFR 101.9(c), (c)(2)(iii)-(iv) done; the per-nutrient exceptions are yours to apply
Declared strings for a standard vertical panel 21 CFR 101.9(c), (d)(7) text only, no layout
Calories against 4-4-9 21 CFR 101.9(c)(1)(i)(B), (C) heuristic warning, not a rule
Serving size from reference amounts (RACCs), household measures 21 CFR 101.9(b)(2)-(6), 101.12 not covered
The nine major food allergens 21 U.S.C. 321(qq)(1) done
"Contains" names every major allergen 21 U.S.C. 343(w)(1)(A) done
Without "Contains": source named in the ingredient list 21 U.S.C. 343(w)(1)(B) done
Tree nuts, fish, Crustacean shellfish by type or species 21 U.S.C. 343(w)(2) done
Highly refined oils 21 U.S.C. 321(qq)(2)(A) flagged as a question
Twelve tree nuts; coconut and molluscs not major allergens FDA Q&A Edition 5 (Jan. 2025), B.9, C.7, C.8 done, guidance
Milk and eggs from other animals FDA Q&A Edition 5, C.1-C.4 done, guidance
"Contains" wording; reserved for major allergens; unexplained items FDA Q&A Edition 5, D.7, D.8, D.10 done, guidance
Advisory ("may contain") statements FDA Q&A Edition 5, D.12, D.14 done, guidance
Type size and placement of the "Contains" statement 21 U.S.C. 343(w)(1)(A) not covered (needs the artwork)
Health warning text 27 CFR 16.21 done
"GOVERNMENT WARNING" in capitals and bold; remainder not bold 27 CFR 16.22(a)(2) capitals from the text; bold from what you pass
Maximum characters per inch 27 CFR 16.22(a)(4) done (you measure)
Minimum type size by container 27 CFR 16.22(b)(1)-(3) done (you measure)
Applies from 0.5% alcohol by volume 27 CFR 16.10 done
Legible, contrasting background, not compressed, separate and apart, firmly affixed 27 CFR 16.21, 16.22(a)(1), (a)(3), (c) not covered (needs the artwork)

What this is not

  • Not legal advice, and not a certificate. It applies the rules it lists to the values and words you give it. The manufacturer is responsible for the label. A result with no findings means none of these checks fired; it does not mean a label complies.
  • United States only. No Canada (CFIA), no EU (Regulation 1169/2011, 14 allergens), no other country.
  • FDA-regulated food and TTB health warning only. Not meat, poultry or egg products (USDA FSIS). Nothing about alcohol labels other than the health warning: no alcohol content, net contents, class and type, or COLA rules.
  • Daily Values for adults and children 4 years and older only. Not infants through 12 months, children 1 through 3 years, or pregnant and lactating women (101.9(c)(8)(iv), (c)(9)).
  • Not covered: dual-column labels (101.9(b)(12), (e)); tabular and linear displays for small packages (101.9(j)(13)); the layout, type sizes and rules of the graphic (101.9(d)); aggregate displays; the footnote text; "calories from saturated fat"; folic acid in parentheses; vitamin D in IU; reference amounts and serving-size determination (101.12); nutrient content and health claims (101.13, 101.54 on); Supplement Facts (101.36); restaurant menu labeling (101.11); ingredient-list order and naming (101.4); net quantity; state rules such as California Proposition 65.
  • The allergen check reads words. It cannot see inside "natural flavor", "spices" or a supplier's blend, cannot tell whether an oil is highly refined, and knows nothing about cross-contact. Its synonym table is the library's own beyond what the statute and FDA's guidance name, and it is not complete: a clean result is not proof that a food has no allergen.
  • Guidance is labelled as guidance. Where FDA guidance and the regulation's text differ in force, the result says which one it rests on. Halfway-rounds-up comes from FDA's Food Labeling Guide, not the CFR.
  • Rules change. The text was checked on the eCFR in November 2026 (dates in docs/rules.md). If you find a rule that has moved, please open an issue with the citation.

Development

npm install
npm test          # vitest
npm run build     # tsc -> dist/

A change to a rule needs the citation in a comment on the line that implements it, the same citation on whatever it returns, and tests just below, at and just above each threshold.

License

MIT © Agentic Flow LLC

About

US food and drink label rules as code: Nutrition Facts rounding (21 CFR 101.9), allergen statements, the alcohol health warning. Every result cites its regulation.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages