Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "genmo", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Generates models based off of json-schemas", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
module.exports = function (schema) { | ||
return Math.random() * 100 | ||
var max = getMax(schema) | ||
var min = getMin(schema) | ||
var num = Math.random() * (max - min) + min | ||
if (schema.multipleOf) { | ||
num = Math.floor(num) * schema.multipleOf | ||
} | ||
return num | ||
} | ||
function getMax(schema) { | ||
var exclusiveMax = Boolean(schema.exclusiveMaximum) | ||
var max = schema.maximum || 100 | ||
if (schema.multipleOf) { | ||
var multiple = schema.multipleOf | ||
max = Math.floor(max / multiple) | ||
} | ||
return max | ||
} | ||
function getMin(schema) { | ||
var exclusiveMin = Boolean(schema.exclusiveMinimum) | ||
var min = schema.minimum || 0 | ||
if (schema.multipleOf) { | ||
var multiple = schema.multipleOf | ||
min = Math.ceil(min / multiple) | ||
} | ||
// if we are excluding min, increase min by a small amount to make it | ||
// impossible to get it | ||
return min + (exclusiveMin ? 0.001 : 0) | ||
} |
@@ -11,1 +11,74 @@ var tape = require('tape') | ||
}) | ||
tape('number respects the minimum value', function (t) { | ||
var min = 99 | ||
var schema = { | ||
type: 'number', | ||
minimum: min | ||
} | ||
var allAbove = true | ||
for (var i = 0; i < 100 && allAbove; i++) { | ||
allAbove = number(schema) >= min | ||
} | ||
t.equal(allAbove, true) | ||
t.end() | ||
}) | ||
tape('number respects the exclusiveMinimum value', function (t) { | ||
var min = 99 | ||
var schema = { | ||
type: 'number', | ||
minimum: min, | ||
exclusiveMinimum: true | ||
} | ||
var allAbove = true | ||
for (var i = 0; i < 100 && allAbove; i++) { | ||
allAbove = number(schema) > min | ||
} | ||
t.equal(allAbove, true) | ||
t.end() | ||
}) | ||
tape('number respects the maximum value', function (t) { | ||
var max = 1 | ||
var schema = { | ||
type: 'number', | ||
maximum: max | ||
} | ||
var allBelow = true | ||
for (var i = 0; i < 100 && allBelow; i++) { | ||
allBelow = number(schema) <= max | ||
} | ||
t.equal(allBelow, true) | ||
t.end() | ||
}) | ||
tape('number respects the exclusiveMaximum value', function (t) { | ||
var max = 1 | ||
var schema = { | ||
type: 'number', | ||
maximum: max, | ||
exclusiveMaximum: true | ||
} | ||
var allBelow = true | ||
for (var i = 0; i < 100 && allBelow; i++) { | ||
allBelow = number(schema) < max | ||
} | ||
t.equal(allBelow, true) | ||
t.end() | ||
}) | ||
tape('number respects multipleOf value', function (t) { | ||
var multiple = 1.5 | ||
var schema = { | ||
type: 'number', | ||
multipleOf: multiple | ||
} | ||
t.equal(number(schema) % multiple, 0) | ||
t.end() | ||
}) |
12518
469