DEV Community

Waqar Anwar
Waqar Anwar

Posted on AI-assisted

Mexico's warning octagons in JavaScript: the two thresholds most implementations miss

If you sell packaged food in Mexico, the front of the pack may need up to five black octagons: EXCESO CALORÍAS, EXCESO AZÚCARES, EXCESO GRASAS SATURADAS, EXCESO GRASAS TRANS and EXCESO SODIO. They come from NOM-051-SCFI/SSA1-2010 as modified in the Diario Oficial on 27 March 2020, and the numbers live in one place: Tabla 6.

The table looks small enough to code in ten minutes. I built this logic for a nutrition label tool, and an audit of our version found three bugs: two seals it failed to show, and one it showed when it shouldn't. This post walks through the corrected implementation and the traps.

Not legal advice. Check the current text in the DOF and COFEPRIS's NOM-051 manual before you rely on any of this for a real label.

Tabla 6, as data

Every comparison in the table is inclusive (>=), and every value is per 100 g for solids or per 100 mL for liquids.

export const TABLE_6 = {
  energy: {
    solidKcalPer100g: 275,
    liquidKcalPer100ml: 70,
    liquidKcalFromFreeSugarsPer100ml: 8,   // trap #1
  },
  sugarsPctEnergy: 10,        // % of energy from FREE sugars
  saturatedFatPctEnergy: 10,  // % of energy from saturated fat
  transFatPctEnergy: 1,       // % of energy from trans fat
  sodium: {
    mgPerKcal: 1,
    mgPer100: 300,                     // trap #2
    zeroCalorieBeverageMgPer100ml: 45,
  },
};

// Mandatory left-to-right display order
export const SEAL_ORDER = ['calories', 'sugars', 'saturatedFat', 'transFat', 'sodium'];
Enter fullscreen mode Exit fullscreen mode

Three of the five criteria are percent of energy, so you need energy from each nutrient. The norm uses 4 kcal/g for carbohydrate and 9 kcal/g for fat:

const freeSugarKcal = sugarsG * 4;
const satFatKcal    = satFatG * 9;
const transFatKcal  = transFatG * 9;
const pct = (part) => (kcal > 0 ? (part / kcal) * 100 : 0);
Enter fullscreen mode Exit fullscreen mode

Trap #1: liquids have two calorie arms

Most implementations write the liquid calorie rule as kcal >= 70. Tabla 6 says ≥ 70 kcal total or ≥ 8 kcal from free sugars per 100 mL.

That second arm matters. A regular cola has about 42 kcal per 100 mL, well under 70. But almost all of that energy is sugar: 10.6 g × 4 = 42 kcal from free sugars, which is far above 8. So the cola must carry EXCESO CALORÍAS. Code that only checks kcal >= 70 leaves off a mandatory seal.

calories: isLiquid
  ? kcal >= T.energy.liquidKcalPer100ml
    || freeSugarKcal >= T.energy.liquidKcalFromFreeSugarsPer100ml
  : kcal >= T.energy.solidKcalPer100g,
Enter fullscreen mode Exit fullscreen mode

Trap #2: sodium has an absolute arm too

The sodium rule is ≥ 1 mg per kcal or ≥ 300 mg per 100 g/mL. Ratio-only code misses energy-dense salty foods.

Take a snack with 550 kcal and 480 mg sodium per 100 g. The ratio is 480 / 550 = 0.87 mg/kcal, under 1, so a ratio-only check passes it. But 480 ≥ 300, so EXCESO SODIO is mandatory.

Zero-calorie drinks get their own line, ≥ 45 mg per 100 mL. The norm doesn't define "sin calorías". The nutrition-declaration rounding rules treat under 5 kcal as zero, so that's the cut-off I use:

sodium: (() => {
  if (isLiquid && kcal < 5) return sodium >= T.sodium.zeroCalorieBeverageMgPer100ml;
  const ratioMet = kcal > 0 && sodium / kcal >= T.sodium.mgPerKcal;
  return ratioMet || sodium >= T.sodium.mgPer100;
})(),
Enter fullscreen mode Exit fullscreen mode

Trap #3: "free sugars" is not "total sugars"

The sugar criteria use free sugars: sugars added during manufacture, plus sugars naturally present in honey, syrups and fruit or vegetable juices. Lactose in milk and sugar inside whole fruit are not free sugars.

If you feed the "Sugars" line from the nutrition panel straight into this function, plain milk picks up an EXCESO AZÚCARES seal it shouldn't have. Make the parameter name say what it means:

