@profullstack/fasting
Version:
A comprehensive CLI and Node.js module for 16:8 intermittent fasting with meal tracking, weight monitoring, and fast history with visual charts
117 lines (92 loc) ⢠3.95 kB
JavaScript
/**
* API usage example for the Fasting App
* Demonstrates programmatic usage of the Node.js module
*/
import {
startFast,
endFast,
getCurrentFast,
getFastHistory,
getFastStats,
logMeal,
logDrink,
getTodaysEntries,
logWeight,
getWeightHistory
} from '../lib/index.js';
console.log('š§ Fasting App - API Usage Example\n');
async function demonstrateAPI() {
try {
// Fast tracking
console.log('š Fast Tracking API:');
console.log('ā'.repeat(30));
// Check if there's already an ongoing fast
let currentFast = getCurrentFast();
if (currentFast) {
console.log('ā ļø Found existing fast, ending it first...');
const completedFast = endFast();
console.log(`ā
Existing fast completed! Duration: ${completedFast.durationHours} hours`);
}
console.log('Starting a new fast...');
const fastStart = startFast();
console.log(`ā
Fast started at: ${new Date(fastStart).toLocaleString()}`);
currentFast = getCurrentFast();
console.log(`š Current fast ID: ${currentFast.id}`);
// Simulate ending the fast after some time
console.log('\nEnding the fast...');
const completedFast = endFast();
console.log(`ā
Fast completed! Duration: ${completedFast.durationHours} hours`);
// Meal and drink logging
console.log('\nš½ļø Meal & Drink Logging API:');
console.log('ā'.repeat(35));
logMeal('Grilled salmon with vegetables', 420);
console.log('ā
Logged meal: Grilled salmon with vegetables (420 cal)');
logDrink('Green smoothie', 180);
console.log('ā
Logged drink: Green smoothie (180 cal)');
logMeal('Greek yogurt with berries', 150);
console.log('ā
Logged meal: Greek yogurt with berries (150 cal)');
const todaysEntries = getTodaysEntries();
console.log(`\nš Today's entries: ${todaysEntries.length} items`);
let totalCalories = 0;
todaysEntries.forEach(entry => {
const time = new Date(entry.timestamp).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
const type = entry.type === 'meal' ? 'š½ļø' : 'š„¤';
console.log(` ${time} ${type} ${entry.description} (${entry.calories} cal)`);
totalCalories += entry.calories || 0;
});
console.log(` Total calories: ${totalCalories}`);
// Weight tracking
console.log('\nāļø Weight Tracking API:');
console.log('ā'.repeat(30));
logWeight(175.5);
console.log('ā
Logged weight: 175.5 lbs');
logWeight(175.2);
console.log('ā
Logged weight: 175.2 lbs');
logWeight(174.8);
console.log('ā
Logged weight: 174.8 lbs');
const weightHistory = getWeightHistory();
console.log(`\nš Weight history: ${weightHistory.length} entries`);
weightHistory.slice(-3).forEach(entry => {
const date = new Date(entry.timestamp).toLocaleDateString();
console.log(` ${date}: ${entry.weight} lbs`);
});
// Fast statistics
console.log('\nš Fast Statistics API:');
console.log('ā'.repeat(30));
const fastStats = getFastStats();
console.log(`Total completed fasts: ${fastStats.totalFasts}`);
console.log(`Average duration: ${fastStats.averageDuration}h`);
console.log(`Longest fast: ${fastStats.longestFast}h`);
console.log(`Shortest fast: ${fastStats.shortestFast}h`);
const fastHistory = getFastHistory();
console.log(`\nš Fast history: ${fastHistory.length} completed fasts`);
console.log('\nš API demonstration completed!');
console.log('\nThis example shows how to use the fasting app programmatically.');
console.log('All functions return data that can be used in your own applications.');
} catch (error) {
console.error('ā Error during API demonstration:', error.message);
process.exit(1);
}
}
demonstrateAPI();