Skip to content

Commit

Permalink
Merge pull request #11 from michaeldzjap/fix/10-incorrect-esm-build
Browse files Browse the repository at this point in the history
Fix incorrect ESM build
  • Loading branch information
michaeldzjap authored Aug 10, 2024
2 parents ea8f921 + 7f5fa18 commit 4a64ca9
Show file tree
Hide file tree
Showing 24 changed files with 659 additions and 2,627 deletions.
4 changes: 0 additions & 4 deletions .eslintignore

This file was deleted.

65 changes: 0 additions & 65 deletions .eslintrc.json

This file was deleted.

48 changes: 24 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ jobs:
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [20.x, 21.x, 22.x]
steps:
- name: Check out repository code
uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install
run: npm ci
- name: Lint
run: npm run lint
- name: Build
run: npm run prod
- name: Test
run: npm run test
- name: Upload coverage report
if: ${{ matrix.os == 'ubuntu-latest' && matrix.node-version == '22.x' }}
uses: codecov/codecov-action@v3
- name: SonarCloud Scan
if: ${{ matrix.os == 'ubuntu-latest' && matrix.node-version == '22.x' }}
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
- name: Check out repository code
uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install
run: npm ci
- name: Lint
run: npm run lint
- name: Build
run: npm run prod
- name: Test
run: npm run test
- name: Upload coverage report
if: ${{ matrix.os == 'ubuntu-latest' && matrix.node-version == '22.x' }}
uses: codecov/codecov-action@v3
- name: SonarCloud Scan
if: ${{ matrix.os == 'ubuntu-latest' && matrix.node-version == '22.x' }}
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
8 changes: 0 additions & 8 deletions .prettierrc.js

This file was deleted.

9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=michaeldzjap_rand-seed&metric=alert_status)](https://sonarcloud.io/dashboard?id=michaeldzjap_rand-seed)
[![License](https://img.shields.io/npm/l/rand-seed.svg)](https://github.com/michaeldzjap/rand-seed/blob/master/LICENSE)


# rand-seed

A small seedable random number generator library written in TypeScript

The default `Math.random()` function doesn't allow for setting a seed. This library offers a number of different semi-random number generators which may be initialised with an arbitrary seed in string format, hence, making it possible to produce sequences of semi-random numbers that are always the same for a given seed. The implemented algorithms are detailed [here](https://stackoverflow.com/a/47593316/7024747) (note that I am not the author of that post, I merely used it for the implementation of this library).

## Installation

This package is available through _npm_:

```
npm install --save rand-seed
```

## Usage

Either import directly

```html
Expand All @@ -30,7 +32,7 @@ Either import directly
or import in your own scripts using

```javascript
import Rand, {PRNG} from 'rand-seed';
import Rand, { PRNG } from 'rand-seed';
```

Then simply create a new instance with an (optional) seed:
Expand All @@ -41,12 +43,13 @@ const rand = new Rand('1234');
rand.next(); // Generate a new random number
```

If no seed is specified the call to `rand.next()` will simply be forwarded to `Math.random()`. In case of a supplied seed, the _sfc32_ algorithm will be used by default. Currently, three different algorithms are provided: _sfc32_, _mulberry32_ and _xoshiro128**_. If you would like to use a different algorithm, create a new instance like so:
If no seed is specified the call to `rand.next()` will simply be forwarded to `Math.random()`. In case of a supplied seed, the _sfc32_ algorithm will be used by default. Currently, three different algorithms are provided: _sfc32_, _mulberry32_ and _xoshiro128\*\*_. If you would like to use a different algorithm, create a new instance like so:

```javascript
// Create a new random number generator using the xoshiro128** algorithm
const rand = new Rand('1234', PRNG.xoshiro128ss);
```

## Example

A simple example is included. This may be run with _node_: `node example/index.js`
14 changes: 0 additions & 14 deletions __tests__/.eslintrc.json

This file was deleted.

4 changes: 2 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ignore:
- "__tests__/**/*"
- "example/**/*"
- '__tests__/**/*'
- 'example/**/*'
20 changes: 20 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @ts-check

import eslint from '@eslint/js';
import prettierConfig from 'eslint-config-prettier';
import jest from 'eslint-plugin-jest';
import jestDom from 'eslint-plugin-jest-dom';
import tseslint from 'typescript-eslint';

export default [
{
ignores: ['coverage/**', 'dist/**', 'example/**', '__tests__/**'],
},
...tseslint.config(eslint.configs.recommended, ...tseslint.configs.recommended, prettierConfig),
...tseslint.config({
files: ['__tests__/**'],
plugins: { jest },
...jest.configs['flat/recommended'],
...jestDom.configs['flat/recommended'],
}),
];
12 changes: 12 additions & 0 deletions example/cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Rand = require('../../dist/rand-seed').default;
const PRNG = require('../../dist/rand-seed');

let rand = new Rand('1234', PRNG.mulberry32);
const result1 = Array.from({ length: 10 }, () => rand.next());

rand = new Rand('1234');
const result2 = Array.from({ length: 10 }, () => rand.next());

console.log('RESULT1:', result1);
console.log('RESULT2:', result2);
console.log('RESULT1 == RESULT2:', JSON.stringify(result1) === JSON.stringify(result2));
7 changes: 3 additions & 4 deletions example/index.js → example/es/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const Rand = require('../dist/rand-seed').default;
const PRNG = require('../dist/rand-seed');
import Rand, { PRNG } from '../../dist/rand-seed.mjs';

let rand = new Rand('1234', PRNG.mulberry32);
const result1 = Array.from({length: 10}, () => rand.next());
const result1 = Array.from({ length: 10 }, () => rand.next());

rand = new Rand('1234');
const result2 = Array.from({length: 10}, () => rand.next());
const result2 = Array.from({ length: 10 }, () => rand.next());

console.log('RESULT1:', result1);
console.log('RESULT2:', result2);
Expand Down
22 changes: 22 additions & 0 deletions example/es/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "example",
"version": "1.0.0",
"description": "A simple example of rand-seed",
"main": "index.js",
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/michaeldzjap/rand-seed.git"
},
"keywords": [
"random",
"seed",
"prng"
],
"author": "Michael Dzjaparidze",
"license": "MIT",
"bugs": {
"url": "https://github.com/michaeldzjap/rand-seed/issues"
},
"homepage": "https://github.com/michaeldzjap/rand-seed#readme"
}
10 changes: 0 additions & 10 deletions jest.config.js

This file was deleted.

15 changes: 15 additions & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** @type {import('jest').Config} */
export default {
testEnvironment: 'node',
testMatch: ['**/__tests__/**/*.[jt]s?(x)'],
testPathIgnorePatterns: ['<rootDir>/node_modules/'],
transform: {
'\\.[jt]sx?$': [
'ts-jest',
{
tsconfig: 'tsconfig.test.json',
},
],
},
collectCoverage: true,
};
Loading

0 comments on commit 4a64ca9

Please sign in to comment.