-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prevent missing entries in submission
- Loading branch information
1 parent
fb7d14a
commit a25efd7
Showing
4 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { NutrimentPrediction } from "./insight.types"; | ||
|
||
function hasValue(v: Pick<NutrimentPrediction, "value" | "unit">) { | ||
return v && v.value !== null && v.value !== undefined && v.value !== ""; | ||
} | ||
export function doesNotRemoveData({ | ||
nutrimentId, | ||
values, | ||
nutriments, | ||
category, | ||
}: { | ||
nutrimentId: string; | ||
values: Record<string, Pick<NutrimentPrediction, "value" | "unit">>; | ||
nutriments?: any; | ||
category: "serving" | "100g"; | ||
}) { | ||
if (nutriments === undefined) { | ||
return true; | ||
} | ||
if (hasValue(values[`${nutrimentId}_${category}`])) { | ||
// The form has value for this nutrient | ||
return true; | ||
} | ||
if ( | ||
nutriments?.[`${nutrimentId}_serving`] == null && | ||
nutriments?.[`${nutrimentId}_100g`] == null | ||
) { | ||
// OFF does not have value for this nutrient. | ||
// We check 100g and serving, to avoid validating empty serving if 100g are defined. | ||
return true; | ||
} | ||
|
||
// Exceptions for sodium if we have value for the sodium or the inverse | ||
if (nutrimentId === "sodium" && hasValue(values[`salt_${category}`])) { | ||
return true; // no sodium, but salt | ||
} | ||
if (nutrimentId === "salt" && hasValue(values[`sodium_${category}`])) { | ||
return true; // no salt but sodium | ||
} | ||
return false; | ||
} |