Quick Reference: finance_utils.js API¶
Import Statement¶
createImage(src, alt, className)¶
Creates an image element with standard attributes.
Parameters:
- src (string) - Image source URL
- alt (string) - Alt text (default: '')
- className (string) - CSS class name (default: '')
Returns: - (HTMLImageElement) - Configured image element
Example:
initGLightbox(selector)¶
Initializes GLightbox with error handling.
Parameters:
- selector (string) - CSS selector for lightbox elements (default: '.glightbox')
Returns: - (void)
Example:
createErrorElement(message, type)¶
Creates a styled error/info message element.
Parameters:
- message (string) - Message text
- type (string) - Message type: 'error' or 'info' (default: 'error')
Returns: - (HTMLElement) - Styled message element
Example:
Data Filtering Functions¶
filterUSCreditCards(data)¶
Filters for US credit cards only.
Parameters:
- data (Array) - Finance data array
Returns: - (Array) - Filtered US credit cards
Example:
filterUSAccounts(data)¶
Filters for US bank and saving accounts.
Parameters:
- data (Array) - Finance data array
Returns: - (Array) - Filtered US accounts
Example:
filterTaiwanAccounts(data)¶
Filters for Taiwan accounts (all types).
Parameters:
- data (Array) - Finance data array
Returns: - (Array) - Filtered Taiwan accounts
Example:
filterSubscriptions(data)¶
Filters for US subscription entries.
Parameters:
- data (Array) - Finance data array
Returns: - (Array) - Filtered subscriptions
Example:
isSubscriptionEntry(item)¶
Checks if an entry is a subscription.
Parameters:
- item (Object) - Data item
Returns: - (boolean) - True if item is a subscription
Example:
cleanSubscriptionName(name)¶
Removes subscription prefix from name.
Parameters:
- name (string) - Subscription name
Returns: - (string) - Cleaned name
Example:
Date Utilities¶
parseFinanceDate(dateString)¶
Parses date string in MM/DD/YYYY or YYYY-MM-DD format.
Parameters:
- dateString (string) - Date string
Returns: - (Date|null) - Date object or null if invalid
Example:
parseDayOfMonth(value)¶
Parses day of month from various formats.
Parameters:
- value (string|number) - Day value
Returns: - (number) - Day of month (1-31)
Example:
clamp(value, min, max)¶
Clamps a value between min and max.
Parameters:
- value (number) - Value to clamp
- min (number) - Minimum value
- max (number) - Maximum value
Returns: - (number) - Clamped value
Example:
Classification Functions¶
isTaiwanInstitution(name)¶
Determines if an institution is Taiwan-based by checking for non-ASCII characters.
Parameters:
- name (string) - Institution name
Returns: - (boolean) - True if institution is Taiwan-based
Example:
isIconEntry(item)¶
Checks if a data item is an icon entry.
Parameters:
- item (Object) - Data item to check
Returns: - (boolean) - True if item is an icon entry
Example:
Icon Management¶
fallbackIcons¶
Object containing Material Design icon SVG strings for all card networks.
Available Icons:
- bank
- creditCard
- creditCardCheck
- chartAreaspline
- mastercard
- visa
- amex
- discover
- jcb
Example:
loadIcons(data)¶
Loads icons from JSON data or falls back to default icons.
Parameters:
- data (Array) - JSON data array
Returns: - (Object) - Icons object
Example:
Date & Age Calculations¶
calculateAge(dateString)¶
Calculates the age from a date string in YYYY/MM/DD format.
Parameters:
- dateString (string) - Date in YYYY/MM/DD format
Returns: - (string) - Formatted age string (e.g., "Age: 2 years, 3 months, 5 days")
Example:
formatDuration(ms)¶
Formats a duration in milliseconds to years, months, days.
Parameters:
- ms (number) - Duration in milliseconds
Returns: - (string) - Formatted duration string
Example:
formatDate(date)¶
Formats a Date object as YYYY/MM/DD.
Parameters:
- date (Date) - Date object
Returns: - (string) - Formatted date string
Example:
Fetch Utilities¶
fetchFinanceData(path)¶
Fetches and parses finance.json with error handling.
Parameters:
- path (string) - Path to finance.json (default: './finance.json')
Returns:
- (Promise
Example:
UI Components¶
showFinanceModal(contentHtml, options)¶
Shows a modal dialog with financial details. Creates a dismissible modal overlay with the provided HTML content.
Parameters:
- contentHtml (string) - HTML content to display in the modal
- options (Object) - Optional configuration
- maxWidth (string) - Maximum width of modal (default: '450px')
- fontSize (string) - Font size of content (default: '1.5em')
Returns: - (void)
Features: - Responsive overlay with backdrop blur - Dismissible via close button, clicking overlay, or pressing Escape key - Theme-aware styling (respects dark/light mode) - Prevents duplicate modals - Proper cleanup of event listeners
Example:
Utility Functions¶
debounce(func, wait, immediate)¶
Debounce function to limit the rate at which a function can fire.
Parameters:
- func (Function) - Function to debounce
- wait (number) - Wait time in milliseconds
- immediate (boolean) - Whether to call on leading edge
Returns: - (Function) - Debounced function
Example:
extractLast4(str)¶
Extracts the last 4 digits from a string (card/account number).
Parameters:
- str (string) - Input string
Returns: - (string) - Last 4 digits
Example:
Constants¶
MS_PER_DAY¶
Milliseconds per day constant.
Value: 24 * 60 * 60 * 1000 = 86400000
Example:
SUBSCRIPTION_PREFIX¶
Regular expression pattern for subscription entries.
Value: /^\[subscription\]\s*/i
Example:
Common Usage Patterns¶
Pattern 1: Fetching and Processing Data¶
Pattern 2: Displaying Card Age¶
Pattern 3: Debounced Event Handler¶
Migration Checklist¶
When refactoring a file:
- Add import statement at the top
- Remove duplicate function definitions
- Replace local function calls with imported ones
- Remove duplicate icon definitions
- Test thoroughly
- Check console for errors
- Verify all features work