diff --git a/src/index.ts b/src/index.ts index ea918b4..f65a51f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,5 +2,5 @@ export * from './generators' export * from './random' export { default } from './random' export * from './rng' -export * from './rng-factory' export type * from './types' +export * from './utils' diff --git a/src/random.ts b/src/random.ts index 99bf5a4..14c5dee 100644 --- a/src/random.ts +++ b/src/random.ts @@ -14,7 +14,7 @@ import { uniform } from './distributions/uniform' import { uniformBoolean } from './distributions/uniform-boolean' import { uniformInt } from './distributions/uniform-int' import { MathRandomRNG } from './generators/math-random' -import { RNGFactory } from './rng-factory' +import { createRNG } from './utils' /** * Distribution function @@ -47,8 +47,9 @@ export class Random { protected readonly _cache: { [k: string]: ICacheEntry } = {} + constructor(seedOrRNG: SeedOrRNG = new MathRandomRNG()) { - this._rng = RNGFactory(seedOrRNG) + this._rng = createRNG(seedOrRNG) } /** @@ -81,7 +82,7 @@ export class Random { * ``` */ use(seedOrRNG: SeedOrRNG) { - this._rng = RNGFactory(seedOrRNG) + this._rng = createRNG(seedOrRNG) } // -------------------------------------------------------------------------- diff --git a/src/rng-factory.ts b/src/rng-factory.ts deleted file mode 100644 index 6d9592f..0000000 --- a/src/rng-factory.ts +++ /dev/null @@ -1,22 +0,0 @@ -import type { SeedOrRNG } from './types' -import { ARC4RNG } from './generators/arc4' -import { FunctionRNG } from './generators/function' -import { RNG } from './rng' - -export const RNGFactory = (seedOrRNG?: SeedOrRNG) => { - switch (typeof seedOrRNG) { - case 'object': - if (seedOrRNG instanceof RNG) { - return seedOrRNG - } - break - - case 'function': - return new FunctionRNG(seedOrRNG) - - default: - return new ARC4RNG(seedOrRNG) - } - - throw new Error(`invalid RNG seed or instance "${seedOrRNG}"`) -} diff --git a/src/utils.ts b/src/utils.ts index 727e2bd..9312c44 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,25 @@ -import type { Seed } from './types' +import type { Seed, SeedOrRNG } from './types' +import { ARC4RNG } from './generators/arc4' +import { FunctionRNG } from './generators/function' +import { RNG } from './rng' + +export function createRNG(seedOrRNG?: SeedOrRNG) { + switch (typeof seedOrRNG) { + case 'object': + if (seedOrRNG instanceof RNG) { + return seedOrRNG + } + break + + case 'function': + return new FunctionRNG(seedOrRNG) + + default: + return new ARC4RNG(seedOrRNG) + } + + throw new Error(`invalid RNG seed or instance "${seedOrRNG}"`) +} export function processSeed(seed?: Seed): number { if (seed === undefined) {