From f8425ee3b00b0abb14ba349096f2a043dfe477a9 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Sat, 19 Dec 2020 01:25:44 -0500 Subject: [PATCH] Import updated helper_functions file --- src/helper_functions.js | 60 ++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/src/helper_functions.js b/src/helper_functions.js index efa0fbc..5249d41 100644 --- a/src/helper_functions.js +++ b/src/helper_functions.js @@ -1,18 +1,16 @@ /** - * Contains misc helper functions. + * Various helper functions to make JavaScript development easier. + * + * https://git.brandon-rodriguez.com/javascript/helper_functions + * Version 1.0 */ // System Imports. const chalk = require('chalk'); - const readline = require('readline-sync'); - - // User Imports. /** - * Update strings to have an equivalent the "format()" found in Python strings. - * - * Found from https://stackoverflow.com/a/4974690 + * Update strings to have an equivalent to the "format()" function found in Python strings. */ String.prototype.format = function() { let i = 0, args = arguments; @@ -24,9 +22,6 @@ String.prototype.format = function() { /** * Gets human-readible string of current datetime. - * - * Date parsing logic from https://stackoverflow.com/a/5416970 - * Extra padding logic found at https://stackoverflow.com/a/20460414 */ function getCurrentDate() { let currentDate = new Date; @@ -44,9 +39,7 @@ function getCurrentDate() { /** * Pauses current execution to prompt user for yes/no input. - * Returns True on yes response, and False on all other responses. - * - * "Readline" logic from https://stackoverflow.com/a/50890409 + * :return: True on yes response, and False on all other responses. */ function userConfirmation() { @@ -71,7 +64,48 @@ function userConfirmation() { } +/** + * Equivalent of "sleep" in other languages, to delay code for x milliseconds. + * :param ms: Value in milliseconds to wait (1000 milliseconds = 1 second). + */ +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + + +/** + * Generates a random number between two values. + * :param min: Inclusive floor for random number. Defaults to 0. + * :param max: Inclusive ceiling for random number. Defaults to 10 or [ min+1 ], whichever is greater. + */ +function generateRandomNum(min, max) { + + // Validate min. + if (isNaN(parseInt(min))) { + // Not a valid number. Default to 0. + min = 0; + } + + // Validate max. + if (isNaN(parseInt(max))) { + // Not a valid number. Default to 10, or [ min+1 ], whichever is greater. + max = 10; + if (max <= min) { + max = min + 1; + } + } + + // Get random number between values (inclusive). + Math.ceil(min); + Math.floor(max) + return Math.floor(Math.random() * (max - min + 1) + min); +} + + + module.exports = { + generateRandomNum, getCurrentDate, + sleep, userConfirmation, }; -- GitLab