diff --git a/src/features/mass_deleter.js b/src/features/mass_deleter.js index c93a1ee59..3655b70bc 100644 --- a/src/features/mass_deleter.js +++ b/src/features/mass_deleter.js @@ -2,6 +2,7 @@ import { dom } from '../utils/dom.js'; import { megaEdit } from '../utils/mega_editor.js'; import { modalCancelButton, modalCompleteButton, showErrorModal, showModal } from '../utils/modals.js'; import { addSidebarItem, removeSidebarItem } from '../utils/sidebar.js'; +import { dateTimeFormat } from '../utils/text_format.js'; import { apiFetch } from '../utils/tumblr_helpers.js'; const timezoneOffsetMs = new Date().getTimezoneOffset() * 60000; @@ -18,14 +19,6 @@ const createNowString = () => { return `${YYYY}-${MM}-${DD}T${hh}:${mm}`; }; -const dateTimeFormat = new Intl.DateTimeFormat(document.documentElement.lang, { - year: 'numeric', - month: 'long', - day: 'numeric', - hour: 'numeric', - minute: 'numeric', - timeZoneName: 'short' -}); const showDeleteDraftsPrompt = () => { const form = dom('form', { id: 'xkit-mass-deleter-delete-drafts' }, { submit: confirmDeleteDrafts }, [ diff --git a/src/features/mass_privater.js b/src/features/mass_privater.js index 69ca20b6c..5efa633fd 100644 --- a/src/features/mass_privater.js +++ b/src/features/mass_privater.js @@ -2,6 +2,7 @@ import { dom } from '../utils/dom.js'; import { megaEdit } from '../utils/mega_editor.js'; import { showModal, modalCancelButton, modalCompleteButton, hideModal, showErrorModal } from '../utils/modals.js'; import { addSidebarItem, removeSidebarItem } from '../utils/sidebar.js'; +import { dateTimeFormat, elementsAsList } from '../utils/text_format.js'; import { apiFetch } from '../utils/tumblr_helpers.js'; import { userBlogs } from '../utils/user.js'; @@ -12,29 +13,6 @@ const createTagSpan = tag => dom('span', { class: 'mass-privater-tag' }, null, [ const createBlogSpan = name => dom('span', { class: 'mass-privater-blog' }, null, [name]); const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); -const dateTimeFormat = new Intl.DateTimeFormat(document.documentElement.lang, { - year: 'numeric', - month: 'long', - day: 'numeric', - hour: 'numeric', - minute: 'numeric', - timeZoneName: 'short' -}); - -/** - * Adds string elements between an array's items to format it as an English prose list. - * The Oxford comma is included. - * @param {any[]} array - Input array of any number of items - * @param {string} andOr - String 'and' or 'or', used before the last item - * @returns {any[]} An array alternating between the input items and strings - */ -const elementsAsList = (array, andOr) => - array.flatMap((item, i) => { - if (i === array.length - 1) return [item]; - if (i === array.length - 2) return array.length === 2 ? [item, ` ${andOr} `] : [item, `, ${andOr} `]; - return [item, ', ']; - }); - const timezoneOffsetMs = new Date().getTimezoneOffset() * 60000; const createNowString = () => { diff --git a/src/features/timeformat.js b/src/features/timeformat.js index 1a6ddd927..b312dbbd1 100644 --- a/src/features/timeformat.js +++ b/src/features/timeformat.js @@ -2,36 +2,11 @@ import moment from '../lib/moment.js'; import { keyToCss } from '../utils/css_map.js'; import { pageModifications } from '../utils/mutations.js'; import { getPreferences } from '../utils/preferences.js'; +import { constructRelativeTimeString } from '../utils/text_format.js'; let format; let displayRelative; -const relativeTimeFormat = new Intl.RelativeTimeFormat(document.documentElement.lang, { style: 'long' }); -const thresholds = [ - { unit: 'year', denominator: 31557600 }, - { unit: 'month', denominator: 2629800 }, - { unit: 'week', denominator: 604800 }, - { unit: 'day', denominator: 86400 }, - { unit: 'hour', denominator: 3600 }, - { unit: 'minute', denominator: 60 }, - { unit: 'second', denominator: 1 } -]; - -const constructRelativeTimeString = function (unixTime) { - const now = Math.trunc(new Date().getTime() / 1000); - const unixDiff = unixTime - now; - const unixDiffAbsolute = Math.abs(unixDiff); - - for (const { unit, denominator } of thresholds) { - if (unixDiffAbsolute >= denominator) { - const value = Math.trunc(unixDiff / denominator); - return relativeTimeFormat.format(value, unit); - } - } - - return relativeTimeFormat.format(-0, 'second'); -}; - const formatTimeElements = function (timeElements) { timeElements.forEach(timeElement => { const momentDate = moment(timeElement.dateTime, moment.ISO_8601); diff --git a/src/features/timestamps.js b/src/features/timestamps.js index b65d7e792..f444e6844 100644 --- a/src/features/timestamps.js +++ b/src/features/timestamps.js @@ -4,6 +4,7 @@ import { apiFetch } from '../utils/tumblr_helpers.js'; import { onNewPosts } from '../utils/mutations.js'; import { getPreferences } from '../utils/preferences.js'; import { keyToCss } from '../utils/css_map.js'; +import { constructRelativeTimeString } from '../utils/text_format.js'; const noteCountSelector = keyToCss('noteCount'); const reblogHeaderSelector = keyToCss('reblogHeader'); @@ -39,16 +40,6 @@ const longTimeFormat = new Intl.DateTimeFormat(locale, { second: '2-digit', timeZoneName: 'short' }); -const relativeTimeFormat = new Intl.RelativeTimeFormat(locale, { style: 'long' }); -const thresholds = [ - { unit: 'year', denominator: 31557600 }, - { unit: 'month', denominator: 2629800 }, - { unit: 'week', denominator: 604800 }, - { unit: 'day', denominator: 86400 }, - { unit: 'hour', denominator: 3600 }, - { unit: 'minute', denominator: 60 }, - { unit: 'second', denominator: 1 } -]; const constructTimeString = function (unixTime) { const date = new Date(unixTime * 1000); @@ -91,21 +82,6 @@ const constructISOString = function (unixTime) { return `${fourDigitYear}-${twoDigitMonth}-${twoDigitDate}T${twoDigitHours}:${twoDigitMinutes}:${twoDigitSeconds}${timezoneOffsetIsNegative ? '+' : '-'}${twoDigitTimezoneOffsetHours}:${twoDigitTimezoneOffsetMinutes}`; }; -const constructRelativeTimeString = function (unixTime) { - const now = Math.trunc(new Date().getTime() / 1000); - const unixDiff = unixTime - now; - const unixDiffAbsolute = Math.abs(unixDiff); - - for (const { unit, denominator } of thresholds) { - if (unixDiffAbsolute >= denominator) { - const value = Math.trunc(unixDiff / denominator); - return relativeTimeFormat.format(value, unit); - } - } - - return relativeTimeFormat.format(-0, 'second'); -}; - const addPostTimestamps = async function () { getPostElements({ excludeClass: 'xkit-timestamps-done' }).forEach(async postElement => { const { id } = postElement.dataset; diff --git a/src/utils/text_format.js b/src/utils/text_format.js new file mode 100644 index 000000000..ecb8774f0 --- /dev/null +++ b/src/utils/text_format.js @@ -0,0 +1,48 @@ +const thresholds = [ + { unit: 'year', denominator: 31557600 }, + { unit: 'month', denominator: 2629800 }, + { unit: 'week', denominator: 604800 }, + { unit: 'day', denominator: 86400 }, + { unit: 'hour', denominator: 3600 }, + { unit: 'minute', denominator: 60 }, + { unit: 'second', denominator: 1 } +]; + +const relativeTimeFormat = new Intl.RelativeTimeFormat(document.documentElement.lang, { style: 'long' }); +export const constructRelativeTimeString = function (unixTime) { + const now = Math.trunc(new Date().getTime() / 1000); + const unixDiff = unixTime - now; + const unixDiffAbsolute = Math.abs(unixDiff); + + for (const { unit, denominator } of thresholds) { + if (unixDiffAbsolute >= denominator) { + const value = Math.trunc(unixDiff / denominator); + return relativeTimeFormat.format(value, unit); + } + } + + return relativeTimeFormat.format(-0, 'second'); +}; + +export const dateTimeFormat = new Intl.DateTimeFormat(document.documentElement.lang, { + year: 'numeric', + month: 'long', + day: 'numeric', + hour: 'numeric', + minute: 'numeric', + timeZoneName: 'short' +}); + +/** + * Adds string elements between an array's items to format it as an English prose list. + * The Oxford comma is included. + * @param {any[]} array - Input array of any number of items + * @param {string} andOr - String 'and' or 'or', used before the last item + * @returns {any[]} An array alternating between the input items and strings + */ +export const elementsAsList = (array, andOr) => + array.flatMap((item, i) => { + if (i === array.length - 1) return [item]; + if (i === array.length - 2) return array.length === 2 ? [item, ` ${andOr} `] : [item, `, ${andOr} `]; + return [item, ', ']; + });