UNPKG

@grandom/string-umd

Version:
33 lines (32 loc) 9.05 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.710Z */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).grandomStringSeeded=e()}(this,(function(){"use strict";function t(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var e={};class n{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=n.DEFAULT_INTEGER_MINIMUM,e=n.DEFAULT_INTEGER_MAXIMUM,r=n.DEFAULT_INCLUDE_MINIMUM,s=n.DEFAULT_INCLUDE_MAXIMUM){let i=0,o=0;return r&&!s?(i=0,o=0):r&&s?o=1:!r&&s?i=1:(i=1,o=-1),Math.floor(this._next()*(e-t+o))+t+i}nextFloat(t=n.DEFAULT_FLOAT_MINIMUM,e=n.DEFAULT_FLOAT_MAXIMUM,r=n.DEFAULT_INCLUDE_MINIMUM,s=n.DEFAULT_INCLUDE_MAXIMUM){const i=this._next()*(e-t)+t;if(r&&!s)return i;{const n=i+Number.EPSILON*i;return r&&s?n<e?n:e:!r&&s?n>t?n:t+Number.EPSILON:n<=t?t+Number.EPSILON:n>=e?e-Number.EPSILON:n}}nextBigInt(t=n.DEFAULT_BIGINT_MINIMUM,e=n.DEFAULT_BIGINT_MAXIMUM,r=n.DEFAULT_INCLUDE_MINIMUM,s=n.DEFAULT_INCLUDE_MAXIMUM){let i=0n,o=0n;return r&&!s?(i=0n,o=0n):r&&s?o=1n:!r&&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}}n.DEFAULT_INCLUDE_MINIMUM=!0,n.DEFAULT_INCLUDE_MAXIMUM=!1,n.DEFAULT_FLOAT_MINIMUM=0,n.DEFAULT_FLOAT_MAXIMUM=1,n.DEFAULT_INTEGER_MINIMUM=0,n.DEFAULT_INTEGER_MAXIMUM=4294967296,n.DEFAULT_BIGINT_MINIMUM=0n,n.DEFAULT_BIGINT_MAXIMUM=18446744073709551616n;var r=n;var s=class{constructor(t){this._engine=t}get engine(){return this._engine}},i=r,o=s;e.RandomEngine=i;var _=e.RandomGenerator=o; /*! * @grandom/mt19937 v1.0.2 * https://github.com/grandom-library/mt19937#readme * * Copyright (c) 2023 Richard King <richrdkng@gmail.com> (www.richrdkng.com) * Released under the BSD-3-Clause License * https://github.com/grandom-library/mt19937#readme/blob/main/LICENSE * * Date: 2023-08-29T07:37:31.671Z */var h=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 a=2166136261,I={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)},M={32:BigInt(a),64:14695981039346656037n,128:144066263297769815596495629667062367629n,256:100029257958052580907070968620625704837092796014241193945225284501741471925557n,512:9659303129496669498009435400716310466090418745672637896108374329434462657994582932197716438449813051892206539805784495328239340083876191928701583869517785n,1024:14197795064947621068722070641403218320880622795441933960878474914617582723252296732303717722150864096521202355549365628174669108571814760471015076148029755969804077320157692458563003215304957150157403644460363550505412711285966361610267868082893823963790439336411086884584107735010676915n};var g=e,u=h,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=a;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 I))throw new RangeError(`The 'hashSize' argument must be one of 32, 64, 128, 256, 512, 1024, got: ${e}.`);const r=e,s=I[r];let i=M[r];for(let e=0;e<n;e++)i^=BigInt(t.charCodeAt(e)),i=BigInt.asUintN(r,i*s);return i};class d extends g.RandomEngine{constructor(){super("mt19937"),this._engine=new u,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 l=t(d);class A extends _{string(t,e,n){if(void 0!==t){let e=A.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),A.ALPHANUMERIC)}return this._engine.nextString(A.DEFAULT_LENGTH,A.ALPHANUMERIC)}}A.DEFAULT_LENGTH=16,A.NUMBERS="0123456789",A.LETTERS="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",A.ALPHANUMERIC=A.LETTERS+A.NUMBERS;const c=new l,f=new A(c),N=f.string.bind(f);return Object.defineProperties(N,{SeededEngine:{value:l},RandomString:{value:A},seed:{get:()=>c.seed,set:t=>{c.seed=t}}}),N})); //# sourceMappingURL=seeded.min.js.map