@candourorg/utils
v1.0.64
Published
Candour utilities.
Keywords
Readme
Candour Utilities Library
A utility library with functions for string manipulation, date formatting, number conversion, and more.
Installation
Install the library using npm or yarn:
bun add @candourorg/utilsyarn add @candourorg/utilsnpm install @candourorg/utilsExamples of How to Use Functions from the Candour Utilities Library
1. CapitalizeFirstLetter
Description: Capitalizes the first letter of a string.
Example Usage:
import { CapitalizeFirstLetter } from '@candourorg/utils';
const text = 'hello world';
const capitalized = CapitalizeFirstLetter(text);
console.log(capitalized); // Output: "Hello world"2. FormatDate
Description: Formats a date string into the format DD-MMM-YYYY.
Example Usage:
import { FormatDate } from '@candour-utils/react';
const date = '2025-01-21';
const formattedDate = FormatDate(date);
console.log(formattedDate); // Output: "21-Jan-2025"3. FormatDateWithTime
Description: Formats a date string into the format DD-MMM-YYYY, HH:MM AM/PM.
Example Usage:
import { FormatDateWithTime } from '@candour-utils/react';
const date = '2025-01-21T14:30:00';
const formattedDateWithTime = FormatDateWithTime(date);
console.log(formattedDateWithTime); // Output: "21-Jan-2025, 02:30 PM"Conversion Functions
Conversion utility functions:
CapitalizeFirstLetter Capitalizes the first letter of a given string.
ConvertDateWithTimeStamp Converts a timestamp into a formatted date string.
ConvertNumberToKFormat Converts large numbers to a shortened "K" format (e.g.,
1200→1.2K).FormatDate Formats a date object or string into a human-readable date.
FormatDateWithTime Formats a date object or string, including the time.
FormatDateRange Formats date range e.g
paymentStartDatein payslip.FormatDocumentType Converts a document type code to a human-readable string.
FormatNumberWithCommas This function convert numbers to string format and add commas to a number for better readability. You can optionally pass
currencySymbolas second argument if you want to prefix currency before the number. You an equally pass a third argumentwithSpaceif you want to put space between the currencySymbol and the number. (e.g.,1000→1,000) | (1000→$1,000) | (1000→$ 1,000)HexToRgba Converts a hexadecimal color code to an RGBA format.
MaskEmail Masks an email address, hiding part of it (e.g.,
[email protected]→u***@example.com).SterilizeNumberInputs Removes invalid characters from number inputs.
UserInitials Extracts the initials from a user's full name.
truncateToTwoDecimals formats a floating point number to two decimal places
truncateText truncate text and a add '...' (e.g,.
Interdominational->interdom...).BackgroundColor Provides utility for determining or managing background colors.
CheckPathname Checks if a given pathname matches specific patterns or criteria.
Encrypt Encrypts sensitive data for secure storage or transfer.
Decrypt Decrypts previously encrypted data.
ObfuscateValues Obfuscates sensitive values to prevent unauthorized access.
DeobfuscateValues Reverses the obfuscation process to retrieve the original values.
ExtractDocumentType Extracts the document type from a given input.
GenerateRandomKey Generates a random key for various use cases.
HandleEnterKeyDown Handles the
Enterkey press for specific user input scenarios.HandleOTPKeyDown Manages key events for one-time password (OTP) inputs.
HandleInputChange Handles changes in input fields, especially for OTP-like input systems.
HandlePaste Handles paste events for input fields, ensuring proper formatting and data.
GetStatusClasses Provides utility for retrieving status-specific CSS classes.
GetStatusText Retrieves the text representation of a specific status.
Status A type definition for status labels.
UserDataProps A type definition for user data properties.
formatSlugName formats slug name and replace whitespace with -
formatChartsNumber Formats chart number
createInitials This function creates business name initial
getTokenExpiration Helper function to decode a JWT and get its expiration time in seconds
formatTokenExpiration Helper function to convert seconds into days, hours, minutes, and seconds
getCurrencySymbol This function helps to get the currency symbol
getTaxFields This function helps to display the correct country tax fields depending on the country pass to it
countriesArray This is an array of countries with currency symbol and country code
countryCurrencies This is an array of contries with their currency code, currency name, currency symbol
countriesCurrenciesArray This function is used to map through the
countryCurrenciesarray and modify to return another arrays of object label, value and countryGetOrdinalWord Converts a number into its ordinal word form (e.g.,
1→First).formatEmployeeTaxDetailsLabel Format employee tax details label (e.g.,
taxIdentificationNumber→Tax identification number).getBankCodeField Maps a currency symbol to its corresponding bank code field (e.g., £ → sortCode).
formatEmployeeTaxDetails Converts an array of tax details into a key-value object with camelCase keys.
toCamelCaseTitle Converts a space-separated string into camelCase format.
toCamelCase Checks if a string is already in camelCase; if not, converts it.
isValuePresent
Checks if a value is notnull,undefined, or an empty string.getCurrencyLabelAndCode
Returns the currency label and corresponding bank code based on the provided payment currency.
