@har-sdk/openapi-sampler
Version:
[](https://codeclimate.com/github/NeuraLegion/har-sdk/maintainability) [ {
this.EPS = 0.001;
}
sample(schema) {
const type = schema.type ? schema.type : 'number';
const integer = type === 'integer';
const minimum = this.getMinimum(schema);
const maximum = this.getMaximum(schema);
let res;
let usingMaximum = false;
if (minimum) {
res = this.sampleUsingMinimum(minimum, integer);
}
else if (maximum) {
usingMaximum = true;
res = this.sampleUsingMaximum(maximum, integer);
}
if (schema.multipleOf) {
res = this.roundUp(res !== null && res !== void 0 ? res : schema.multipleOf, schema.multipleOf);
res = usingMaximum ? res - schema.multipleOf : res;
}
return this.ensureBoundaries(res !== null && res !== void 0 ? res : 42, { minimum, maximum, schema });
}
sampleUsingMinimum(boundary, integer) {
let schemaMinimum = boundary.value;
let exclusiveMinimum = boundary.exclusive;
if (integer && !Number.isInteger(schemaMinimum)) {
schemaMinimum = Math.ceil(schemaMinimum);
exclusiveMinimum = false;
}
return exclusiveMinimum
? schemaMinimum + (integer ? 1 : this.EPS)
: schemaMinimum;
}
sampleUsingMaximum(boundary, integer) {
let schemaMaximum = boundary.value;
let exclusiveMaximum = boundary.exclusive;
if (integer && !Number.isInteger(schemaMaximum)) {
schemaMaximum = Math.floor(schemaMaximum);
exclusiveMaximum = false;
}
return exclusiveMaximum
? schemaMaximum - (integer ? 1 : this.EPS)
: schemaMaximum;
}
roundUp(value, multipleOf) {
if (!multipleOf || !value) {
return 0;
}
return Math.ceil(value / multipleOf) * multipleOf;
}
ensureBoundaries(value, options) {
const { minimum, maximum, schema } = options;
let valid = true;
if (minimum) {
valid = this.isValidMinimum(minimum, value);
}
if (maximum) {
valid = this.isValidMaximum(maximum, value);
}
if (valid &&
schema.multipleOf &&
!Number.isInteger(value / schema.multipleOf)) {
valid = false;
}
if (!valid) {
throw new Error(`Sample numeric cannot be generated by boundaries: ${this.formatConditions(options)}`);
}
return value;
}
isValidMinimum(minimum, value) {
return minimum.exclusive ? value > minimum.value : value >= minimum.value;
}
isValidMaximum(maximum, value) {
return maximum.exclusive ? value < maximum.value : value <= maximum.value;
}
formatConditions({ minimum, maximum, schema }) {
let res = '';
if (minimum) {
res += `${minimum.value} ${minimum.exclusive ? '<' : '<='} `;
}
res += 'x';
if (maximum) {
res += ` ${maximum.exclusive ? '<' : '<='} ${maximum.value}`;
}
if ('multipleOf' in schema) {
res += `, multipleOf: ${schema.multipleOf}`;
}
return res;
}
getMinimum(schema) {
var _a;
let value = (_a = schema.minimum) !== null && _a !== void 0 ? _a : undefined;
let exclusive = !!schema.exclusiveMinimum;
if (typeof schema.exclusiveMinimum === 'number') {
value = schema.exclusiveMinimum;
exclusive = true;
}
return value !== undefined
? {
value,
exclusive
}
: undefined;
}
getMaximum(schema) {
var _a;
let value = (_a = schema.maximum) !== null && _a !== void 0 ? _a : undefined;
let exclusive = !!schema.exclusiveMaximum;
if (typeof schema.exclusiveMaximum === 'number') {
value = schema.exclusiveMaximum;
exclusive = true;
}
return value !== undefined
? {
value,
exclusive
}
: undefined;
}
}
//# sourceMappingURL=NumberSampler.js.map