baldav/src/parser.js
4aiman b26264538b GPS point ordering + GPX export + chart zoom fixes
GPS background recording:
- Batched writes (10s flush) in native service
- Polling lock prevents parallel processing
- Track by timestamp instead of line count
- Recovery idempotent via max DB timestamp
- Seq numbers on live recorded points

GPX export:
- Button in session detail
- SAF file picker on Android (ACTION_CREATE_DOCUMENT)
- GPS config in metadata (accuracy, minSpeed, minDist)

Charts:
- Pinch zoom enabled at creation (Hammer.js registers handlers)
- touch-action: pan-y blocks pinch in normal view (page scrolls)
- Fullscreen: CSS-only expand (no canvas manipulation)
- Downsampled to 500 points for smooth rendering
- Back gesture collapses fullscreen before closing modal

Build: --real flag finds first non-emulator device
2026-05-16 21:21:13 +03:00

112 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Parse free-text amount input into grams
// Handles: "200г", "половина пачки 400г", "2 ст.л.", "3 куска", etc.
import { MEASURE_PATTERNS } from "./measures.js";
// Extract a number near a gram-like unit
const GRAMS_RE = /(\d+(?:[.,]\d+)?)\s*(г|гр|грамм|g)\b/i;
// Extract fraction of a package
const FRACTION_PACK_RE = /(?:полов\w*|пол|1\/2)\s*(?:пачк\w*|упаков\w*|банк\w*)/i;
const THIRD_PACK_RE = /(?:треть|1\/3)\s*(?:пачк\w*|упаков\w*|банк\w*)/i;
const QUARTER_PACK_RE = /(?:четверт\w*|1\/4)\s*(?:пачк\w*|упаков\w*|банк\w*)/i;
const PACK_WEIGHT_RE = /(\d+)\s*(г|гр|грамм|g)\b/i;
// Count of items
const COUNT_RE = /(\d+)\s*(кус\w*|штук\w*|шт\.?)\b/i;
// Duration for activities (minutes/hours)
const DURATION_MIN_RE = /(\d+)\s*(мин\w*|m)\b/i;
const DURATION_HR_RE = /(\d+(?:[.,]\d+)?)\s*(час\w*|ч\.?|h)\b/i;
// Just a bare number
const BARE_NUMBER_RE = /^(\d+(?:[.,]\d+)?)$/;
export function parseAmount(amountStr, description) {
if (!amountStr || !amountStr.trim()) {
return { grams: null, minutes: null, count: null, source: "default" };
}
const text = amountStr.trim().toLowerCase();
// 1. Exact grams: "200г"
let m = text.match(GRAMS_RE);
if (m) {
return { grams: parseFloat(m[1].replace(",", ".")), minutes: null, count: null, source: "grams" };
}
// 2. Fraction of package: "половина пачки 400г"
const combined = (amountStr + " " + (description || "")).toLowerCase();
if (FRACTION_PACK_RE.test(combined)) {
const pw = combined.match(PACK_WEIGHT_RE);
if (pw) {
return { grams: parseFloat(pw[1]) / 2, minutes: null, count: null, source: "half-pack" };
}
}
if (THIRD_PACK_RE.test(combined)) {
const pw = combined.match(PACK_WEIGHT_RE);
if (pw) {
return { grams: Math.round(parseFloat(pw[1]) / 3), minutes: null, count: null, source: "third-pack" };
}
}
if (QUARTER_PACK_RE.test(combined)) {
const pw = combined.match(PACK_WEIGHT_RE);
if (pw) {
return { grams: parseFloat(pw[1]) / 4, minutes: null, count: null, source: "quarter-pack" };
}
}
// 3. Duration (for activities): "30 мин", "1.5 часа"
m = text.match(DURATION_HR_RE);
if (m) {
return { grams: null, minutes: Math.round(parseFloat(m[1].replace(",", ".")) * 60), count: null, source: "hours" };
}
m = text.match(DURATION_MIN_RE);
if (m) {
return { grams: null, minutes: parseInt(m[1]), count: null, source: "minutes" };
}
// 4. Household measure: "2 ст.л." or just "ст.л."
for (const mp of MEASURE_PATTERNS) {
if (mp.pattern.test(text)) {
// Check for a multiplier before the measure
const before = text.split(mp.pattern)[0].trim();
const multiplier = before.match(/(\d+)/) ? parseInt(before.match(/(\d+)/)[1]) : 1;
return { grams: mp.grams * multiplier, minutes: null, count: null, source: "measure" };
}
}
// 5. Count: "3 куска", "2 шт"
m = text.match(COUNT_RE);
if (m) {
return { grams: null, minutes: null, count: parseInt(m[1]), source: "count" };
}
// 6. Bare number → assume grams
m = text.match(BARE_NUMBER_RE);
if (m) {
return { grams: parseFloat(m[1].replace(",", ".")), minutes: null, count: null, source: "bare-grams" };
}
return { grams: null, minutes: null, count: null, source: "unknown" };
}
// Estimate kcal from parsed amount + food data
export function estimateKcal(parsed, food) {
if (!food) return null;
if (parsed.grams != null) {
return Math.round((food.kcal / 100) * parsed.grams);
}
if (parsed.count != null) {
return Math.round((food.kcal / 100) * food.portion * parsed.count);
}
// Use default portion
return Math.round((food.kcal / 100) * food.portion);
}