/** * @function stripRteStyle * @summary * Returns string with inline style tag stripped out * @param {string} stringWithStyleTag * @returns {string} */ export function stripRteStyle(stringWithStyleTag) { const regexExp = /[\s*]style="(.*?)"/g; return stringWithStyleTag.replace(regexExp, ''); } /** * @function createOrderedListFromStringOfParagraphs * @summary * Returns string with

tags changed to

  • and wrapped in an
      * @param {string} stringOfParagraphs single string with 1 to N

      tags * @returns {string} converted string wrapped in an

        */ export function createOrderedListFromStringOfParagraphs(stringOfParagraphs) { return `
          ${stringOfParagraphs.replaceAll('

          ', '').replaceAll('

          ', '

        1. ').replaceAll('

          ', '
        2. ')}
        `; } /** * @function toTitleCase * @summary Returns title cased string version of 'text' * @param {string} text * @returns {string} */ export function toTitleCase(text) { const temp = text?.toLowerCase()?.split(' ') ?? []; for (let i = 0; i < temp.length; i++) { temp[i] = temp[i].charAt(0).toUpperCase() + temp[i].slice(1); } return temp.join(' '); } /** * @function toDisplayPhoneNumber * @summary If 10 or 11 digits are in phoneNumber, then the expected number including dashes will be returned * in either d-ddd-ddd-dddd or ddd-ddd-dddd format. Otherwise, an empty string will be returned. * @param {string} phoneNumber * @returns {string} */ export function toDisplayPhoneNumber(phoneNumber) { let result = ''; if (!phoneNumber) { return result; } let remainingDigits = phoneNumber.match(/\d+/g).join(''); if (remainingDigits.length === 11) { result += `${remainingDigits[0]}-`; remainingDigits = remainingDigits.substring(1); } if (remainingDigits.length === 10) { result += remainingDigits.replace(/^(\d{3})(\d{3})(\d{4})/, '$1-$2-$3'); } return result; } /** * @function formatAddress * @summary given address fields, returns a string representation of said address * @param {string} addressLine1 * @param {string} addressLine2 * @param {string} city * @param {string} state * @param {string} zipCode * @returns {string} */ export function formatAddress(addressLine1, addressLine2, city, state, zipCode) { let address = ''; if (addressLine1) { address += addressLine1; } if (addressLine2) { address += address ? `, ${addressLine2}` : addressLine2; } if (city) { address += address ? `, ${city}` : city; } address = toTitleCase(address); if (state) { address += address ? `, ${state}` : state; } if (zipCode) { address += address ? ` ${zipCode}` : zipCode; } return address; } const currencyFormatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }); /** * @function formatAmountInDollars * @param {string, number} amount * @returns {string} */ export function formatAmountInDollars(amount) { const numericAmount = typeof amount === 'string' ? parseFloat(amount) : amount; if (Number.isNaN(numericAmount) || amount === null || amount === undefined) { return ''; } return currencyFormatter.format(amount); } const defaultDateFormatter = new Intl.DateTimeFormat('en-US'); export function formatDate(date, formatter) { const dateFormatter = formatter ?? defaultDateFormatter; if (!Number.isNaN(date)) { return dateFormatter.format(date); } return ''; }