@grandom/pick-umd
Version:
Various UMD builds of a great random picker.
13 lines (12 loc) • 4.53 kB
JavaScript
/*!
* @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.799Z
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).grandomPickBasic=t()}(this,(function(){"use strict";function e(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var t={};class n{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=n.DEFAULT_INTEGER_MINIMUM,t=n.DEFAULT_INTEGER_MAXIMUM,r=n.DEFAULT_INCLUDE_MINIMUM,o=n.DEFAULT_INCLUDE_MAXIMUM){let i=0,_=0;return r&&!o?(i=0,_=0):r&&o?_=1:!r&&o?i=1:(i=1,_=-1),Math.floor(this._next()*(t-e+_))+e+i}nextFloat(e=n.DEFAULT_FLOAT_MINIMUM,t=n.DEFAULT_FLOAT_MAXIMUM,r=n.DEFAULT_INCLUDE_MINIMUM,o=n.DEFAULT_INCLUDE_MAXIMUM){const i=this._next()*(t-e)+e;if(r&&!o)return i;{const n=i+Number.EPSILON*i;return r&&o?n<t?n:t:!r&&o?n>e?n:e+Number.EPSILON:n<=e?e+Number.EPSILON:n>=t?t-Number.EPSILON:n}}nextBigInt(e=n.DEFAULT_BIGINT_MINIMUM,t=n.DEFAULT_BIGINT_MAXIMUM,r=n.DEFAULT_INCLUDE_MINIMUM,o=n.DEFAULT_INCLUDE_MAXIMUM){let i=0n,_=0n;return r&&!o?(i=0n,_=0n):r&&o?_=1n:!r&&o?i=1n:(i=1n,_=-1n),BigInt(Math.floor(this._next()*Number.MAX_SAFE_INTEGER))*(BigInt(t)-BigInt(e)+_)/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}}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 o=r,i=class{constructor(e){this._engine=e}get engine(){return this._engine}};t.RandomEngine=o;var _=t.RandomGenerator=i,u=t;class a extends u.RandomEngine{constructor(){super("basic")}_next(){return Math.random()}}var s=e(a);const M=void 0;class E extends _{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:M;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:M;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:M;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 l=new E(new s),c=l.pick.bind(l);return Object.defineProperties(c,{BasicEngine:{value:s},RandomPick:{value:E}}),c}));
//# sourceMappingURL=basic.min.js.map