Finance JavaScript Utilities¶
� Overview¶
This directory contains shared JavaScript utilities for finance-related features across the documentation site. All finance scripts use ES6 modules and import from the central finance_utils.js utility library.
🗂️ File Structure¶
Core Utilities¶
finance_utils.js- Shared utility functions, constants, and icons used across all finance scripts
Feature Scripts¶
credit_card_age.js- Calculates and displays average credit card agefinances_charts.js- Renders Workday payment charts using D3.jscard_gallery.js- Displays credit card image gallery with GLightboxcard_benefits_renderer.js- Renders filterable card benefits tableus_finances_renderer.js- Renders US financial accounts with timelinetw_finances_renderer.js- Renders Taiwan financial accountspdf_modal_handler.js- Handles PDF modal displayutility_bill_table_renderer.js- Renders utility bill tables
Documentation¶
API_REFERENCE.md- Complete API documentation for finance_utils.jsARCHITECTURE_DIAGRAM.md- System architecture and data flow diagrams
🚀 Quick Start¶
Using finance_utils.js in Your Script¶
All finance scripts are ES6 modules. Import what you need from finance_utils.js:
Loading Scripts in HTML/Markdown¶
Scripts must be loaded with type="module" in your markdown files:
📖 API Documentation¶
See API_REFERENCE.md for complete documentation of all available functions, parameters, and usage examples.
🏗️ Architecture¶
Data Flow¶
Module Dependencies¶
�️ Adding New Features¶
Step 1: Create Your Script¶
Create a new JS file in this directory:
Step 2: Load in Markdown¶
Add to your markdown file (e.g., finance.md):
Step 3: Use Shared Utilities¶
Leverage existing utilities instead of duplicating code:
- Use fetchFinanceData() instead of raw fetch
- Use isTaiwanInstitution() for classification
- Use calculateAge() for date calculations
- Use loadIcons() for consistent icon loading
🐛 Debugging & Error Tracing¶
Common Issues¶
1. Import Errors
type="module"
- ✅ Verify import path is relative: './finance_utils.js'
- ✅ Ensure finance_utils.js exists in same directory
2. Data Loading Errors
3. Function Not Found
Browser Console Debugging¶
Each script logs useful information:
Enable verbose logging to trace data flow.
📝 Best Practices¶
Code Style¶
- ✅ Use async/await for asynchronous operations
- ✅ Always wrap in try/catch blocks
- ✅ Provide user-friendly error messages
- ✅ Check for container existence before rendering
Performance¶
- ✅ Use
debounce()for event handlers (resize, scroll, etc.) - ✅ Batch DOM updates using DocumentFragment
- ✅ Cache data when possible (avoid redundant fetches)
Maintainability¶
- ✅ Import shared utilities instead of duplicating code
- ✅ Keep feature scripts focused on their specific task
- ✅ Add comments for complex logic
- ✅ Update API_REFERENCE.md when adding new utilities
📊 Statistics¶
Current State:
- Shared utilities: 20+ functions organized into:
- DOM Utilities (4): getContainer, createImage, initGLightbox, createErrorElement
- Data Filtering (6): filterUSCreditCards, filterUSAccounts, filterTaiwanAccounts, filterSubscriptions, isSubscriptionEntry, cleanSubscriptionName
- Date Utilities (3): parseFinanceDate, parseDayOfMonth, clamp
- Classification (2): isTaiwanInstitution, isIconEntry
- Data Fetching (2): fetchFinanceData, loadIcons
- UI Components (1): showFinanceModal
- Other Utilities (2): debounce, extractLast4
- Date/Age (3): calculateAge, formatDuration, formatDate
- Feature scripts: 6 files fully refactored
- Lines consolidated: ~300+ lines of duplicate code eliminated
- Module architecture: ES6 imports/exports throughout
- Zero syntax errors: All files verified
🔗 Related Resources¶
- Data source:
/docs/Info/finance.json - Main page:
/docs/Info/finance.md - API docs: API_REFERENCE.md
- Architecture: ARCHITECTURE_DIAGRAM.md
Last Updated: January 2025
Maintainer: See repository contributors
Read these files in order:
1. REFACTORING_SUMMARY.md - Start here for overview
2. API_REFERENCE.md - Learn what utilities are available
3. BEFORE_AFTER_COMPARISON.md - See concrete examples
4. REFACTORING_GUIDE.md - Get detailed instructions
3. Choose Your Approach¶
Option A: ES Modules (Recommended)
- Modern, clean, explicit imports
- Better IDE support
- Requires changing script tags to <script type="module">
Option B: Global Script - Simpler, works with existing setup - Functions available globally - Less modern but functional
4. Start Refactoring¶
Begin with the simplest file:
1. credit_card_age.js - Only needs 4 imports, saves 68 lines
2. Test thoroughly
3. Move to next file
4. Repeat
📊 Impact Summary¶
Code Reduction¶
Benefits¶
✅ Immediate - 392 lines of duplicate code eliminated - Single source of truth for common functions - Easier bug fixes (fix once, works everywhere)
✅ Long-term - Faster development (reuse existing utilities) - Better maintainability (centralized updates) - Fewer bugs (less code duplication) - Improved consistency (same behavior everywhere)
Effort Required¶
- Time: ~3.5 hours total
- Difficulty: Easy to Medium
- Risk: Low (no functional changes)
📁 File Structure¶
🚀 Implementation Steps¶
Phase 1: Preparation (30 minutes)¶
- ✅ Read
REFACTORING_SUMMARY.md - ✅ Review
API_REFERENCE.md - ✅ Check
EXAMPLE_credit_card_age_refactored.js - ✅ Decide on ES Modules vs Global Script
Phase 2: Setup (15 minutes)¶
- Update
finance.mdscript tags (if using ES Modules) - Test that
finance_utils.jsloads correctly - Check browser console for any errors
Phase 3: Refactor Files (2 hours)¶
credit_card_age.js(15 min)- Add imports
- Remove duplicate functions
-
Test thoroughly
-
finances_charts.js(10 min) - Import debounce
-
Remove local debounce function
-
card_gallery.js(10 min) - Import extractLast4
-
Remove local function
-
card_benefits_renderer.js(10 min) - Import classification functions
-
Remove duplicates
-
us_finances_renderer.js(30 min) - Import utilities and icons
- Remove all duplicate code
-
Update icon loading
-
tw_finances_renderer.js(30 min) - Import utilities and icons
- Remove all duplicate code
- Update icon loading
Phase 4: Testing (1 hour)¶
- Load finance.md in browser
- Check console for errors
- Verify all cards render correctly
- Test credit card age calculation
- Test US finances rendering
- Test TW finances rendering
- Test card gallery
- Test card benefits table
- Test modals and interactions
- Test on mobile
📝 Available Utilities¶
Classification¶
isTaiwanInstitution()- Check if institution is Taiwan-basedisIconEntry()- Check if item is an icon entry
Icons¶
fallbackIcons- Complete icon libraryloadIcons()- Load icons from JSON with fallback
Date & Time¶
calculateAge()- Calculate age from date stringformatDuration()- Format milliseconds to readable durationformatDate()- Format Date object to YYYY/MM/DD
Data Fetching¶
fetchFinanceData()- Fetch finance.json with error handling
Utilities¶
debounce()- Debounce function callsextractLast4()- Extract last 4 digits from string
Constants¶
MS_PER_DAY- Milliseconds per daySUBSCRIPTION_PREFIX- Subscription regex pattern
🔍 Usage Example¶
❓ FAQ¶
Q: Will this break my existing code?¶
A: No! The refactoring doesn't change any functionality, it just eliminates duplicate code.
Q: Do I have to refactor all files at once?¶
A: No! You can refactor one file at a time and test as you go.
Q: What if I prefer not to use ES modules?¶
A: See Option B in REFACTORING_GUIDE.md for the global script approach.
Q: Can I still add new features?¶
A: Yes! In fact, it's easier now because you only add new utilities once.
Q: What if I find a bug in a shared utility?¶
A: Fix it once in finance_utils.js and it's fixed everywhere automatically!
Q: How do I add a new shared utility?¶
A: Add it to finance_utils.js, export it, and import it where needed.
🎓 Learning Resources¶
- New to ES Modules? → MDN ES Modules Guide
- Want to learn more? → Check out the JSDoc comments in
finance_utils.js - Need examples? → See
EXAMPLE_credit_card_age_refactored.js
🤝 Support¶
If you encounter issues:
1. Check browser console for errors
2. Review REFACTORING_GUIDE.md
3. Compare with EXAMPLE_credit_card_age_refactored.js
4. Verify all imports/exports are correct
✅ Success Checklist¶
After refactoring, you should have: - [ ] No duplicate function definitions - [ ] No duplicate icon definitions - [ ] Imports at the top of each file - [ ] All tests passing - [ ] No console errors - [ ] Same functionality as before - [ ] ~392 lines of code eliminated - [ ] Better maintainability
🎉 Benefits Realized¶
Once complete, you'll enjoy: - 15% less code to maintain - Single source of truth for utilities - Easier debugging - fix once, works everywhere - Faster development - reuse existing code - Better organization - clear separation of concerns - More confidence - consistent behavior across files
📈 Next Steps¶
- Read
REFACTORING_SUMMARY.md - Choose your implementation approach
- Start with
credit_card_age.js - Test thoroughly
- Continue with remaining files
- Enjoy your cleaner, more maintainable code! 🚀
Created: October 2025
Status: Ready to implement
Estimated effort: 3.5 hours
Expected benefits: 392 lines saved, significantly better maintainability