{"version":3,"file":"time_helpers-HqW4eMJx.js","sources":["../../../app/javascript/helpers/time_helpers.js"],"sourcesContent":["import moment from \"moment\"\n\nconst defaultDurationFormatWithDays = \"D_ h_ m[min]\"\nconst defaultDurationFormatWithoutSeconds = \"h[h] m[min]\"\nconst defaultDurationFormat = \"h[h] m[min] s[s]\"\nconst defaultDurationFormatMS = \"h[h] m[min] s[s] SSS[ms]\"\nconst defaultDateFormat = \"ll\"\nconst defaultTimeFormat = \"LT\"\nconst defaultDateTimeFormatWithSeconds = \"ll LTS\"\nconst defaultTimeFormatWithSeconds = \"LTS\"\nconst defaultDateTimeFormat = \"ll LT\"\n\nconst timeHelpers = {\n\n /**\n * Format a duration in milliseconds to a human-readable string\n * If the duration is less than 1 second, the format includes milliseconds\n * @param {Number} durationTimestamp\n * @return {String}\n */\n formatDurationMs(durationTimestamp) {\n const duration = moment.duration(durationTimestamp)\n const format = duration.asSeconds() < 1 ? defaultDurationFormatMS : defaultDurationFormat\n\n return duration.format(format)\n },\n\n /**\n * Format a duration in milliseconds to a human-readable string\n * If the duration is less than 1 second, it returns the duration is s with 1 decimal rounded to the nearest tenth\n * (0.01s -> 0.1s) If the duration is less than 100ms, it returns <0.1s\n * If the duration is 0, it returns 0s\n * @param {Number} durationTimestamp\n * @return {String}\n */\n formatDuration(durationTimestamp, format = defaultDurationFormat) {\n const duration = moment.duration(durationTimestamp)\n\n if (duration.asMilliseconds() === 0) return \"0s\"\n if (duration.asMilliseconds() < 100) return \"0.1s\"\n if (duration.asSeconds() < 1) return `${duration.asSeconds().toFixed(1)}s`\n\n return duration.format(format, { trim: \"both\" })\n },\n formatDurationRange(start, end, format = defaultDurationFormat) {\n const startDate = moment(start)\n const endDate = moment(end)\n\n const duration = Math.abs(endDate - startDate)\n const isNegative = endDate < startDate\n\n return `${isNegative ? \"-\" : \"\"}${this.formatDuration(duration, format)}`\n },\n\n formatTimeDateWithSeconds(date) {\n return moment(date).format(defaultDateTimeFormatWithSeconds)\n },\n\n formatDate(date) {\n if (!date) return \"\"\n\n return moment(date).format(defaultDateFormat)\n },\n\n formatTime(date) {\n if (!date) return \"\"\n\n return moment(date).format(defaultTimeFormat)\n },\n\n formatDateTime(date) {\n return moment(date).format(defaultDateTimeFormat, { trim: true })\n },\n\n /**\n * Format a date range to a human-readable string\n * If the start and end dates are on the same day, it returns the date and time range\n * If the start and end dates are on different days, it returns the date and time range\n * @param {Number} start\n * @param {Number} end\n * @param {Object} options\n * @param {string} options.separator\n * @param {boolean} options.showDateIfSameDay\n * @param {string} options.timeFormat\n * @param {string} options.dateTimeFormat\n * @param {string} options.startPlaceholder\n * @param {string} options.endPlaceholder\n * @returns {string}\n */\n formatDateRange(\n start,\n end,\n {\n separator = \"→\",\n showDateIfSameDay = true,\n timeFormat = defaultTimeFormat,\n dateTimeFormat = defaultDateTimeFormat,\n startPlaceholder = \"?\",\n endPlaceholder = \"?\",\n } = {}\n ) {\n const startDate = moment(start)\n const endDate = moment(end)\n\n const formatedStartTime = startDate.format(timeFormat)\n const formatedStartDate = this.formatDateWithoutYear(startDate, dateTimeFormat)\n const formatedEndDate = this.formatDateWithoutYear(endDate, dateTimeFormat)\n const formatedEndTime = endDate.format(timeFormat)\n\n const shouldShowStartDate = showDateIfSameDay || !startDate.isSame(endDate, \"day\")\n const shouldShowEndDate = !startDate.isSame(endDate, \"day\")\n\n const formatedStartDateOrTime = shouldShowStartDate ? formatedStartDate : formatedStartTime\n const formatedEndDateOrTime = shouldShowEndDate ? formatedEndDate : formatedEndTime\n\n return [\n start ? formatedStartDateOrTime : startPlaceholder,\n separator,\n end ? formatedEndDateOrTime : endPlaceholder,\n ].join(\" \")\n },\n\n /**\n * Format date without year\n * Use any moment.js format and it will remove the year from it\n *\n * Borrowed from https://stackoverflow.com/a/44308074\n * @param date\n * @param {string} format moment.js format\n * @returns {string}\n */\n formatDateWithoutYear(date, format) {\n const year = moment(date).format(\"YYYY\")\n return moment(date).format(format)\n .replace(year, \"\")\n .replace(/\\s\\s+/g, \" \")// remove double spaces, if any\n .trim() // remove spaces from the start and the end\n .replace(/[рг]\\./, \"\") // remove year letter from RU/UK locales\n .replace(/de$/, \"\") // remove year prefix from PT\n .replace(/b\\.$/, \"\") // remove year prefix from SE\n .trim() // remove spaces from the start and the end\n .replace(/,$/g, \"\") // remove comma from the end\n },\n\n formatDateRangeDuration(start, end, format = defaultDurationFormatWithDays) {\n if (!start || !end) return \"\"\n\n const startDate = moment(start)\n const endDate = moment(end)\n\n return moment.duration(endDate.diff(startDate)).format(format, { trim: \"both\" })\n\n },\n\n /**\n * Return formatted date with year if it's not the current year\n * @param date\n * @param {string} format moment.js format\n */\n autoFormatDate(date, format = defaultDateFormat) {\n if(moment(date).year() === moment().year()) {return this.formatDateWithoutYear(date, format)}\n\n return moment(date).format(format)\n },\n\n /**\n * Return formatted date and time with year if it's not the current year\n * @param date\n * @returns {string}\n */\n autoFormatDateTime(date) {\n return this.autoFormatDate(date, defaultDateTimeFormat)\n },\n\n /**\n * Checks if two dates fall on the same day.\n *\n * @param {Date, String} date1 - The first date to compare.\n * @param {Date, String} date2 - The second date to compare.\n * @returns {boolean} Returns true if the dates are on the same day, false otherwise.\n */\n isSameDay(date1, date2) {\n return moment(date1).isSame(date2, \"day\")\n },\n\n /**\n * Return the date only (no time) in ISO 8601 format (YYYY-MM-DD).\n * @param date\n * @returns {string}\n */\n toIsoDate(date) {\n return moment(date).format(\"YYYY-MM-DD\")\n },\n\n /**\n * Return a date object from a time string (HH:mm).\n * @param timeString\n * @returns {Date}\n */\n timeToDate(timeString) {\n return moment(timeString, \"HH:mm\").toDate()\n },\n}\n\nexport {\n defaultTimeFormatWithSeconds,\n defaultDateTimeFormatWithSeconds,\n defaultDurationFormatWithoutSeconds,\n}\nexport default timeHelpers\n"],"names":["defaultDurationFormatWithDays","defaultDurationFormatWithoutSeconds","defaultDurationFormat","defaultDurationFormatMS","defaultDateFormat","defaultTimeFormat","defaultDateTimeFormatWithSeconds","defaultTimeFormatWithSeconds","defaultDateTimeFormat","timeHelpers","durationTimestamp","duration","moment","format","start","end","startDate","endDate","date","separator","showDateIfSameDay","timeFormat","dateTimeFormat","startPlaceholder","endPlaceholder","formatedStartTime","formatedStartDate","formatedEndDate","formatedEndTime","shouldShowStartDate","shouldShowEndDate","year","date1","date2","timeString"],"mappings":"0aAEA,MAAMA,EAAgC,eAChCC,EAAsC,cACtCC,EAAwB,mBACxBC,EAA0B,2BAC1BC,EAAoB,KACpBC,EAAoB,KACpBC,EAAmC,SACnCC,EAA+B,MAC/BC,EAAwB,QAExBC,EAAc,CAQlB,iBAAiBC,EAAmB,CAClC,MAAMC,EAAWC,EAAO,SAASF,CAAiB,EAC5CG,EAASF,EAAS,UAAS,EAAK,EAAIR,EAA0BD,EAEpE,OAAOS,EAAS,OAAOE,CAAM,CAC9B,EAUD,eAAeH,EAAmBG,EAASX,EAAuB,CAChE,MAAMS,EAAWC,EAAO,SAASF,CAAiB,EAElD,OAAIC,EAAS,mBAAqB,EAAU,KACxCA,EAAS,iBAAmB,IAAY,OACxCA,EAAS,YAAc,EAAU,GAAGA,EAAS,UAAW,EAAC,QAAQ,CAAC,CAAC,IAEhEA,EAAS,OAAOE,EAAQ,CAAE,KAAM,MAAQ,CAAA,CAChD,EACD,oBAAoBC,EAAOC,EAAKF,EAASX,EAAuB,CAC9D,MAAMc,EAAYJ,EAAOE,CAAK,EACxBG,EAAUL,EAAOG,CAAG,EAEpBJ,EAAW,KAAK,IAAIM,EAAUD,CAAS,EAG7C,MAAO,GAFYC,EAAUD,EAEN,IAAM,EAAE,GAAG,KAAK,eAAeL,EAAUE,CAAM,CAAC,EACxE,EAED,0BAA0BK,EAAM,CAC9B,OAAON,EAAOM,CAAI,EAAE,OAAOZ,CAAgC,CAC5D,EAED,WAAWY,EAAM,CACf,OAAKA,EAEEN,EAAOM,CAAI,EAAE,OAAOd,CAAiB,EAF1B,EAGnB,EAED,WAAWc,EAAM,CACf,OAAKA,EAEEN,EAAOM,CAAI,EAAE,OAAOb,CAAiB,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