Utilities for formatting various data types into human-readable strings.
Converts a number of bytes into a human-readable file size string.
Parameters:
bytes(number): Number of bytesdecimals(number, optional): Number of decimal places (default: 2)
Returns: The formatted size string (e.g. '1.24 MB')
Example:
import { formatBytes } from '@atlas/ui';
formatBytes(1024); // '1.00 KB'
formatBytes(1536, 1); // '1.5 KB'
formatBytes(1073741824); // '1.00 GB'
formatBytes(0); // '0 Bytes'Formats a date string or Date object into a localized date string.
Parameters:
utcDatetime(string | Date): The date to formatuserTimezone(string, optional): User's timezone (default: 'UTC')locale(string, optional): Locale string (default: 'en-US')
Returns: The formatted date string
Example:
import { formatDate } from '@atlas/ui';
formatDate('2023-01-15T12:00:00'); // '1/15/2023'
formatDate('2023-01-15T12:00:00', 'America/New_York'); // '1/15/2023'
formatDate(new Date(2023, 0, 15), 'Europe/London', 'en-GB'); // '15/01/2023'Formats a date string or Date object into a localized date and time string.
Parameters:
utcDatetime(string | Date): The date to formatuserTimezone(string, optional): User's timezone (default: 'UTC')locale(string, optional): Locale string (default: 'en-US')
Returns: The formatted date and time string
Example:
import { formatDatetime } from '@atlas/ui';
formatDatetime('2023-01-15T12:00:00'); // '1/15/2023, 12:00 PM'
formatDatetime('2023-01-15T12:00:00', 'America/New_York'); // '1/15/2023, 7:00 AM'
formatDatetime(new Date(2023, 0, 15, 12), 'Europe/London', 'en-GB'); // '15/01/2023, 12:00 pm'Converts a YYYY-MM-DD date string into MM/DD/YYYY format.
Parameters:
dateString(string): Date string inYYYY-MM-DDformat
Returns: The formatted date string
Example:
import { formatYmdDate } from '@atlas/ui';
formatYmdDate('2023-12-05'); // '12/05/2023'
formatYmdDate('2023-01-01'); // '01/01/2023'Formats a number with thousands separators and optional decimal places.
Parameters:
n(string | number): The number to formatdecimals(number, optional): Number of decimal places (default: 0)
Returns: The formatted number string
Example:
import { formatNumber } from '@atlas/ui';
formatNumber(1000); // '1,000'
formatNumber(1234.56, 2); // '1,234.56'
formatNumber('5000'); // '5,000'
formatNumber('abc'); // 'Invalid'Formats a numeric decimal into a percentage string.
Parameters:
value(number): The decimal value (e.g. 0.25 = 25%)decimals(number, optional): Number of decimal places (default: 0)
Returns: The formatted percentage string
Example:
import { formatPercentage } from '@atlas/ui';
formatPercentage(0.25); // '25%'
formatPercentage(0.3333, 2); // '33.33%'
formatPercentage(1); // '100%'
formatPercentage(0); // '0%'Converts a string into a safe, lowercase slug.
Parameters:
str(string): The input string
Returns: A hyphenated slug (e.g. 'my-post-title')
Example:
import { formatSlug } from '@atlas/ui';
formatSlug('Hello World'); // 'hello-world'
formatSlug('My Blog Post!'); // 'my-blog-post'
formatSlug('Café & Résumé'); // 'cafe-resume'
formatSlug(' Multiple Spaces '); // 'multiple-spaces'Normalizes and ensures a URL includes a valid protocol.
Parameters:
url(string): Input string (e.g. 'google.com')https(boolean, optional): Force HTTPS (default: false)
Returns: A valid HTTP(S) URL (e.g. 'https://google.com')
Example:
import { formatValidURL } from '@atlas/ui';
formatValidURL('google.com'); // 'http://google.com'
formatValidURL('google.com', true); // 'https://google.com'
formatValidURL('http://example.com'); // 'http://example.com'
formatValidURL('http://example.com', true); // 'https://example.com'Formats a US-based phone number from 10 digits into (999) 999-9999 format. If the
input includes a leading 1 (e.g. 19995551234), the extra digit is ignored. An
optional flag can prefix the result with +1.
Parameters:
phoneNumber(string | number): The phone number to format (10 digits or 11 digits starting with1)withCountryCode(boolean, optional): Prefix the formatted number with+1(default:false)
Returns: The formatted phone number string (e.g. (123) 456-7890 or +1 (123) 456-7890)
Example:
import { formatUSPhoneNumber } from '@atlas/ui';
formatUSPhoneNumber('1234567890'); // '(123) 456-7890'
formatUSPhoneNumber(9876543210, true); // '+1 (987) 654-3210'
formatUSPhoneNumber('19998887777'); // '(999) 888-7777'Utilities for validating data types and formats.
Checks if a value is empty (null, undefined, empty string, empty array, or empty object).
Parameters:
value(any): The value to check
Returns: Boolean indicating if the value is empty
Example:
import { isEmpty } from '@atlas/ui';
isEmpty(''); // true
isEmpty(null); // true
isEmpty(undefined); // true
isEmpty([]); // true
isEmpty({}); // true
isEmpty('text'); // false
isEmpty([1, 2, 3]); // false
isEmpty({ key: 'value' }); // falseChecks if a value is a valid number or numeric string.
Parameters:
value(any): The value to check
Returns: Boolean indicating if the value is numeric
Example:
import { isNumeric } from '@atlas/ui';
isNumeric(123); // true
isNumeric('123'); // true
isNumeric('123.45'); // true
isNumeric('-123'); // true
isNumeric('abc'); // false
isNumeric('123abc'); // false
isNumeric(null); // falseChecks if a string is a valid email address format.
Parameters:
email(string): The email string to validate
Returns: Boolean indicating if the string is a valid email format
Example:
import { isValidEmail } from '@atlas/ui';
isValidEmail('user@example.com'); // true
isValidEmail('name.surname@domain.co'); // true
isValidEmail('email@subdomain.domain.com'); // true
isValidEmail('plainaddress'); // false
isValidEmail('email@domain'); // false
isValidEmail('@domain.com'); // falseChecks if a string is a valid URL format.
Parameters:
url(string): The URL string to validate
Returns: Boolean indicating if the string is a valid URL format
Example:
import { isValidURL } from '@atlas/ui';
isValidURL('https://google.com'); // true
isValidURL('http://subdomain.domain.co.uk'); // true
isValidURL('https://domain.com/path?query=123'); // true
isValidURL('domain.com'); // false (no protocol)
isValidURL('https://'); // false (no domain)
isValidURL('not a url'); // falseDetects if the code is running in a client/browser environment (as opposed to server-side).
Parameters: None
Returns: Boolean indicating if running in a browser
Example:
import { isClient } from '@atlas/ui';
// Only execute browser-specific code when in the browser
if (isClient) {
window.addEventListener('resize', handleResize);
}Returns a random element from an array.
Parameters:
items(any[]): Array of items
Returns: A random element from the array or undefined for empty arrays
Example:
import { getRandomItem } from '@atlas/ui';
const numbers = [1, 2, 3, 4];
getRandomItem(numbers); // 1 | 2 | 3 | 4Rounds a number to a specified number of decimal places.
Parameters:
num(number): The number to rounddecimals(number, optional): Decimal places (default: 2)
Returns: The rounded number
Example:
import { roundTo } from '@atlas/ui';
roundTo(1.005, 2); // 1.01
roundTo(123.456, 1); // 123.5Determines whether the current Inertia page URL matches a given path. This utility returns false during server-side rendering to avoid accessing browser-only globals.
Parameters:
itemPath(string): The path to compare against the current page URL.itemParent(string, optional): Parent path that overridesitemPath.eq(boolean, optional): If true, requires an exact match; otherwise checks that the current path starts with the route path.
Returns: Boolean indicating whether the current page is active.
Example:
import { isPageActive } from '@atlas/ui';
isPageActive('/users'); // true when current URL begins with '/users'
isPageActive('/users', undefined, true); // true only when URL is exactly '/users'
isPageActive('/profile', '/users'); // checks against '/users'