From cdd6a48b47d32b6acc188bedd0d2cb66a677b3d0 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sun, 6 Feb 2022 22:26:49 +0100 Subject: [PATCH] Add random --- README.md | 14 ++++++++++++-- dist/fnTools.js | 26 ++++++++++++++++++++++++-- src/fnTools.ts | 23 +++++++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 897fdde..ab4c627 100644 --- a/README.md +++ b/README.md @@ -8,16 +8,26 @@ Simple functional functions common to any development in Typescript or JavaScrip - **uniqValuesArray**: Creates a duplicate-free version of an array. - **cloneJSON**: Clone JSON. - **updateJSON**: Returns a JSON with an updated value. +- **getRandom**: Returns a random number from a range, with an optional number of decimal places. ## Example -```typescript -import * as fn from 'fnTools' +```javascript +import * as fn from 'fnTools.js'; + +fn.getRandom(0, 6); +// 2 fn.range(4); // [0, 1, 2, 3, 4] ``` +## CDN + +```html + +``` + ## Compile Install Typescript diff --git a/dist/fnTools.js b/dist/fnTools.js index 7d6f00a..623017a 100644 --- a/dist/fnTools.js +++ b/dist/fnTools.js @@ -1,7 +1,7 @@ define("fnTools", ["require", "exports"], function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); - exports.cloneJSON = exports.uniqValuesArray = exports.updateJSON = exports.range = exports.dec = exports.inc = void 0; + exports.getRandom = exports.cloneJSON = exports.uniqValuesArray = exports.updateJSON = exports.range = exports.dec = exports.inc = void 0; /** * Increase the value of a `number`. * @@ -16,7 +16,7 @@ define("fnTools", ["require", "exports"], function (require, exports) { * inc(5, 3) * // => 8 * - * dec(7.3) + * inc(7.3) * // => 8.3 * */ @@ -130,4 +130,26 @@ define("fnTools", ["require", "exports"], function (require, exports) { return JSON.parse(JSON.stringify(json)); } exports.cloneJSON = cloneJSON; + /** + * Return random number. + * + * @param {number} min - Minimum number. It is inclusive. + * @param {number} max - Maximum number. It is inclusive. + * @param {number} decimals - Number of decimals. Default 0. + * @return {number} + * @example + * + * getRandom(0, 4) + * // => 3 (Maybe) + * + * getRandom(0, 10, 2) + * // => 8.31 (Maybe) + * + */ + function getRandom(min, max, decimals = 0) { + const minRandom = Math.ceil(min); + const maxRandom = Math.floor(max); + return (Math.random() * (maxRandom - minRandom + 1) + minRandom).toFixed(decimals); + } + exports.getRandom = getRandom; }); diff --git a/src/fnTools.ts b/src/fnTools.ts index 89e9563..e96349c 100644 --- a/src/fnTools.ts +++ b/src/fnTools.ts @@ -132,4 +132,27 @@ export function uniqValuesArray(array: any[]): any[] { */ export function cloneJSON(json: JSON): JSON { return JSON.parse(JSON.stringify(json)); +} + + +/** + * Return random number. + * + * @param {number} min - Minimum number. It is inclusive. + * @param {number} max - Maximum number. It is inclusive. + * @param {number} decimals - Number of decimals. Default 0. + * @return {number} + * @example + * + * getRandom(0, 4) + * // => 3 (Maybe) + * + * getRandom(0, 10, 2) + * // => 8.31 (Maybe) + * + */ +export function getRandom(min, max, decimals=0) { + const minRandom = Math.ceil(min); + const maxRandom = Math.floor(max); + return (Math.random() * (maxRandom - minRandom + 1) + minRandom).toFixed(decimals); } \ No newline at end of file