Utility Functions
Comprehensive collection of utility functions for common programming tasks
These utility functions are framework-agnostic and can be used in any JavaScript or TypeScript project. All functions are fully typed and tree-shakeable for optimal bundle size.
Installation
String Utilities
Functions for manipulating and formatting strings
capitalize
capitalize(str: string): stringCapitalize the first letter of a string
Example
titleCase
titleCase(str: string): stringConvert string to title case
Example
truncate
truncate(str: string, maxLength: number, suffix?: string): stringTruncate a string to a maximum length with optional suffix
Example
kebabCase
kebabCase(str: string): stringConvert a string to kebab-case
Example
camelCase
camelCase(str: string): stringConvert a string to camelCase
Example
snakeCase
snakeCase(str: string): stringConvert a string to snake_case
Example
removeWhitespace
removeWhitespace(str: string): stringRemove all whitespace from a string
Example
pluralize
pluralize(word: string, count: number, plural?: string): stringPluralize a word based on count
Example
Number Utilities
Functions for formatting and manipulating numbers
formatNumber
formatNumber(num: number, separator?: string): stringFormat a number with thousand separators
Example
formatCurrency
formatCurrency(num: number, currency?: string, decimals?: number): stringFormat a number as currency
Example
formatPercentage
formatPercentage(num: number, decimals?: number, isDecimal?: boolean): stringFormat a number as a percentage
Example
formatBytes
formatBytes(bytes: number, decimals?: number): stringFormat bytes to human-readable size
Example
clamp
clamp(num: number, min: number, max: number): numberClamp a number between min and max values
Example
round
round(num: number, decimals?: number): numberRound a number to specified decimal places
Example
randomInt
randomInt(min: number, max: number): numberGenerate a random integer between min and max
Example
getPercentage
getPercentage(value: number, total: number): numberCalculate percentage of a value
Example
Date Utilities
Functions for formatting and manipulating dates
formatDate
formatDate(date: Date | string | number, format?: "short" | "medium" | "long" | "full"): stringFormat a date to a readable string
Example
formatTime
formatTime(date: Date | string | number, format?: "short" | "medium" | "long"): stringFormat a date to time string
Example
getRelativeTime
getRelativeTime(date: Date | string | number, baseDate?: Date): stringGet relative time string (e.g., "2 hours ago")
Example
isToday
isToday(date: Date | string | number): booleanCheck if a date is today
Example
isYesterday
isYesterday(date: Date | string | number): booleanCheck if a date is yesterday
Example
addDays
addDays(date: Date | string | number, days: number): DateAdd days to a date (negative to subtract)
Example
getDaysDifference
getDaysDifference(date1: Date | string | number, date2: Date | string | number): numberGet the difference between two dates in days
Example
Sidebar Storage Utilities (Advanced)
Advanced utilities for managing sidebar state persistence and preventing FOUC (Flash of Unstyled Content). For practical examples and implementation guide, see the Astro Helpers page.
getSidebarState
getSidebarState(strategy: StorageStrategy, storageName: string): string | nullGets the sidebar collapsed state from storage (cookie or localStorage)
Example
setSidebarState
setSidebarState(strategy: StorageStrategy, storageName: string, value: boolean): voidSets the sidebar collapsed state in storage
Example
getInlineStorageScript
getInlineStorageScript(storageName: string, strategy?: "localStorage" | "auto"): stringGenerates an inline script to prevent flash of incorrect sidebar state. Add this to your <head> element
Example
Next Steps
Combine these utilities with Sofondo components and hooks to build powerful applications.