UNPKG

@dialpad/dialtone-vue

Version:

Vue component library for Dialpad's design system Dialtone

1 lines 5.99 kB
{"version":3,"file":"index.cjs","sources":["../../../common/dates/index.js"],"sourcesContent":["import {\n format,\n formatDuration,\n intervalToDuration,\n formatDistance,\n isToday,\n isYesterday,\n isThisWeek,\n isThisYear,\n} from 'date-fns';\nimport { capitalizeFirstLetter } from '../utils';\n\nlet dialtoneLocale;\n\n// Base functions just wrap core date-fns functions, but this allows us to do checks and set default options.\n\nfunction _baseFormat (date, formatString) {\n _checkLocaleSet();\n return format(date, formatString, {\n locale: dialtoneLocale,\n });\n}\n\nfunction _baseFormatDuration (duration, formatString) {\n _checkLocaleSet();\n return formatDuration(duration, {\n locale: dialtoneLocale,\n format: formatString,\n });\n}\n\nfunction _baseFormatDistance (date, baseDate) {\n _checkLocaleSet();\n return formatDistance(date, baseDate, {\n locale: dialtoneLocale,\n });\n}\n\nfunction _isLocaleSet () {\n return dialtoneLocale !== undefined;\n}\n\nfunction _checkLocaleSet () {\n if (!_isLocaleSet()) {\n throw new Error('Locale not set, please call setDateLocale(locale) and pass ' +\n 'in a datefns locale object as the locale param before calling this function');\n }\n}\n\n/**\n * Sets the locale for date-fns. This should be called before any date-fns functions are called.\n * @param {Locale} locale A date-fns locale object\n */\nexport function setDateLocale (locale) {\n dialtoneLocale = locale;\n}\n\n/**\n * This formats a date to the Dialtone standard medium date format as shown here:\n * https://dialtone.dialpad.com/guides/writing-guidelines/#formats-by-length\n * @param {Date} date A javascript date object\n * @returns {string} A string in the format of 'September 2, 2022'\n */\nexport function getDateMedium (date) {\n return _baseFormat(date, 'MMMM d, y');\n}\n\n/**\n * Converts a call duration in total number of seconds to a human readable string\n * such as 'less than a minute' or '4 hours 34 minutes'.\n * @param {number} durationInSeconds The duration of the call in seconds\n * @returns {string} A human readable string representing the duration of the call\n */\nexport function durationInHHMM (durationInSeconds) {\n if (durationInSeconds < 60) {\n // returns 'less than a minute', we're doing it like this instead of returning a string\n // so datefns handles i18n.\n return _baseFormatDistance(0, 29 * 1000);\n }\n const duration = intervalToDuration({\n start: 0,\n end: durationInSeconds * 1000,\n });\n return _baseFormatDuration(duration, ['hours', 'minutes']);\n}\n\n/**\n * gets the human readable name of the day relative to the current time. For example, if you pass in -1 it will\n * say \"Yesterday\" if you pass in 0 it will say \"Today\", if you pass in 1 it will say \"Tomorrow\".\n * @param {number} days The number of days relative to the current time\n * @returns {string} A human readable string representing the distance between the date and now\n */\nfunction _getRelativeDaysText (days) {\n const rtl = new Intl.RelativeTimeFormat(dialtoneLocale.code, { numeric: 'auto' });\n return capitalizeFirstLetter(rtl.formatToParts(days, 'day')[0].value, dialtoneLocale.code);\n}\n\n/**\n * Returns the distance between the passed in date and now in a human readable format, typically used\n * when showing a history of items in a log such as a feed list.\n *\n * datefns does not support 'today' and 'yesterday' without showing time so we use Intl for these cases.\n *\n * examples below to explain\n * the different potential formats:\n *\n * If current day:\n * Today\n *\n * If previous day:\n * Yesterday\n *\n * Older than yesterday, but in the same calendar week:\n * Monday\n *\n * Older than the most recent calendar week, but in the same year:\n * Monday, October 14\n *\n * older than a calendar year:\n * October 14, 2022\n *\n *\n * @param {Date} date The timestamp of the item's date\n * @returns {string} A human readable string representing the distance between the date and now\n */\nexport function relativeDate (date) {\n if (isToday(date)) {\n return _getRelativeDaysText(0);\n } else if (isYesterday(date)) {\n return _getRelativeDaysText(-1);\n } else if (isThisWeek(date)) {\n return _baseFormat(date, 'EEEE');\n } else if (isThisYear(date)) {\n return _baseFormat(date, 'EEEE, MMMM d');\n } else {\n return _baseFormat(date, 'MMMM d, y');\n }\n}\n"],"names":["dialtoneLocale","_baseFormat","date","formatString","_checkLocaleSet","format","_baseFormatDuration","duration","formatDuration","_baseFormatDistance","baseDate","formatDistance","_isLocaleSet","setDateLocale","locale","getDateMedium","durationInHHMM","durationInSeconds","intervalToDuration","_getRelativeDaysText","days","rtl","capitalizeFirstLetter","relativeDate","isToday","isYesterday","isThisWeek","isThisYear"],"mappings":"4IAYA,IAAI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