Skip to content

Commit

Permalink
refactor!: make mersenne internal (#1444)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored Oct 14, 2022
1 parent a7cd422 commit 5aa747f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 20 deletions.
13 changes: 6 additions & 7 deletions src/faker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { LocaleDefinition } from './definitions';
import { FakerError } from './errors/faker-error';
import { MersenneModule } from './internal/mersenne/mersenne';
import type { KnownLocale } from './locales';
import { AddressModule } from './modules/address';
import { AnimalModule } from './modules/animal';
Expand All @@ -16,7 +17,6 @@ import { HelpersModule } from './modules/helpers';
import { ImageModule } from './modules/image';
import { InternetModule } from './modules/internet';
import { LoremModule } from './modules/lorem';
import { MersenneModule } from './modules/mersenne';
import { MusicModule } from './modules/music';
import { NameModule } from './modules/name';
import { PhoneModule } from './modules/phone';
Expand Down Expand Up @@ -74,10 +74,9 @@ export class Faker {

readonly definitions: LocaleDefinition = this.initDefinitions();

/**
* @internal
*/
readonly mersenne: MersenneModule = new MersenneModule();
/** @internal */
private readonly _mersenne: MersenneModule = new MersenneModule();

readonly random: RandomModule = new RandomModule(this);

readonly helpers: HelpersModule = new HelpersModule(this);
Expand Down Expand Up @@ -241,9 +240,9 @@ export class Faker {
seed: number | number[] = Math.ceil(Math.random() * Number.MAX_SAFE_INTEGER)
): number | number[] {
if (Array.isArray(seed) && seed.length) {
this.mersenne.seed_array(seed);
this._mersenne.seed_array(seed);
} else if (!Array.isArray(seed) && !isNaN(seed)) {
this.mersenne.seed(seed);
this._mersenne.seed(seed);
}

return seed;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@
* Any feedback is very welcome.
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
* email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
*
* @internal
*/

export default class MersenneTwister19937 {
private readonly N = 624;
private readonly M = 397;
Expand Down
7 changes: 6 additions & 1 deletion src/modules/datatype/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Faker } from '../..';
import { FakerError } from '../../errors/faker-error';
import type { MersenneModule } from '../../internal/mersenne/mersenne';

/**
* Module to generate various primitive values and data types.
Expand Down Expand Up @@ -54,8 +55,12 @@ export class DatatypeModule {
throw new FakerError(`Max ${max} should be greater than min ${min}.`);
}

const mersenne: MersenneModule =
// @ts-expect-error: access private member field
this.faker._mersenne;

const randomNumber = Math.floor(
this.faker.mersenne.rand(max / precision + 1, min / precision)
mersenne.rand(max / precision + 1, min / precision)
);

// Workaround problem in float point arithmetics for e.g. 6681493 / 0.01
Expand Down
2 changes: 1 addition & 1 deletion test/all_functional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const IGNORED_MODULES = [
'definitions',
'fake',
'helpers',
'mersenne',
'_mersenne',
];

function isTestableModule(mod: string) {
Expand Down
2 changes: 1 addition & 1 deletion test/mersenne.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeAll, beforeEach, describe, expect, it } from 'vitest';
import { FakerError } from '../src/errors/faker-error';
import { MersenneModule } from '../src/modules/mersenne';
import { MersenneModule } from '../src/internal/mersenne/mersenne';
import { seededRuns } from './support/seededRuns';

const minMaxTestCases = [
Expand Down
16 changes: 7 additions & 9 deletions test/scripts/apidoc/examplesAndDeprecations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ describe('examples and deprecations', () => {

const modules: Record<string, DeclarationReflection[]> = selectApiModules(
project
)
.filter((module) => module.name !== 'MersenneModule')
.reduce(
(a, v) => ({
...a,
[v.name]: v.getChildrenByKind(ReflectionKind.Method),
}),
{}
);
).reduce(
(a, v) => ({
...a,
[v.name]: v.getChildrenByKind(ReflectionKind.Method),
}),
{}
);

const consoleSpies: Array<SpyInstance> = Object.keys(console)
.filter((key) => typeof console[key] === 'function')
Expand Down

0 comments on commit 5aa747f

Please sign in to comment.