Skip to content

Commit

Permalink
created getRandomValuesObject
Browse files Browse the repository at this point in the history
  • Loading branch information
mankal111 committed Feb 10, 2019
1 parent edfcfb9 commit 88ab328
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,25 @@ function getRandomElement(array) {
return array[getNumber({ min: 0, max: array.length - 1, type: 'integer' })];
}

/**
* Returns an object with random values in given names and given specs
* @param {{name: String, type: Object}[]} specs - The array with the names and the spec
* @returns {Object} An object with the random values in the given names as keys
*/
function getRandomValuesObject(specs) {
return specs.reduce((result, item) => {
// eslint-disable-next-line no-param-reassign
result[item.name] = getNumber(item.type);
return result;
},
{});
}

const JXRand = {
getNumber,
getInterval,
getRandomElement,
getRandomValuesObject,
};

module.exports = JXRand;
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,17 @@ describe('getNumber', () => {
expect(givenArray.includes(JXRand.getRandomElement(givenArray))).to.equal(true);
});
});

describe('getRandomValuesObject', () => {
it('should return an object with random values described in the argument array', () => {
const randomValuesSpecs = [
{ name: 'var1', type: { min: 1, max: 3, type: 'integer' } },
{ name: 'var2', type: { min: 4, max: 6, type: 'integer' } },
];
const randomValuesObject = JXRand.getRandomValuesObject(randomValuesSpecs);
expect(randomValuesObject.var1).to.not.be.below(1);
expect(randomValuesObject.var1).to.not.be.above(3);
expect(randomValuesObject.var2).to.not.be.below(4);
expect(randomValuesObject.var2).to.not.be.above(6);
});
});

0 comments on commit 88ab328

Please sign in to comment.