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.
47 lines (46 loc) • 2.58 kB
JavaScript
(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", "../gen/NumGen"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// import Gen functions
const NumGen_1 = require("../gen/NumGen"); // function for generating a random word
// function for generating a random number
/**
* This TypeScript function generates a random number ID of a specified length, with the option to
* exclude zero and use a custom set of numbers.
* @param {num} [length=1] - The length parameter is a number that specifies the length of the
* generated number ID.
* @param {bool} [WithZero=true] - A boolean value that determines whether or not the generated number
* can include the digit 0. If set to true, the digit 0 will be included in the possible numbers to
* generate. If set to false, the digit 0 will be removed from the possible numbers to generate.
* @param {num[] | undefined} CustomNumbers - An optional parameter that allows the user to provide a
* custom array of numbers to generate the random number ID from. If not provided, the function will
* use the default array of numbers from 0 to 9.
* @returns a Promise that resolves to a number (num).
*/
function GenerateNumberID(length = 1, WithZero = true, CustomNumbers) {
/* Creating an array of numbers from 0 to 9 that will be used to generate the random 10-digit number
ID. */
const NumbersArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; // All Possible Numbers to generate
// Filtered Array
const FilteredArray = CustomNumbers !== undefined ? CustomNumbers : NumbersArray;
if (WithZero === false) {
const ZeroIndexNum = FilteredArray.indexOf(0); // Get the index of Zero
FilteredArray.splice(ZeroIndexNum, 1); // Remove Zero from the Array
const GenerationResult = (0, NumGen_1.default)(length, FilteredArray); // Generate the Random Number
return GenerationResult; // Return the Result
}
else {
const GenerationResult = (0, NumGen_1.default)(length, FilteredArray); // Generate the Random Number
return GenerationResult; // Return the Result
}
}
exports.default = GenerateNumberID;
});