UNPKG

zen-digital-utils

Version:

A collection of utility functions for JavaScript/TypeScript projects

240 lines (230 loc) 9.96 kB
/** * Interface representing a Brazilian state with its code and name */ interface BrazilianState { /** Two-letter state code (e.g., 'SP', 'RJ') */ value: string; /** Full state name (e.g., 'São Paulo', 'Rio de Janeiro') */ label: string; } declare const brazilianStates: BrazilianState[]; declare function hideScroll(isOpen: boolean): void; /** * Converts a date string in 'YYYY-MM-DD' format to 'DD/MM/YYYY' format * @param thisDate - Date string in 'YYYY-MM-DD' format * @returns Formatted date string in 'DD/MM/YYYY' format */ declare function formattedDate(thisDate: string): string; /** * Formats a date according to the specified format string * @param date - The date to format * @param format - Format string (e.g., 'YYYY-MM-DD', 'DD/MM/YYYY') * @returns Formatted date string */ declare function formatDate(date: Date, format?: string): string; /** * Formats a datetime string by separating date and time with a bullet point * @param dateTimeString - String containing date and time separated by space * @returns Formatted date and time string */ declare function formatDateTime(dateTimeString: string): string; /** * Formats a string as a birthdate (DD/MM/YYYY) * @param value - String to format as birthdate * @returns Formatted birthdate string */ declare function formatBirthdate(value: string): string; /** * Calculates the number of days between two dates * @param startDate - The first date * @param endDate - The second date * @returns The number of days between the two dates (absolute value) */ declare function getDaysBetween(startDate: Date, endDate: Date): number; /** * Checks if a date falls on a weekend (Saturday or Sunday) * @param date - The date to check * @returns True if the date is a weekend, false otherwise */ declare function isWeekend(date: Date): boolean; /** * Capitalizes the first letter of a string * @param text - The string to capitalize * @returns The string with first letter capitalized */ declare function capitalize(text: string): string; /** * Capitalizes the first letter of each word in a string * @param text - The string to capitalize * @returns The string with each word capitalized */ declare function capitalizeText(text: string): string; /** * Converts a string to a URL-friendly slug * @param text - The string to convert to a slug * @returns URL-friendly slug string */ declare function slugify(text: string): string; /** * Truncates a string to a specified length and adds an ellipsis if truncated * @param text - The string to truncate * @param length - Maximum length of the returned string (excluding suffix) * @param suffix - String to append if truncated (default: '...') * @returns Truncated string */ declare function truncate(text: string, length: number, suffix?: string): string; /** * Removes all characters except letters and spaces * @param value - The string to format * @returns String with only letters and spaces */ declare function formatTextOnly(value: string): string; /** * Formats a full name by removing special characters and normalizing spaces * @param value - The full name to format * @returns Formatted full name */ declare function formatFullName(value: string): string; /** * Creates an acronym from a full name (first letter of first name and first letter of last name) * @param fullName - The full name to create an acronym from * @returns Two-letter acronym */ declare function getAcronym(fullName: string): string; /** * Formats a number as a percentage with two decimal places * @param value - The number or string to format * @returns Formatted percentage string */ declare function formatPercent(value: number | string): string; /** * Formats a string as a Brazilian ZIP code (CEP) * @param value - The string to format as a ZIP code * @returns Formatted ZIP code (format: 12345-678) */ declare function formatZipCode(value: string): string; declare function formatCurrencyInput(value: string): string; declare function formatCurrency(value: number | string): string; /** * Formats a number with a custom currency symbol * @param value - The number or string to format * @param currencySymbol - The currency symbol to use (defaults to $) * @returns Formatted currency string */ declare function formatWithCurrencySymbol(value: number | string, currencySymbol?: string): string; /** * Formats a number as currency using internationalization API * @param value - The number to format * @param locale - The locale to use for formatting (defaults to 'pt-BR') * @param currencyCode - The ISO currency code (defaults to 'BRL') * @returns Formatted currency string */ declare function formatInternationalCurrency(value: number | string, locale?: string, currencyCode?: string): string; /** * Formats a number as currency without the currency symbol * @param value - The number or string to format * @returns Formatted number string with decimal and thousands separators */ declare function formatCurrencyWithoutSymbol(value: number | string): string; /** * Formats a number as currency using accounting notation (negative values in parentheses) * @param value - The number or string to format * @returns Formatted currency string in accounting format */ declare function formatAccountingCurrency(value: number | string): string; declare function parseBrazilianCurrency(str: string): number; /** * Converts a currency value to cents (integer) * @param value - The currency value as string or number * @returns Integer value in cents */ declare function toCents(value: string | number): number; declare function formatPhoneNumberForWhatsApp(value: string): string; declare function formatNumber(value: string): string; declare function formatPhoneNumber(value: string): string; declare function formatCountBankNumber(value: string): string; declare function formatBranchNumber(value: string): string; declare function formatBranchNumberWithDefault(value: string): string; declare function formatCpf(value: string): string; /** * Deep merges two objects together * @param target - The target object to merge into * @param source - The source object to merge from * @returns A new object with properties from both objects deeply merged */ declare function deepMerge<T extends object, U extends object>(target: T, source: U): T & U; /** * Creates a new object with only the specified properties * @param obj - The source object * @param keys - Array of keys to pick from the source object * @returns A new object with only the specified properties */ declare function pick<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>; /** * Creates a new object without the specified properties * @param obj - The source object * @param keys - Array of keys to omit from the source object * @returns A new object without the specified properties */ declare function omit<T extends object, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>; /** * Splits an array into chunks of a specified size * @param array - The array to split into chunks * @param size - The size of each chunk * @returns Array of array chunks */ declare function chunk<T>(array: T[], size: number): T[][]; /** * Removes duplicate values from an array * @param array - The array to remove duplicates from * @returns Array with unique values */ declare function unique<T>(array: T[]): T[]; /** * Sorts an array of objects by a specified property * @param array - The array of objects to sort * @param key - The property to sort by * @param direction - Sort direction: 'asc' (default) or 'desc' * @returns Sorted array */ declare function sortBy<T extends Record<string, any>>(array: T[], key: keyof T, direction?: 'asc' | 'desc'): T[]; /** * Groups an array of objects by a specified property * @param array - The array of objects to group * @param key - The property to group by * @returns Object with groups */ declare function groupBy<T extends Record<string, any>, K extends keyof T>(array: T[], key: K): Record<string, T[]>; /** * Flattens a nested array structure by one level * @param array - The array to flatten * @returns Flattened array */ declare function flatten<T>(array: Array<T | T[]>): T[]; /** * Returns an array containing elements present in all provided arrays * @param arrays - Arrays to find common elements from * @returns Array of common elements */ declare function intersection<T>(...arrays: T[][]): T[]; /** * Returns elements from the first array that are not in the second array * @param array - The source array * @param excludeArray - The array of values to exclude * @returns Array with excluded values removed */ declare function difference<T>(array: T[], excludeArray: T[]): T[]; /** * Randomly shuffles elements in an array (Fisher-Yates algorithm) * @param array - The array to shuffle * @returns New shuffled array */ declare function shuffle<T>(array: T[]): T[]; /** * Divides an array into two groups based on a predicate function * @param array - The array to divide * @param predicate - Function that returns true or false for each element * @returns Array containing two subarrays: matches and non-matches */ declare function partition<T>(array: T[], predicate: (item: T) => boolean): [T[], T[]]; export { brazilianStates, capitalize, capitalizeText, chunk, deepMerge, difference, flatten, formatAccountingCurrency, formatBirthdate, formatBranchNumber, formatBranchNumberWithDefault, formatCountBankNumber, formatCpf, formatCurrency, formatCurrencyInput, formatCurrencyWithoutSymbol, formatDate, formatDateTime, formatFullName, formatInternationalCurrency, formatNumber, formatPercent, formatPhoneNumber, formatPhoneNumberForWhatsApp, formatTextOnly, formatWithCurrencySymbol, formatZipCode, formattedDate, getAcronym, getDaysBetween, groupBy, hideScroll, intersection, isWeekend, omit, parseBrazilianCurrency, partition, pick, shuffle, slugify, sortBy, toCents, truncate, unique };