utils/format.js

import _ from 'lodash';

/**
 * Utilities for formatting and normalizing certain display aspects.
 * @module FormatUtils
 */
const FormatUtils = {
    /**
     * Formats a phone number to (XXX) XXX-XXXX for cleaner display.
     * @static
     * @function phone
     * @param {String} phoneNumber - The phone number to format.
     * @example
     * FormatUtils.phone('+13125556789'); // (312) 555-6789
     * @returns {String} - The formatted phone number.
     */
    phone(phoneNumber) {
        if (!_.isString(phoneNumber)) {
            throw new Error('The phone number must be provided as a string.');
        }

        return phoneNumber.replace(/^(?:\+1)?(\d{3})(\d{3})(\d{4})/, '($1) $2-$3');
    }
};

export default FormatUtils;