From 61007d58ea5401e43c51502c94205b81783c1eae Mon Sep 17 00:00:00 2001 From: Luc Patiny Date: Thu, 16 May 2024 16:28:55 +0200 Subject: [PATCH] chore!: update dependencies and remove default export --- .eslintrc.yml | 2 ++ .github/workflows/documentationjs.yml | 20 +++++++++++++++ .github/workflows/nodejs.yml | 12 +++++++++ .github/workflows/release.yml | 16 ++++++++++++ .gitignore | 2 ++ .prettierrc | 7 ++++++ History.md => CHANGELOG.md | 0 package.json | 27 ++++++++++---------- rollup.config.js => rollup.config.mjs | 0 src/__tests__/{test.js => index.test.js} | 32 ++++++++++++------------ src/index.js | 22 ++++++++-------- 11 files changed, 99 insertions(+), 41 deletions(-) create mode 100644 .github/workflows/documentationjs.yml create mode 100644 .github/workflows/nodejs.yml create mode 100644 .github/workflows/release.yml create mode 100644 .prettierrc rename History.md => CHANGELOG.md (100%) rename rollup.config.js => rollup.config.mjs (100%) rename src/__tests__/{test.js => index.test.js} (71%) diff --git a/.eslintrc.yml b/.eslintrc.yml index f40ac5c..f4ab74d 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -1,3 +1,5 @@ extends: cheminfo parserOptions: sourceType: module +env: + jest: true diff --git a/.github/workflows/documentationjs.yml b/.github/workflows/documentationjs.yml new file mode 100644 index 0000000..62a3273 --- /dev/null +++ b/.github/workflows/documentationjs.yml @@ -0,0 +1,20 @@ +name: Deploy documentation.js on GitHub pages + +on: + release: + types: [published] + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Build documentation + uses: zakodium/documentationjs-action@v1 + - name: Deploy to GitHub pages + uses: JamesIves/github-pages-deploy-action@releases/v3 + with: + GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }} + BRANCH: gh-pages + FOLDER: docs + CLEAN: true diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..2e2007c --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,12 @@ +name: Node.js CI + +on: + push: + branches: + - main + pull_request: + +jobs: + nodejs: + # Documentation: https://github.com/zakodium/workflows#nodejs-ci + uses: zakodium/workflows/.github/workflows/nodejs.yml@nodejs-v1 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..7f5db58 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,16 @@ +name: Release + +on: + push: + branches: + - main + +jobs: + release: + # Documentation: https://github.com/zakodium/workflows#release + uses: zakodium/workflows/.github/workflows/release.yml@release-v1 + with: + npm: true + secrets: + github-token: ${{ secrets.BOT_TOKEN }} + npm-token: ${{ secrets.NPM_BOT_TOKEN }} diff --git a/.gitignore b/.gitignore index fbd9508..1f2b0a4 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,5 @@ jspm_packages .node_repl_history lib +docs +.DS_Store diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..a23e760 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "arrowParens": "always", + "semi": true, + "singleQuote": true, + "tabWidth": 2, + "trailingComma": "all" +} diff --git a/History.md b/CHANGELOG.md similarity index 100% rename from History.md rename to CHANGELOG.md diff --git a/package.json b/package.json index 6a7e018..612a881 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,10 @@ "compile": "rollup -c", "eslint": "eslint src", "eslint-fix": "npm run eslint -- --fix", - "prepublishOnly": "npm run compile", - "test": "npm run test-coverage && npm run eslint", + "prepack": "npm run compile", + "prettier": "prettier --check src", + "prettier-write": "prettier --write src", + "test": "npm run test-coverage && npm run eslint && npm run prettier", "test-only": "jest", "test-coverage": "jest --coverage" }, @@ -28,20 +30,17 @@ "url": "https://github.com/mljs/regression-theil-sen/issues" }, "homepage": "https://github.com/mljs/regression-theil-sen#readme", - "jest": { - "testEnvironment": "node" - }, "devDependencies": { - "@babel/plugin-transform-modules-commonjs": "^7.4.4", - "eslint": "^6.0.1", - "eslint-config-cheminfo": "^1.20.1", - "eslint-plugin-import": "^2.18.0", - "eslint-plugin-jest": "^22.7.1", - "jest": "^24.8.0", - "rollup": "^1.16.3" + "@babel/plugin-transform-modules-commonjs": "^7.24.1", + "@types/jest": "^29.5.12", + "eslint": "^8.57.0", + "eslint-config-cheminfo": "^9.2.0", + "jest": "^29.7.0", + "prettier": "^3.2.5", + "rollup": "^4.17.2" }, "dependencies": { - "ml-array-median": "^1.1.1", - "ml-regression-base": "^2.0.1" + "ml-array-median": "^1.1.6", + "ml-regression-base": "^4.0.0" } } diff --git a/rollup.config.js b/rollup.config.mjs similarity index 100% rename from rollup.config.js rename to rollup.config.mjs diff --git a/src/__tests__/test.js b/src/__tests__/index.test.js similarity index 71% rename from src/__tests__/test.js rename to src/__tests__/index.test.js index cc12204..ed22fbd 100644 --- a/src/__tests__/test.js +++ b/src/__tests__/index.test.js @@ -1,11 +1,11 @@ -import TheilSenRegression from '..'; +import { TheilSenRegression } from '..'; describe('Theil-Sen regression', () => { it('Simple case', () => { - var inputs = [1, 2, 3, 4, 5]; - var outputs = [2, 3, 4, 5, 6]; + let inputs = [1, 2, 3, 4, 5]; + let outputs = [2, 3, 4, 5, 6]; - var regression = new TheilSenRegression(inputs, outputs); + let regression = new TheilSenRegression(inputs, outputs); expect(regression.coefficients).toHaveLength(2); expect(regression.slope).toBe(regression.coefficients[1]); @@ -14,7 +14,7 @@ describe('Theil-Sen regression', () => { expect(regression.slope).toBeCloseTo(1, 5); expect(regression.intercept).toBeCloseTo(1, 5); - var y = regression.predict(85); + let y = regression.predict(85); expect(regression.computeX(y)).toBeCloseTo(85, 5); expect(y).toBeCloseTo(86, 5); @@ -25,20 +25,20 @@ describe('Theil-Sen regression', () => { it('Outlier', () => { // outlier in the 4th value - var inputs = [1, 2, 3, 4, 10, 12, 18]; - var outputs = [10, 14, 180, 22, 46, 54, 78]; + let inputs = [1, 2, 3, 4, 10, 12, 18]; + let outputs = [10, 14, 180, 22, 46, 54, 78]; - var regression = new TheilSenRegression(inputs, outputs); + let regression = new TheilSenRegression(inputs, outputs); expect(regression.slope).toBeCloseTo(4, 3); expect(regression.intercept).toBeCloseTo(6, 3); }); it('Constant', () => { - var inputs = [0, 1, 2, 3]; - var outputs = [2, 2, 2, 2]; + let inputs = [0, 1, 2, 3]; + let outputs = [2, 2, 2, 2]; - var regression = new TheilSenRegression(inputs, outputs); + let regression = new TheilSenRegression(inputs, outputs); expect(regression.toString()).toStrictEqual('f(x) = 2'); expect(regression.toString(1)).toStrictEqual('f(x) = 2'); @@ -46,7 +46,7 @@ describe('Theil-Sen regression', () => { }); it('Load and export model', () => { - var regression = TheilSenRegression.load({ + let regression = TheilSenRegression.load({ name: 'TheilSenRegression', slope: -1, intercept: 0, @@ -54,20 +54,20 @@ describe('Theil-Sen regression', () => { r: 1, r2: 1, chi2: 145.8, - rmsd: 0 - } + rmsd: 0, + }, }); expect(regression.slope).toBe(-1); expect(regression.intercept).toBe(0); expect(regression.toString()).toStrictEqual('f(x) = - 1 * x'); - var model = regression.toJSON(); + let model = regression.toJSON(); expect(model.name).toStrictEqual('TheilSenRegression'); expect(model.slope).toBe(-1); expect(model.intercept).toBe(0); expect(() => TheilSenRegression.load({ name: 1 })).toThrow( - 'not a Theil-Sen model' + 'not a Theil-Sen model', ); }); }); diff --git a/src/index.js b/src/index.js index c45456f..7040d15 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,11 @@ -import BaseRegression, { +import median from 'ml-array-median'; +import { + BaseRegression, checkArrayLength, - maybeToPrecision + maybeToPrecision, } from 'ml-regression-base'; -import median from 'ml-array-median'; -export default class TheilSenRegression extends BaseRegression { +export class TheilSenRegression extends BaseRegression { /** * Theil–Sen estimator * https://en.wikipedia.org/wiki/Theil%E2%80%93Sen_estimator @@ -30,7 +31,7 @@ export default class TheilSenRegression extends BaseRegression { return { name: 'TheilSenRegression', slope: this.slope, - intercept: this.intercept + intercept: this.intercept, }; } @@ -43,15 +44,14 @@ export default class TheilSenRegression extends BaseRegression { } toString(precision) { - var result = 'f(x) = '; + let result = 'f(x) = '; if (this.slope) { - var xFactor = maybeToPrecision(this.slope, precision); + let xFactor = maybeToPrecision(this.slope, precision); result += `${Math.abs(xFactor - 1) < 1e-5 ? '' : `${xFactor} * `}x`; if (this.intercept) { - var absIntercept = Math.abs(this.intercept); - var operator = absIntercept === this.intercept ? '+' : '-'; - result += - ` ${operator} ${maybeToPrecision(absIntercept, precision)}`; + let absIntercept = Math.abs(this.intercept); + let operator = absIntercept === this.intercept ? '+' : '-'; + result += ` ${operator} ${maybeToPrecision(absIntercept, precision)}`; } } else { result += maybeToPrecision(this.intercept, precision);