UNPKG

@grandom/pick-umd

Version:
13 lines (12 loc) 5.16 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.806Z */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("crypto")):"function"==typeof define&&define.amd?define(["crypto"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).grandomPickCrypto=t(e.require$$1)}(this,(function(e){"use strict";function t(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function n(e){throw new Error('Could not dynamically require "'+e+'". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.')}var r={};class o{constructor(e){this._name=e}_next(){throw new Error("_next() must be implemented.")}get name(){return this._name}nextBoolean(){return this._next()<.5}nextInteger(e=o.DEFAULT_INTEGER_MINIMUM,t=o.DEFAULT_INTEGER_MAXIMUM,n=o.DEFAULT_INCLUDE_MINIMUM,r=o.DEFAULT_INCLUDE_MAXIMUM){let i=0,u=0;return n&&!r?(i=0,u=0):n&&r?u=1:!n&&r?i=1:(i=1,u=-1),Math.floor(this._next()*(t-e+u))+e+i}nextFloat(e=o.DEFAULT_FLOAT_MINIMUM,t=o.DEFAULT_FLOAT_MAXIMUM,n=o.DEFAULT_INCLUDE_MINIMUM,r=o.DEFAULT_INCLUDE_MAXIMUM){const i=this._next()*(t-e)+e;if(n&&!r)return i;{const o=i+Number.EPSILON*i;return n&&r?o<t?o:t:!n&&r?o>e?o:e+Number.EPSILON:o<=e?e+Number.EPSILON:o>=t?t-Number.EPSILON:o}}nextBigInt(e=o.DEFAULT_BIGINT_MINIMUM,t=o.DEFAULT_BIGINT_MAXIMUM,n=o.DEFAULT_INCLUDE_MINIMUM,r=o.DEFAULT_INCLUDE_MAXIMUM){let i=0n,u=0n;return n&&!r?(i=0n,u=0n):n&&r?u=1n:!n&&r?i=1n:(i=1n,u=-1n),BigInt(Math.floor(this._next()*Number.MAX_SAFE_INTEGER))*(BigInt(t)-BigInt(e)+u)/BigInt(Number.MAX_SAFE_INTEGER)+BigInt(e)+i}nextString(e,t){const n=t.length;let r="";for(let o=0;o<e;o++)r+=t.charAt(Math.floor(this._next()*n));return r}nextWeighted(e,t){let n=0;for(let e=0;e<t.length;e++)n+=t[e];const r=this._next()*n;let o=0;for(let n=0;n<e.length;n++)if(o+=t[n],o>r)return e[n];return e[e.length-1]}pickArray(e){return e[Math.floor(this._next()*e.length)]}shuffleArray(e){let t,n;for(let r=e.length-1;r>0;r--)t=Math.floor(this._next()*(r+1)),n=e[r],e[r]=e[t],e[t]=n}}o.DEFAULT_INCLUDE_MINIMUM=!0,o.DEFAULT_INCLUDE_MAXIMUM=!1,o.DEFAULT_FLOAT_MINIMUM=0,o.DEFAULT_FLOAT_MAXIMUM=1,o.DEFAULT_INTEGER_MINIMUM=0,o.DEFAULT_INTEGER_MAXIMUM=4294967296,o.DEFAULT_BIGINT_MINIMUM=0n,o.DEFAULT_BIGINT_MAXIMUM=18446744073709551616n;var i=o;var u=i,a=class{constructor(e){this._engine=e}get engine(){return this._engine}};r.RandomEngine=u;var s=r.RandomGenerator=a,l=r;class _ extends l.RandomEngine{constructor(){if(super("crypto"),"undefined"!=typeof window&&window.crypto&&window.crypto.getRandomValues)this._next=()=>{const e=new Uint8Array(4);return window.crypto.getRandomValues(e),Math.abs((e[0]<<24|e[1]<<16|e[2]<<8|e[3])/4294967296)};else{if(void 0===n)throw new Error("Secure random number generation is not supported in this environment.");{const t=e;this._next=()=>t.randomBytes(4).readUInt32BE(0)/4294967296}}}}var c=t(_);const E=void 0;class f extends s{pick(e,t,n){if(void 0!==e){let r,o={};if(void 0!==t){if("number"==typeof t)r=t;else{if("object"!=typeof t||null===t)throw new TypeError(`2nd argument must be a number (count), or an object (options), got: ${t} (typeof === '${typeof t}').`);o=t}if(void 0!==n){if("object"!=typeof n||null===n)throw new TypeError(`3rd argument must be an object (options), got: ${n} (typeof === '${typeof n}').`);o=n}}if("string"==typeof e)return this._pickString(e,r,o);if(Array.isArray(e))return this._pickArray(e,r,o);if("object"==typeof e&&null!==e)return this._pickObject(e,r,o)}throw new TypeError(`Must be called with a string, array, or object, got: ${e} (typeof === '${typeof e}').`)}_pickString(e,t,n){if(void 0!==t&&(t!=t||t<1||t>Number.MAX_SAFE_INTEGER))throw new RangeError(`Count must be 1 >= count <= Number.MAX_SAFE_INTEGER, got: ${t}.`);if(0===e.length)return"fallback"in n?n.fallback:E;if(t>1){let n="";for(let r=0;r<t;r++)n+=this._engine.pickArray(e);return n}return this._engine.pickArray(e)}_pickArray(e,t,n){if(void 0!==t&&(t!=t||t<1||t>Number.MAX_SAFE_INTEGER))throw new RangeError(`Count must be 1 >= count <= Number.MAX_SAFE_INTEGER, got: ${t}.`);if(0===e.length)return"fallback"in n?n.fallback:E;if(t>1){const n=[];for(let r=0;r<t;r++)n.push(this._engine.pickArray(e));return n}return this._engine.pickArray(e)}_pickObject(e,t,n){if(void 0!==t&&(t!=t||t<1||t>Number.MAX_SAFE_INTEGER))throw new RangeError(`Count must be 1 >= count <= Number.MAX_SAFE_INTEGER, got: ${t}.`);const r=Object.keys(e);if(0===r.length)return"fallback"in n?n.fallback:E;if(t>1){const n=[];let o;for(let i=0;i<t;i++)o=this._engine.pickArray(r),n.push([o,e[o]]);return n}const o=this._engine.pickArray(r);return[o,e[o]]}}const M=new f(new c),I=M.pick.bind(M);return Object.defineProperties(I,{CryptoEngine:{value:c},RandomPick:{value:f}}),I})); //# sourceMappingURL=crypto.min.js.map