@alwatr/random
Version:
A lightweight utility library for generating random numbers, strings, UUIDs and more
5 lines (4 loc) • 3.43 kB
JavaScript
/** 📦 @alwatr/random v5.1.29 */
__dev_mode__: console.debug("📦 @alwatr/random v5.1.29");
;var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var main_exports={};__export(main_exports,{bytesToHex:()=>bytesToHex,randArray:()=>randArray,randBoolean:()=>randBoolean,randColor:()=>randColor,randFloat:()=>randFloat,randInteger:()=>randInteger,randNumber:()=>randNumber,randPick:()=>randPick,randShuffle:()=>randShuffle,randStep:()=>randStep,randString:()=>randString,randUuid:()=>randUuid});module.exports=__toCommonJS(main_exports);var import_global_this=require("@alwatr/global-this");var globalThis=(0,import_global_this.getGlobalThis)();var hasCrypto=(()=>typeof globalThis.crypto!=="undefined")();function bytesToHex(bytes){let result="";for(const byte of bytes){const hex=byte.toString(16);result+=hex.length===1?"0"+hex:hex}return result}function randNumber(){return Math.random()}function randFloat(min,max){return Math.random()*(max-min)+min}function randInteger(min,max){return Math.floor(randFloat(min,max+1))}function randString(minLength,maxLength=minLength,chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"){const length=maxLength===minLength?minLength:randInteger(minLength,maxLength);if(length<=0)return"";const charsLength=chars.length;let result="";if(length<=10){for(let i=0;i<length;i++){result+=chars.charAt(Math.floor(Math.random()*charsLength))}return result}const resultArray=new Array(length);for(let i=0;i<length;i++){resultArray[i]=chars.charAt(Math.floor(Math.random()*charsLength))}return resultArray.join("")}function randStep(min,max,step){if(step===0){return min}const steps=Math.floor((max-min)/step);return min+randInteger(0,steps)*step}function randShuffle(array){for(let i=array.length-1;i>0;i--){const j=randInteger(0,i);[array[i],array[j]]=[array[j],array[i]]}return array}function randPick(array){if(array.length===0)throw new Error("Cannot pick from empty array");return array[randInteger(0,array.length-1)]}function randArray(array,min=0,max=255){for(let i=array.length-1;i>=0;i--){array[i]=randInteger(min,max)}return array}function randUuid(){if(hasCrypto&&globalThis.crypto?.randomUUID){return globalThis.crypto.randomUUID()}const bytes=randArray(new Uint8Array(16));bytes[6]=bytes[6]&15|64;bytes[8]=bytes[8]&191|128;return`${bytesToHex(bytes.subarray(0,4))}-${bytesToHex(bytes.subarray(4,6))}-${bytesToHex(bytes.subarray(6,8))}-${bytesToHex(bytes.subarray(8,10))}-${bytesToHex(bytes.subarray(10,16))}`}function randBoolean(probability=.5){return Math.random()<probability}function randColor(){const bytes=randArray(new Array(3));return`#${bytesToHex(bytes)}`}0&&(module.exports={bytesToHex,randArray,randBoolean,randColor,randFloat,randInteger,randNumber,randPick,randShuffle,randStep,randString,randUuid});
//# sourceMappingURL=main.cjs.map