Skip to content

Commit

Permalink
Merge pull request #64 from Cambalab/63-As-a-user-i-want-to-get-a-sub…
Browse files Browse the repository at this point in the history
…group-of-arrays-from-given-options

63 as a user i want to get a subgroup of arrays from given options
  • Loading branch information
sgobotta authored Mar 21, 2020
2 parents a2d724b + dc7c47c commit 1ed6b41
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
10 changes: 6 additions & 4 deletions lib/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
18 changes: 14 additions & 4 deletions lib/helpers/numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -26,6 +35,7 @@ const incrementNumber = (value, { index, from = 0 }) => {
export {
incrementNumber,
randomBetween,
randomArray,
randomBetweenWithString,
randomElementInArray,
randomElementsInArray,
}
3 changes: 2 additions & 1 deletion lib/parse-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
14 changes: 11 additions & 3 deletions test/unit/helpers/numbers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1ed6b41

Please sign in to comment.