Skip to content

Commit

Permalink
Match prep method on search (#228)
Browse files Browse the repository at this point in the history
If prep method is included after unit e.g. "2 cup chopped or 1 cup
cooked", search should match on the prep method.
  • Loading branch information
ZengLawrence authored Nov 2, 2024
1 parent 9694e13 commit 7d6828c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "diet-diary",
"version": "2.26.4",
"version": "2.27.0",
"private": true,
"homepage": ".",
"dependencies": {
Expand Down
36 changes: 23 additions & 13 deletions src/features/suggestions/generate/calculateServing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@ import { multiply } from '../../../model/servingFunction';
import parseAmount, { DecomposedAmount } from '../parser/DecomposedAmount';
import convert, { isMeasurementConvertible, Unit } from '../convert';

function measurementFor(unit: Unit, { measurement, alternateMeasurement }: DecomposedAmount) {
if (unit === "unknown") {
if (alternateMeasurement && alternateMeasurement.unit === "unknown") {
return alternateMeasurement;
function measurementFor(unit: Unit, { measurement, alternateMeasurement }: DecomposedAmount, prepMethod?: string) {
const isUnitConvertibleTo = _.partial(isMeasurementConvertible, unit);
const allMeasurements = alternateMeasurement ? [measurement, alternateMeasurement] : [measurement];
const canBeConverted = _.filter(allMeasurements, isUnitConvertibleTo);

if (prepMethod) {
const containsPrepMethod = (m: typeof measurement) => {
const words = _.words(_.lowerCase(m.unitText));
return _.includes(words, prepMethod);
}
return measurement;
};
const matchedPrepMethod = _.filter(canBeConverted, containsPrepMethod);
if (matchedPrepMethod) {
return _.defaultTo(_.head(matchedPrepMethod), measurement);
}
}
return _.defaultTo(_.head(canBeConverted), measurement);
}

const isUnitConvertibleTo = _.partial(isMeasurementConvertible, unit);
if (isUnitConvertibleTo(measurement)) {
return measurement;
} else if (alternateMeasurement && isUnitConvertibleTo(alternateMeasurement)) {
return alternateMeasurement;
function prepMethod(unitText: string | undefined) {
const words = _.words(_.lowerCase(unitText));
if (_.size(words) > 1) {
return words[1];
} else {
return undefined;
}
return measurement;
}

function servingFor(unitServing: Serving, servingAmount: DecomposedAmount, amount: string) {
const from = parseAmount(amount).measurement;
const to = measurementFor(from.unit, servingAmount);
const to = measurementFor(from.unit, servingAmount, prepMethod(from.unitText));

try {
const normalizedQuantity = convert(from.quantity, from.unit, to.unit);
Expand Down
25 changes: 25 additions & 0 deletions src/features/suggestions/generate/generateSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,28 @@ test("diameter size calculation", () => {
generateSuggestions(new MockRefObject("cheese pizza 1/8 of 12-inch "), assert);

})

test("use prep method -- word after unit e.g. 1 cup cooked to determine the measurement", () => {
const assert = (suggestions: Suggestion[]) => {
expect(_.size(suggestions)).toBeGreaterThanOrEqual(2);
// the input
expect(suggestions[0]).toEqual(
{
foodName: "cabbage, green or red",
amount: "1 cup cooked",
}
);
expect(suggestions[1]).toMatchObject(
{
foodName: "cabbage, green or red",
amount: "1 cup cooked",
serving: {
"vegetable": 2,
}
}
);

}
generateSuggestions(new MockRefObject("cabbage, green or red 1 cup cooked"), assert);

})

0 comments on commit 7d6828c

Please sign in to comment.