diff --git a/index.js b/index.js index 392a7e2..540aa7d 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/test/test.js b/test/test.js index 5591ac2..4be6268 100644 --- a/test/test.js +++ b/test/test.js @@ -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); + }); +});