UNPKG

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.

55 lines (54 loc) 3.36 kB
// This File is used as a middleware to generate Random Symbols (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 symbols with a specified length. * @param {num} length - The length parameter is a number that specifies the length of the symbol * string that will be generated by the function. * @returns a string that consists of randomly generated symbols of a specified length. */ function GenerateSymbol(length, Symbols) { /* This code initializes an empty array called `FinalID` with a type of `str[]` (an array of strings). This array will be used to store the randomly generated symbols that will be used to create the final symbol string of the specified length. */ // Final ID Array const FinalID = []; // Final ID /* This code block is a while loop that generates a random number between 0 and 25 (inclusive) using the Math.random() method. The generated number is then rounded to the nearest integer using the Math.round() method. The rounded number is used as an index to access a random symbol from the SymbolArray. The selected symbol is then pushed into the FinalID array. This process is repeated for the number of rounds specified by the length parameter passed to the GenerateSymbol function. The loop continues until the CountRound variable reaches 0. Finally, the FinalID array is joined into a string using the join() method and returned as the final symbol string. */ // Looping through the rounds while (length > 0) { const FinalRandomNumForSymbol = GenerateNumberIndex(Symbols); // Generate Random Number Index Between 0 and Array Length - 1 FinalID.push(Symbols[FinalRandomNumForSymbol]); // 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 symbols, into a string by joining all the elements of the array together using the `join()` method. The resulting string is then stored in the `FinalSymbolIDString` variable, which is of type `str` (string). Finally, the `FinalSymbolIDString` variable is returned as the final symbol string generated by the `GenerateSymbol` function. */ // Make the Final ID From Array to String const FinalSymbolIDString = FinalID.join(''); // Join the Array to String return FinalSymbolIDString; // Return the Final ID } exports.default = GenerateSymbol; // Generate Random Number Index function GenerateNumberIndex(ArraySymbol) { const TempRandomNumBer = Math.random() * (ArraySymbol.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 } });