Skip to content

Commit

Permalink
feat(engines): add MT19937 as the DefaultEngine
Browse files Browse the repository at this point in the history
Also refactor and reorganize src and test files using UpperCamelCase to
support sensible class usage.

BREAKING CHANGE: complete refactor with a DefaultEngine
  • Loading branch information
richrdkng committed Sep 5, 2023
1 parent def6f3e commit 0c742f0
Show file tree
Hide file tree
Showing 26 changed files with 69 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/common/engines/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@grandom/engines",
"version": "0.0.0-development",
"description": "Random number generator engines for the grandom library.",
"description": "Various RNG engines for the grandom library.",
"author": "Richard King <richrdkng@gmail.com> (www.richrdkng.com)",
"license": "MIT",
"main": "index.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { RandomEngine } from '@grandom/core'

export default class CryptoEngine extends RandomEngine {
export default class CrossCryptoEngine extends RandomEngine {
constructor () {
super('crypto')
super('cross-crypto')

if (this._isBrowserEnvironment()) {
this._next = this._initBrowserCrypto()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// TODO: implement seed types instead of any (e.g.: string, number, number[], string[], etc.)

import { RandomEngine } from '@grandom/core'
import MT19937 from '@grandom/mt19937'
import fnv1a from '@grandom/fnv1a'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { RandomEngine } from '@grandom/core'

export default class BasicEngine extends RandomEngine {
export default class MathRandomEngine extends RandomEngine {
constructor () {
super('basic')
super('math-random')
}

_next (): number {
Expand Down
10 changes: 7 additions & 3 deletions packages/common/engines/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export { default as BasicEngine } from './basic'
export { default as CryptoEngine } from './crypto'
export { default as SeededEngine } from './mt19937'
// common engine shorthands
export { default as BasicEngine } from './MathRandomEngine'
export { default as SeededEngine } from './MT19937Engine'
export { default as CryptoEngine } from './CrossCryptoEngine'

// the default engine is a seedable engine
export { default as DefaultEngine } from './MT19937Engine'
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import crypto from 'node:crypto'
import CryptoEngine from '../../src/crypto'
import CryptoEngine from '../../src/CrossCryptoEngine'

describe('CryptoEngine', () => {
describe('environments', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CryptoEngine from '../../src/crypto'
import CryptoEngine from '../../src/CrossCryptoEngine'

describe('CryptoEngine', () => {
describe('environments', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BigIntStats
} from '@testyard/stats'

import CryptoEngine from '../../src/crypto'
import CryptoEngine from '../../src/CrossCryptoEngine'

const HALF_LENGTH = LENGTH / 2
const engine = new CryptoEngine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BooleanStats
} from '@testyard/stats'

import CryptoEngine from '../../src/crypto'
import CryptoEngine from '../../src/CrossCryptoEngine'

const HALF_LENGTH = LENGTH / 2
const engine = new CryptoEngine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
FloatStats
} from '@testyard/stats'

import CryptoEngine from '../../src/crypto'
import CryptoEngine from '../../src/CrossCryptoEngine'

const HALF_LENGTH = LENGTH / 2
const engine = new CryptoEngine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
IntegerStats
} from '@testyard/stats'

import CryptoEngine from '../../src/crypto'
import CryptoEngine from '../../src/CrossCryptoEngine'

const HALF_LENGTH = LENGTH / 2
const engine = new CryptoEngine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BigIntStats
} from '@testyard/stats'

import MT19937Engine from '../../src/mt19937'
import MT19937Engine from '../../src/MT19937Engine'

const engine = new MT19937Engine()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BooleanStats
} from '@testyard/stats'

import MT19937Engine from '../../src/mt19937'
import MT19937Engine from '../../src/MT19937Engine'

const engine = new MT19937Engine()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
FloatStats
} from '@testyard/stats'

import MT19937Engine from '../../src/mt19937'
import MT19937Engine from '../../src/MT19937Engine'

const engine = new MT19937Engine()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
IntegerStats
} from '@testyard/stats'

import MT19937Engine from '../../src/mt19937'
import MT19937Engine from '../../src/MT19937Engine'

const engine = new MT19937Engine()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import MT19937Engine from '../../src/mt19937'
import MT19937Engine from '../../src/MT19937Engine'

describe('MT19937Engine', () => {
describe('set/get seed', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BigIntStats
} from '@testyard/stats'

import BasicEngine from '../../src/basic'
import BasicEngine from '../../src/MathRandomEngine'

const engine = new BasicEngine()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BooleanStats
} from '@testyard/stats'

import BasicEngine from '../../src/basic'
import BasicEngine from '../../src/MathRandomEngine'

const engine = new BasicEngine()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
FloatStats
} from '@testyard/stats'

import BasicEngine from '../../src/basic'
import BasicEngine from '../../src/MathRandomEngine'

const engine = new BasicEngine()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
IntegerStats
} from '@testyard/stats'

import BasicEngine from '../../src/basic'
import BasicEngine from '../../src/MathRandomEngine'

const engine = new BasicEngine()

Expand Down
32 changes: 32 additions & 0 deletions packages/common/engines/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
BasicEngine,
SeededEngine,
CryptoEngine,
DefaultEngine
} from '../src'

describe('@grandom/engines', () => {
test('basic engine', () => {
const engine = new BasicEngine()

expect(engine.name).toBe('math-random')
})

test('seeded engine', () => {
const engine = new SeededEngine()

expect(engine.name).toBe('mt19937')
})

test('crypto engine', () => {
const engine = new CryptoEngine()

expect(engine.name).toBe('cross-crypto')
})

test('default engine', () => {
const engine = new DefaultEngine()

expect(engine.name).toBe('mt19937')
})
})
2 changes: 1 addition & 1 deletion packages/common/engines/umd/index.basic.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default } from '../src/basic'
export { default } from '../src/MathRandomEngine'
2 changes: 1 addition & 1 deletion packages/common/engines/umd/index.crypto.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default } from '../src/crypto'
export { default } from '../src/CrossCryptoEngine'
2 changes: 1 addition & 1 deletion packages/common/engines/umd/index.seeded.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default } from '../src/mt19937'
export { default } from '../src/MT19937Engine'
6 changes: 4 additions & 2 deletions packages/common/engines/umd/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
BasicEngine,
SeededEngine,
CryptoEngine
CryptoEngine,
DefaultEngine
} from '../src'

export default {
BasicEngine,
SeededEngine,
CryptoEngine
CryptoEngine,
DefaultEngine
}
2 changes: 1 addition & 1 deletion packages/common/engines/umd/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@grandom/engines-umd",
"version": "0.0.0-development",
"description": "Various UMD builds of random number generator engines for the grandom library.",
"description": "Various UMD builds of RNG engines for the grandom library.",
"author": "Richard King <richrdkng@gmail.com> (www.richrdkng.com)",
"repository": {
"type": "git",
Expand Down

0 comments on commit 0c742f0

Please sign in to comment.