badgen.net/libs/utils/stars.ts

12 wiersze
400 B
TypeScript
Czysty Zwykły widok Historia

2019-06-03 12:19:00 +00:00
export default (rating, max = 5) => {
2018-07-26 13:53:45 +00:00
const base = Math.floor(rating)
const fraction = rating - base
const full = '★'.repeat(fraction < 0.66 ? base : base + 1)
// TODO: update when Unicode 11 goes mainstream
// between 0.33 and 0.66 should be `half star` symbol
const half = fraction >= 0.33 && fraction <= 0.66 ? '★' : ''
2019-06-03 12:19:00 +00:00
// @ts-ignore
2018-07-26 13:53:45 +00:00
return (full + half).padEnd(max, '☆')
}