uniquegen
Version:
uniquegen is an package for Node.js projects, enabling the generation of random numbers and words. It offers flexibility and ease of use, making it a valuable tool for developers.
56 lines (55 loc) • 3.42 kB
JavaScript
// This File is used as a middleware to generate Random Words
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* This TypeScript function generates a random string of specified length using letters from a to z.
* @param {num} length - The length parameter is a number that determines the length of the generated
* word. It is used to specify the number of rounds in the while loop that generates the random word.
* @returns a randomly generated string of characters with a length specified by the input parameter
* `length`.
*/
function GenerateWord(length, Words) {
/* The code is initializing an empty array called `FinalID` with a type of `str[]`, which means it is
an array of strings. This array will be used to store the randomly generated characters to create
the final ID string. */
// Final ID Array
const FinalID = []; // Final ID
/* This code block is a while loop that generates a random string of characters. The loop runs for the
number of rounds specified by the input parameter `length`. In each round, a random number between 0
and 25 is generated using the `Math.random()` function. This number is then rounded to the nearest
integer using the `Math.round()` function. The rounded number is used as an index to select a
character from the `Words` array, which contains all possible letters from 'a' to 'z'. The selected
character is then pushed to the `FinalID` array. Finally, the number of rounds is decreased by 1,
and the loop continues until all rounds have been completed. */
// Looping through the rounds
while (length > 0) {
const FinalRandomNumForText = GenerateNumberIndex(Words); // Generate Random Number Index Between 0 and Array Length - 1
// Push the Number to Final ID Array
FinalID.push(Words[FinalRandomNumForText]); // Push the Number to Final ID
length--; // Decrease the Number of Rounds
}
/* This code block is converting the `FinalID` array, which contains the randomly generated
characters, into a string by joining all the elements of the array together with no separator
between them. The resulting string is stored in the constant variable `FinalIDString`, which has a
type of `str`, meaning it is a string. Finally, the function returns the `FinalIDString` variable,
which contains the randomly generated string of characters. */
const FinalTextIDString = FinalID.join(''); // Join the Array to String
return FinalTextIDString; // Return the Final ID
}
exports.default = GenerateWord;
// Generate Random Number Index
function GenerateNumberIndex(ArrayWords) {
const TempRandomNumBer = Math.random() * (ArrayWords.length - 1 - 0) + 0; // Generate Random Number Between 0 and Array Length - 1
const FinalRandomNumber = Math.round(TempRandomNumBer); // Round the Number
return FinalRandomNumber; // Return the Final Random Number
}
});