average-rating
Advanced tools
Comparing version 0.1.4 to 0.1.6
{ | ||
"version": "0.1.4", | ||
"version": "0.1.6", | ||
"name": "average-rating", | ||
@@ -4,0 +4,0 @@ "description": "Calculate average score and rating based on Wilson Score Equation", |
@@ -11,11 +11,15 @@ # average-rating | ||
``` | ||
import {score, average} from 'average-rating'; | ||
import {rate, average, score} from 'average-rating'; | ||
// from 1 to 5 stars | ||
let rating = [134055, 57472, 143135, 365957, 1448459]; | ||
score(rating); // --> 0.84 | ||
rate(rating); // --> 0.84 | ||
average(rating); // --> 4.4 | ||
// get Winson score for a pair of (Positive, Negative) voting | ||
score(0, 1000); // --> 0 | ||
score(1000, 0); // --> 0.96 | ||
score(1000, 1000); // --> 0.48 | ||
``` | ||
## Test | ||
@@ -22,0 +26,0 @@ |
@@ -9,4 +9,13 @@ /** | ||
var getWilsonScore = (p, n) => { | ||
if(p === 0 && n === 0){ | ||
return 0; | ||
} | ||
let r = ((p + 1.9208) / (p + n) - 1.96 * Math.sqrt((p * n) / (p + n) + 0.9604) / (p + n)) / (1 + 3.8416 / (p + n)); | ||
return r.toFixed(2); | ||
} | ||
module.exports = { | ||
score: (rating) => { | ||
score: getWilsonScore, | ||
rate: (rating) => { | ||
@@ -23,8 +32,3 @@ var n = 0, p = 0; | ||
if(p === 0 && n === 0){ | ||
return 0; | ||
} | ||
let r = ((p + 1.9208) / (p + n) - 1.96 * Math.sqrt((p * n) / (p + n) + 0.9604) / (p + n)) / (1 + 3.8416 / (p + n)); | ||
return r.toFixed(2); | ||
return getWilsonScore(p, n); | ||
}, | ||
@@ -31,0 +35,0 @@ average: (rating) => { |
@@ -14,3 +14,2 @@ /** | ||
var chai = require('chai'); | ||
var bella = require('bellajs'); | ||
@@ -17,0 +16,0 @@ chai.should(); |
@@ -14,3 +14,2 @@ /** | ||
var chai = require('chai'); | ||
var bella = require('bellajs'); | ||
@@ -20,4 +19,2 @@ chai.should(); | ||
var samples = require('../samples'); | ||
var rootDir = '../../../src/'; | ||
@@ -27,9 +24,27 @@ | ||
let samples = [ | ||
{p: 0, n: 1, e: 0}, | ||
{p: 0, n: 5, e: 0}, | ||
{p: 0, n: 10, e: 0}, | ||
{p: 0, n: 20, e: 0}, | ||
{p: 0, n: 100, e: 0}, | ||
{p: 0, n: 1000, e: 0}, | ||
{n: 0, p: 1, e: 0.21}, | ||
{n: 0, p: 5, e: 0.57}, | ||
{n: 0, p: 10, e: 0.72}, | ||
{n: 0, p: 20, e: 0.84}, | ||
{n: 0, p: 100, e: 0.96}, | ||
{n: 0, p: 1000, e: 1}, | ||
{n: 1, p: 1, e: 0.09}, | ||
{n: 5, p: 5, e: 0.24}, | ||
{n: 500, p: 500, e: 0.47}, | ||
{n: 1000, p: 1000, e: 0.48} | ||
]; | ||
describe('.score()', () => { | ||
samples.forEach((sample) => { | ||
let rating = sample.rating; | ||
let expectation = sample.expect.score; | ||
describe(rating.join(', '), () => { | ||
let result = Number(AverageRating.score(rating)); | ||
let expectation = sample.e; | ||
describe(`Positive: ${sample.p}, Negative: ${sample.n}`, () => { | ||
let result = Number(AverageRating.score(sample.p, sample.n)); | ||
it(' should be ' + expectation, (done) => { | ||
@@ -36,0 +51,0 @@ expect(result).to.equal(expectation); |
@@ -10,3 +10,3 @@ 'use strict'; | ||
var dirs = ['average', 'score']; | ||
var dirs = ['average', 'rate', 'score']; | ||
dirs.forEach((dir) => { | ||
@@ -13,0 +13,0 @@ let where = './test/specs/' + dir; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12789
14
287
32
5