Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: build/type issue with DataGenerator #671

Merged
merged 1 commit into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/mocks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import seedrandom from 'seedrandom';

import { DataGenerator } from '../utils/data_generators/data_generator';
import { DataGenerator, RandomNumberGenerator } from '../utils/data_generators/data_generator';

/**
* Forces object to be partial type for mocking tests
Expand All @@ -31,13 +31,6 @@ export const forcedType = <T extends object>(obj: Partial<T>): T => {
return obj as T;
};

export type RandomNumberGenerator = (
min?: number,
max?: number,
fractionDigits?: number,
inclusive?: boolean,
) => number;

/**
* Return rng function with optional `min`, `max` and `fractionDigits` params
*
Expand Down
22 changes: 19 additions & 3 deletions src/utils/data_generators/data_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,31 @@
* under the License. */

import { Simple1DNoise } from './simple_noise';
import { RandomNumberGenerator } from '../../mocks/utils';

export type RandomNumberGenerator = (
min?: number,
max?: number,
fractionDigits?: number,
inclusive?: boolean,
) => number;

function defaultRNG(min = 0, max = 1, fractionDigits = 0, inclusive = true) {
const precision = Math.pow(10, Math.max(fractionDigits, 0));
const scaledMax = max * precision;
const scaledMin = min * precision;
const offset = inclusive ? 1 : 0;
const num = Math.floor(Math.random() * (scaledMax - scaledMin + offset)) + scaledMin;

return num / precision;
}

export class DataGenerator {
private randomNumberGenerator: RandomNumberGenerator;
private generator: Simple1DNoise;
private frequency: number;
constructor(frequency = 500, randomNumberGenerator: RandomNumberGenerator) {
constructor(frequency = 500, randomNumberGenerator: RandomNumberGenerator = defaultRNG) {
this.randomNumberGenerator = randomNumberGenerator;
this.generator = new Simple1DNoise(randomNumberGenerator);
this.generator = new Simple1DNoise(this.randomNumberGenerator);
this.frequency = frequency;
}
generateBasicSeries(totalPoints = 50, offset = 0, amplitude = 1) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/data_generators/simple_noise.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RandomNumberGenerator } from '../../mocks/utils';

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
Expand All @@ -18,6 +16,8 @@ import { RandomNumberGenerator } from '../../mocks/utils';
* specific language governing permissions and limitations
* under the License. */

import { RandomNumberGenerator } from './data_generator';

export class Simple1DNoise {
private maxVertices: number;
private maxVerticesMask: number;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
},
"extends": "./tsconfig",
"include": ["src/**/*"],
"exclude": ["**/*.test.*", "**/__mocks__"]
"exclude": ["**/*.test.*", "**/__mocks__", "src/mocks/**/*"]
}