From edb87507f236c377e3f6927d81c1f81847f0c966 Mon Sep 17 00:00:00 2001 From: Itamar Davidyan <91004029+itamar-owlytics@users.noreply.github.com> Date: Wed, 29 Dec 2021 11:23:14 +0200 Subject: [PATCH] Use class syntax and extends keyword instead of util.inherits (#16) `util.inherits` is now deprecated due to issues with how it sets the prototype chain, see: https://github.com/nodejs/node/issues/4179 --- hypothesis/one-data-set.js | 18 ++++++++++-------- hypothesis/two-data-set.js | 25 ++++++++++++++----------- hypothesis/welch.js | 30 ++++++++++++++++-------------- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/hypothesis/one-data-set.js b/hypothesis/one-data-set.js index 9e3c165..2d55708 100644 --- a/hypothesis/one-data-set.js +++ b/hypothesis/one-data-set.js @@ -2,17 +2,19 @@ const Distribution = require('distributions').Studentt; -const util = require('util'); const AbstactStudentT = require('./abstact.js'); -function StudentT(data, options) { - AbstactStudentT.call(this, options); +class StudentT extends AbstactStudentT { - this._df = data.size - 1; - this._dist = new Distribution(this._df); + constructor(data, options) { + super(options); + this._df = data.size - 1; + this._dist = new Distribution(this._df); + + this._se = Math.sqrt(data.variance / data.size); + this._mean = data.mean; + } - this._se = Math.sqrt(data.variance / data.size); - this._mean = data.mean; } -util.inherits(StudentT, AbstactStudentT); + module.exports = StudentT; diff --git a/hypothesis/two-data-set.js b/hypothesis/two-data-set.js index 728db34..b228a2c 100644 --- a/hypothesis/two-data-set.js +++ b/hypothesis/two-data-set.js @@ -2,21 +2,24 @@ const Distribution = require('distributions').Studentt; -const util = require('util'); const AbstactStudentT = require('./abstact.js'); -function StudentT(left, right, options) { - AbstactStudentT.call(this, options); +class StudentT extends AbstactStudentT { + + constructor(left, right, options) { + super(options); - this._df = left.size + right.size - 2; - this._dist = new Distribution(this._df); + this._df = left.size + right.size - 2; + this._dist = new Distribution(this._df); - const commonVariance = ((left.size - 1) * left.variance + - (right.size - 1) * right.variance - ) / this._df; + const commonVariance = ((left.size - 1) * left.variance + + (right.size - 1) * right.variance + ) / this._df; + + this._se = Math.sqrt(commonVariance * (1 / left.size + 1 / right.size)); + this._mean = left.mean - right.mean; + } - this._se = Math.sqrt(commonVariance * (1 / left.size + 1 / right.size)); - this._mean = left.mean - right.mean; } -util.inherits(StudentT, AbstactStudentT); + module.exports = StudentT; diff --git a/hypothesis/welch.js b/hypothesis/welch.js index df4522f..da9f439 100644 --- a/hypothesis/welch.js +++ b/hypothesis/welch.js @@ -2,24 +2,26 @@ const Distribution = require('distributions').Studentt; -const util = require('util'); const AbstactStudentT = require('./abstact.js'); -function StudentT(left, right, options) { - AbstactStudentT.call(this, options); +class StudentT extends AbstactStudentT { + + constructor(left, right, options) { + super(options); - const leftSE = left.variance / left.size; - const rightSE = right.variance / right.size; - const commonVariance = leftSE + rightSE; + const leftSE = left.variance / left.size; + const rightSE = right.variance / right.size; + const commonVariance = leftSE + rightSE; - this._df = Math.pow(commonVariance, 2) / ( - Math.pow(leftSE, 2) / (left.size - 1) + - Math.pow(rightSE, 2) / (right.size - 1) - ); - this._dist = new Distribution(this._df); + this._df = Math.pow(commonVariance, 2) / ( + Math.pow(leftSE, 2) / (left.size - 1) + + Math.pow(rightSE, 2) / (right.size - 1) + ); + this._dist = new Distribution(this._df); - this._se = Math.sqrt(commonVariance); - this._mean = left.mean - right.mean; + this._se = Math.sqrt(commonVariance); + this._mean = left.mean - right.mean; + } } -util.inherits(StudentT, AbstactStudentT); + module.exports = StudentT;