npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ndaidong/average-rating

v3.0.1

Published

Calculate average score and rating based on Wilson Score Equation

Downloads

18

Readme

average-rating

Calculate average and scoring based on Wilson Score Equation

CodeQL CI test Coverage Status NPM JSR

Google app on Google Play

Setup & Usage

Deno

https://jsr.io/@ndaidong/average-rating

deno add @ndaidong/average-rating

# npm (use any of npx, yarn dlx, pnpm dlx, or bunx)
npx jsr add @ndaidong/average-rating
// es6 module
import { average, rate, score } from "@ndaidong/average-rating";

// CommonJS
const {
  score,
  rate,
  average,
} = require("@ndaidong/average-rating");

score(80, 20); // => 0.71
rate([134055, 57472, 143135, 365957, 1448459]); // => 0.84
average([134055, 57472, 143135, 365957, 1448459]); // => 4.4

You can use JSR packages without an install step using jsr: specifiers:

import { average } from "jsr:@ndaidong/average-rating";

average([134055, 57472, 143135, 365957, 1448459]); // => 4.4

You can also use npm: specifiers as before:

import { average } from "npm:@ndaidong/average-rating";

average([134055, 57472, 143135, 365957, 1448459]); // => 4.4

Or import from esm.sh

import { average } from "https://esm.sh/@ndaidong/average-rating";

average([134055, 57472, 143135, 365957, 1448459]); // => 4.4

Node.js & Bun

https://www.npmjs.com/package/@ndaidong/average-rating

npm i @ndaidong/average-rating
# pnpm
pnpm i @ndaidong/average-rating
# yarn
yarn add @ndaidong/average-rating
# bun
bun add @ndaidong/average-rating
import { average } from "@ndaidong/average-rating";

average([134055, 57472, 143135, 365957, 1448459]); // => 4.4

You can also use CJS style:

const { average } = require("@ndaidong/average-rating");

average([134055, 57472, 143135, 365957, 1448459]); // => 4.4

Browsers:

<script type="module">
import { average } from "https://esm.sh/@ndaidong/average-rating";
// import { average } from 'https://unpkg.com/@ndaidong/average-rating/esm/mod.js';

average([134055, 57472, 143135, 365957, 1448459]) // => 4.4
</script>

APIs

.score(Number positive, Number negative)

This method returns a normalized value between 0 and 1, but it's applicable for systems with only positive and negative ratings (like/dislike, thumbs up/thumbs down). Examples include videos on YouTube or answers on Stack Overflow. In these systems, users can express their opinion by voting for either a positive or negative option.

Let's illustrate how this method works with a blog post that received 80 likes and 20 dislikes:

import { score } from "@ndaidong/average-rating";

score(80, 20); // => 0.71

.rate(Array ratings)

This method returns a normalized value between 0 and 1, commonly used in rating systems with 5 levels. Examples include applications on Google Play Store or books on Amazon. In these systems, each item receives a user rating between 1 and 5 stars.

Let's take a product with a large volume of reviews as an example. Here's how we calculate its rating:

  • 134,055 customers rated it 1 star
  • 57,472 gave it a 2-star rating
  • There are 143,135 ratings of 3 stars
  • It received a 4-star rating from 365,957 users
  • And a whopping 1,448,459 customers rated it 5 stars
import { rate } from "@ndaidong/average-rating";

rate([134055, 57472, 143135, 365957, 1448459]); // => 0.84
Update
  • Since v1.1.5, this rate method accepts custom range of ratings. 5 or more values are OK.
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

.average(Array ratings)

Return a value from 0 to 5.

Calculate normal average value for the systems of 5 rating levels.

import { average } from "@ndaidong/average-rating";

average([134055, 57472, 143135, 365957, 1448459]); // => 4.4

Development

Since v3.x.x, we switched to Deno platform, and use DNT to build Node.js packages.

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

# test
deno test

# build npm packages
deno task build

cd npm
node test_runner.js

License

The MIT License (MIT)