Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions scripts/nutrition-import-loseit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Lose It! food-log export. Calories and nutrients are the totals for the
* logged amount (50 g of oats is 185 kcal), so the amount must not become the
* serving count that the diary multiplies by.
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { parseLoseit } from '../server/lib/nutrition-import/loseit.js';

const CSV = [
'Date,Name,Icon,Type,Quantity,Units,Calories,Deleted,Fat (g),Protein (g),Carbohydrates (g),Saturated Fat (g),Sugars (g),Fiber (g),Cholesterol (mg),Sodium (mg)',
'08/27/2025,Porridge Oats,Cereal,Breakfast,50.0,Grams,185,0,3.90,6,29.50,0.60,0.45,4.40,0,15',
].join('\n') + '\n';

test('the logged amount is the portion, not the number of servings', () => {
const [item] = parseLoseit(CSV);
assert.equal(item.quantity, 1);
assert.equal(item.portion, 50);
assert.equal(item.unit, 'Grams');
assert.equal(item.nutrition.calories, 185);
});
9 changes: 6 additions & 3 deletions server/lib/nutrition-import/loseit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export function parseLoseit(text) {
const calories = parseNumber(getField(row, 'calories'));
if (calories == null) continue;

const quantity = parseNumber(getField(row, 'quantity')) ?? 1;
// Calories and nutrients are the totals for the logged amount, so the item is one
// serving with the amount as its portion (as the other importers do).
const amount = parseNumber(getField(row, 'quantity'));
const unit = getField(row, 'units') || null;

const nutrition = { calories };
Expand All @@ -58,8 +60,9 @@ export function parseLoseit(text) {
mealLabel: getField(row, 'type') || '',
name,
brand: null,
quantity,
portion: unit ? `${quantity} ${unit}` : null,
quantity: 1,
portion: amount,
unit,
nutrition,
notes: null,
sourceRow: row._rowNum,
Expand Down