Skip to content

Commit

Permalink
Merge pull request #4 from ndaidong/dev
Browse files Browse the repository at this point in the history
v1.1.3
  • Loading branch information
ndaidong authored Nov 18, 2017
2 parents 9d98460 + 762cd8c commit 1530a2a
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 155 deletions.
18 changes: 17 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
{
"extends": "eslint-config-ggc"
"env": {
"es6": true,
"node": true,
"browser": true
},
"extends": ["eslint:recommended", "google"],
"rules": {
"max-len": [
"error",
{
"code": 120,
"ignoreTrailingComments": true,
"ignoreComments": true,
"ignoreUrls": true
}
]
}
}
26 changes: 5 additions & 21 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
# Logs
logs
*.log
*.debug

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

.coveralls.yml
coverage
.nyc_output

.lint.html
yarn.lock
coverage.lcov
package-lock.json
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: node_js
node_js:
- "6.7"
- "6.4"
- "9.2"
- "8.0"
- "7.0"
- "6.0"
after_success:
- npm run coveralls
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,33 @@ Calculate average and scoring based on Wilson Score Equation

[![NPM](https://badge.fury.io/js/average-rating.svg)](https://badge.fury.io/js/average-rating)
[![Build Status](https://travis-ci.org/ndaidong/average-rating.svg?branch=master)](https://travis-ci.org/ndaidong/average-rating)
[![Coverage Status](https://coveralls.io/repos/github/ndaidong/average-rating/badge.svg?branch=master)](https://coveralls.io/github/ndaidong/average-rating?branch=master)
[![codecov](https://codecov.io/gh/ndaidong/average-rating/branch/master/graph/badge.svg)](https://codecov.io/gh/ndaidong/average-rating)
[![Dependency Status](https://gemnasium.com/badges/github.com/ndaidong/average-rating.svg)](https://gemnasium.com/github.com/ndaidong/average-rating)
[![Known Vulnerabilities](https://snyk.io/test/npm/average-rating/badge.svg)](https://snyk.io/test/npm/average-rating)
[![NSP Status](https://nodesecurity.io/orgs/techpush/projects/ff38031c-c6ba-4606-92e9-ebe07d649fa7/badge)](https://nodesecurity.io/orgs/techpush/projects/ff38031c-c6ba-4606-92e9-ebe07d649fa7)

![Google app on Google Play](http://i.imgur.com/NgQX5OW.png)

## Usage

```
var av = require('average-rating');
var rate = av.rate;
var average = av.average;
var score = av.score;
const {
score,
rate,
average
} = require('average-rating');
// from 1 to 5 stars
let rating = [134055, 57472, 143135, 365957, 1448459];
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
// from 1 to 5 stars
let rating = [134055, 57472, 143135, 365957, 1448459];
rate(rating); // --> 0.84
// calculate average
average(rating); // --> 4.4
```

# API reference
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
* Starting app
* @ndaidong
**/
exports = module.exports = require('./src/main');
exports.version = require('./package').version;
const main = require('./src/main');
main.version = require('./package').version;
module.exports = main;
26 changes: 14 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.1.12",
"version": "1.1.3",
"name": "average-rating",
"description": "Calculate average score and rating based on Wilson Score Equation",
"homepage": "https://www.npmjs.com/package/average-rating",
Expand All @@ -14,22 +14,24 @@
},
"scripts": {
"lint": "eslint .",
"snyk": "snyk test",
"nsp": "nsp check --output checkstyle",
"tape": "nyc tape test/start.js | tap-spec",
"report": "nyc report --reporter=lcov",
"pretest": "npm run lint && npm run snyk",
"tape:debug": "DEBUG=*:* nyc tape test/start.js | tap-spec",
"report": "nyc report --reporter=lcov > coverage.lcov && codecov",
"pretest": "npm run lint && npm run nsp",
"test": "npm run tape",
"posttest": "npm run report",
"coveralls": "npm test && cat ./coverage/lcov.info | coveralls"
"posttest": "CODECOV_TOKEN=47b63968-97ab-4ad1-8812-267ce37a3e5f npm run report",
"reset": "node reset"
},
"devDependencies": {
"coveralls": "latest",
"eslint": "latest",
"eslint-config-ggc": "latest",
"codecov": "^3.0.0",
"eslint": "^4.11.0",
"eslint-config-google": "^0.9.1",
"install-peers": "^1.0.2",
"nsp": "^2.8.1",
"nyc": "latest",
"snyk": "latest",
"tap-spec": "latest",
"tape": "latest"
"tap-spec": "^4.1.1",
"tape": "^4.8.0"
},
"keywords": [
"rating",
Expand Down
34 changes: 34 additions & 0 deletions reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env node

const {
existsSync,
unlinkSync,
} = require('fs');

const {
execSync,
} = require('child_process');

const dirs = [
'.nyc_output',
'coverage',
'node_modules',
];

const files = [
'yarn.lock',
'package-lock.json',
'coverage.lcov',
];

dirs.forEach((d) => {
execSync(`rm -rf ${d}`);
});

files.forEach((f) => {
if (existsSync(f)) {
unlinkSync(f);
}
});


67 changes: 36 additions & 31 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,50 @@
* Refer: https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval
**/

var getWilsonScore = (p, n) => {
const score = (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: getWilsonScore,
rate: (rating) => {
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];

return score(p, n);
};

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];
const average = (rating) => {
let total = rating.reduce((prev, current) => {
return prev + current;
}, 0);

return getWilsonScore(p, n);
},
average: (rating) => {
let total = rating.reduce((prev, current) => {
return prev + current;
});
if (total === 0) {
return 0;
}
let sum = 0;
let k = 1;
rating.forEach((item) => {
sum += item * k;
k++;
});
let r = sum / total;
return r.toFixed(1);
if (total === 0) {
return 0;
}

let sum = 0;
let k = 1;
rating.forEach((item) => {
sum += item * k;
k++;
});
let r = sum / total;
return r.toFixed(1);
};

module.exports = {
score,
rate,
average,
};
7 changes: 3 additions & 4 deletions test/specs/average/average.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
* @ndaidong
*/

var test = require('tape');
const test = require('tape');

var AverageRating = require('../../../index');
const AverageRating = require('../../../index');

var samples = require('../samples');
const samples = require('../samples');

test('Testing "average" method', (assert) => {

samples.forEach((sample) => {
let rating = sample.rating;
let expect = sample.expect.average;
Expand Down
7 changes: 3 additions & 4 deletions test/specs/rate/rate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
* @ndaidong
*/

var test = require('tape');
const test = require('tape');

var AverageRating = require('../../../index');
const AverageRating = require('../../../index');

var samples = require('../samples');
const samples = require('../samples');

test('Testing "rate" method', (assert) => {

samples.forEach((sample) => {
let rating = sample.rating;
let expect = sample.expect.score;
Expand Down
Loading

0 comments on commit 1530a2a

Please sign in to comment.