UNPKG

@grandom/pick-umd

Version:
23 lines (22 loc) 10.5 kB
/*! * @grandom/pick v1.4.0 * https://github.com/grandom-library/grandom-js/tree/main/packages/pick#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/pick#readme/blob/main/LICENSE * * Date: 2023-09-06T22:05:56.793Z */ !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).grandomPick=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 r={};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,i=n.DEFAULT_INCLUDE_MAXIMUM){let s=0,o=0;return r&&!i?(s=0,o=0):r&&i?o=1:!r&&i?s=1:(s=1,o=-1),Math.floor(this._next()*(e-t+o))+t+s}nextFloat(t=n.DEFAULT_FLOAT_MINIMUM,e=n.DEFAULT_FLOAT_MAXIMUM,r=n.DEFAULT_INCLUDE_MINIMUM,i=n.DEFAULT_INCLUDE_MAXIMUM){const s=this._next()*(e-t)+t;if(r&&!i)return s;{const n=s+Number.EPSILON*s;return r&&i?n<e?n:e:!r&&i?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,i=n.DEFAULT_INCLUDE_MAXIMUM){let s=0n,o=0n;return r&&!i?(s=0n,o=0n):r&&i?o=1n:!r&&i?s=1n:(s=1n,o=-1n),BigInt(Math.floor(this._next()*Number.MAX_SAFE_INTEGER))*(BigInt(e)-BigInt(t)+o)/BigInt(Number.MAX_SAFE_INTEGER)+BigInt(t)+s}nextString(t,e){const r=e.length;let n="";for(let i=0;i<t;i++)n+=e.charAt(Math.floor(this._next()*r));return n}nextWeighted(t,e){let r=0;for(let t=0;t<e.length;t++)r+=e[t];const n=this._next()*r;let i=0;for(let r=0;r<t.length;r++)if(i+=e[r],i>n)return t[r];return t[t.length-1]}pickArray(t){return t[Math.floor(this._next()*t.length)]}shuffleArray(t){let e,r;for(let n=t.length-1;n>0;n--)e=Math.floor(this._next()*(n+1)),r=t[n],t[n]=t[e],t[e]=r}}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 i=n;var s=i,o=class{constructor(t){this._engine=t}get engine(){return this._engine}};r.RandomEngine=s;var a=r.RandomGenerator=o,_=r;class h extends _.RandomEngine{constructor(){super("basic")}_next(){return Math.random()}}var u=e(h);var c=class{constructor(t,e){let r,n;void 0!==t&&(("number"==typeof t||Array.isArray(t))&&(r=t),null!==t&&"object"==typeof t?n=t:null!==e&&"object"==typeof e&&(n=e)),void 0===r&&(r="function"==typeof n?.autoSeeder?n.autoSeeder():Math.floor(Math.random()*(new Date).getTime())),this._N=n?.stateLength??624,this._M=n?.statePeriod??397,this._MATRIX_A=n?.matrixA??2567483615,this._UPPER_MASK=n?.upperMask??2147483648,this._LOWER_MASK=n?.lowerMask??2147483647,this._stateVector=new Array(this._N),this._stateIndex=this._N+1,this._init(r)}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 r;for(r=0;r<this._N-this._M;r++)t=this._stateVector[r]&this._UPPER_MASK|this._stateVector[r+1]&this._LOWER_MASK,this._stateVector[r]=this._stateVector[r+this._M]^t>>>1^e[1&t];for(;r<this._N-1;r++)t=this._stateVector[r]&this._UPPER_MASK|this._stateVector[r+1]&this._LOWER_MASK,this._stateVector[r]=this._stateVector[r+(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,r=0,n=this._N>t.length?this._N:t.length;for(this._initWithNumber(19650218);n;n--){const n=this._stateVector[e-1]^this._stateVector[e-1]>>>30;this._stateVector[e]=(this._stateVector[e]^(1664525*((4294901760&n)>>>16)<<16)+1664525*(65535&n))+t[r]+r,this._stateVector[e]>>>=0,e++,r++,e>=this._N&&(this._stateVector[0]=this._stateVector[this._N-1],e=1),r>=t.length&&(r=0)}for(n=this._N-1;n;n--){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 l=2166136261,g={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)},I={32:BigInt(l),64:14695981039346656037n,128:144066263297769815596495629667062367629n,256:100029257958052580907070968620625704837092796014241193945225284501741471925557n,512:9659303129496669498009435400716310466090418745672637896108374329434462657994582932197716438449813051892206539805784495328239340083876191928701583869517785n,1024:14197795064947621068722070641403218320880622795441933960878474914617582723252296732303717722150864096521202355549365628174669108571814760471015076148029755969804077320157692458563003215304957150157403644460363550505412711285966361610267868082893823963790439336411086884584107735010676915n};var f=r,d=c,M=function(t,e){if("string"!=typeof t)throw new TypeError(`The 'string' argument must be a string, got: ${t} (typeof === '${typeof t}').`);const r=t.length;if(void 0===e){let e=l;for(let n=0;n<r;n++)e^=t.charCodeAt(n),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 g))throw new RangeError(`The 'hashSize' argument must be one of 32, 64, 128, 256, 512, 1024, got: ${e}.`);const n=e,i=g[n];let s=I[n];for(let e=0;e<r;e++)s^=BigInt(t.charCodeAt(e)),s=BigInt.asUintN(n,s*i);return s};class E extends f.RandomEngine{constructor(){super("mt19937"),this._engine=new d,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=M(t),this._seed=t}}_next(){return this._engine.randomFloat2()}}var A=e(E);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 p=r;class y extends p.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 N=e(y);const b=void 0;class T extends a{pick(t,e,r){if(void 0!==t){let n,i={};if(void 0!==e){if("number"==typeof e)n=e;else{if("object"!=typeof e||null===e)throw new TypeError(`2nd argument must be a number (count), or an object (options), got: ${e} (typeof === '${typeof e}').`);i=e}if(void 0!==r){if("object"!=typeof r||null===r)throw new TypeError(`3rd argument must be an object (options), got: ${r} (typeof === '${typeof r}').`);i=r}}if("string"==typeof t)return this._pickString(t,n,i);if(Array.isArray(t))return this._pickArray(t,n,i);if("object"==typeof t&&null!==t)return this._pickObject(t,n,i)}throw new TypeError(`Must be called with a string, array, or object, got: ${t} (typeof === '${typeof t}').`)}_pickString(t,e,r){if(void 0!==e&&(e!=e||e<1||e>Number.MAX_SAFE_INTEGER))throw new RangeError(`Count must be 1 >= count <= Number.MAX_SAFE_INTEGER, got: ${e}.`);if(0===t.length)return"fallback"in r?r.fallback:b;if(e>1){let r="";for(let n=0;n<e;n++)r+=this._engine.pickArray(t);return r}return this._engine.pickArray(t)}_pickArray(t,e,r){if(void 0!==e&&(e!=e||e<1||e>Number.MAX_SAFE_INTEGER))throw new RangeError(`Count must be 1 >= count <= Number.MAX_SAFE_INTEGER, got: ${e}.`);if(0===t.length)return"fallback"in r?r.fallback:b;if(e>1){const r=[];for(let n=0;n<e;n++)r.push(this._engine.pickArray(t));return r}return this._engine.pickArray(t)}_pickObject(t,e,r){if(void 0!==e&&(e!=e||e<1||e>Number.MAX_SAFE_INTEGER))throw new RangeError(`Count must be 1 >= count <= Number.MAX_SAFE_INTEGER, got: ${e}.`);const n=Object.keys(t);if(0===n.length)return"fallback"in r?r.fallback:b;if(e>1){const r=[];let i;for(let s=0;s<e;s++)i=this._engine.pickArray(n),r.push([i,t[i]]);return r}const i=this._engine.pickArray(n);return[i,t[i]]}}const U=new T(new u),w=U.pick.bind(U);return Object.defineProperties(w,{BasicEngine:{value:u},SeededEngine:{value:A},CryptoEngine:{value:N},RandomPick:{value:T}}),w})); //# sourceMappingURL=min.js.map