From fea9cf3e733ee6eab187d902216258a0ec172ed4 Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Thu, 5 Oct 2023 11:47:06 +0100 Subject: [PATCH] Add randomInteger() to new GeneralHelper 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. --- test/support/general.helper.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 test/support/general.helper.js diff --git a/test/support/general.helper.js b/test/support/general.helper.js new file mode 100644 index 0000000000..b818f450c5 --- /dev/null +++ b/test/support/general.helper.js @@ -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 +}