UNPKG

@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
#!/usr/bin/env node /** * 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();