More precise star display with test (#1179)
This commit is contained in:
@@ -8,8 +8,14 @@ const moment = require('moment');
|
||||
moment().format();
|
||||
|
||||
function starRating(rating) {
|
||||
const flooredRating = Math.floor(rating);
|
||||
let stars = '';
|
||||
while (stars.length < rating) { stars += '★'; }
|
||||
while (stars.length < flooredRating) { stars += '★'; }
|
||||
const decimal = rating - flooredRating;
|
||||
if (decimal >= 0.875) { stars += '★'; }
|
||||
else if (decimal >= 0.625) { stars += '¾'; }
|
||||
else if (decimal >= 0.375) { stars += '½'; }
|
||||
else if (decimal >= 0.125) { stars += '¼'; }
|
||||
while (stars.length < 5) { stars += '☆'; }
|
||||
return stars;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
const assert = require('assert');
|
||||
const {
|
||||
maybePluralize
|
||||
maybePluralize,
|
||||
starRating
|
||||
} = require('./text-formatters');
|
||||
|
||||
describe('text formatters', function() {
|
||||
@@ -17,4 +18,12 @@ describe('text formatters', function() {
|
||||
assert.equal(maybePluralize('box', [123, 456], 'boxes'), 'boxes');
|
||||
assert.equal(maybePluralize('box', undefined, 'boxes'), 'boxes');
|
||||
});
|
||||
|
||||
it('should format star rating', function () {
|
||||
assert.equal(starRating(4.9), '★★★★★');
|
||||
assert.equal(starRating(3.7), '★★★¾☆');
|
||||
assert.equal(starRating(2.566), '★★½☆☆');
|
||||
assert.equal(starRating(2.2), '★★¼☆☆');
|
||||
assert.equal(starRating(3), '★★★☆☆');
|
||||
})
|
||||
});
|
||||
|
||||
@@ -6385,7 +6385,7 @@ cache(function(data, match, sendBadge, request) {
|
||||
badgeData.colorscheme = floorCountColor(rating, 2, 3, 4);
|
||||
break;
|
||||
case 'stars':
|
||||
rating = Math.round(value.ratingValue);
|
||||
rating = parseFloat(value.ratingValue);
|
||||
badgeData.text[0] = data.label || 'rating';
|
||||
badgeData.text[1] = starRating(rating);
|
||||
badgeData.colorscheme = floorCountColor(rating, 2, 3, 4);
|
||||
|
||||
@@ -11,7 +11,7 @@ const isVPlusTripleDottedVersion = withRegex(/^v[0-9]+.[0-9]+.[0-9]+$/);
|
||||
|
||||
const isVPlusDottedVersionAtLeastOne = withRegex(/^v\d+(\.\d+)?(\.\d+)?$/);
|
||||
|
||||
const isStarRating = withRegex(/^[\u2605\u2606]{5}$/);
|
||||
const isStarRating = withRegex(/^(?=.{5}$)(\u2605{0,5}[\u00BC\u00BD\u00BE]?\u2606{0,5})$/);
|
||||
|
||||
const isMetric = withRegex(/^[0-9]+[kMGTPEZY]?$/);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user