Candour utilities.
npm install @candourorg/utilsA utility library with functions for string manipulation, date formatting, number conversion, and more.
---
Install the library using npm or yarn:
``bash`
bun add @candourorg/utils
`bash`
yarn add @candourorg/utils
`bash`
npm install @candourorg/utils
---
Description: Capitalizes the first letter of a string.
`typescript
import { CapitalizeFirstLetter } from '@candourorg/utils';
const text = 'hello world';
const capitalized = CapitalizeFirstLetter(text);
console.log(capitalized); // Output: "Hello world"
`
Description: Formats a date string into the format DD-MMM-YYYY.
`typescript
import { FormatDate } from '@candour-utils/react';
const date = '2025-01-21';
const formattedDate = FormatDate(date);
console.log(formattedDate); // Output: "21-Jan-2025"
`
Description: Formats a date string into the format DD-MMM-YYYY, HH:MM AM/PM.
`typescript
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 utility functions:
1. CapitalizeFirstLetter
Capitalizes the first letter of a given string.
2. ConvertDateWithTimeStamp
Converts a timestamp into a formatted date string.
3. ConvertNumberToKFormat
Converts large numbers to a shortened "K" format (e.g., 1200 → 1.2K).
4. FormatDate
Formats a date object or string into a human-readable date.
5. FormatDateWithTime
Formats a date object or string, including the time.
6. FormatDateRange
Formats date range e.g paymentStartDate in payslip.
7. FormatDocumentType
Converts a document type code to a human-readable string.
8. FormatNumberWithCommas
This function convert numbers to string format and add commas to a number for better readability. You can optionally pass currencySymbol as second argument if you want to prefix currency before the number. You an equally pass a third argument withSpace if you want to put space between the currencySymbol and the number. (e.g., 1000 → 1,000) | (1000 → $1,000) | (1000 → $ 1,000)
9. HexToRgba
Converts a hexadecimal color code to an RGBA format.
10. MaskEmail
Masks an email address, hiding part of it (e.g., user@example.com → u*@example.com).
11. SterilizeNumberInputs
Removes invalid characters from number inputs.
12. UserInitials
Extracts the initials from a user's full name.
13. truncateToTwoDecimals
formats a floating point number to two decimal places
14. truncateText
truncate text and a add '...' (e.g,. Interdominational -> interdom...).
15. BackgroundColor
Provides utility for determining or managing background colors.
16. CheckPathname
Checks if a given pathname matches specific patterns or criteria.
17. Encrypt
Encrypts sensitive data for secure storage or transfer.
18. Decrypt
Decrypts previously encrypted data.
19. ObfuscateValues
Obfuscates sensitive values to prevent unauthorized access.
20. DeobfuscateValues
Reverses the obfuscation process to retrieve the original values.
21. ExtractDocumentType
Extracts the document type from a given input.
22. GenerateRandomKey
Generates a random key for various use cases.
23. HandleEnterKeyDown
Handles the Enter key press for specific user input scenarios.
24. HandleOTPKeyDown
Manages key events for one-time password (OTP) inputs.
25. HandleInputChange
Handles changes in input fields, especially for OTP-like input systems.
26. HandlePaste
Handles paste events for input fields, ensuring proper formatting and data.
27. GetStatusClasses
Provides utility for retrieving status-specific CSS classes.
28. GetStatusText
Retrieves the text representation of a specific status.
29. Status
A type definition for status labels.
30. UserDataProps
A type definition for user data properties.
31. formatSlugName
formats slug name and replace whitespace with -
32. formatChartsNumber
Formats chart number
33. createInitials
This function creates business name initial
34. getTokenExpiration
Helper function to decode a JWT and get its expiration time in seconds
35. formatTokenExpiration
Helper function to convert seconds into days, hours, minutes, and seconds
36. getCurrencySymbol
This function helps to get the currency symbol
37. getTaxFields
This function helps to display the correct country tax fields depending on the country pass to it
38. countriesArray
This is an array of countries with currency symbol and country code
39. countryCurrencies
This is an array of contries with their currency code, currency name, currency symbol
40. countriesCurrenciesArray
This function is used to map through the countryCurrencies array and modify to return another arrays of object label, value and country
41. GetOrdinalWord
Converts a number into its ordinal word form (e.g., 1 → First).
42. formatEmployeeTaxDetailsLabel
Format employee tax details label (e.g., taxIdentificationNumber → Tax identification number).
43. getBankCodeField
Maps a currency symbol to its corresponding bank code field (e.g., £ → sortCode).
44. formatEmployeeTaxDetails
Converts an array of tax details into a key-value object with camelCase keys.
45. toCamelCaseTitle
Converts a space-separated string into camelCase format.
46. toCamelCase
Checks if a string is already in camelCase; if not, converts it.
47. isValuePresent
Checks if a value is not null, undefined`, or an empty string.
48. getCurrencyLabelAndCode
Returns the currency label and corresponding bank code based on the provided payment currency.