Skip to content

Commit

Permalink
Add randomInteger() to new GeneralHelper
Browse files Browse the repository at this point in the history
Where a helper is hard coding a UUID for a field is going to be easy to make unique; we just call our `GeneralLib.generateUUID()` function.

But many helpers also need to generate references, licence number being a good example. If we are going to make these unique we're going to need a means to generate random numbers. Often they need to be within a certain range, for example, 100 to 999 because the number must be a certain number of digits long.

So, we're adding a new `randomInteger()` function which can handle this requirement. Doing so forced us to make a decision as to where to put it. We decided to create a `GeneralHelper` module that can be used going forward for any shared functionality in the helpers.
  • Loading branch information
Cruikshanks committed Oct 5, 2023
1 parent baa3b5b commit fea9cf3
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions test/support/general.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

/**
* General helper functions available to all helpers
* @module GeneralHelper
*/

/**
* Generate a random integer within a range (inclusive)
*
* @param {Number} min lowest number (integer) in the range (inclusive)
* @param {Number} max largest number (integer) in the range (inclusive)
*
* Credit https://stackoverflow.com/a/7228322
*
* @returns a number between min and max (inclusive)
*/
function randomInteger (min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}

module.exports = {
randomInteger
}

0 comments on commit fea9cf3

Please sign in to comment.