Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

average-rating

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

average-rating - npm Package Compare versions

Comparing version 1.2.1 to 2.0.0

build.js

31

package.json
{
"version": "1.2.1",
"version": "2.0.0",
"name": "average-rating",

@@ -11,16 +11,29 @@ "description": "Calculate average score and rating based on Wilson Score Equation",

"author": "@ndaidong",
"main": "./index.js",
"main": "./dist/cjs/average-rating.js",
"module": "./src/main.js",
"browser": "./dist/average-rating.min.js",
"type": "module",
"engines": {
"node": ">= 10.14.2"
"node": ">= 14"
},
"scripts": {
"lint": "eslint src tests/specs",
"pretest": "npm run lint",
"test": "tap tests/start.js --coverage --reporter=spec --coverage-report=html --no-browser",
"citest": "tap tests/start.js --coverage --reporter=spec --coverage-report=lcov --no-browser",
"lint": "standard ./src",
"pretest": "npm run lint && npm run build",
"test": "NODE_ENV=test jest --verbose --coverage=true --unhandled-rejections=strict --detectOpenHandles",
"build": "node build.js src/main.js",
"reset": "node reset"
},
"babel": {
"env": {
"test": {
"plugins": [
"@babel/plugin-transform-modules-commonjs"
]
}
}
},
"devDependencies": {
"eslint-config-goes": "^1.1.8",
"tap": "^14.10.8"
"@babel/plugin-transform-modules-commonjs": "^7.16.7",
"esbuild": "^0.14.10",
"jest": "^27.3.1"
},

@@ -27,0 +40,0 @@ "keywords": [

@@ -8,8 +8,46 @@ # average-rating

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ndaidong_average-rating&metric=alert_status)](https://sonarcloud.io/dashboard?id=ndaidong_average-rating)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
![Google app on Google Play](https://i.imgur.com/XKEEpdb.png)
## Setup
- Node.js
```bash
npm i average-rating
# pnpm
pnpm i average-rating
# yarn
yarn add average-rating
```
- CDN
- ES6 Module: [average-rating.esm.js](https://unpkg.com/average-rating/dist/average-rating.esm.js)
- CommonJS: [average-rating.js](https://unpkg.com/average-rating/dist/cjs/average-rating.js)
- For old browsers: [average-rating.min.js](https://unpkg.com/average-rating/dist/average-rating.min.js)
## Usage
### Node.js:
Sync v14, ECMAScript modules [have became the official standard format](https://nodejs.org/docs/latest-v14.x/api/esm.html#esm_modules_ecmascript_modules).
Just [enable](https://nodejs.org/api/packages.html#determining-module-system) and enjoy with ES6 import/export syntax.
```js
import {
score,
rate,
average
} from 'average-rating'
```
For regular CommonJS environment, `require` can be used as below:
```js
const {

@@ -19,19 +57,45 @@ score,

average
} = require('average-rating');
} = require('average-rating/cjs/average-rating.js')
```
### Browsers:
Currently ECMAScript modules work fine on almost browsers:
```html
<script type="module">
import { average } from 'https://unpkg.com/average-rating/dist/average-rating.esm.js'
average([134055, 57472, 143135, 365957, 1448459])
</script>
```
With outdated browsers, we can use traditional way:
```html
<script type="text/javascript" src="https://unpkg.com/average-rating/dist/average-rating.min.js"></script>
<script>
const { average } = window.AverageRating
average([134055, 57472, 143135, 365957, 1448459])
</script>
```
Example:
```js
// get Winson score for a pair of (Positive, Negative) voting
score(0, 1000); // --> 0
score(1000, 0); // --> 0.96
score(1000, 1000); // --> 0.48
score(0, 1000) // --> 0
score(1000, 0) // --> 0.96
score(1000, 1000) // --> 0.48
// from 1 to 5 stars
const rating = [134055, 57472, 143135, 365957, 1448459];
rate(rating); // --> 0.84
const rating = [134055, 57472, 143135, 365957, 1448459]
rate(rating) // --> 0.84
// calculate average
average(rating); // --> 4.4
average(rating) // --> 4.4
```
# API reference
## APIs

@@ -54,11 +118,9 @@ ### .score(Number positive, Number negative)

```
const input = [3, 4, 2, 6, 12, 46, 134, 213, 116, 91, 45, 15, 58, 96, 1654]; // 15 values
rate(input); // => 0.85
```js
const 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
rate([3, 4, 2, 6, 12, 46, 134, 213, 116, 91]) // => 0.74
```
### .average(Array ratings)

@@ -73,3 +135,3 @@

```
```bash
git clone https://github.com/ndaidong/average-rating.git

@@ -76,0 +138,0 @@ cd average-rating

@@ -1,34 +0,37 @@

#!/usr/bin/env node
/**
* reset.js
* @ndaidong
**/
const {
import {
existsSync,
unlinkSync,
} = require('fs');
unlinkSync
} from 'fs'
const {
execSync,
} = require('child_process');
import { execSync } from 'child_process'
const dirs = [
'dist',
'docs',
'.nyc_output',
'coverage',
'node_modules',
];
'.nuxt'
]
const files = [
'yarn.lock',
'pnpm-lock.yaml',
'package-lock.json',
'coverage.lcov',
];
'coverage.lcov'
]
dirs.forEach((d) => {
execSync(`rm -rf ${d}`);
});
execSync(`rm -rf ${d}`)
})
files.forEach((f) => {
if (existsSync(f)) {
unlinkSync(f);
unlinkSync(f)
}
});
})

@@ -7,52 +7,43 @@ /**

const score = (p, n) => {
export const score = (p, n) => {
if (p === 0 && n === 0) {
return 0;
return 0
}
const r = ((p + 1.9208) / (p + n) - 1.96 * Math.sqrt(p * n / (p + n) + 0.9604) / (p + n)) / (1 + 3.8416 / (p + n));
return Number(r.toFixed(2));
};
const r = ((p + 1.9208) / (p + n) - 1.96 * Math.sqrt(p * n / (p + n) + 0.9604) / (p + n)) / (1 + 3.8416 / (p + n))
return Number(r.toFixed(2))
}
export const rate = (rating) => {
const size = rating.length
const rate = (rating) => {
const size = rating.length;
let n = rating[0]
let p = rating[size - 1]
let n = rating[0];
let p = rating[size - 1];
const step = (1 / (size - 1)).toFixed(2);
const totalStep = size - 1;
const step = (1 / (size - 1)).toFixed(2)
const totalStep = size - 1
for (let i = 1; i < totalStep; i++) {
const ep = (step * i).toFixed(2);
p += rating[i] * ep;
n += rating[totalStep - i] * ep;
const ep = (step * i).toFixed(2)
p += rating[i] * ep
n += rating[totalStep - i] * ep
}
return score(p, n);
};
return score(p, n)
}
const average = (rating) => {
export const average = (rating) => {
const total = rating.reduce((prev, current) => {
return prev + current;
}, 0);
return prev + current
}, 0)
if (total === 0) {
return 0;
return 0
}
let sum = 0;
let k = 1;
let sum = 0
let k = 1
rating.forEach((item) => {
sum += item * k;
k++;
});
const r = sum / total;
return Number(r.toFixed(1));
};
module.exports = {
score,
rate,
average,
};
sum += item * k
k++
})
const r = sum / total
return Number(r.toFixed(1))
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc