Skip to content

Commit

Permalink
Show unit name if ends in right parenthesis (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZengLawrence authored Dec 24, 2024
1 parent 07cc9b5 commit cf39357
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 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.32.1",
"version": "2.32.2",
"private": true,
"homepage": ".",
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ quantity
;

unit
: WORD+ | WORD* LEFT_PAREN (WHOLE_NUMBER | WORD | SLASH | PERIOD)+ RIGHT_PAREN
: WORD+ | WORD* LEFT_PAREN (WHOLE_NUMBER | FRACTION | WORD | SLASH | PERIOD)+ RIGHT_PAREN
;

WORD
Expand Down
10 changes: 10 additions & 0 deletions src/features/suggestions/parser/DecomposedFoodDescription.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ test("Given food name and amount with a trailing space, then amount with trailin
amount: "1 cup",
unitCompleted: true,
});
})

test("Given food name and amount with a right parenthesis, then food name completed and unit completed flag are true", () => {
const result = decompose("doughnut 1 (3 1/4 dia.)");
expect(result).toMatchObject({
foodName: "doughnut",
foodNameCompleted: true,
amount: "1 (3 1/4 dia.)",
unitCompleted: true,
});
})
11 changes: 9 additions & 2 deletions src/features/suggestions/parser/DecomposedFoodDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ function isEndedWithSpace(s: string) {
return isSpaceAfter(s, _.size(s) - 1);
}

function isEndedWithRightParenthesis(s: string) {
return s.substring(_.size(s) - 1, _.size(s)) === ")";
}

function isUnitCompleted(foodDescription: string) {
return isEndedWithSpace(foodDescription) || isEndedWithRightParenthesis(foodDescription);
}

export default function decompose(foodDescription: string) {
const { foodName, amount, measurement } = parse(foodDescription);
const unitText = measurement?.unitText;
const foodNameCompleted = isSpaceAfter(foodDescription, _.size(foodName));
const unitCompleted = (amount && unitText) ? isEndedWithSpace(foodDescription) : false;
const unitCompleted = measurement?.unitText ? isUnitCompleted(foodDescription) : false;
return {
foodName,
amount,
Expand Down

0 comments on commit cf39357

Please sign in to comment.