diff --git a/.eslintrc.yml b/.eslintrc.yml deleted file mode 100644 index db0a338..0000000 --- a/.eslintrc.yml +++ /dev/null @@ -1,5 +0,0 @@ -extends: cheminfo-typescript -parserOptions: - sourceType: module -env: - jest: true diff --git a/README.md b/README.md index 59c389d..821f96c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # ml-random [![NPM version][npm-image]][npm-url] -[![build status][travis-image]][travis-url] +[![build status][ci-image]][ci-url] [![Test coverage][codecov-image]][codecov-url] [![npm download][download-image]][download-url] @@ -25,8 +25,8 @@ const Random = require('ml-random').default; [npm-image]: https://img.shields.io/npm/v/ml-random.svg?style=flat-square [npm-url]: https://www.npmjs.com/package/ml-random -[travis-image]: https://img.shields.io/travis/mljs/random/master.svg?style=flat-square -[travis-url]: https://travis-ci.org/mljs/random +[ci-image]: https://github.com/mljs/random/workflows/Node.js%20CI/badge.svg?branch=main +[ci-url]: https://github.com/mljs/random/actions?query=workflow%3A%22Node.js+CI%22 [codecov-image]: https://img.shields.io/codecov/c/github/mljs/random.svg?style=flat-square [codecov-url]: https://codecov.io/gh/mljs/random [download-image]: https://img.shields.io/npm/dm/ml-random.svg?style=flat-square diff --git a/eslint.config.mjs b/eslint.config.mjs new file mode 100644 index 0000000..ac3ada0 --- /dev/null +++ b/eslint.config.mjs @@ -0,0 +1,10 @@ +import cheminfo from 'eslint-config-cheminfo-typescript'; + +export default [ + ...cheminfo, + { + languageOptions: { + }, + rules: {} + } +] \ No newline at end of file diff --git a/package.json b/package.json index a22ebd0..66fdbe5 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "prettier": "prettier --check src", "prettier-write": "prettier --write src", "test": "npm run test-only && npm run eslint && npm run prettier && npm run check-types", - "test-only": "jest --coverage", + "test-only": "vitest run --coverage", "tsc": "npm run clean && npm run tsc-cjs && npm run tsc-esm", "tsc-cjs": "tsc --project tsconfig.cjs.json", "tsc-esm": "tsc --project tsconfig.esm.json" @@ -36,17 +36,17 @@ }, "homepage": "https://github.com/mljs/random#readme", "dependencies": { - "ml-xsadd": "^2.0.0" + "ml-xsadd": "^3.0.0" }, "devDependencies": { - "@babel/plugin-transform-modules-commonjs": "^7.21.5", - "@babel/preset-typescript": "^7.21.5", - "@types/jest": "^29.5.1", - "eslint": "^8.40.0", - "eslint-config-cheminfo-typescript": "^11.3.1", - "jest": "^29.5.0", - "prettier": "^2.8.8", - "rimraf": "^5.0.0", - "typescript": "^5.0.4" + "@babel/plugin-transform-modules-commonjs": "^7.25.7", + "@babel/preset-typescript": "^7.25.7", + "@vitest/coverage-v8": "^2.1.3", + "eslint": "^9.12.0", + "eslint-config-cheminfo-typescript": "^16.0.0", + "prettier": "^3.3.3", + "rimraf": "^6.0.1", + "typescript": "^5.6.3", + "vitest": "^2.1.3" } } diff --git a/src/__tests__/__snapshots__/index.test.ts.snap b/src/__tests__/__snapshots__/index.test.ts.snap new file mode 100644 index 0000000..7f325e2 --- /dev/null +++ b/src/__tests__/__snapshots__/index.test.ts.snap @@ -0,0 +1,141 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`random.choice > should choose values with replacement 1`] = ` +[ + 4, + 5, + 3, + 9, + 0, + 7, + 2, + 3, + 2, + 3, + 0, + 9, + 3, + 8, + 6, + 5, + 7, + 2, + 5, + 5, +] +`; + +exports[`random.choice > should select elements without replacement 1`] = ` +[ + 4, + 5, + 3, + 9, + 0, + 7, + 1, + 2, + 6, + 8, +] +`; + +exports[`random.choice should choose values with replacement 1`] = ` +[ + 4, + 5, + 3, + 9, + 0, + 7, + 2, + 3, + 2, + 3, + 0, + 9, + 3, + 8, + 6, + 5, + 7, + 2, + 5, + 5, +] +`; + +exports[`random.choice should select elements without replacement 1`] = ` +[ + 4, + 5, + 3, + 9, + 0, + 7, + 1, + 2, + 6, + 8, +] +`; + +exports[`random.random > should generate random numbers 1`] = ` +[ + 0.36992722749710083, + 0.009614884853363037, + 0.11960279941558838, + 0.541417121887207, + 0.07362216711044312, + 0.20413708686828613, + 0.36353737115859985, + 0.18186044692993164, + 0.9514840841293335, + 0.7254511713981628, +] +`; + +exports[`random.random should generate random numbers 1`] = ` +[ + 0.36992722749710083, + 0.009614884853363037, + 0.11960279941558838, + 0.541417121887207, + 0.07362216711044312, + 0.20413708686828613, + 0.36353737115859985, + 0.18186044692993164, + 0.9514840841293335, + 0.7254511713981628, +] +`; + +exports[`random.randomSample > should generate an array of random numbers 1`] = ` +[ + 0.7623295187950134, + 0.24819517135620117, + 0.6318815350532532, + 0.5753740072250366, + 0.860216498374939, + 0.2248849868774414, + 0.8180570006370544, + 0.8344047665596008, + 0.8892887234687805, + 0.47667670249938965, +] +`; + +exports[`random.randomSample should generate an array of random numbers 1`] = ` +[ + 0.7623295187950134, + 0.24819517135620117, + 0.6318815350532532, + 0.5753740072250366, + 0.860216498374939, + 0.2248849868774414, + 0.8180570006370544, + 0.8344047665596008, + 0.8892887234687805, + 0.47667670249938965, +] +`; diff --git a/src/__tests__/test.ts b/src/__tests__/index.test.ts similarity index 89% rename from src/__tests__/test.ts rename to src/__tests__/index.test.ts index faa1655..8a7c818 100644 --- a/src/__tests__/test.ts +++ b/src/__tests__/index.test.ts @@ -1,4 +1,5 @@ -import XSAdd from 'ml-xsadd'; +import { XSadd } from 'ml-xsadd'; +import { it, describe, beforeEach, expect } from 'vitest'; import Random from '..'; @@ -21,7 +22,7 @@ describe('random.choice', () => { }); it('should select elements without replacement', () => { - const xsadd = new XSAdd(28); + const xsadd = new XSadd(28); random = new Random(xsadd.random); expect(random.choice(10, { size: 10 })).toMatchSnapshot(); }); @@ -47,10 +48,7 @@ describe('random.choice', () => { replace: true, probabilities: [0.3, 0.7], }); - const count = r.reduce( - (prev, current) => (current === 0 ? prev + 1 : prev), - 0, - ); + const count = r.filter((value) => value === 0).length; expect(count / samples).toBeCloseTo(0.3, 2); }); @@ -90,8 +88,8 @@ describe('random.randomSample', () => { it('should generate an array of random numbers', () => { const numbers = random.randomSample(10); expect(numbers).toHaveLength(10); - numbers.forEach((num) => expect(num).toBeLessThan(1)); - numbers.forEach((num) => expect(num).toBeGreaterThanOrEqual(0)); + for (const num of numbers) expect(num).toBeLessThan(1); + for (const num of numbers) expect(num).toBeGreaterThanOrEqual(0); expect(numbers).toMatchSnapshot(); }); }); @@ -130,8 +128,5 @@ describe('random.randInt', () => { }); function freq(arr: number[], n: number) { - return ( - arr.reduce((prev, current) => (current === n ? prev + 1 : prev), 0) / - arr.length - ); + return arr.filter((value) => value === n).length / arr.length; } diff --git a/src/choice.ts b/src/choice.ts index d9c2fe6..1e02327 100644 --- a/src/choice.ts +++ b/src/choice.ts @@ -45,11 +45,10 @@ function randomChoice( cumSum[i] = cumSum[i - 1] + probabilities[i]; } - if (Math.abs(1 - cumSum[cumSum.length - 1]) > PROB_TOLERANCE) { + //@ts-expect-error we know length is at least 1 + if (Math.abs(1 - cumSum.at(-1)) > PROB_TOLERANCE) { throw new Error( - `probabilities should sum to 1, but instead sums to ${ - cumSum[cumSum.length - 1] - }`, + `probabilities should sum to 1, but instead sums to ${cumSum.at(-1)}`, ); } } diff --git a/src/index.ts b/src/index.ts index 38a3432..daefb84 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import XSAdd from 'ml-xsadd'; +import { XSadd } from 'ml-xsadd'; import { ChoiceOptions } from './Options'; import { RandomNumberGenerator } from './RandomNumberGenerator'; @@ -14,7 +14,7 @@ export default class Random { */ constructor(seedOrRandom: RandomNumberGenerator | number = Math.random) { if (typeof seedOrRandom === 'number') { - const xsadd = new XSAdd(seedOrRandom); + const xsadd = new XSadd(seedOrRandom); this.randomGenerator = xsadd.random; } else { this.randomGenerator = seedOrRandom; @@ -25,7 +25,7 @@ export default class Random { * Returns an array of elements choosen from a list * @param values - The values to choose from. If a number, the list will be a range of integer from 0 to that number. * @param options - option object - * @return The choosen values + * @returns The choosen values */ public choice(values: T[], options?: ChoiceOptions): T[]; @@ -42,7 +42,7 @@ export default class Random { /** * Draw a random number from a uniform distribution on [0,1) - * @return The random number + * @returns The random number */ public random(): number { return this.randomGenerator(); @@ -64,7 +64,7 @@ export default class Random { /** * Draw several random number from a uniform distribution on [0, 1) * @param size - The number of number to draw - * @return - The list of drawn numbers. + * @returns - The list of drawn numbers. */ public randomSample(size: number): number[] { const result: number[] = []; diff --git a/tsconfig.json b/tsconfig.json index b21afef..c25e54b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,12 +1,13 @@ { "compilerOptions": { "esModuleInterop": true, - "moduleResolution": "node16", + "moduleResolution": "node", "noImplicitAny": false, + "skipLibCheck": true, "outDir": "lib", "sourceMap": true, "strict": true, - "target": "es2020" + "target": "es2022" }, "include": ["./src/**/*"] }