Skip to content

Commit

Permalink
feat: drop Node.js v12 support
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Node.js v12 is no longer supported.
  • Loading branch information
kenany committed Nov 14, 2022
1 parent 3dd318a commit f2c2395
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 78 deletions.
5 changes: 1 addition & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"extends": "@kenan",
"rules": {
"no-var": 0
}
"extends": "@kenan"
}
5 changes: 4 additions & 1 deletion .github/renovate.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": ["local>kenany/renovate-config"]
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": ["local>kenany/renovate-config"],
"assignees": ["kenany"],
"reviewers": ["kenany"]
}
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [12, 14, 16, 18]
node-version: [14, 16, 18]
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v3.0.2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
steps:
- uses: actions/setup-node@v3.5.0
with:
node-version: 16
node-version: 18
- name: Update npm
run: |
npm install -g npm
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ player play a match against each of the other 999 players. After each individual
match, I calculate the new ratings of the first player and his opponent. The
result:

``` sh
``` bash
$ node bench/index.js

Starting...
2 tests completed.

glicko2-lite x 433 ops/sec ±0.42% (87 runs sampled)
glicko2 x 33.84 ops/sec ±1.40% (59 runs sampled)
glicko2-lite x 677 ops/sec ±0.48% (89 runs sampled)
glicko2 x 33.62 ops/sec ±1.19% (59 runs sampled)
```

[1]: https://github.com/mmai/glicko2js
Expand Down
93 changes: 48 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
'use strict';

var assign = require('lodash.assign');
var map = require('lodash.map');
var reduce = require('lodash.reduce');

/** @typedef {[rating: number, rd: number, vol: number]} Opponent */

/**
Expand All @@ -21,9 +17,9 @@ var reduce = require('lodash.reduce');
* @param {{ rating: number; }} options
*/
function scale(rating, rd, options) {
var mu = (rating - options.rating) / 173.7178;
var phi = rd / 173.7178;
return { mu: mu, phi: phi };
const mu = (rating - options.rating) / 173.7178;
const phi = rd / 173.7178;
return { mu, phi };
}

/**
Expand Down Expand Up @@ -51,8 +47,8 @@ function e(mu, muj, phij) {
* @returns {ScaledOpponent[]}
*/
function scaleOpponents(mu, opponents, options) {
return map(opponents, function(opp) {
var scaled = scale(opp[0], opp[1], options);
return opponents.map((opp) => {
const scaled = scale(opp[0], opp[1], options);
return {
muj: scaled.mu,
phij: scaled.phi,
Expand All @@ -68,9 +64,10 @@ function scaleOpponents(mu, opponents, options) {
* @returns {number}
*/
function updateRating(opponents) {
return 1 / reduce(opponents, function(sum, opp) {
return sum + Math.pow(opp.gphij, 2) * opp.emmp * (1 - opp.emmp);
}, 0);
return 1 / opponents.reduce(
(sum, opp) => sum + Math.pow(opp.gphij, 2) * opp.emmp * (1 - opp.emmp),
0
);
}

/**
Expand All @@ -79,9 +76,10 @@ function updateRating(opponents) {
* @returns {number}
*/
function computeDelta(v, opponents) {
return v * reduce(opponents, function(sum, opp) {
return sum + opp.gphij * (opp.score - opp.emmp);
}, 0);
return v * opponents.reduce(
(sum, opp) => sum + opp.gphij * (opp.score - opp.emmp),
0
);
}

/**
Expand All @@ -92,17 +90,17 @@ function computeDelta(v, opponents) {
* @param {{ tau: number; }} options
*/
function volF(phi, v, delta, a, options) {
var phi2 = Math.pow(phi, 2);
var d2 = Math.pow(delta, 2);
const phi2 = Math.pow(phi, 2);
const d2 = Math.pow(delta, 2);

/**
* @param {number} x
*/
return function(x) {
var ex = Math.exp(x);
var a2 = phi2 + v + ex;
var p2 = (x - a) / Math.pow(options.tau, 2);
var p1 = (ex * (d2 - phi2 - v - ex)) / (2 * Math.pow(a2, 2));
const ex = Math.exp(x);
const a2 = phi2 + v + ex;
const p2 = (x - a) / Math.pow(options.tau, 2);
const p1 = (ex * (d2 - phi2 - v - ex)) / (2 * Math.pow(a2, 2));
return p1 - p2;
};
}
Expand All @@ -117,32 +115,32 @@ function volF(phi, v, delta, a, options) {
*/
function computeVolatility(sigma, phi, v, delta, options) {
// 5.1
var a = Math.log(Math.pow(sigma, 2));
var f = volF(phi, v, delta, a, options);
let a = Math.log(Math.pow(sigma, 2));
const f = volF(phi, v, delta, a, options);

// 5.2
/** @type {number} */
var b;
let b;
if (Math.pow(delta, 2) > Math.pow(phi, 2) + v) {
b = Math.log(Math.pow(delta, 2) - Math.pow(phi, 2) - v);
}
else {
var k = 1;
let k = 1;
while (f(a - k * options.tau) < 0) {
k++;
}
b = a - k * options.tau;
}

// 5.3
var fa = f(a);
var fb = f(b);
let fa = f(a);
let fb = f(b);

// 5.4
while (Math.abs(b - a) > 0.000001) {
/** @type {number} */
var c = a + (a - b) * fa / (fb - fa);
var fc = f(c);
const c = a + (a - b) * fa / (fb - fa);
const fc = f(c);

if (fc * fb < 0) {
a = b;
Expand Down Expand Up @@ -176,10 +174,11 @@ function phiStar(sigmap, phi) {
* @param {readonly ScaledOpponent[]} opponents
*/
function newRating(phis, mu, v, opponents) {
var phip = 1 / Math.sqrt(1 / Math.pow(phis, 2) + 1 / v);
var mup = mu + Math.pow(phip, 2) * reduce(opponents, function(sum, opp) {
return sum + opp.gphij * (opp.score - opp.emmp);
}, 0);
const phip = 1 / Math.sqrt(1 / Math.pow(phis, 2) + 1 / v);
const mup = mu + Math.pow(phip, 2) * opponents.reduce(
(sum, opp) => sum + opp.gphij * (opp.score - opp.emmp),
0
);
return { mu: mup, phi: phip };
}

Expand All @@ -189,9 +188,9 @@ function newRating(phis, mu, v, opponents) {
* @param {{ rating: number; }} options
*/
function unscale(mup, phip, options) {
var rating = 173.7178 * mup + options.rating;
var rd = 173.7178 * phip;
return { rating: rating, rd: rd };
const rating = 173.7178 * mup + options.rating;
const rd = 173.7178 * phip;
return { rating, rd };
}

/**
Expand All @@ -204,28 +203,32 @@ function unscale(mup, phip, options) {
*/
function rate(rating, rd, sigma, opponents, options) {
/** @type {{ rating: number; tau: number; }} */
var opts = assign({}, { rating: 1500, tau: 0.5 }, options || {});
const opts = Object.assign({}, { rating: 1500, tau: 0.5 }, options || {});

// Step 2
var scaled = scale(rating, rd, opts);
var scaledOpponents = scaleOpponents(scaled.mu, opponents, opts);
const scaled = scale(rating, rd, opts);
const scaledOpponents = scaleOpponents(scaled.mu, opponents, opts);

// Step 3
var v = updateRating(scaledOpponents);
const v = updateRating(scaledOpponents);

// Step 4
var delta = computeDelta(v, scaledOpponents);
const delta = computeDelta(v, scaledOpponents);

// Step 5
var sigmap = computeVolatility(sigma, scaled.phi, v, delta, opts);
const sigmap = computeVolatility(sigma, scaled.phi, v, delta, opts);

// Step 6
var phis = phiStar(sigmap, scaled.phi);
const phis = phiStar(sigmap, scaled.phi);

// Step 7
var updated = newRating(phis, scaled.mu, v, scaledOpponents);
const updated = newRating(phis, scaled.mu, v, scaledOpponents);

return assign({}, unscale(updated.mu, updated.phi, opts), { vol: sigmap });
return Object.assign(
{},
unscale(updated.mu, updated.phi, opts),
{ vol: sigmap }
);
}

module.exports = rate;
17 changes: 5 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "Kenan Yildirim <kenan@kenany.me> (https://kenany.me/)",
"main": "index.js",
"engines": {
"node": "12 || 14 || 16 || >=18"
"node": "14 || 16 || >=18"
},
"files": [
"CHANGELOG.md",
Expand All @@ -29,27 +29,20 @@
"posttest": "npm run -s lint",
"lint": "eslint index.js test/index.js bench/index.js"
},
"dependencies": {
"lodash.assign": "^4.2.0",
"lodash.map": "^4.6.0",
"lodash.reduce": "^4.6.0"
},
"dependencies": {},
"devDependencies": {
"@kenan/eslint-config": "^9.0.4",
"@kenan/eslint-config": "^10.0.1",
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/git": "^10.0.1",
"@tsconfig/node12": "^1.0.11",
"@tsconfig/node14": "^1.0.3",
"@types/almost-equal": "^1.1.0",
"@types/lodash.assign": "^4.2.7",
"@types/lodash.isfunction": "^3.0.7",
"@types/lodash.map": "^4.6.13",
"@types/lodash.reduce": "^4.6.7",
"@types/tape": "^4.13.2",
"almost-equal": "^1.1.0",
"beautify-benchmark": "^0.2.4",
"benchmark": "^2.1.4",
"conventional-changelog-conventionalcommits": "^5.0.0",
"eslint": "^7.32.0",
"eslint": "^8.27.0",
"glicko2": "^1.0.0",
"lodash.isfunction": "^3.0.9",
"semantic-release": "^19.0.5",
Expand Down
20 changes: 10 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
'use strict';

var test = require('tape');
var isFunction = require('lodash.isfunction');
var almostEqual = require('almost-equal');
const test = require('tape');
const isFunction = require('lodash.isfunction');
const almostEqual = require('almost-equal');

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

test('exports a function', function(t) {
test('exports a function', (t) => {
t.plan(1);
t.ok(isFunction(glicko2));
});

test('calculates new ratings', function(t) {
test('calculates new ratings', (t) => {
t.plan(3);

var a = { rating: 1500, rd: 200, vol: 0.06 };
var b = { rating: 1400, rd: 30, vol: 0.06 };
var c = { rating: 1550, rd: 100, vol: 0.06 };
var d = { rating: 1700, rd: 300, vol: 0.06 };
let a = { rating: 1500, rd: 200, vol: 0.06 };
const b = { rating: 1400, rd: 30, vol: 0.06 };
const c = { rating: 1550, rd: 100, vol: 0.06 };
const d = { rating: 1700, rd: 300, vol: 0.06 };

// a beats b
// c beats a
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@tsconfig/node12/tsconfig.json",
"extends": "@tsconfig/node14/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
Expand Down

0 comments on commit f2c2395

Please sign in to comment.