feat: added support for adding plurality to ingredients

This commit is contained in:
Leyla Becker 2026-02-22 19:53:18 -06:00
parent 750bfb912f
commit 8855d6ba19
9 changed files with 468 additions and 8 deletions

View file

@ -9,6 +9,7 @@
const { findAllMeasurements, unitType } = require('./matcher');
const { matchDensity } = require('./densities');
const { matchPlural } = require('./plurals');
// ─── Number Formatting ──────────────────────────────────
@ -598,6 +599,19 @@ const SERVING_VESSEL_RE = /\b(glass(?:es)?|mugs?|dish(?:es)?|jars?|containers?|h
// ─── Token Replacement ──────────────────────────────────
function countValueForPlurality(amount) {
if (amount.min !== undefined) return amount.max.value; // range: use max
return amount.value;
}
function applyCasing(original, replacement) {
if (!original || !replacement) return replacement;
if (original[0] === original[0].toUpperCase() && original[0] !== original[0].toLowerCase()) {
return replacement[0].toUpperCase() + replacement.slice(1);
}
return replacement;
}
function replaceMeasurementsInToken(token, Token, inToolsSection) {
const text = token.content;
const measurements = findAllMeasurements(text);
@ -616,8 +630,54 @@ function replaceMeasurementsInToken(token, Token, inToolsSection) {
let lastEnd = 0;
for (const m of measurements) {
// Add text before this measurement
if (m.index > lastEnd) {
// Check for noun before count measurement
let nounMatch = null;
if (m.type === 'count') {
const fullTextBefore = text.slice(0, m.index);
nounMatch = matchPlural(fullTextBefore);
// Only use noun match if it falls within the current segment
if (nounMatch && nounMatch.matchStart < lastEnd) nounMatch = null;
}
if (nounMatch) {
const nounStart = nounMatch.matchStart;
const nounEnd = nounStart + nounMatch.matchLength;
// Prefix text (before the noun)
if (nounStart > lastEnd) {
const prefix = new Token('text', '', 0);
prefix.content = text.slice(lastEnd, nounStart);
newTokens.push(prefix);
}
// Determine correct noun form based on count value
const countVal = countValueForPlurality(m.amount);
const form = countVal === 1 ? nounMatch.singular : nounMatch.plural;
const originalNoun = text.slice(nounStart, nounEnd);
const displayNoun = applyCasing(originalNoun, form);
// Open noun span
const nounOpen = new Token('count_noun_open', 'span', 1);
nounOpen.attrSet('class', 'count-noun');
nounOpen.attrSet('data-singular', nounMatch.singular);
nounOpen.attrSet('data-plural', nounMatch.plural);
newTokens.push(nounOpen);
const nounContent = new Token('text', '', 0);
nounContent.content = displayNoun;
newTokens.push(nounContent);
const nounClose = new Token('count_noun_close', 'span', -1);
newTokens.push(nounClose);
// Trailing space between noun and count
if (nounEnd < m.index) {
const trailing = new Token('text', '', 0);
trailing.content = text.slice(nounEnd, m.index);
newTokens.push(trailing);
}
} else if (m.index > lastEnd) {
// Normal case: just add text before measurement
const before = new Token('text', '', 0);
before.content = text.slice(lastEnd, m.index);
newTokens.push(before);
@ -633,6 +693,11 @@ function replaceMeasurementsInToken(token, Token, inToolsSection) {
open.attrSet('data-alt', altText);
open.attrSet('title', m.match);
// Mark count span as having an adjacent noun
if (nounMatch) {
open.attrSet('data-has-noun', 'true');
}
// Add scaling data for weight/volume/count
const scaleData = computeScaleData(m);
if (scaleData) {