Business Logic
The @running-days/business-logic package contains pure functions for all core calculations. It has no I/O, no database access - just algorithms and transformations.
Philosophy
All business logic is:
- Pure: Same inputs always produce same outputs
- Testable: No external dependencies to mock
- Portable: Can run in Node.js, browser, or edge runtime
Modules
| Module | Description |
|---|---|
| Workout Processor | Parse, validate, and aggregate workout data |
| Streak Calculator | Calculate current, longest, and historical streaks |
| Achievement Service | Milestone detection and progress tracking |
Installation
bash
pnpm add @running-days/business-logicUsage
typescript
import {
processWorkout,
calculateStreaks,
detectNewMilestones
} from '@running-days/business-logic';
// Process a raw workout from HealthKit
const workout = processWorkout(rawWorkout);
// Calculate streaks from daily stats
const streaks = calculateStreaks(dailyStats);
// Check for new achievement milestones
const newMilestones = detectNewMilestones(previousDays, currentDays);Type Safety
All functions are fully typed. Import types from @running-days/types:
typescript
import type { Workout, DailyStats, Goal } from '@running-days/types';Testing
The package has comprehensive unit tests:
bash
pnpm --filter @running-days/business-logic testTests cover:
- Edge cases (timezone boundaries, leap years)
- Invalid input handling
- Performance with large datasets