76410.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. "use strict";
  2. exports.id = 76410;
  3. exports.ids = [76410];
  4. exports.modules = {
  5. /***/ 76410:
  6. /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
  7. /* harmony export */ __webpack_require__.d(__webpack_exports__, {
  8. /* harmony export */ "Bz": () => (/* binding */ dateToDateString),
  9. /* harmony export */ "Cl": () => (/* binding */ getCurrentMonth),
  10. /* harmony export */ "EP": () => (/* binding */ getTimezone),
  11. /* harmony export */ "OQ": () => (/* binding */ getMonthDateObject),
  12. /* harmony export */ "QH": () => (/* binding */ getCurrentDay),
  13. /* harmony export */ "Rp": () => (/* binding */ milliSecondsToSeconds),
  14. /* harmony export */ "S4": () => (/* binding */ numberToPaddedString),
  15. /* harmony export */ "SV": () => (/* binding */ getMonthsInYear),
  16. /* harmony export */ "ZC": () => (/* binding */ dateToReadableFormat),
  17. /* harmony export */ "Zu": () => (/* binding */ getFullDayName),
  18. /* harmony export */ "by": () => (/* binding */ secondsToReadableFormat),
  19. /* harmony export */ "fR": () => (/* binding */ makeDateRangeFromMonth),
  20. /* harmony export */ "pE": () => (/* binding */ secondsToMilliSeconds)
  21. /* harmony export */ });
  22. /* unused harmony exports secondsFormatter, getEarliestDate, parseDate, formatDateRelatively, dateToYearMonthDay, getFullMonthName */
  23. /* harmony import */ var _locale__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(24709);
  24. /* harmony import */ var _number__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(19303);
  25. // Converts seconds to (hours), minutes, and seconds
  26. const secondsFormatter = (seconds, locale)=>{
  27. if (!seconds || Number.isNaN(seconds)) {
  28. return "";
  29. }
  30. return new Date(seconds * 1000).toLocaleTimeString(getLangFullLocale(locale), {
  31. timeZone: "Etc/UTC",
  32. hour12: false,
  33. minute: "2-digit",
  34. second: "2-digit",
  35. ...seconds >= 3600 && {
  36. hour: "2-digit"
  37. }
  38. });
  39. };
  40. /**
  41. * Convert seconds to the format of `x hours, y minutes, z seconds`.
  42. * Or any combination of the three.
  43. *
  44. * @param {numbers} s seconds
  45. * @param {Translate} t translate function
  46. * @param {string} locale locale
  47. * @returns {string}
  48. */ // eslint-disable-next-line react-func/max-lines-per-function
  49. const secondsToReadableFormat = (s, t, locale)=>{
  50. const results = [];
  51. let seconds = s;
  52. let minutes = Math.floor(seconds / 60);
  53. const hours = Math.floor(minutes / 60);
  54. if (hours > 0) {
  55. results.push(t("reading-goal:x-hours", {
  56. count: hours,
  57. hours: (0,_locale__WEBPACK_IMPORTED_MODULE_0__/* .toLocalizedNumber */ .rQ)((0,_number__WEBPACK_IMPORTED_MODULE_1__/* .convertNumberToDecimal */ .uZ)(hours), locale)
  58. }));
  59. minutes %= 60;
  60. seconds %= 60;
  61. }
  62. if (minutes > 0) {
  63. results.push(t("reading-goal:x-minutes", {
  64. count: minutes,
  65. minutes: (0,_locale__WEBPACK_IMPORTED_MODULE_0__/* .toLocalizedNumber */ .rQ)((0,_number__WEBPACK_IMPORTED_MODULE_1__/* .convertNumberToDecimal */ .uZ)(minutes), locale)
  66. }));
  67. seconds %= 60;
  68. }
  69. // if there are seconds left, or if the duration is 0 (in this case, `results.length` = 0), add seconds
  70. if (seconds > 0 || results.length === 0) {
  71. results.push(t("reading-goal:x-seconds", {
  72. count: seconds,
  73. seconds: (0,_locale__WEBPACK_IMPORTED_MODULE_0__/* .toLocalizedNumber */ .rQ)((0,_number__WEBPACK_IMPORTED_MODULE_1__/* .convertNumberToDecimal */ .uZ)(seconds), locale)
  74. }));
  75. }
  76. return results.join(", ");
  77. };
  78. /**
  79. * Convert milliseconds to seconds.
  80. *
  81. * @param {number} milliSeconds
  82. * @returns {number}
  83. */ const milliSecondsToSeconds = (milliSeconds)=>milliSeconds / 1000;
  84. /**
  85. * Convert milliseconds to seconds.
  86. *
  87. * @param {number} seconds
  88. * @returns {number}
  89. */ const secondsToMilliSeconds = (seconds)=>seconds * 1000;
  90. /**
  91. * Get the earliest date of a groups of date string.
  92. *
  93. * @param {string[]} dates
  94. * @returns {number}
  95. */ const getEarliestDate = (dates)=>dates.map((dateString)=>parseDate(dateString)).sort((a, b)=>a - b)[0];
  96. /**
  97. * Parse a date string.
  98. *
  99. * @param {string} date
  100. * @returns {number}
  101. */ const parseDate = (date)=>Date.parse(date);
  102. /**
  103. * Format date to a string
  104. *
  105. * @param {Date} date
  106. * @param {string} locale
  107. * @returns {string} date
  108. */ const formatDateRelatively = (date, locale, now = new Date())=>{
  109. const fullLocale = LANG_LOCALE_MAP[locale];
  110. // Formatter for "Today" and "Yesterday" etc
  111. const relative = new Intl.RelativeTimeFormat(fullLocale, {
  112. numeric: "auto"
  113. });
  114. const nowDate = now.setHours(0, 0, 0, 0);
  115. const then = date.setHours(0, 0, 0, 0);
  116. const days = (then - nowDate) / 86400000;
  117. if (days < -365) {
  118. const years = Math.round(days / 365);
  119. return relative.format(years, "year");
  120. }
  121. if (days < -7) {
  122. const weeks = Math.round(days / 7);
  123. return relative.format(weeks, "weeks");
  124. }
  125. return relative.format(days, "day");
  126. };
  127. // Intl.DateTimeFormat is performance heavy so we are caching the formatter.
  128. let dateTimeFormatter = null;
  129. let timezone = null;
  130. /**
  131. * Returns the current timezone.
  132. *
  133. * @example `Europe/London`
  134. * @returns {string}
  135. */ const getTimezone = ()=>{
  136. if (timezone) return timezone;
  137. if (!dateTimeFormatter) dateTimeFormatter = new Intl.DateTimeFormat();
  138. timezone = dateTimeFormatter.resolvedOptions().timeZone;
  139. return timezone;
  140. };
  141. /**
  142. * Given a Date, return the year, month and day values
  143. *
  144. * @param {Date} date
  145. * @returns {{year: number, month: number, day: number}}
  146. */ const dateToYearMonthDay = (date)=>{
  147. const year = date.getFullYear();
  148. const month = date.getMonth() + 1;
  149. const day = date.getDate();
  150. return {
  151. year,
  152. month,
  153. day
  154. };
  155. };
  156. const getCurrentMonth = ()=>new Date().getMonth() + 1;
  157. const getCurrentDay = ()=>new Date().getDate();
  158. /**
  159. * Converts a date instance to a string in this format: YYYY-MM-DD
  160. *
  161. * @param {Date} date
  162. * @returns {string}
  163. */ const dateToDateString = (date)=>{
  164. const { year , month , day } = date instanceof Date ? dateToYearMonthDay(date) : date;
  165. return `${year}-${numberToPaddedString(month)}-${numberToPaddedString(day)}`;
  166. };
  167. /**
  168. * Gets the full day name in a given locale.
  169. * Example: `Monday` in `en`
  170. *
  171. * @param {Date} day
  172. * @param {string} locale
  173. * @returns {string}
  174. *
  175. */ const getFullDayName = (day, locale)=>{
  176. return day.toLocaleDateString(locale, {
  177. weekday: "long",
  178. timeZone: "UTC"
  179. });
  180. };
  181. /**
  182. * Gets the full month name in a given locale.
  183. * Example: `April` in `en`
  184. *
  185. * @param {Date} month
  186. * @param {string} locale
  187. * @returns {string}
  188. *
  189. */ const getFullMonthName = (month, locale)=>{
  190. return month.toLocaleDateString(locale, {
  191. month: "long",
  192. timeZone: "UTC"
  193. });
  194. };
  195. /**
  196. * Formats a date to a readable format.
  197. *
  198. * Example output: `Sunday, April 16`
  199. *
  200. * @param {Date | string} date Date instance or date string
  201. * @param {string} locale
  202. * @param {Intl.DateTimeFormatOptions} options
  203. * @returns {string}
  204. *
  205. */ const dateToReadableFormat = (date, locale, options = {})=>{
  206. const dateInstance = typeof date === "string" ? new Date(date) : date;
  207. return dateInstance.toLocaleDateString((0,_locale__WEBPACK_IMPORTED_MODULE_0__/* .getLangFullLocale */ .ic)(locale), {
  208. day: "numeric",
  209. month: "long",
  210. weekday: "long",
  211. timeZone: "UTC",
  212. ...options
  213. });
  214. };
  215. /**
  216. * Convert a number into a padded string with 0. E.g. 1 -> 01
  217. *
  218. * @param {number} number
  219. * @returns {string}
  220. */ const numberToPaddedString = (number)=>{
  221. return number.toString().padStart(2, "0");
  222. };
  223. /**
  224. * Given a month and a year, this function returns the first and last day of the month in format: YYYY-MM-DD.
  225. *
  226. * @param {number} month
  227. * @param {number} year
  228. * @returns {DateRange}
  229. */ const makeDateRangeFromMonth = (month, year)=>{
  230. const from = `${year}-${numberToPaddedString(month)}-01`;
  231. const to = `${year}-${numberToPaddedString(month)}-${numberToPaddedString(new Date(year, month, 0).getDate())}`;
  232. return {
  233. from,
  234. to
  235. };
  236. };
  237. /**
  238. * Get a Date out of year and month numbers.
  239. *
  240. * @param {year} year
  241. * @param {month} month
  242. * @returns {Date}
  243. */ const getMonthDateObject = (year, month)=>{
  244. return new Date(year, month, 0);
  245. };
  246. /**
  247. * This function returns an array of months in a given year.
  248. *
  249. * @param {number} year
  250. * @param {string} locale
  251. * @returns {Month[]}
  252. */ const getMonthsInYear = (year, locale)=>{
  253. const all = [];
  254. for(let i = 1; i <= 12; i += 1){
  255. const monthDate = getMonthDateObject(year, i);
  256. const daysInMonth = monthDate.getDate();
  257. all.push({
  258. id: i,
  259. name: getFullMonthName(monthDate, locale),
  260. daysCount: daysInMonth
  261. });
  262. }
  263. return all;
  264. };
  265. /***/ })
  266. };
  267. ;
  268. //# sourceMappingURL=76410.js.map