Skip to content

Commit

Permalink
fix: build/type issue with DataGenerator (#671)
Browse files Browse the repository at this point in the history
- Move around types to exclude mocks directory
- Remove import of seedrandom from dist files
- Add default rng function for DataGenerator
  • Loading branch information
nickofthyme committed May 8, 2020
1 parent 7166e42 commit 86dd2b1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
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/**/*"]
}

0 comments on commit 86dd2b1

Please sign in to comment.