average-rating
Advanced tools
Comparing version 1.1.4 to 1.1.5
{ | ||
"env": { | ||
"es6": true, | ||
"node": true, | ||
"browser": true | ||
}, | ||
"extends": ["eslint:recommended", "google"], | ||
"rules": { | ||
"max-len": [ | ||
"error", | ||
{ | ||
"code": 120, | ||
"ignoreTrailingComments": true, | ||
"ignoreComments": true, | ||
"ignoreUrls": true | ||
} | ||
] | ||
} | ||
"extends": "eslint-config-goes" | ||
} |
{ | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"name": "average-rating", | ||
@@ -27,9 +27,9 @@ "description": "Calculate average score and rating based on Wilson Score Equation", | ||
"devDependencies": { | ||
"codecov": "^3.0.0", | ||
"eslint": "^4.11.0", | ||
"eslint-config-google": "^0.9.1", | ||
"nsp": "^3.1.0", | ||
"nyc": "^11.3.0", | ||
"codecov": "^3.0.1", | ||
"eslint": "^4.19.1", | ||
"eslint-config-goes": "^1.0.0", | ||
"nsp": "^3.2.1", | ||
"nyc": "^11.7.1", | ||
"tap-spec": "^4.1.1", | ||
"tape": "^4.8.0" | ||
"tape": "^4.9.0" | ||
}, | ||
@@ -36,0 +36,0 @@ "keywords": [ |
@@ -49,2 +49,15 @@ # average-rating | ||
##### Update | ||
- Since v1.1.5, this `rate` method accepts custom range of ratings. 5 or more values are OK. | ||
``` | ||
let input = [3, 4, 2, 6, 12, 46, 134, 213, 116, 91, 45, 15, 58, 96, 1654]; // 15 values | ||
rate(input); // => 0.85 | ||
rate([3, 4, 2, 6, 12, 46, 134, 213, 116, 91]); // => 0.74 | ||
``` | ||
### .average(Array ratings) | ||
@@ -51,0 +64,0 @@ |
@@ -12,20 +12,23 @@ /** | ||
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 Number(r.toFixed(2)); | ||
}; | ||
const rate = (rating) => { | ||
let n = 0; | ||
let p = 0; | ||
n += rating[0]; | ||
n += rating[1] * 0.75; | ||
p += rating[1] * 0.25; | ||
n += rating[2] * 0.5; | ||
p += rating[2] * 0.5; | ||
n += rating[3] * 0.25; | ||
p += rating[3] * 0.75; | ||
p += rating[4]; | ||
let size = rating.length; | ||
let n = rating[0]; | ||
let p = rating[size - 1]; | ||
let step = (1 / (size - 1)).toFixed(2); | ||
let totalStep = size - 1; | ||
for (let i = 1; i < totalStep; i++) { | ||
let ep = (step * i).toFixed(2); | ||
p += rating[i] * ep; | ||
n += rating[totalStep - i] * ep; | ||
} | ||
return score(p, n); | ||
}; | ||
const average = (rating) => { | ||
@@ -47,5 +50,6 @@ let total = rating.reduce((prev, current) => { | ||
let r = sum / total; | ||
return r.toFixed(1); | ||
return Number(r.toFixed(1)); | ||
}; | ||
module.exports = { | ||
@@ -52,0 +56,0 @@ score, |
@@ -18,3 +18,3 @@ /** | ||
let s = rating.join(', '); | ||
assert.deepEqual(actual, expect, `.rage([${s}]) should be ${expect}`); | ||
assert.deepEqual(actual, expect, `.rate([${s}]) should be ${expect}`); | ||
}); | ||
@@ -24,1 +24,39 @@ | ||
}); | ||
test('Testing "rate" with custom range', (assert) => { | ||
let data = [ | ||
{ | ||
input: [3, 4, 2, 6, 12, 46, 134, 213, 116, 91, 45, 15, 58, 96, 1654], | ||
expect: 0.85, | ||
}, | ||
{ | ||
input: [3, 4, 2, 6, 12, 46, 134, 213, 116, 91], | ||
expect: 0.74, | ||
}, | ||
{ | ||
input: [1311, 655, 1008, 1847, 4685, 13522, 31570, 34238, 18180, 11029], | ||
expect: 0.72, | ||
}, | ||
{ | ||
input: [125, 166, 17, 290, 400, 310], | ||
expect: 0.62, | ||
}, | ||
{ | ||
input: [125, 166, 17, 290, 400, 310, 1800], | ||
expect: 0.79, | ||
}, | ||
]; | ||
data.map((sample) => { | ||
let { | ||
input, | ||
expect, | ||
} = sample; | ||
let actual = AverageRating.rate(input); | ||
let params = input.join(', '); | ||
assert.deepEqual(actual, expect, `.rate([${params}]) should be ${expect}`); | ||
return actual; | ||
}); | ||
assert.end(); | ||
}); |
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
11472
360
82