Skip to content

Commit

Permalink
Merge pull request #20 from ndaidong/2.0.4
Browse files Browse the repository at this point in the history
v2.0.4
  • Loading branch information
ndaidong authored Mar 12, 2023
2 parents 5f8e540 + c55eb3d commit 2ba7ccf
Show file tree
Hide file tree
Showing 15 changed files with 299 additions and 142 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
119 changes: 119 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"env": {
"es6": true,
"node": true,
"jest": true,
"browser": true
},
"globals": {},
"plugins": [],
"overrides": [],
"extends": ["eslint:recommended"],
"rules": {
"arrow-spacing": ["error", { "before": true, "after": true }],
"block-spacing": ["error", "always"],
"brace-style": ["error", "1tbs", { "allowSingleLine": true }],
"camelcase": ["error", {
"allow": ["^UNSAFE_"],
"properties": "never",
"ignoreGlobals": true
}],
"comma-dangle": ["error", {
"arrays": "always-multiline",
"objects": "always-multiline",
"imports": "never",
"exports": "never",
"functions": "never"
}],
"comma-spacing": ["error", { "before": false, "after": true }],
"eol-last": "error",
"eqeqeq": ["error", "always", { "null": "ignore" }],
"func-call-spacing": ["error", "never"],
"indent": [
"error",
2,
{
"MemberExpression": 1,
"FunctionDeclaration": {
"body": 1,
"parameters": 2
},
"SwitchCase": 1
}
],
"key-spacing": ["error", { "beforeColon": false, "afterColon": true }],
"keyword-spacing": ["error", { "before": true, "after": true }],
"lines-between-class-members": ["error", "always", { "exceptAfterSingleLine": true }],
"max-len": [
"error",
{
"code": 120,
"ignoreTrailingComments": true,
"ignoreComments": true,
"ignoreUrls": true
}
],
"max-lines": [
"error",
{
"max": 360,
"skipBlankLines": true,
"skipComments": false
}
],
"max-lines-per-function": [
"error",
{
"max": 250,
"skipBlankLines": true
}
],
"max-params": ["error", 4],
"no-array-constructor": "error",
"no-mixed-spaces-and-tabs": "error",
"no-multi-spaces": "error",
"no-multi-str": "error",
"no-multiple-empty-lines": [
"error",
{
"max": 1,
"maxEOF": 0
}
],
"no-restricted-syntax": [
"error",
"WithStatement",
"BinaryExpression[operator='in']"
],
"no-trailing-spaces": "error",
"no-use-before-define": [
"error",
{
"functions": true,
"classes": true,
"variables": false
}
],
"no-var": "warn",
"object-curly-spacing": ["error", "always"],
"padded-blocks": [
"error",
{
"blocks": "never",
"switches": "never",
"classes": "never"
}
],
"quotes": ["error", "single"],
"space-before-blocks": ["error", "always"],
"space-before-function-paren": ["error", "always"],
"space-infix-ops": "error",
"space-unary-ops": ["error", { "words": true, "nonwords": false }],
"space-in-parens": ["error", "never"],
"semi": ["error", "never"]
}
}
14 changes: 7 additions & 7 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@ on: [push, pull_request]
jobs:
test:

runs-on: ubuntu-20.04
runs-on: ubuntu-22.04

strategy:
matrix:
node_version: [14.x, 15.x, 16.x, 17.x, 18.x]
node_version: [14.x, 16.x, 18.x, 19.x]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: setup Node.js v${{ matrix.node_version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node_version }}

