UNPKG

@grandom/string-umd

Version:
23 lines (22 loc) 9.55 kB
/*! * @grandom/string v1.2.7 * https://github.com/grandom-library/grandom-js/tree/main/packages/string#readme * * Copyright (c) 2023 Richard King <richrdkng@gmail.com> (www.richrdkng.com) * Released under the MIT License * https://github.com/grandom-library/grandom-js/tree/main/packages/string#readme/blob/main/LICENSE * * Date: 2023-09-06T22:07:07.701Z */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("crypto")):"function"==typeof define&&define.amd?define(["crypto"],e):(t="undefined"!=typeof globalThis?globalThis:t||self).grandomString=e(t.require$$1)}(this,(function(t){"use strict";function e(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var n={};class r{constructor(t){this._name=t}_next(){throw new Error("_next() must be implemented.")}get name(){return this._name}nextBoolean(){return this._next()<.5}nextInteger(t=r.DEFAULT_INTEGER_MINIMUM,e=r.DEFAULT_INTEGER_MAXIMUM,n=r.DEFAULT_INCLUDE_MINIMUM,s=r.DEFAULT_INCLUDE_MAXIMUM){let i=0,o=0;return n&&!s?(i=0,o=0):n&&s?o=1:!n&&s?i=1:(i=1,o=-1),Math.floor(this._next()*(e-t+o))+t+i}nextFloat(t=r.DEFAULT_FLOAT_MINIMUM,e=r.DEFAULT_FLOAT_MAXIMUM,n=r.DEFAULT_INCLUDE_MINIMUM,s=r.DEFAULT_INCLUDE_MAXIMUM){const i=this._next()*(e-t)+t;if(n&&!s)return i;{const r=i+Number.EPSILON*i;return n&&s?r<e?r:e:!n&&s?r>t?r:t+Number.EPSILON:r<=t?t+Number.EPSILON:r>=e?e-Number.EPSILON:r}}nextBigInt(t=r.DEFAULT_BIGINT_MINIMUM,e=r.DEFAULT_BIGINT_MAXIMUM,n=r.DEFAULT_INCLUDE_MINIMUM,s=r.DEFAULT_INCLUDE_MAXIMUM){let i=0n,o=0n;return n&&!s?(i=0n,o=0n):n&&s?o=1n:!n&&s?i=1n:(i=1n,o=-1n),BigInt(Math.floor(this._next()*Number.MAX_SAFE_INTEGER))*(BigInt(e)-BigInt(t)+o)/BigInt(Number.MAX_SAFE_INTEGER)+BigInt(t)+i}nextString(t,e){const n=e.length;let r="";for(let s=0;s<t;s++)r+=e.charAt(Math.floor(this._next()*n));return r}nextWeighted(t,e){let n=0;for(let t=0;t<e.length;t++)n+=e[t];const r=this._next()*n;let s=0;for(let n=0;n<t.length;n++)if(s+=e[n],s>r)return t[n];return t[t.length-1]}pickArray(t){return t[Math.floor(this._next()*t.length)]}shuffleArray(t){let e,n;for(let r=t.length-1;r>0;r--)e=Math.floor(this._next()*(r+1)),n=t[r],t[r]=t[e],t[e]=n}}r.DEFAULT_INCLUDE_MINIMUM=!0,r.DEFAULT_INCLUDE_MAXIMUM=!1,r.DEFAULT_FLOAT_MINIMUM=0,r.DEFAULT_FLOAT_MAXIMUM=1,r.DEFAULT_INTEGER_MINIMUM=0,r.DEFAULT_INTEGER_MAXIMUM=4294967296,r.DEFAULT_BIGINT_MINIMUM=0n,r.DEFAULT_BIGINT_MAXIMUM=18446744073709551616n;var s=r;var i=s,o=class{constructor(t){this._engine=t}get engine(){return this._engine}};n.RandomEngine=i;var a=n.RandomGenerator=o,h=n;class _ extends h.RandomEngine{constructor(){super("basic")}_next(){return Math.random()}}var u=e(_);var I=class{constructor(t,e){let n,r;void 0!==t&&(("number"==typeof t||Array.isArray(t))&&(n=t),null!==t&&"object"==typeof t?r=t:null!==e&&"object"==typeof e&&(r=e)),void 0===n&&(n="function"==typeof r?.autoSeeder?r.autoSeeder():Math.floor(Math.random()*(new Date).getTime())),this._N=r?.stateLength??624,this._M=r?.statePeriod??397,this._MATRIX_A=r?.matrixA??2567483615,this._UPPER_MASK=r?.upperMask??2147483648,this._LOWER_MASK=r?.lowerMask??2147483647,this._stateVector=new Array(this._N),this._stateIndex=this._N+1,this._init(n)}get seed(){return this._seed}set seed(t){this._init(t)}get stateLength(){return this._N}get statePeriod(){return this._M}get matrixA(){return this._MATRIX_A}get upperMask(){return this._UPPER_MASK}get lowerMask(){return this._LOWER_MASK}randomInt32(){let t;const e=[0,this._MATRIX_A];if(this._stateIndex>=this._N){let n;for(n=0;n<this._N-this._M;n++)t=this._stateVector[n]&this._UPPER_MASK|this._stateVector[n+1]&this._LOWER_MASK,this._stateVector[n]=this._stateVector[n+this._M]^t>>>1^e[1&t];for(;n<this._N-1;n++)t=this._stateVector[n]&this._UPPER_MASK|this._stateVector[n+1]&this._LOWER_MASK,this._stateVector[n]=this._stateVector[n+(this._M-this._N)]^t>>>1^e[1&t];t=this._stateVector[this._N-1]&this._UPPER_MASK|this._stateVector[0]&this._LOWER_MASK,this._stateVector[this._N-1]=this._stateVector[this._M-1]^t>>>1^e[1&t],this._stateIndex=0}return t=this._stateVector[this._stateIndex++],t^=t>>>11,t^=t<<7&2636928640,t^=t<<15&4022730752,t^=t>>>18,t>>>0}randomInt31(){return this.randomInt32()>>>1}randomFloat1(){return this.randomInt32()*(1/4294967295)}randomFloat2(){return this.randomInt32()*(1/4294967296)}randomFloat3(){return(this.randomInt32()+.5)*(1/4294967296)}randomFloatRes53(){return(67108864*(this.randomInt32()>>>5)+(this.randomInt32()>>>6))*(1/9007199254740992)}_init(t){this._seed=t,Array.isArray(t)?this._initWithArray(t):this._initWithNumber(t)}_initWithNumber(t){for(this._stateVector[0]=t>>>0,this._stateIndex=1;this._stateIndex<this._N;this._stateIndex++){const t=this._stateVector[this._stateIndex-1]^this._stateVector[this._stateIndex-1]>>>30;this._stateVector[this._stateIndex]=(1812433253*((4294901760&t)>>>16)<<16)+1812433253*(65535&t)+this._stateIndex,this._stateVector[this._stateIndex]>>>=0}}_initWithArray(t){let e=1,n=0,r=this._N>t.length?this._N:t.length;for(this._initWithNumber(19650218);r;r--){const r=this._stateVector[e-1]^this._stateVector[e-1]>>>30;this._stateVector[e]=(this._stateVector[e]^(1664525*((4294901760&r)>>>16)<<16)+1664525*(65535&r))+t[n]+n,this._stateVector[e]>>>=0,e++,n++,e>=this._N&&(this._stateVector[0]=this._stateVector[this._N-1],e=1),n>=t.length&&(n=0)}for(r=this._N-1;r;r--){const t=this._stateVector[e-1]^this._stateVector[e-1]>>>30;this._stateVector[e]=(this._stateVector[e]^(1566083941*((4294901760&t)>>>16)<<16)+1566083941*(65535&t))-e,this._stateVector[e]>>>=0,e++,e>=this._N&&(this._stateVector[0]=this._stateVector[this._N-1],e=1)}this._stateVector[0]=2147483648}}; /*! * @grandom/fnv1a v1.0.3 * https://github.com/grandom-library/fnv1a#readme * * Copyright (c) 2023 Richard King <richrdkng@gmail.com> (www.richrdkng.com) * Released under the MIT License * https://github.com/grandom-library/fnv1a#readme/blob/main/LICENSE * * Date: 2023-08-28T07:39:33.259Z */const g=2166136261,M={32:BigInt(16777619),64:BigInt(1099511628211),128:BigInt(2)**BigInt(88)+BigInt(315),256:BigInt(2)**BigInt(168)+BigInt(355),512:BigInt(2)**BigInt(344)+BigInt(343),1024:BigInt(2)**BigInt(680)+BigInt(397)},c={32:BigInt(g),64:14695981039346656037n,128:144066263297769815596495629667062367629n,256:100029257958052580907070968620625704837092796014241193945225284501741471925557n,512:9659303129496669498009435400716310466090418745672637896108374329434462657994582932197716438449813051892206539805784495328239340083876191928701583869517785n,1024:14197795064947621068722070641403218320880622795441933960878474914617582723252296732303717722150864096521202355549365628174669108571814760471015076148029755969804077320157692458563003215304957150157403644460363550505412711285966361610267868082893823963790439336411086884584107735010676915n};var d=n,l=I,E=function(t,e){if("string"!=typeof t)throw new TypeError(`The 'string' argument must be a string, got: ${t} (typeof === '${typeof t}').`);const n=t.length;if(void 0===e){let e=g;for(let r=0;r<n;r++)e^=t.charCodeAt(r),e+=(e<<1)+(e<<4)+(e<<7)+(e<<8)+(e<<24);return e>>>0}if("number"!=typeof e)throw new TypeError(`The 'hashSize' argument must be a number, got: ${e} (typeof === '${typeof e}').`);if(!(e in M))throw new RangeError(`The 'hashSize' argument must be one of 32, 64, 128, 256, 512, 1024, got: ${e}.`);const r=e,s=M[r];let i=c[r];for(let e=0;e<n;e++)i^=BigInt(t.charCodeAt(e)),i=BigInt.asUintN(r,i*s);return i};class f extends d.RandomEngine{constructor(){super("mt19937"),this._engine=new l,this.seed=Math.floor(Math.random()*(new Date).getTime())}get seed(){return this._seed}set seed(t){if("number"==typeof t)this._engine.seed=t,this._seed=t;else{if("string"!=typeof t)throw new TypeError(`Seed must be a number, or a string, got: ${t} (typeof === '${typeof t}').`);this._engine.seed=E(t),this._seed=t}}_next(){return this._engine.randomFloat2()}}var A=e(f);function m(t){throw new Error('Could not dynamically require "'+t+'". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.')}var N=n;class U extends N.RandomEngine{constructor(){if(super("crypto"),"undefined"!=typeof window&&window.crypto&&window.crypto.getRandomValues)this._next=()=>{const t=new Uint8Array(4);return window.crypto.getRandomValues(t),Math.abs((t[0]<<24|t[1]<<16|t[2]<<8|t[3])/4294967296)};else{if(void 0===m)throw new Error("Secure random number generation is not supported in this environment.");{const e=t;this._next=()=>e.randomBytes(4).readUInt32BE(0)/4294967296}}}}var T=e(U);class p extends a{string(t,e,n){if(void 0!==t){let e=p.DEFAULT_LENGTH;if("number"==typeof t)e=t;else if("object"==typeof t&&null!==t)"number"==typeof t.length&&(e=t.length);else if("number"!=typeof t)throw new TypeError(`length must be a number, got: ${t} (typeof === "${typeof t}").`);if(e!=e)throw new RangeError(`length must be a non-NaN number, got: ${e}.`);if(e<0||e>Number.MAX_SAFE_INTEGER)throw new RangeError(`length must be >= 0 <= 2^53-1 (9,007,199,254,740,991), got ${e}.`);return 0===e?"":this._engine.nextString(Math.floor(e),p.ALPHANUMERIC)}return this._engine.nextString(p.DEFAULT_LENGTH,p.ALPHANUMERIC)}}p.DEFAULT_LENGTH=16,p.NUMBERS="0123456789",p.LETTERS="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",p.ALPHANUMERIC=p.LETTERS+p.NUMBERS;const y=new p(new u),L=y.string.bind(y);return Object.defineProperties(L,{BasicEngine:{value:u},SeededEngine:{value:A},CryptoEngine:{value:T},RandomString:{value:p}}),L})); //# sourceMappingURL=min.js.map