Skip to content
This repository has been archived by the owner on May 28, 2020. It is now read-only.

fix: randomInt should be set min = 0 max = INTMAX value #38

Merged
merged 1 commit into from Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/template/operation/index.js
@@ -1,20 +1,25 @@
'use strict'


function getRandom(min = 0, max = Number.MAX_SAFE_INTEGER) {
if (min > max) {
max = min;
min = 0;
}
return Math.trunc(Math.random() * (max - min) + min);
}

function randomInt(value) {
if (typeof value !== 'string') {
return Math.trunc(Math.random() * 100);
if (typeof value === 'number') {
return getRandom(value);
}
const matched = value.match(/(\d+)-(\d+)/);
if (!matched) {
return Math.trunc(Math.random() * 100);
return getRandom();
}
const min = matched[1];
const max = matched[2];
if (min > max) {
return Math.trunc(Math.random() * 100);
}
return Math.trunc((Math.random() * (max - min) + min));
return getRandom(min, max);
}

module.exports = (operation, ...values) => {
Expand Down
6 changes: 3 additions & 3 deletions test/lib/template/format.operationalKey.js
Expand Up @@ -13,13 +13,13 @@ test('format: {randomInt:id}', () => {
test('format: {randomInt:id} but no range', () => {
const result = format({ id: '{randomInt:id}' }, { id: 1 });
// default value is over 1
assert(result.id >= 0);
assert(result.id <= 100);
assert(result.id > 1);
assert(result.id <= Number.MAX_SAFE_INTEGER);
});

test('format: {randomInt:id} but range 1000-100', () => {
const result = format({ id: '{randomInt:id}' }, { id: '1000-100' });
assert(result.id >= 0);
assert(result.id <= 100);
assert(result.id <= 1000);
});