{"version":3,"sources":["webpack:///./app/javascript/utilities/localDateTime.js","webpack:///./app/javascript/packs/localizeArticleDates.js"],"names":["timestampToLocalDateTime","timestamp","locale","options","time","Date","formattedTime","Intl","DateTimeFormat","format","year","replace","e","addLocalizedDateTimeToElementsTitles","elements","timestampAttribute","i","length","element","getAttribute","localDateTime","timestampToLocalDateTimeLong","setAttribute","localizeTimeElements","timeOptions","navigator","language","textContent","weekday","month","day","hour","minute","second","globalThis","timestampToLocalDateTimeShort","currentYear","getFullYear","articlesDates","document","querySelectorAll"],"mappings":"2FAYO,SAASA,EAAyBC,EAAWC,EAAQC,GAC1D,IAAKF,EACH,MAAO,GAGT,IACE,IAAMG,EAAO,IAAIC,KAAKJ,GAChBK,EAAgB,IAAIC,KAAKC,eAC7BN,GAAU,UACVC,GACAM,OAAOL,GACT,MAAwB,YAAjBD,EAAQO,KACXJ,EAAcK,QAAQ,KAAM,MAC5BL,CAGN,CAFE,MAAOM,GACP,MAAO,EACT,CACF,CAEO,SAASC,EAAqCC,EAAUC,GAC7D,IAAK,IAAIC,EAAI,EAAGA,EAAIF,EAASG,OAAQD,GAAK,EAAG,CAC3C,IAAME,EAAUJ,EAASE,GAGnBf,EAAYiB,EAAQC,aAAaJ,GAAsB,YAE7D,GAAId,EAAW,CAKb,IAAMmB,EAAgBC,EAA6BpB,GACnDiB,EAAQI,aAAa,QAASF,EAChC,CACF,CACF,CAEO,SAASG,EAAqBT,EAAUU,GAC7C,IAAK,IAAIR,EAAI,EAAGA,EAAIF,EAASG,OAAQD,GAAK,EAAG,CAC3C,IAAME,EAAUJ,EAASE,GAEnBf,EAAYiB,EAAQC,aAAa,YACvC,GAAIlB,EAAW,CACb,IAAMmB,EAAgBpB,EACpBC,EACAwB,UAAUC,SACVF,GAGFN,EAAQS,YAAcP,CACxB,CACF,CACF,CAEA,SAASC,EAA6BpB,GAGpC,OAAOD,EAAyBC,EAAWwB,UAAUC,SAAU,CAC7DE,QAAS,OACTlB,KAAM,UACNmB,MAAO,OACPC,IAAK,UACLC,KAAM,UACNC,OAAQ,UACRC,OAAQ,WAEZ,CA9EA,sGAuG0B,qBAAfC,aACTA,WAAWb,6BAA+BA,EAC1Ca,WAAWC,8BAzBb,SAAuClC,GAIrC,GAAIA,EAAW,CACb,IAAMmC,GAAc,IAAI/B,MAAOgC,cAGzBb,EAAc,CAClBM,IAAK,UACLD,MAAO,SAOT,OAXkB,IAAIxB,KAAKJ,GAAWoC,gBAOpBD,IAChBZ,EAAYd,KAAO,WAGdV,EAAyBC,EAAWwB,UAAUC,SAAUF,EACjE,CAEA,MAAO,EACT,E,mCCrGA,WAIQc,EAJR,SAIQA,EAAgBC,SAASC,iBAC7B,qCAGF3B,YAAqCyB,EAAe,W","file":"js/localizeArticleDates-11ac8f9618642a49dab5.chunk.js","sourcesContent":["/* Local date/time utilities */\n\n/*\n Convert string timestamp to local time, using the given locale.\n\n timestamp should be something like '2019-05-03T16:02:50.908Z'\n locale can be `navigator.language` or a custom locale. defaults to 'default'\n options are `Intl.DateTimeFormat` options\n\n see \n for more information.\n*/\nexport function timestampToLocalDateTime(timestamp, locale, options) {\n if (!timestamp) {\n return '';\n }\n\n try {\n const time = new Date(timestamp);\n const formattedTime = new Intl.DateTimeFormat(\n locale || 'default',\n options,\n ).format(time);\n return options.year === '2-digit'\n ? formattedTime.replace(', ', \" '\")\n : formattedTime;\n } catch (e) {\n return '';\n }\n}\n\nexport function addLocalizedDateTimeToElementsTitles(elements, timestampAttribute) {\n for (let i = 0; i < elements.length; i += 1) {\n const element = elements[i];\n\n // get UTC timestamp set by the server\n const timestamp = element.getAttribute(timestampAttribute || 'datetime');\n\n if (timestamp) {\n // add a full datetime to the element title, visible on hover.\n // `navigator.language` is used to allow the date to be localized\n // according to the browser's locale\n // see \n const localDateTime = timestampToLocalDateTimeLong(timestamp);\n element.setAttribute('title', localDateTime);\n }\n }\n}\n\nexport function localizeTimeElements(elements, timeOptions) {\n for (let i = 0; i < elements.length; i += 1) {\n const element = elements[i];\n\n const timestamp = element.getAttribute('datetime');\n if (timestamp) {\n const localDateTime = timestampToLocalDateTime(\n timestamp,\n navigator.language,\n timeOptions,\n );\n\n element.textContent = localDateTime;\n }\n }\n}\n\nfunction timestampToLocalDateTimeLong(timestamp) {\n // example: \"Wednesday, April 3, 2019, 2:55:14 PM\"\n\n return timestampToLocalDateTime(timestamp, navigator.language, {\n weekday: 'long',\n year: 'numeric',\n month: 'long',\n day: 'numeric',\n hour: 'numeric',\n minute: 'numeric',\n second: 'numeric',\n });\n}\n\nfunction timestampToLocalDateTimeShort(timestamp) {\n // example: \"10 Dec 2018\" if it is not the current year\n // example: \"6 Sep\" if it is the current year\n\n if (timestamp) {\n const currentYear = new Date().getFullYear();\n const givenYear = new Date(timestamp).getFullYear();\n\n const timeOptions = {\n day: 'numeric',\n month: 'short',\n };\n\n if (givenYear !== currentYear) {\n timeOptions.year = 'numeric';\n }\n\n return timestampToLocalDateTime(timestamp, navigator.language, timeOptions);\n }\n\n return '';\n}\n\nif (typeof globalThis !== 'undefined') {\n globalThis.timestampToLocalDateTimeLong = timestampToLocalDateTimeLong; // eslint-disable-line no-undef\n globalThis.timestampToLocalDateTimeShort = timestampToLocalDateTimeShort; // eslint-disable-line no-undef\n}\n","/* Show article date/time according to user's locale */\nimport { addLocalizedDateTimeToElementsTitles } from '../utilities/localDateTime';\n\nconst initializeArticleDate = () => {\n const articlesDates = document.querySelectorAll(\n '.crayons-story time, article time',\n );\n\n addLocalizedDateTimeToElementsTitles(articlesDates, 'datetime');\n};\n\ninitializeArticleDate();\n"],"sourceRoot":""}