Sofondo
SofondoFrameworkDocs

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

npm install @sofondo/core

String Utilities

Functions for manipulating and formatting strings

capitalize

capitalize(str: string): string

Capitalize the first letter of a string

Example

import { capitalize } from '@sofondo/core';

const result = capitalize('hello world');
// Result: "Hello world"

titleCase

titleCase(str: string): string

Convert string to title case

Example

import { titleCase } from '@sofondo/core';

const result = titleCase('hello world');
// Result: "Hello World"

truncate

truncate(str: string, maxLength: number, suffix?: string): string

Truncate a string to a maximum length with optional suffix

Example

import { truncate } from '@sofondo/core';

const result = truncate('This is a long text', 10);
// Result: "This is..."

kebabCase

kebabCase(str: string): string

Convert a string to kebab-case

Example

import { kebabCase } from '@sofondo/core';

const result = kebabCase('HelloWorld');
// Result: "hello-world"

camelCase

camelCase(str: string): string

Convert a string to camelCase

Example

import { camelCase } from '@sofondo/core';

const result = camelCase('hello-world');
// Result: "helloWorld"

snakeCase

snakeCase(str: string): string

Convert a string to snake_case

Example

import { snakeCase } from '@sofondo/core';

const result = snakeCase('HelloWorld');
// Result: "hello_world"

removeWhitespace

removeWhitespace(str: string): string

Remove all whitespace from a string

Example

import { removeWhitespace } from '@sofondo/core';

const result = removeWhitespace('Hello World');
// Result: "HelloWorld"

pluralize

pluralize(word: string, count: number, plural?: string): string

Pluralize a word based on count

Example

import { pluralize } from '@sofondo/core';

const result1 = pluralize('item', 1);
// Result: "item"

const result2 = pluralize('item', 5);
// Result: "items"

const result3 = pluralize('person', 3, 'people');
// Result: "people"

Number Utilities

Functions for formatting and manipulating numbers

formatNumber

formatNumber(num: number, separator?: string): string

Format a number with thousand separators

Example

import { formatNumber } from '@sofondo/core';

const result = formatNumber(1234567);
// Result: "1,234,567"

formatCurrency

formatCurrency(num: number, currency?: string, decimals?: number): string

Format a number as currency

Example

import { formatCurrency } from '@sofondo/core';

const result = formatCurrency(1234.56);
// Result: "$1,234.56"

formatPercentage

formatPercentage(num: number, decimals?: number, isDecimal?: boolean): string

Format a number as a percentage

Example

import { formatPercentage } from '@sofondo/core';

const result1 = formatPercentage(0.75, 1, true);
// Result: "75.0%"

const result2 = formatPercentage(75, 1);
// Result: "75.0%"

formatBytes

formatBytes(bytes: number, decimals?: number): string

Format bytes to human-readable size

Example

import { formatBytes } from '@sofondo/core';

const result = formatBytes(1536);
// Result: "1.50 KB"

clamp

clamp(num: number, min: number, max: number): number

Clamp a number between min and max values

Example

import { clamp } from '@sofondo/core';

const result = clamp(150, 0, 100);
// Result: 100

round

round(num: number, decimals?: number): number

Round a number to specified decimal places

Example

import { round } from '@sofondo/core';

const result = round(3.14159, 2);
// Result: 3.14

randomInt

randomInt(min: number, max: number): number

Generate a random integer between min and max

Example

import { randomInt } from '@sofondo/core';

const result = randomInt(1, 10);
// Result: random number between 1 and 10

getPercentage

getPercentage(value: number, total: number): number

Calculate percentage of a value

Example

import { getPercentage } from '@sofondo/core';

const result = getPercentage(25, 100);
// Result: 25

Date Utilities

Functions for formatting and manipulating dates

formatDate

formatDate(date: Date | string | number, format?: "short" | "medium" | "long" | "full"): string

Format a date to a readable string

Example

import { formatDate } from '@sofondo/core';

const result = formatDate(new Date(), 'medium');
// Result: "Jan 15, 2024"

formatTime

formatTime(date: Date | string | number, format?: "short" | "medium" | "long"): string

Format a date to time string

Example

import { formatTime } from '@sofondo/core';

const result = formatTime(new Date(), 'short');
// Result: "2:30 PM"

getRelativeTime

getRelativeTime(date: Date | string | number, baseDate?: Date): string

Get relative time string (e.g., "2 hours ago")

Example

import { getRelativeTime } from '@sofondo/core';

const yesterday = new Date(Date.now() - 86400000);
const result = getRelativeTime(yesterday);
// Result: "yesterday"

isToday

isToday(date: Date | string | number): boolean

Check if a date is today

Example

import { isToday } from '@sofondo/core';

const result = isToday(new Date());
// Result: true

isYesterday

isYesterday(date: Date | string | number): boolean

Check if a date is yesterday

Example

import { isYesterday } from '@sofondo/core';

const yesterday = new Date(Date.now() - 86400000);
const result = isYesterday(yesterday);
// Result: true

addDays

addDays(date: Date | string | number, days: number): Date

Add days to a date (negative to subtract)

Example

import { addDays } from '@sofondo/core';

const tomorrow = addDays(new Date(), 1);
const lastWeek = addDays(new Date(), -7);

getDaysDifference

getDaysDifference(date1: Date | string | number, date2: Date | string | number): number

Get the difference between two dates in days

Example

import { getDaysDifference } from '@sofondo/core';

const date1 = new Date('2024-01-01');
const date2 = new Date('2024-01-10');
const result = getDaysDifference(date1, date2);
// Result: 9

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 | null

Gets the sidebar collapsed state from storage (cookie or localStorage)

Example

import { getSidebarState } from '@sofondo/react/utils/sidebarStorage';

const collapsed = getSidebarState('auto', 'sidebar:collapsed');
// Returns 'true', 'false', or null

setSidebarState

setSidebarState(strategy: StorageStrategy, storageName: string, value: boolean): void

Sets the sidebar collapsed state in storage

Example

import { setSidebarState } from '@sofondo/react/utils/sidebarStorage';

setSidebarState('auto', 'sidebar:collapsed', true);

getInlineStorageScript

getInlineStorageScript(storageName: string, strategy?: "localStorage" | "auto"): string

Generates an inline script to prevent flash of incorrect sidebar state. Add this to your <head> element

Example

import { getInlineStorageScript } from '@sofondo/react/utils/sidebarStorage';

// In your Astro layout:
<script is:inline set:html={getInlineStorageScript('sidebar:collapsed', 'auto')} />

Next Steps

Combine these utilities with Sofondo components and hooks to build powerful applications.