- name: run npm scripts
run: |
npm i -g standard
npm install
npm run lint
npm run build --if-present
npm run test
- name: sync to coveralls
- name: Coveralls GitHub Action
uses: coverallsapp/github-action@v1.1.2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: cache node modules
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
Expand Down
8 changes: 8 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules
coverage
.github
pnpm-lock.yaml
examples
build.js
build.test.js
reset.js
106 changes: 66 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,84 @@ Calculate average and scoring based on Wilson Score Equation
![CI test](https://github.com/ndaidong/average-rating/workflows/ci-test/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/ndaidong/average-rating/badge.svg)](https://coveralls.io/github/ndaidong/average-rating)
![CodeQL](https://github.com/ndaidong/average-rating/workflows/CodeQL/badge.svg)
[![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
## Install & Usage

- 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)
```bash
npm i average-rating

## Usage
# pnpm
pnpm i average-rating

### Node.js:
# yarn
yarn add average-rating
```

```js
```ts
// es6 module
import {
score,
rate,
average
} from 'average-rating'

// with CommonJS environment
// const { score, rate, average } = require('feed-reader/dist/cjs/average-rating.js')
// CommonJS
const {
score,
rate,
average
} = require('average-rating')

// or specify exactly path to CommonJS variant
const {
score,
rate,
average
} = require('average-rating/dist/cjs/average-rating.js')

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

### Browsers:
### Deno

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'
```ts
// deno > 1.28
import {
score,
rate,
average
} from 'npm:average-rating'

average([134055, 57472, 143135, 365957, 1448459])
</script>
// deno < 1.28
import {
score,
rate,
average
} from 'https://esm.sh/average-rating'
```

With outdated browsers, we can use traditional way:
### CDN

```html
<script type="text/javascript" src="https://unpkg.com/average-rating/dist/average-rating.min.js"></script>
- 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)

<script>
const { average } = window.AverageRating
average([134055, 57472, 143135, 365957, 1448459])
</script>
```
Currently ECMAScript modules work fine on almost browsers:

Example:
```html
<script type="module">
import {
score,
rate,
average
} from 'https://unpkg.com/average-rating/dist/average-rating.esm.js'
```js
// get Winson score for a pair of (Positive, Negative) voting
score(0, 1000) // --> 0
score(1000, 0) // --> 0.96
Expand All @@ -85,6 +94,23 @@ rate(rating) // --> 0.84
// calculate average
average(rating) // --> 4.4
</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 {
score,
rate,
average
} = window.AverageRating
// method call
</script>
```

## APIs
Expand Down
20 changes: 10 additions & 10 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const buildTime = (new Date()).toISOString()
const comment = [
`// ${pkg.name}@${pkg.version}, by ${pkg.author}`,
`built with esbuild at ${buildTime}`,
`published under ${pkg.license} license`
`published under ${pkg.license} license`,
].join(' - ')

const baseOpt = {
Expand All @@ -26,7 +26,7 @@ const baseOpt = {
charset: 'utf8',
target: ['es2020', 'node14'],
minify: false,
write: true
write: true,
}

const esmVersion = {
Expand All @@ -36,8 +36,8 @@ const esmVersion = {
mainFields: ['module'],
outfile: 'dist/average-rating.esm.js',
banner: {
js: comment
}
js: comment,
},
}
buildSync(esmVersion)

Expand All @@ -48,15 +48,15 @@ const cjsVersion = {
mainFields: ['main'],
outfile: 'dist/cjs/average-rating.js',
banner: {
js: comment
}
js: comment,
},
}
buildSync(cjsVersion)

const cjspkg = {
name: pkg.name + '-cjs',
name: pkg.name,
version: pkg.version,
main: './average-rating.js'
main: './average-rating.js',
}
writeFileSync(
'dist/cjs/package.json',
Expand All @@ -74,7 +74,7 @@ const iifeVersion = {
outfile: 'dist/average-rating.min.js',
minify: true,
banner: {
js: comment
}
js: comment,
},
}
buildSync(iifeVersion)
2 changes: 1 addition & 1 deletion build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const runtest = (fname) => {
const arr = [
esmFile,
cjsFile,
minFile
minFile,
]

arr.forEach((fname) => {
Expand Down
Loading

0 comments on commit 2ba7ccf

Please sign in to comment.