diff --git a/src/helperise/random_helpers/yipo.js b/src/helperise/random_helpers/yipo.js new file mode 100644 index 0000000..89c9052 --- /dev/null +++ b/src/helperise/random_helpers/yipo.js @@ -0,0 +1,26 @@ +/** + * Returns a random number between two values (0 -> 1 by default) + * - If only one argument is specified, that will become the maximum range, with 0 as the minimum. + * - If two arguments are specified, the first will be the minimum, and the second will be the maximum. + * + * @returns {number} + */ +function yipo (args) { + if (Array.isArray(args)) { + let [ min, max, ] = args; + min = Number(min) || 0; + max = Number(max) || 0; + + if (min && !max) { + max = min; + min = 0; + } + + if (!max) max = 1; + + return min + (Math.random() * (max - min)); + } + throw new Error("Yorlang system error"); +} + +module.exports = yipo; diff --git a/src/helperise/registeredHelperIse.js b/src/helperise/registeredHelperIse.js index 19eaea5..abf5d92 100644 --- a/src/helperise/registeredHelperIse.js +++ b/src/helperise/registeredHelperIse.js @@ -6,5 +6,6 @@ helperIseDeclarations["waNinu"] = require("./string_helpers/wa_ni_nu.js"); helperIseDeclarations["fiRopo"] = require("./string_helpers/fi_ro_po.js"); helperIseDeclarations["teSibi"] = require("./input_output/tesibi.js"); helperIseDeclarations["aago"] = require("./datetime_helpers/aago.js"); +helperIseDeclarations["yipo"] = require("./random_helpers/yipo.js"); module.exports = helperIseDeclarations; diff --git a/src/tests/helperise/random_helpers/yipo.test.js b/src/tests/helperise/random_helpers/yipo.test.js new file mode 100644 index 0000000..1dae2cb --- /dev/null +++ b/src/tests/helperise/random_helpers/yipo.test.js @@ -0,0 +1,24 @@ +const yipo = require("../../../helperise/random_helpers/yipo.js"); + +describe("Yipo Test suite", () => { + test("It should return a random number between 0 and 1", () => { + for (let i = 1; i <= 5; i++) { + expect(yipo([])).toBeGreaterThan(0) + expect(yipo([])).toBeLessThan(1) + } + }); + + test("It should return a random number between 1 and 2", () => { + for (let i = 1; i <= 5; i++) { + expect(yipo([1, 2])).toBeGreaterThan(1) + expect(yipo([1, 2])).toBeLessThan(2) + } + }); + + test("It should return a random number between 0 and 7", () => { + for (let i = 1; i <= 5; i++) { + expect(yipo([7])).toBeGreaterThan(0) + expect(yipo([7])).toBeLessThan(7) + } + }); +});