diff --git a/README.md b/README.md index 0dc5bc3..c7494aa 100644 --- a/README.md +++ b/README.md @@ -295,13 +295,42 @@ This is how the script knows we want to nest objects ```json { - "whichDisneyMovieToWatchTonight": { + "whichMovieToWatchTonight": { "type": "randomElementInArray", - "value": ["Frozen", "Mulan", "The Lion King", "Aladdin"] + "value": ["Frozen", "Mulan", "The Lion King", "Aladdin", "Pulp Fiction"] } } ``` +*output* +```json +{ + "whichMovieToWatchTonight": "Pulp Fiction" +} +``` + +##### randomElementsInArray + +*This one returns a random group of elements from an array of options.* + +`value: Array` a list of options to pick from. + +```json +{ + "whichMoviesToWatchTonight": { + "type": "randomElementsInArray", + "value": ["Frozen", "Mulan", "The Lion King", "Aladdin", "Pulp Fiction"] + } +} +``` + +*output* +```json +{ + "whichMoviesToWatchTonight": ["Pulp Fiction", "Aladdin"] +} +``` + ##### randomNumberBetweenWithString *Just another version of randomNumberBetween that accepts a range of numbers, a prefix as a string and a suffix as a string* diff --git a/lib/helpers/index.js b/lib/helpers/index.js index d16715f..1fd216e 100644 --- a/lib/helpers/index.js +++ b/lib/helpers/index.js @@ -2,13 +2,15 @@ import './misc' import { incrementNumber, randomBetween, - randomArray, - randomBetweenWithString + randomBetweenWithString, + randomElementInArray, + randomElementsInArray, } from './numbers' export const numbers = { incrementNumber, randomBetween, - randomArray, - randomBetweenWithString + randomBetweenWithString, + randomElementInArray, + randomElementsInArray, } diff --git a/lib/helpers/numbers.js b/lib/helpers/numbers.js index 187d3fa..9f266f8 100644 --- a/lib/helpers/numbers.js +++ b/lib/helpers/numbers.js @@ -3,9 +3,18 @@ const randomBetween = ([min, max]) => { return Math.floor(Math.random()*(max-min+1)+min) } -var randomArray = function randomElementInArray(arr) { - const ele = Math.floor(Math.random()*(arr.length - 1)) - return arr[ele]; +const randomElementInArray = function randomElementInArray(value) { + const ele = Math.floor(Math.random() * (value.length - 1)) + return value[ele]; +} + +const randomElementsInArray = (value) => { + const subArray = [...value] + for (let i = subArray.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [subArray[i], subArray[j]] = [subArray[j], subArray[i]]; + } + return subArray.slice(0, Math.floor(Math.random() * subArray.length) || 1); } const randomBetweenWithString = (value, { @@ -26,6 +35,7 @@ const incrementNumber = (value, { index, from = 0 }) => { export { incrementNumber, randomBetween, - randomArray, randomBetweenWithString, + randomElementInArray, + randomElementsInArray, } diff --git a/lib/parse-model.js b/lib/parse-model.js index aef97dc..a178125 100644 --- a/lib/parse-model.js +++ b/lib/parse-model.js @@ -121,7 +121,8 @@ const modelAttributeTypes = { // ---- numbers incrementNumber: numbers.incrementNumber, randomNumberBetween: numbers.randomBetween, - randomElementInArray: numbers.randomArray, + randomElementInArray: numbers.randomElementInArray, + randomElementsInArray: numbers.randomElementsInArray, randomNumberBetweenWithString: numbers.randomBetweenWithString } diff --git a/test/unit/helpers/numbers.spec.js b/test/unit/helpers/numbers.spec.js index 468c3bd..c48e6e9 100644 --- a/test/unit/helpers/numbers.spec.js +++ b/test/unit/helpers/numbers.spec.js @@ -17,14 +17,22 @@ describe('Numbers', () => { expect(randomNumber).to.be.within(from, to) }) - it('randomArray - returns a a random value from an array', () => { - const { randomArray } = numbers + it('randomElementInArray - returns a random value from an array', () => { + const { randomElementInArray } = numbers const arr = ['One','Two','Three','Four]'] - const element = randomArray(arr) + const element = randomElementInArray(arr) expect(arr).to.include(element); }) + it('randomElementsInArray - returns a random subgroup from an array', () => { + const { randomElementsInArray } = numbers + const arr = ['One','Two','Three','Four]'] + const elements = randomElementsInArray(arr) + + expect(elements).to.be.an('array').that.is.not.empty; + // expect(arr).to.include(element); + }) it('randomBetweenWithString - returns a number between 1 and 2500000 with prefix \'#\' and suffix \'*\'', () => { const { randomBetweenWithString } = numbers