UNPKG

randomize-unique

Version:

Random Interger, Random Float, Random Boolean, Random String or Password, Random Array, Unique Image Name, Random RGB-HEX-HSL Color Generator

106 lines (91 loc) 2.6 kB
exports.randomInteger = (min = 1, max = 100) => { return Math.floor(Math.random() * (max - min)) + min; }; exports.randomFloat = (min = 1, max = 10000, fraction = 2) => { return (Math.random() * (max - min) + min).toFixed(fraction); }; exports.randomBoolean = () => { return !Math.round(Math.random()); }; exports.randomString = ( length = 8, isCapitalAllow = false, isNumberAllow = false, isSpecialCharAllow = false ) => { var result = ""; var characters = "abcdefghijklmnopqrstuvwxyz"; if (isCapitalAllow === true) { characters = characters + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } if (isNumberAllow === true) { characters = characters + "0123456789"; } if (isSpecialCharAllow === true) { characters = characters + "!@#$%&*"; } charactersLength = characters.length; for (var i = 0; i < length; i++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; }; exports.randomArray = (length = 10) => { return Array.from({ length: length }, () => Math.floor(Math.random() * length) ); }; exports.randomStringArray = (length = 5, minStringLength = 5, maxStringLength = 10, isSpaceAllow = true) => { var result = ""; var characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; if (isSpaceAllow === true) { characters = characters + " "; } var charactersLength = characters.length; var lst = []; for (var i = 0; i < length; i++) { for (var j = 0; j < Math.floor(Math.random() * (maxStringLength - minStringLength)) + minStringLength; j++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } lst.push(result); result = ""; } return list; }; exports.uniqueImageName = (prefix = "", suffix = "") => { var name = Date.now(); if (prefix !== "") { name = prefix + name; } if (suffix !== "") { name = name + suffix; } return name; }; exports.randomRGBColor = () => { return ( "rgb(" + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) + ")" ); }; exports.randomHEXColor = () => { return "#000000".replace(/0/g, function () { return (~~(Math.random() * 16)).toString(16); }); }; exports.randomHSLColor = () => { return ( "hsl(" + Math.floor(Math.random() * 360) + "," + Math.floor(Math.random() * 100) + "%," + Math.floor(Math.random() * 100) + "%)" ); };