Refacto update formatters (#335)
This commit is contained in:
@@ -32,6 +32,7 @@ export default async function metrics({login, q}, {graphql, rest, plugins, conf,
|
||||
templates:Templates,
|
||||
metadata:conf.metadata,
|
||||
...utils,
|
||||
...utils.formatters({timeZone:q["config.timezone"]}),
|
||||
...(/markdown/.test(convert)
|
||||
? {
|
||||
imgb64(url, options) {
|
||||
|
||||
@@ -48,8 +48,18 @@ export function s(value, end = "") {
|
||||
return value !== 1 ? {y:"ies", "":"s"}[end] : end
|
||||
}
|
||||
|
||||
/**Formatters */
|
||||
export function formatters({timeZone} = {}) {
|
||||
//Check options
|
||||
try {
|
||||
new Date().toLocaleString("fr", {timeZoneName:"short", timeZone})
|
||||
}
|
||||
catch {
|
||||
timeZone = undefined
|
||||
}
|
||||
|
||||
/**Formatter */
|
||||
export function format(n, {sign = false, unit = true, fixed} = {}) {
|
||||
const format = function(n, {sign = false, unit = true, fixed} = {}) {
|
||||
if (unit) {
|
||||
for (const {u, v} of [{u:"b", v:10 ** 9}, {u:"m", v:10 ** 6}, {u:"k", v:10 ** 3}]) {
|
||||
if (n / v >= 1)
|
||||
@@ -60,39 +70,38 @@ export function format(n, {sign = false, unit = true, fixed} = {}) {
|
||||
}
|
||||
|
||||
/**Bytes formatter */
|
||||
export function bytes(n) {
|
||||
format.bytes = function (n) {
|
||||
for (const {u, v} of [{u:"E", v:10 ** 18}, {u:"P", v:10 ** 15}, {u:"T", v:10 ** 12}, {u:"G", v:10 ** 9}, {u:"M", v:10 ** 6}, {u:"k", v:10 ** 3}]) {
|
||||
if (n / v >= 1)
|
||||
return `${(n / v).toFixed(2).substr(0, 4).replace(/[.]0*$/, "")} ${u}B`
|
||||
}
|
||||
return `${n} byte${n > 1 ? "s" : ""}`
|
||||
}
|
||||
format.bytes = bytes
|
||||
|
||||
/**Percentage formatter */
|
||||
export function percentage(n, {rescale = true} = {}) {
|
||||
format.percentage = function (n, {rescale = true} = {}) {
|
||||
return `${
|
||||
(n * (rescale ? 100 : 1)).toFixed(2)
|
||||
.replace(/(?<=[.])(?<decimal>[1-9]*)0+$/, "$<decimal>")
|
||||
.replace(/[.]$/, "")
|
||||
}%`
|
||||
}
|
||||
format.percentage = percentage
|
||||
|
||||
/**Text ellipsis formatter */
|
||||
export function ellipsis(text, {length = 20} = {}) {
|
||||
format.ellipsis = function(text, {length = 20} = {}) {
|
||||
text = `${text}`
|
||||
if (text.length < length)
|
||||
return text
|
||||
return `${text.substring(0, length)}…`
|
||||
}
|
||||
format.ellipsis = ellipsis
|
||||
|
||||
/**Date formatter */
|
||||
export function date(string, options) {
|
||||
return new Intl.DateTimeFormat("en-GB", options).format(new Date(string))
|
||||
format.date = function(string, options) {
|
||||
return new Intl.DateTimeFormat("en-GB", {timeZone, ...options}).format(new Date(string))
|
||||
}
|
||||
|
||||
return {format}
|
||||
}
|
||||
format.date = date
|
||||
|
||||
/**Array shuffler */
|
||||
export function shuffle(array) {
|
||||
|
||||
@@ -27,7 +27,7 @@ export default async function({login, q, imports, data, computed, graphql, queri
|
||||
.sort((a, b) => (order[b.rank] + b.progress * 0.99) - (order[a.rank] + a.progress * 0.99))
|
||||
.map(({title, unlock, ...achievement}) => ({
|
||||
title:({S:`Master ${title.toLocaleLowerCase()}`, A:`Super ${title.toLocaleLowerCase()}`, B:`Great ${title.toLocaleLowerCase()}`}[achievement.rank] ?? title),
|
||||
unlock:!/invalid date/i.test(unlock) ? `${imports.date(unlock, {timeStyle:"short", timeZone:data.config.timezone?.name})} on ${imports.date(unlock, {dateStyle:"short", timeZone:data.config.timezone?.name})}` : null,
|
||||
unlock:!/invalid date/i.test(unlock) ? `${imports.format.date(unlock, {timeStyle:"short"})} on ${imports.format.date(unlock, {dateStyle:"short"})}` : null,
|
||||
...achievement,
|
||||
}))
|
||||
.map(({icon, ...achievement}) => ({icon:icon.replace(/#primary/g, colors[achievement.rank][0]).replace(/#secondary/g, colors[achievement.rank][1]), ...achievement}))
|
||||
|
||||
@@ -73,7 +73,7 @@ export default async function({login, q}, {conf, data, rest, graphql, plugins, q
|
||||
}
|
||||
|
||||
//Total disk usage
|
||||
computed.diskUsage = `${imports.bytes(data.user.repositories.totalDiskUsage * 1000)}`
|
||||
computed.diskUsage = `${imports.format.bytes(data.user.repositories.totalDiskUsage * 1000)}`
|
||||
|
||||
//Compute licenses stats
|
||||
computed.licenses.favorite = Object.entries(computed.licenses.used).sort(([_an, a], [_bn, b]) => b - a).slice(0, 1).map(([name, _value]) => name) ?? ""
|
||||
|
||||
@@ -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.date(played_at, {timeStyle:"short", timeZone:data.config.timezone?.name})} on ${imports.date(played_at, {dateStyle:"short", timeZone:data.config.timezone?.name})}` : null,
|
||||
played_at:played_at ? `${imports.format.date(played_at, {timeStyle:"short"})} on ${imports.format.date(played_at, {dateStyle:"short"})}` : null,
|
||||
}))
|
||||
//Ensure no duplicate are added
|
||||
for (const track of loaded) {
|
||||
|
||||
@@ -32,14 +32,14 @@ export default async function({login, q, imports, data, account}, {enabled = fal
|
||||
//Load and format answers
|
||||
console.debug(`metrics/compute/${login}/plugins > stackoverflow > querying api for ${key}`)
|
||||
const {data:{items}} = await imports.axios.get(`${api.user}/answers?site=stackoverflow&pagesize=${limit}&filter=${filters.answer}&${sort}`)
|
||||
result[key] = await Promise.all(items.map(item => format.answer(item, {imports, data, codelines})))
|
||||
result[key] = await Promise.all(items.map(item => format.answer(item, {imports, codelines})))
|
||||
console.debug(`metrics/compute/${login}/plugins > stackoverflow > loaded ${result[key].length} items`)
|
||||
//Load related questions
|
||||
const ids = result[key].map(({question_id}) => question_id).filter(id => id)
|
||||
if (ids) {
|
||||
console.debug(`metrics/compute/${login}/plugins > stackoverflow > loading ${ids.length} related items`)
|
||||
const {data:{items}} = await imports.axios.get(`${api.base}/questions/${ids.join(";")}?site=stackoverflow&filter=${filters.question}`)
|
||||
await Promise.all(items.map(item => format.question(item, {imports, data, codelines})))
|
||||
await Promise.all(items.map(item => format.question(item, {imports, codelines})))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,14 +48,14 @@ export default async function({login, q, imports, data, account}, {enabled = fal
|
||||
//Load and format questions
|
||||
console.debug(`metrics/compute/${login}/plugins > stackoverflow > querying api for ${key}`)
|
||||
const {data:{items}} = await imports.axios.get(`${api.user}/questions?site=stackoverflow&pagesize=${limit}&filter=${filters.question}&${sort}`)
|
||||
result[key] = await Promise.all(items.map(item => format.question(item, {imports, data, codelines})))
|
||||
result[key] = await Promise.all(items.map(item => format.question(item, {imports, codelines})))
|
||||
console.debug(`metrics/compute/${login}/plugins > stackoverflow > loaded ${result[key].length} items`)
|
||||
//Load related answers
|
||||
const ids = result[key].map(({accepted_answer_id}) => accepted_answer_id).filter(id => id)
|
||||
if (ids) {
|
||||
console.debug(`metrics/compute/${login}/plugins > stackoverflow > loading ${ids.length} related items`)
|
||||
const {data:{items}} = await imports.axios.get(`${api.base}/answers/${ids.join(";")}?site=stackoverflow&filter=${filters.answer}`)
|
||||
await Promise.all(items.map(item => format.answer(item, {imports, data, codelines})))
|
||||
await Promise.all(items.map(item => format.answer(item, {imports, codelines})))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ const format = {
|
||||
return text.replace(/<!-- language: lang-(?<lang>\w+) -->\s*(?<snippet> {4}[\s\S]+?)(?=(?:<!-- end snippet -->)|(?:<!-- language: lang-))/g, "```$<lang>\n$<snippet>```")
|
||||
},
|
||||
/**Format answers */
|
||||
async answer({body_markdown:body, score, up_vote_count:upvotes, down_vote_count:downvotes, is_accepted:accepted, comment_count:comments = 0, creation_date, owner:{display_name:author}, link, answer_id:id, question_id}, {imports, data, codelines}) {
|
||||
async answer({body_markdown:body, score, up_vote_count:upvotes, down_vote_count:downvotes, is_accepted:accepted, comment_count:comments = 0, creation_date, owner:{display_name:author}, link, answer_id:id, question_id}, {imports, codelines}) {
|
||||
const formatted = {
|
||||
type:"answer",
|
||||
body:await imports.markdown(format.code(imports.htmlunescape(body)), {codelines}),
|
||||
@@ -89,7 +89,7 @@ const format = {
|
||||
accepted,
|
||||
comments,
|
||||
author,
|
||||
created:imports.date(creation_date * 1000, {dateStyle:"short", timeZone:data.config.timezone?.name}),
|
||||
created:imports.format.date(creation_date * 1000, {dateStyle:"short"}),
|
||||
link,
|
||||
id,
|
||||
question_id,
|
||||
@@ -120,7 +120,7 @@ const format = {
|
||||
question_id:id,
|
||||
accepted_answer_id = null,
|
||||
},
|
||||
{imports, data, codelines},
|
||||
{imports, codelines},
|
||||
) {
|
||||
const formatted = {
|
||||
type:"question",
|
||||
@@ -136,7 +136,7 @@ const format = {
|
||||
comments,
|
||||
views,
|
||||
author,
|
||||
created:imports.date(creation_date * 1000, {dateStyle:"short", timeZone:data.config.timezone?.name}),
|
||||
created:imports.format.date(creation_date * 1000, {dateStyle:"short"}),
|
||||
link,
|
||||
id,
|
||||
accepted_answer_id,
|
||||
|
||||
@@ -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.date(tweet.created_at, {timeStyle:"short", timeZone:data.config.timezone?.name})} on ${imports.date(tweet.created_at, {dateStyle:"short", timeZone:data.config.timezone?.name})}`
|
||||
tweet.createdAt = `${imports.format.date(tweet.created_at, {timeStyle:"short"})} on ${imports.format.date(tweet.created_at, {dateStyle:"short"})}`
|
||||
tweet.text = imports.htmlescape(
|
||||
//Escape tags
|
||||
imports.htmlescape(tweet.text, {"<":true, ">":true})
|
||||
|
||||
Reference in New Issue
Block a user