@gent-js/gent
Version:
template-based data generator.
33 lines (32 loc) • 968 B
JavaScript
import { faker } from "@faker-js/faker";
import { MAX_PROBABILITY, MIN_PROBABILITY, } from "./jsonableParametersConsts.js";
export class AbstractJsonable {
probability;
weight;
constructor(probability, weight) {
this.probability = probability;
this.weight = weight;
}
testProbability(keyOrIndex, context) {
if (this.probability === undefined) {
return true;
}
if (this.probability <= MIN_PROBABILITY) {
return false;
}
if (this.probability >= MAX_PROBABILITY) {
return true;
}
const diceValue = faker.number.float({
min: MIN_PROBABILITY,
max: MAX_PROBABILITY,
});
return diceValue < this.probability;
}
toJSON(keyOrIndex, context) {
if (!this.testProbability(keyOrIndex, context)) {
return undefined;
}
return this.toJSONImpl(keyOrIndex, context);
}
}