Co-authored-by: Jabbar Memon <jabbar@zoop.one> Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
'use strict'
|
|
|
|
const { starRating } = require('../text-formatters')
|
|
const { floorCount: floorCountColor } = require('../color-formatters')
|
|
const { BaseVaadinDirectoryService } = require('./vaadin-directory-base')
|
|
|
|
module.exports = class VaadinDirectoryRating extends BaseVaadinDirectoryService {
|
|
static category = 'rating'
|
|
|
|
static route = {
|
|
base: 'vaadin-directory',
|
|
pattern: ':format(star|stars|rating)/:packageName',
|
|
}
|
|
|
|
static examples = [
|
|
{
|
|
title: 'Vaadin Directory',
|
|
pattern: ':format(stars|rating)/:packageName',
|
|
namedParams: { format: 'rating', packageName: 'vaadinvaadin-grid' },
|
|
staticPreview: this.render({ format: 'rating', score: 4.75 }),
|
|
keywords: ['vaadin-directory'],
|
|
},
|
|
]
|
|
|
|
static defaultBadgeData = {
|
|
label: 'rating',
|
|
}
|
|
|
|
static render({ format, score }) {
|
|
const rating = (Math.round(score * 10) / 10).toFixed(1)
|
|
if (format === 'rating') {
|
|
return {
|
|
label: 'rating',
|
|
message: `${rating}/5`,
|
|
color: floorCountColor(rating, 2, 3, 4),
|
|
}
|
|
}
|
|
return {
|
|
label: 'stars',
|
|
message: starRating(rating),
|
|
color: floorCountColor(rating, 2, 3, 4),
|
|
}
|
|
}
|
|
|
|
async handle({ format, packageName }) {
|
|
const { averageRating } = await this.fetch({ packageName })
|
|
return this.constructor.render({ format, score: averageRating })
|
|
}
|
|
}
|