From 62eeffa21e0bbca5933cb7358128150f6eba0b1a Mon Sep 17 00:00:00 2001 From: Dong Nguyen Date: Tue, 4 Jan 2022 11:00:09 +0700 Subject: [PATCH 1/2] v2.0.1 - Update build, test and docs --- build.js | 21 +++------- build.test.js | 41 +++++++++++++++++++ dist/average-rating.esm.js | 51 ++++++++++++++++++++---- dist/average-rating.min.js | 2 +- dist/cjs/average-rating.js | 82 ++++++++++++++++++++++++++++++++++---- dist/cjs/package.json | 2 +- package.json | 21 ++++++---- 7 files changed, 179 insertions(+), 41 deletions(-) create mode 100644 build.test.js diff --git a/build.js b/build.js index b33a26c..0ab71bc 100644 --- a/build.js +++ b/build.js @@ -14,17 +14,7 @@ execSync('rm -rf dist') execSync('mkdir dist') const buildTime = (new Date()).toISOString() -const longComment = [ - '/**', - ` * ${pkg.name}@${pkg.version}`, - ` * built with esbuild at: ${buildTime}`, - ` * repository: ${pkg.repository.url}`, - ` * maintainer: ${pkg.author}`, - ` * License: ${pkg.license}`, - '**/' -].join('\n') - -const shortComment = [ +const comment = [ `// ${pkg.name}@${pkg.version}, by ${pkg.author}`, `built with esbuild at ${buildTime}`, `published under ${pkg.license} license` @@ -35,7 +25,7 @@ const baseOpt = { bundle: true, charset: 'utf8', target: ['es2020', 'node14'], - minify: true, + minify: false, write: true } @@ -46,7 +36,7 @@ const esmVersion = { mainFields: ['module'], outfile: 'dist/average-rating.esm.js', banner: { - js: longComment + js: comment } } buildSync(esmVersion) @@ -58,7 +48,7 @@ const cjsVersion = { mainFields: ['main'], outfile: 'dist/cjs/average-rating.js', banner: { - js: longComment + js: comment } } buildSync(cjsVersion) @@ -82,8 +72,9 @@ const iifeVersion = { target: ['es2020'], globalName: 'AverageRating', outfile: 'dist/average-rating.min.js', + minify: true, banner: { - js: shortComment + js: comment } } buildSync(iifeVersion) diff --git a/build.test.js b/build.test.js new file mode 100644 index 0000000..0cc5f90 --- /dev/null +++ b/build.test.js @@ -0,0 +1,41 @@ +// release.test + +/* eslint-env jest */ + +import { + existsSync, + readFileSync +} from 'fs' + +const pkg = JSON.parse(readFileSync('./package.json')) + +const esmFile = `${pkg.name}.esm.js` +const cjsFile = `cjs/${pkg.name}.js` +const minFile = `${pkg.name}.min.js` + +const runtest = (fname) => { + const fpath = `./dist/${fname}` + describe(`Validate ${fname} version output`, () => { + test(`Check if ${fpath} file created`, () => { + expect(existsSync(fpath)).toBeTruthy() + }) + const constent = readFileSync(fpath, 'utf8') + const lines = constent.split('\n') + test('Check if file meta contains package info', () => { + expect(lines[0].includes(`${pkg.name}@${pkg.version}`)).toBeTruthy() + expect(lines[0].includes(pkg.author)).toBeTruthy() + expect(lines[0].includes(pkg.license)).toBeTruthy() + }) + }) + return fpath +} + +const arr = [ + esmFile, + cjsFile, + minFile +] + +arr.forEach((fname) => { + runtest(fname) +}) diff --git a/dist/average-rating.esm.js b/dist/average-rating.esm.js index a02a9e7..d591bf4 100644 --- a/dist/average-rating.esm.js +++ b/dist/average-rating.esm.js @@ -1,8 +1,43 @@ -/** - * average-rating@2.0.0 - * built with esbuild at: 2022-01-03T14:48:10.361Z - * repository: https://github.com/ndaidong/average-rating - * maintainer: @ndaidong - * License: MIT -**/ -var n=(t,e)=>{if(t===0&&e===0)return 0;let o=((t+1.9208)/(t+e)-1.96*Math.sqrt(t*e/(t+e)+.9604)/(t+e))/(1+3.8416/(t+e));return Number(o.toFixed(2))},x=t=>{let e=t.length,o=t[0],c=t[e-1],u=(1/(e-1)).toFixed(2),s=e-1;for(let r=1;r{let e=t.reduce((s,r)=>s+r,0);if(e===0)return 0;let o=0,c=1;t.forEach(s=>{o+=s*c,c++});let u=o/e;return Number(u.toFixed(1))};export{d as average,x as rate,n as score}; +// average-rating@2.0.1, by @ndaidong - built with esbuild at 2022-01-04T03:58:54.537Z - published under MIT license +// src/main.js +var score = (p, n) => { + if (p === 0 && n === 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)); +}; +var rate = (rating) => { + const size = rating.length; + let n = rating[0]; + let p = rating[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; + } + return score(p, n); +}; +var average = (rating) => { + const total = rating.reduce((prev, current) => { + return prev + current; + }, 0); + if (total === 0) { + return 0; + } + let sum = 0; + let k = 1; + rating.forEach((item) => { + sum += item * k; + k++; + }); + const r = sum / total; + return Number(r.toFixed(1)); +}; +export { + average, + rate, + score +}; diff --git a/dist/average-rating.min.js b/dist/average-rating.min.js index c28f76a..9d7577b 100644 --- a/dist/average-rating.min.js +++ b/dist/average-rating.min.js @@ -1,2 +1,2 @@ -// average-rating@2.0.0, by @ndaidong - built with esbuild at 2022-01-03T14:48:10.361Z - published under MIT license +// average-rating@2.0.1, by @ndaidong - built with esbuild at 2022-01-04T03:58:54.537Z - published under MIT license var AverageRating=(()=>{var l=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var h=t=>l(t,"__esModule",{value:!0});var i=(t,e)=>{for(var o in e)l(t,o,{get:e[o],enumerable:!0})},m=(t,e,o,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of f(e))!F.call(t,r)&&(o||r!=="default")&&l(t,r,{get:()=>e[r],enumerable:!(s=d(e,r))||s.enumerable});return t};var a=(t=>(e,o)=>t&&t.get(e)||(o=m(h({}),e,1),t&&t.set(e,o),o))(typeof WeakMap!="undefined"?new WeakMap:0);var k={};i(k,{average:()=>N,rate:()=>b,score:()=>x});var x=(t,e)=>{if(t===0&&e===0)return 0;let o=((t+1.9208)/(t+e)-1.96*Math.sqrt(t*e/(t+e)+.9604)/(t+e))/(1+3.8416/(t+e));return Number(o.toFixed(2))},b=t=>{let e=t.length,o=t[0],s=t[e-1],r=(1/(e-1)).toFixed(2),u=e-1;for(let c=1;c{let e=t.reduce((u,c)=>u+c,0);if(e===0)return 0;let o=0,s=1;t.forEach(u=>{o+=u*s,s++});let r=o/e;return Number(r.toFixed(1))};return a(k);})(); diff --git a/dist/cjs/average-rating.js b/dist/cjs/average-rating.js index ff007c2..f3f0bc4 100644 --- a/dist/cjs/average-rating.js +++ b/dist/cjs/average-rating.js @@ -1,8 +1,74 @@ -/** - * average-rating@2.0.0 - * built with esbuild at: 2022-01-03T14:48:10.361Z - * repository: https://github.com/ndaidong/average-rating - * maintainer: @ndaidong - * License: MIT -**/ -var l=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var h=t=>l(t,"__esModule",{value:!0});var i=(t,e)=>{for(var o in e)l(t,o,{get:e[o],enumerable:!0})},m=(t,e,o,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of f(e))!F.call(t,r)&&(o||r!=="default")&&l(t,r,{get:()=>e[r],enumerable:!(s=d(e,r))||s.enumerable});return t};var a=(t=>(e,o)=>t&&t.get(e)||(o=m(h({}),e,1),t&&t.set(e,o),o))(typeof WeakMap!="undefined"?new WeakMap:0);var k={};i(k,{average:()=>N,rate:()=>b,score:()=>x});var x=(t,e)=>{if(t===0&&e===0)return 0;let o=((t+1.9208)/(t+e)-1.96*Math.sqrt(t*e/(t+e)+.9604)/(t+e))/(1+3.8416/(t+e));return Number(o.toFixed(2))},b=t=>{let e=t.length,o=t[0],s=t[e-1],r=(1/(e-1)).toFixed(2),u=e-1;for(let c=1;c{let e=t.reduce((u,c)=>u+c,0);if(e===0)return 0;let o=0,s=1;t.forEach(u=>{o+=u*s,s++});let r=o/e;return Number(r.toFixed(1))};module.exports=a(k);0&&(module.exports={average,rate,score}); +// average-rating@2.0.1, by @ndaidong - built with esbuild at 2022-01-04T03:58:54.537Z - published under MIT license +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __reExport = (target, module2, copyDefault, desc) => { + if (module2 && typeof module2 === "object" || typeof module2 === "function") { + for (let key of __getOwnPropNames(module2)) + if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default")) + __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); + } + return target; +}; +var __toCommonJS = /* @__PURE__ */ ((cache) => { + return (module2, temp) => { + return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp); + }; +})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0); + +// src/main.js +var main_exports = {}; +__export(main_exports, { + average: () => average, + rate: () => rate, + score: () => score +}); +var score = (p, n) => { + if (p === 0 && n === 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)); +}; +var rate = (rating) => { + const size = rating.length; + let n = rating[0]; + let p = rating[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; + } + return score(p, n); +}; +var average = (rating) => { + const total = rating.reduce((prev, current) => { + return prev + current; + }, 0); + if (total === 0) { + return 0; + } + let sum = 0; + let k = 1; + rating.forEach((item) => { + sum += item * k; + k++; + }); + const r = sum / total; + return Number(r.toFixed(1)); +}; +module.exports = __toCommonJS(main_exports); +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + average, + rate, + score +}); diff --git a/dist/cjs/package.json b/dist/cjs/package.json index 756b2db..83bc0e2 100644 --- a/dist/cjs/package.json +++ b/dist/cjs/package.json @@ -1,5 +1,5 @@ { "name": "average-rating-cjs", - "version": "2.0.0", + "version": "2.0.1", "main": "./average-rating.js" } \ No newline at end of file diff --git a/package.json b/package.json index a3716df..fc3bb54 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "2.0.0", + "version": "2.0.1", "name": "average-rating", "description": "Calculate average score and rating based on Wilson Score Equation", "homepage": "https://www.npmjs.com/package/average-rating", @@ -16,12 +16,17 @@ "node": ">= 14" }, "scripts": { - "lint": "standard ./src", - "pretest": "npm run lint && npm run build", + "lint": "standard .", + "pretest": "npm run lint", "test": "NODE_ENV=test jest --verbose --coverage=true --unhandled-rejections=strict --detectOpenHandles", - "build": "node build.js src/main.js", + "build": "node build src/main.js", "reset": "node reset" }, + "devDependencies": { + "@babel/plugin-transform-modules-commonjs": "^7.16.7", + "esbuild": "^0.14.10", + "jest": "^27.3.1" + }, "babel": { "env": { "test": { @@ -31,10 +36,10 @@ } } }, - "devDependencies": { - "@babel/plugin-transform-modules-commonjs": "^7.16.7", - "esbuild": "^0.14.10", - "jest": "^27.3.1" + "standard": { + "ignore": [ + "/dist" + ] }, "keywords": [ "rating", From 9b7f9dcc3c9348489227d16b6f762c743c78acce Mon Sep 17 00:00:00 2001 From: Dong Nguyen Date: Tue, 4 Jan 2022 11:11:57 +0700 Subject: [PATCH 2/2] Update README - Add eval.js for quick test --- README.md | 25 ++++++++++++------------- eval.js | 14 ++++++++++++++ package.json | 1 + 3 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 eval.js diff --git a/README.md b/README.md index c0c70a9..8de6fd1 100644 --- a/README.md +++ b/README.md @@ -33,29 +33,28 @@ Calculate average and scoring based on Wilson Score Equation ### 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: +// with CommonJS environment +// const { score, rate, average } = require('feed-reader/dist/cjs/average-rating.js') -```js -const { - score, - rate, - average -} = require('average-rating/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 ``` + +##### Note: + +> Since Node.js v14, ECMAScript modules [have became the official standard format](https://nodejs.org/docs/latest-v14.x/api/esm.html#esm_modules_ecmascript_modules). +> Just ensure that you are [using module system](https://nodejs.org/api/packages.html#determining-module-system) and enjoy with ES6 import/export syntax. + + ### Browsers: Currently ECMAScript modules work fine on almost browsers: diff --git a/eval.js b/eval.js new file mode 100644 index 0000000..b185bc9 --- /dev/null +++ b/eval.js @@ -0,0 +1,14 @@ +// eval.js + +import { + score, + rate, + average +} from './src/main.js' + +const scoring = [80, 20] +const rating = [134055, 57472, 143135, 365957, 1448459] + +console.log(`score(${scoring.join(', ')}) // => ${score(...scoring)}`) +console.log(`average([${rating.join(', ')}]) // => ${average(rating)}`) +console.log(`rate([${rating.join(', ')}]) // => ${rate(rating)}`) diff --git a/package.json b/package.json index fc3bb54..a07fa9c 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "pretest": "npm run lint", "test": "NODE_ENV=test jest --verbose --coverage=true --unhandled-rejections=strict --detectOpenHandles", "build": "node build src/main.js", + "eval": "node eval", "reset": "node reset" }, "devDependencies": {