2018-07-15 10:20:03 +00:00
|
|
|
const webstore = require('chrome-webstore')
|
2018-07-12 13:37:36 +00:00
|
|
|
const millify = require('millify')
|
|
|
|
|
2018-07-15 10:20:03 +00:00
|
|
|
const round = (value, decimals) =>
|
|
|
|
Number(Math.round(value + 'e' + decimals) + 'e-' + decimals)
|
|
|
|
|
|
|
|
const stars = (average) => {
|
|
|
|
const base = Math.floor(average)
|
|
|
|
const fraction = average - base
|
|
|
|
return ((full = ''.padEnd(base, '★')) =>
|
|
|
|
// TODO: update when Unicode 11 goes mainstream
|
|
|
|
// between 0.33 and 0.66 should be `half star` symbol
|
|
|
|
fraction >= 0.33 && fraction <= 0.66 ? full.padEnd(base + 1, '★') :
|
|
|
|
fraction > 0.66 ? full.padEnd(base + 1, '★') : full
|
|
|
|
)().padEnd(5, '☆')
|
|
|
|
}
|
|
|
|
|
2018-07-12 13:37:36 +00:00
|
|
|
module.exports = async function (method, ...args) {
|
2018-07-15 10:20:03 +00:00
|
|
|
const meta = await webstore.detail({id: args[0]})
|
2018-07-12 13:37:36 +00:00
|
|
|
switch (method) {
|
|
|
|
case 'v':
|
|
|
|
return {
|
|
|
|
subject: 'chrome web store',
|
|
|
|
status: meta.version,
|
|
|
|
color: 'blue'
|
|
|
|
}
|
|
|
|
case 'users':
|
|
|
|
return {
|
|
|
|
subject: 'users',
|
2018-07-15 10:20:03 +00:00
|
|
|
status: millify(parseInt(meta.users.replace(',', ''))),
|
2018-07-12 13:37:36 +00:00
|
|
|
color: 'green'
|
|
|
|
}
|
|
|
|
case 'price':
|
|
|
|
return {
|
|
|
|
subject: 'price',
|
2018-07-15 10:20:03 +00:00
|
|
|
status: meta.price,
|
2018-07-12 13:37:36 +00:00
|
|
|
color: 'green'
|
|
|
|
}
|
|
|
|
case 'rating':
|
|
|
|
return {
|
|
|
|
subject: 'rating',
|
2018-07-15 10:20:03 +00:00
|
|
|
status: `${round(meta.rating.average, 2)}/5`,
|
2018-07-12 13:37:36 +00:00
|
|
|
color: 'green'
|
|
|
|
}
|
|
|
|
case 'stars':
|
|
|
|
return {
|
|
|
|
subject: 'stars',
|
2018-07-15 10:20:03 +00:00
|
|
|
status: stars(meta.rating.average),
|
2018-07-12 13:37:36 +00:00
|
|
|
color: 'green'
|
|
|
|
}
|
|
|
|
case 'rating-count':
|
|
|
|
return {
|
|
|
|
subject: 'rating count',
|
2018-07-15 10:20:03 +00:00
|
|
|
status: `${meta.rating.count} total`,
|
2018-07-12 13:37:36 +00:00
|
|
|
color: 'green'
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return {
|
|
|
|
subject: 'chrome',
|
|
|
|
status: 'unknown',
|
|
|
|
color: 'grey'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|