From 662fe8de2f2915cde393743acaf193443018e7af Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Wed, 13 Oct 2021 22:08:31 -0400 Subject: [PATCH] feat(app/utils, plugins): normalize dates with improved format (#573) --- source/app/metrics/utils.mjs | 8 ++++++++ source/app/web/statics/app.placeholder.js | 8 ++++++++ source/plugins/achievements/index.mjs | 2 +- source/plugins/core/index.mjs | 2 +- source/plugins/music/index.mjs | 2 +- source/plugins/stackoverflow/index.mjs | 4 ++-- source/plugins/tweets/index.mjs | 2 +- source/templates/classic/partials/activity.ejs | 2 +- source/templates/classic/partials/posts.ejs | 2 +- source/templates/classic/partials/rss.ejs | 2 +- source/templates/markdown/example.md | 2 +- source/templates/markdown/partials/activity.ejs | 2 +- source/templates/markdown/partials/posts.ejs | 4 ++-- source/templates/markdown/partials/rss.ejs | 2 +- source/templates/repository/partials/activity.ejs | 2 +- source/templates/repository/partials/rss.ejs | 2 +- 16 files changed, 32 insertions(+), 16 deletions(-) diff --git a/source/app/metrics/utils.mjs b/source/app/metrics/utils.mjs index 363c242a..c5c7f873 100644 --- a/source/app/metrics/utils.mjs +++ b/source/app/metrics/utils.mjs @@ -101,6 +101,14 @@ export function formatters({timeZone} = {}) { /**Date formatter */ format.date = function(string, options) { + if (options.date) { + delete options.date + Object.assign(options, {day:"numeric", month:"short", year:"numeric"}) + } + if (options.time) { + delete options.time + Object.assign(options, {hour:"2-digit", minute:"2-digit", second:"2-digit"}) + } return new Intl.DateTimeFormat("en-GB", {timeZone, ...options}).format(new Date(string)) } diff --git a/source/app/web/statics/app.placeholder.js b/source/app/web/statics/app.placeholder.js index 2afa7419..8d3798dd 100644 --- a/source/app/web/statics/app.placeholder.js +++ b/source/app/web/statics/app.placeholder.js @@ -1131,6 +1131,14 @@ return `${text.substring(0, length)}…` } data.f.date = function(string, options) { + if (options.date) { + delete options.date + Object.assign(options, {day:"numeric", month:"short", year:"numeric"}) + } + if (options.time) { + delete options.time + Object.assign(options, {hour:"2-digit", minute:"2-digit", second:"2-digit"}) + } return new Intl.DateTimeFormat("en-GB", options).format(new Date(string)) } data.f.license = function(text) { diff --git a/source/plugins/achievements/index.mjs b/source/plugins/achievements/index.mjs index 1f36c40c..35a35f32 100644 --- a/source/plugins/achievements/index.mjs +++ b/source/plugins/achievements/index.mjs @@ -28,7 +28,7 @@ export default async function({login, q, imports, data, computed, graphql, queri .map(({title, unlock, ...achievement}) => ({ prefix:({S:"Master", A:"Super", B:"Great"}[achievement.rank] ?? ""), title, - unlock:!/invalid date/i.test(unlock) ? `${imports.format.date(unlock, {timeStyle:"short"})} on ${imports.format.date(unlock, {dateStyle:"short"})}` : null, + unlock:!/invalid date/i.test(unlock) ? `${imports.format.date(unlock, {time:true})} on ${imports.format.date(unlock, {date:true})}` : null, ...achievement, })) .map(({icon, ...achievement}) => ({icon:icon.replace(/#primary/g, colors[achievement.rank][0]).replace(/#secondary/g, colors[achievement.rank][1]), ...achievement})) diff --git a/source/plugins/core/index.mjs b/source/plugins/core/index.mjs index bc825030..7985d545 100644 --- a/source/plugins/core/index.mjs +++ b/source/plugins/core/index.mjs @@ -120,7 +120,7 @@ export default async function({login, q}, {conf, data, rest, graphql, plugins, q data.meta = { version:conf.package.version, author:conf.package.author, - generated:imports.format.date(new Date(), {dateStyle:"short", timeStyle:"short"}) + generated:imports.format.date(new Date(), {date:true, time:true}) } //Debug flags diff --git a/source/plugins/music/index.mjs b/source/plugins/music/index.mjs index a8521e34..cc051ed4 100644 --- a/source/plugins/music/index.mjs +++ b/source/plugins/music/index.mjs @@ -164,7 +164,7 @@ export default async function({login, imports, data, q, account}, {enabled = fal name:track.name, artist:track.artists[0].name, artwork:track.album.images[0].url, - played_at:played_at ? `${imports.format.date(played_at, {timeStyle:"short"})} on ${imports.format.date(played_at, {dateStyle:"short"})}` : null, + played_at:played_at ? `${imports.format.date(played_at, {time:true})} on ${imports.format.date(played_at, {date:true})}` : null, })) //Ensure no duplicate are added for (const track of loaded) { diff --git a/source/plugins/stackoverflow/index.mjs b/source/plugins/stackoverflow/index.mjs index c16723bc..ef3a8e3d 100644 --- a/source/plugins/stackoverflow/index.mjs +++ b/source/plugins/stackoverflow/index.mjs @@ -89,7 +89,7 @@ const format = { accepted, comments, author, - created:imports.format.date(creation_date * 1000, {dateStyle:"short"}), + created:imports.format.date(creation_date * 1000, {date:true}), link, id, question_id, @@ -136,7 +136,7 @@ const format = { comments, views, author, - created:imports.format.date(creation_date * 1000, {dateStyle:"short"}), + created:imports.format.date(creation_date * 1000, {date:true}), link, id, accepted_answer_id, diff --git a/source/plugins/tweets/index.mjs b/source/plugins/tweets/index.mjs index fdc74227..bfdbb142 100644 --- a/source/plugins/tweets/index.mjs +++ b/source/plugins/tweets/index.mjs @@ -69,7 +69,7 @@ export default async function({login, imports, data, q, account}, {enabled = fal //Format text console.debug(`metrics/compute/${login}/plugins > tweets > formatting tweet ${tweet.id}`) - tweet.createdAt = `${imports.format.date(tweet.created_at, {timeStyle:"short"})} on ${imports.format.date(tweet.created_at, {dateStyle:"short"})}` + tweet.createdAt = `${imports.format.date(tweet.created_at, {time:true})} on ${imports.format.date(tweet.created_at, {date:true})}` tweet.text = imports.htmlescape( //Escape tags imports.htmlescape(tweet.text, {"<":true, ">":true}) diff --git a/source/templates/classic/partials/activity.ejs b/source/templates/classic/partials/activity.ejs index 2ba4e083..5023450a 100644 --- a/source/templates/classic/partials/activity.ejs +++ b/source/templates/classic/partials/activity.ejs @@ -169,7 +169,7 @@ <% } %> <% if (plugins.activity.timestamps) { %>
- <%= f.date(timestamp, {timeStyle:"short", dateStyle:"short", timeZone:config.timezone?.name}) %> + <%= f.date(timestamp, {time:true, date:true, timeZone:config.timezone?.name}) %>
<% } %> diff --git a/source/templates/classic/partials/posts.ejs b/source/templates/classic/partials/posts.ejs index b2965be8..f8e14237 100644 --- a/source/templates/classic/partials/posts.ejs +++ b/source/templates/classic/partials/posts.ejs @@ -23,7 +23,7 @@
-
<%= f.date(new Date(date), {dateStyle:"short", timeZone:config.timezone?.name}) %>
+
<%= f.date(new Date(date), {date:true, timeZone:config.timezone?.name}) %>
<% if (plugins.posts.covers) { %>
<% } %> diff --git a/source/templates/classic/partials/rss.ejs b/source/templates/classic/partials/rss.ejs index e657fce1..5ec99710 100644 --- a/source/templates/classic/partials/rss.ejs +++ b/source/templates/classic/partials/rss.ejs @@ -18,7 +18,7 @@
<%= title %>
-
<%= f.date(new Date(date), {dateStyle:"short", timeZone:config.timezone?.name}) %>
+
<%= f.date(new Date(date), {date:true, timeZone:config.timezone?.name}) %>
<% } %> diff --git a/source/templates/markdown/example.md b/source/templates/markdown/example.md index 5ebc3e95..25f80f23 100644 --- a/source/templates/markdown/example.md +++ b/source/templates/markdown/example.md @@ -14,7 +14,7 @@ See [rendering of this file here](https://github.com/lowlighter/lowlighter/blob/ * Same formatting helpers available in templates can be used too ```markdown -I joined GitHub on `{{ f.date(REGISTRATION_DATE, {dateStyle:"short"}) }}`. +I joined GitHub on `{{ f.date(REGISTRATION_DATE, {date:true}) }}`. I contributed to `{{ REPOSITORIES_CONTRIBUTED_TO }}` repositories and made `{{ COMMITS }}` commits. ``` diff --git a/source/templates/markdown/partials/activity.ejs b/source/templates/markdown/partials/activity.ejs index ec221436..814e4c9a 100644 --- a/source/templates/markdown/partials/activity.ejs +++ b/source/templates/markdown/partials/activity.ejs @@ -39,7 +39,7 @@ * 🚮 Deleted <%= event.ref.type %> `<%= event.ref.name %>` from [<%= repo %>](https://github.com/<%= repo %>) <%_ } _%> <%_ if (plugins.activity.timestamps) { _%> - * *On <%= f.date(timestamp, {timeStyle:"short", dateStyle:"short", timeZone:config.timezone?.name}) %>* + * *On <%= f.date(timestamp, {time:true, date:true, timeZone:config.timezone?.name}) %>* <%_ } _%> <%_ } _%> <%_ } else { _%> diff --git a/source/templates/markdown/partials/posts.ejs b/source/templates/markdown/partials/posts.ejs index 41a7dd51..96154304 100644 --- a/source/templates/markdown/partials/posts.ejs +++ b/source/templates/markdown/partials/posts.ejs @@ -19,7 +19,7 @@ <%= description %>
- Published on <%= f.date(new Date(date), {dateStyle:"short", timeZone:config.timezone?.name}) %> + Published on <%= f.date(new Date(date), {date:true, timeZone:config.timezone?.name}) %> @@ -27,7 +27,7 @@ <%_ } else if (plugins.posts.list.length) { _%> <%_ for (const {title, date, link} of plugins.posts.list) { _%> * [<%= title.trim() %>](<%= link %>) - * *Published on <%= f.date(new Date(date), {dateStyle:"short", timeZone:config.timezone?.name}) %>* + * *Published on <%= f.date(new Date(date), {date:true, timeZone:config.timezone?.name}) %>* <%_ } _%> <%_ } else { _%> No recent posts diff --git a/source/templates/markdown/partials/rss.ejs b/source/templates/markdown/partials/rss.ejs index 0f74f497..e2b0222a 100644 --- a/source/templates/markdown/partials/rss.ejs +++ b/source/templates/markdown/partials/rss.ejs @@ -5,7 +5,7 @@ <%_ } else if (plugins.rss.feed.length) { _%> <%_ for (const {title, link, date} of plugins.rss.feed) { _%> * [<%= title %>](<%= link %>) - * *Published on <%= f.date(new Date(date), {dateStyle:"short"}) %>* + * *Published on <%= f.date(new Date(date), {date:true}) %>* <%_ } _%> <%_ } else { _%> Empty RSS feed diff --git a/source/templates/repository/partials/activity.ejs b/source/templates/repository/partials/activity.ejs index 3b600d19..344325a7 100644 --- a/source/templates/repository/partials/activity.ejs +++ b/source/templates/repository/partials/activity.ejs @@ -169,7 +169,7 @@ <% } %> <% if (plugins.activity.timestamps) { %>
- <%= f.date(timestamp, {timeStyle:"short", dateStyle:"short", timeZone:config.timezone?.name}) %> + <%= f.date(timestamp, {time:true, date:true, timeZone:config.timezone?.name}) %>
<% } %> diff --git a/source/templates/repository/partials/rss.ejs b/source/templates/repository/partials/rss.ejs index e657fce1..5ec99710 100644 --- a/source/templates/repository/partials/rss.ejs +++ b/source/templates/repository/partials/rss.ejs @@ -18,7 +18,7 @@
<%= title %>
-
<%= f.date(new Date(date), {dateStyle:"short", timeZone:config.timezone?.name}) %>
+
<%= f.date(new Date(date), {date:true, timeZone:config.timezone?.name}) %>
<% } %>