-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Paz Barda <[email protected]>
- Loading branch information
1 parent
a74b267
commit b1d8cc7
Showing
4 changed files
with
130 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,49 @@ | ||
import { Generator } from '../interfaces'; | ||
import {Generator} from '../interfaces'; | ||
|
||
class RandIntGenerator implements Generator { | ||
private static readonly MIN: string = 'min'; | ||
private static readonly MAX: string = 'max'; | ||
|
||
private fieldToPopulate: string = ''; | ||
private min: number = 0; | ||
private max: number = 0; | ||
|
||
initialise(fieldToPopulate: string, config: { [key: string]: any }): void { | ||
private static readonly MIN: string = 'min'; | ||
private static readonly MAX: string = 'max'; | ||
private fieldToPopulate = ''; | ||
private min = 0; | ||
private max = 0; | ||
|
||
initialise(fieldToPopulate: string, config: {[key: string]: any}): void { | ||
this.fieldToPopulate = this.validateName(fieldToPopulate); | ||
this.validateConfig(config); | ||
this.min = config[RandIntGenerator.MIN]; | ||
this.max = config[RandIntGenerator.MAX]; | ||
} | ||
this.validateConfig(config); | ||
this.min = config[RandIntGenerator.MIN]; | ||
this.max = config[RandIntGenerator.MAX]; | ||
} | ||
|
||
next(_historical: Object[]): Object { | ||
const randomNumber = Math.random(); | ||
var scaledNumber = randomNumber * (this.max - this.min) + this.min; | ||
var truncatedNumber = Math.trunc(scaledNumber); | ||
next(_historical: Object[]): Object { | ||
const randomNumber = Math.random(); | ||
const scaledNumber = randomNumber * (this.max - this.min) + this.min; | ||
const truncatedNumber = Math.trunc(scaledNumber); | ||
const retObject = { | ||
[this.fieldToPopulate]: truncatedNumber | ||
}; | ||
return retObject; | ||
} | ||
|
||
// TODO PB: extract to a utils class? | ||
private validateName(name: string | null): string { | ||
if (name === null || name.trim() === '') { | ||
// TODO PB - custom / more specific error? | ||
throw new Error('name is empty or null'); | ||
} | ||
return name; | ||
[this.fieldToPopulate]: truncatedNumber, | ||
}; | ||
return retObject; | ||
} | ||
|
||
// TODO PB: extract to a utils class? | ||
private validateConfig(config: { [key: string]: any }): void { | ||
if (!config.hasOwnProperty(RandIntGenerator.MIN)) { | ||
// TODO PB - custom / more specific error? | ||
throw new Error('config is missing ' + RandIntGenerator.MIN); | ||
} | ||
if (!config.hasOwnProperty(RandIntGenerator.MAX)) { | ||
// TODO PB - custom / more specific error? | ||
throw new Error('config is missing ' + RandIntGenerator.MAX); | ||
} | ||
} | ||
// TODO PB: extract to a utils class? | ||
private validateName(name: string | null): string { | ||
if (name === null || name.trim() === '') { | ||
// TODO PB - custom / more specific error? | ||
throw new Error('name is empty or null'); | ||
} | ||
return name; | ||
} | ||
|
||
// TODO PB: extract to a utils class? | ||
private validateConfig(config: {[key: string]: any}): void { | ||
if (!Object.prototype.hasOwnProperty.call(config, RandIntGenerator.MIN)) { | ||
// TODO PB - custom / more specific error? | ||
throw new Error('config is missing ' + RandIntGenerator.MIN); | ||
} | ||
if (!Object.prototype.hasOwnProperty.call(config, RandIntGenerator.MAX)) { | ||
// TODO PB - custom / more specific error? | ||
throw new Error('config is missing ' + RandIntGenerator.MAX); | ||
} | ||
} | ||
} | ||
|
||
export default RandIntGenerator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
export interface Generator { | ||
/** | ||
* initialise the generator with the given name and config. | ||
*/ | ||
initialise(name: String, config:{ [key: string]: any }): void | ||
/** | ||
* generate the next value, optionally based on historical values | ||
*/ | ||
next(historical: Object[]): Object; | ||
/** | ||
* initialise the generator with the given name and config. | ||
*/ | ||
initialise(name: String, config: {[key: string]: any}): void; | ||
/** | ||
* generate the next value, optionally based on historical values | ||
*/ | ||
next(historical: Object[]): Object; | ||
} | ||
export default Generator; |