Refactor welcome script with utils
This commit is contained in:
parent
88253e5478
commit
0f1818c9b9
@ -107,10 +107,10 @@ async function populate (bios) {
|
|||||||
return await Promise.all(
|
return await Promise.all(
|
||||||
bios.map(
|
bios.map(
|
||||||
async bio => {
|
async bio => {
|
||||||
bio.user.since = await fetchItem(bio.user.since)
|
bio.user.since = await util.fetchItem(bio.user.since)
|
||||||
bio.user.items = await fetchUserItems(bio.user.name)
|
bio.user.items = await util.fetchUserItems(bio.user.name)
|
||||||
bio.user.credits = sumBy(bio.user.items, 'credits')
|
bio.user.credits = util.sumBy(bio.user.items, 'credits')
|
||||||
bio.user.sats = sumBy(bio.user.items, 'sats') - bio.user.credits
|
bio.user.sats = util.sumBy(bio.user.items, 'sats') - bio.user.credits
|
||||||
if (bio.user.sats > 0 || bio.user.credits > 0) {
|
if (bio.user.sats > 0 || bio.user.credits > 0) {
|
||||||
bio.user.satstandard = bio.user.sats / (bio.user.sats + bio.user.credits)
|
bio.user.satstandard = bio.user.sats / (bio.user.sats + bio.user.credits)
|
||||||
} else {
|
} else {
|
||||||
@ -129,14 +129,14 @@ async function printTable (bios) {
|
|||||||
for (const bio of bios) {
|
for (const bio of bios) {
|
||||||
const { user } = bio
|
const { user } = bio
|
||||||
|
|
||||||
const bioCreatedAt = formatDate(bio.createdAt)
|
const bioCreatedAt = util.formatDate(bio.createdAt)
|
||||||
let col2 = dateLink(bio)
|
let col2 = util.dateLink(bio)
|
||||||
if (Number(bio.id) !== user.since.id) {
|
if (Number(bio.id) !== user.since.id) {
|
||||||
const sinceCreatedAt = formatDate(user.since.createdAt)
|
const sinceCreatedAt = util.formatDate(user.since.createdAt)
|
||||||
// stacking since might not be the same item as the bio
|
// stacking since might not be the same item as the bio
|
||||||
// but it can still have been created on the same day
|
// but it can still have been created on the same day
|
||||||
if (bioCreatedAt !== sinceCreatedAt) {
|
if (bioCreatedAt !== sinceCreatedAt) {
|
||||||
col2 += ` (${dateLink(user.since)})`
|
col2 += ` (${util.dateLink(user.since)})`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(`| @${user.name} | ${col2} | ${user.nitems} | ${user.sats}/${user.credits} | ${user.satstandard.toFixed(2)} |`)
|
console.log(`| @${user.name} | ${col2} | ${user.nitems} | ${user.sats}/${user.credits} | ${user.satstandard.toFixed(2)} |`)
|
||||||
@ -147,23 +147,20 @@ async function printTable (bios) {
|
|||||||
return bios
|
return bios
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatDate (date) {
|
const util = {
|
||||||
|
formatDate (date) {
|
||||||
return new Date(date).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })
|
return new Date(date).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })
|
||||||
}
|
},
|
||||||
|
sumBy (arr, key) {
|
||||||
function sumBy (arr, key) {
|
|
||||||
return arr.reduce((acc, item) => acc + item[key], 0)
|
return arr.reduce((acc, item) => acc + item[key], 0)
|
||||||
}
|
},
|
||||||
|
itemLink (id) {
|
||||||
function itemLink (id) {
|
|
||||||
return `https://stacker.news/items/${id}`
|
return `https://stacker.news/items/${id}`
|
||||||
}
|
},
|
||||||
|
dateLink (item) {
|
||||||
function dateLink (item) {
|
return `[${this.formatDate(item.createdAt)}](${this.itemLink(item.id)})`
|
||||||
return `[${formatDate(item.createdAt)}](${itemLink(item.id)})`
|
},
|
||||||
}
|
async fetchItem (id) {
|
||||||
|
|
||||||
async function fetchItem (id) {
|
|
||||||
const data = await gql(`
|
const data = await gql(`
|
||||||
query Item($id: ID!) {
|
query Item($id: ID!) {
|
||||||
item(id: $id) {
|
item(id: $id) {
|
||||||
@ -173,9 +170,8 @@ async function fetchItem (id) {
|
|||||||
}`, { id }
|
}`, { id }
|
||||||
)
|
)
|
||||||
return data.item
|
return data.item
|
||||||
}
|
},
|
||||||
|
async fetchUserItems (name) {
|
||||||
async function fetchUserItems (name) {
|
|
||||||
const data = await gql(`
|
const data = await gql(`
|
||||||
query UserItems($name: String!) {
|
query UserItems($name: String!) {
|
||||||
items(sort: "user", type: "all", limit: 999, name: $name) {
|
items(sort: "user", type: "all", limit: 999, name: $name) {
|
||||||
@ -190,6 +186,7 @@ async function fetchUserItems (name) {
|
|||||||
)
|
)
|
||||||
return data.items.items
|
return data.items.items
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
assertSettings()
|
assertSettings()
|
||||||
.then(fetchRecentBios)
|
.then(fetchRecentBios)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user