// sugarsGPer100 = FREE sugars, not the "Total Sugars" panel line
Enter fullscreen mode Exit fullscreen mode

Trans fat has a similar exclusion: trans fats naturally present in dairy and meat are left out.

Trap #4: the "added" gating decides what is evaluated

In the phase now in force, the transitional rules say:

  • if sugars were added, evaluate sugars and calories;
  • if fats were added, evaluate saturated fat, trans fat and calories;
  • if sodium was added, evaluate sodium only.

The gating never changes a threshold, because thresholds always use the total amount present. It only picks which criteria run. Mixing those two ideas up is the classic error.

function evaluatedCriteria({ addedSugar, addedFat, addedSodium }) {
  const known = [addedSugar, addedFat, addedSodium].some((v) => v !== undefined);
  if (!known) return new Set(SEAL_ORDER);          // caller doesn't know: evaluate all
  const s = new Set();
  if (addedSugar)  { s.add('sugars'); s.add('calories'); }
  if (addedFat)    { s.add('saturatedFat'); s.add('transFat'); s.add('calories'); }
  if (addedSodium) s.add('sodium');
  return s;
}
Enter fullscreen mode Exit fullscreen mode

Here is what that does to plain whole milk (about 61 kcal, 1.9 g saturated fat, nothing added). Saturated fat supplies roughly 28% of its energy, so if you skip the gating it gets EXCESO GRASAS SATURADAS. With the gating it gets no seals, because nothing was added. Use undefined for "unknown", not false, so older callers keep the safe evaluate-everything behaviour.

Putting it together

export function computeOctagons({
  caloriesPer100 = 0, sugarsGPer100 = 0, saturatedFatGPer100 = 0,
  transFatGPer100 = 0, sodiumMgPer100 = 0, type = 'solid',
  addedSugar, addedFat, addedSodium,
} = {}) {
  const T = TABLE_6;
  const isLiquid = type === 'liquid';
  const kcal = Number(caloriesPer100) || 0;
  const sodium = Number(sodiumMgPer100) || 0;
  const evaluated = evaluatedCriteria({ addedSugar, addedFat, addedSodium });

  const freeSugarKcal = sugarsGPer100 * 4;
  const pct = (part) => (kcal > 0 ? (part / kcal) * 100 : 0);

  const met = {
    calories: isLiquid
      ? kcal >= T.energy.liquidKcalPer100ml || freeSugarKcal >= T.energy.liquidKcalFromFreeSugarsPer100ml
      : kcal >= T.energy.solidKcalPer100g,
    sugars: pct(freeSugarKcal) >= T.sugarsPctEnergy,
    saturatedFat: pct(saturatedFatGPer100 * 9) >= T.saturatedFatPctEnergy,
    transFat: pct(transFatGPer100 * 9) >= T.transFatPctEnergy,
    sodium: (isLiquid && kcal < 5)
      ? sodium >= T.sodium.zeroCalorieBeverageMgPer100ml
      : (kcal > 0 && sodium / kcal >= T.sodium.mgPerKcal) || sodium >= T.sodium.mgPer100,
  };

  return SEAL_ORDER.filter((k) => evaluated.has(k) && met[k]);
}
Enter fullscreen mode Exit fullscreen mode

Results from that function:

Product (per 100 g/mL) Added Seals
Cola: 42 kcal, 10.6 g free sugars sugar calories, sugars
Snack: 550 kcal, 480 mg sodium sodium sodium
Plain milk: 61 kcal, 1.9 g sat fat nothing none
Same milk, flags unknown — saturatedFat

Turn the traps into tests

Each trap above is one test case: a 42 kcal cola, a 480 mg / 550 kcal snack, plain milk with and without the added flags. They're cheap to write and catch exactly the regressions that slip through.

Two more things to plan for:

  1. Make the phase a parameter. NOM-051 has rolled out in phases and the dates have been extended by later DOF notices. The gating is what differs between phases, and a 2025 draft (PROY-NOM-051-SE/SSA1-2025) is rewriting numeral 4.5.3. Don't hard-code today's rules as if they're final.
  2. The legends are separate from the seals. "CONTIENE EDULCORANTES, NO RECOMENDABLE EN NIÑOS" and "CONTIENE CAFEÍNA EVITAR EN NIÑOS" come from numerals 7.1.3 and 7.1.4. Any seal, or the sweetener legend, also bans child-directed imagery on the pack (4.1.5).

I work on NutritionFactLabel, an online nutrition label generator, which is where this came from. Happy to compare notes in the comments if you've implemented front-of-pack rules for another country.

Top comments (0)