Skip to content

Commit

Permalink
chore: update dependences and remove travis
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Oct 18, 2024
1 parent 3b2acf1 commit e20368c
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 42 deletions.
5 changes: 0 additions & 5 deletions .eslintrc.yml

This file was deleted.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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]

Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import cheminfo from 'eslint-config-cheminfo-typescript';

export default [
...cheminfo,
{
languageOptions: {
},
rules: {}
}
]
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
}
141 changes: 141 additions & 0 deletions src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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,
]
`;
19 changes: 7 additions & 12 deletions src/__tests__/test.ts → src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import XSAdd from 'ml-xsadd';
import { XSadd } from 'ml-xsadd';
import { it, describe, beforeEach, expect } from 'vitest';

import Random from '..';

Expand All @@ -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();
});
Expand All @@ -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);
});

Expand Down Expand Up @@ -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();
});
});
Expand Down Expand Up @@ -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;
}
7 changes: 3 additions & 4 deletions src/choice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ function randomChoice<T>(
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)}`,
);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import XSAdd from 'ml-xsadd';
import { XSadd } from 'ml-xsadd';

import { ChoiceOptions } from './Options';
import { RandomNumberGenerator } from './RandomNumberGenerator';
Expand All @@ -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;
Expand All @@ -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<T>(values: T[], options?: ChoiceOptions): T[];
Expand All @@ -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();
Expand All @@ -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[] = [];
Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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/**/*"]
}

0 comments on commit e20368c

Please sign in to comment.