UNPKG

@grandom/pick-umd

Version:
13 lines (12 loc) 4.14 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.810Z */ !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).grandomPickCore=e()}(this,(function(){"use strict";class t{constructor(t){this._name=t}_next(){throw new Error("_next() must be implemented.")}get name(){return this._name}nextBoolean(){return this._next()<.5}nextInteger(e=t.DEFAULT_INTEGER_MINIMUM,n=t.DEFAULT_INTEGER_MAXIMUM,r=t.DEFAULT_INCLUDE_MINIMUM,o=t.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()*(n-e+_))+e+i}nextFloat(e=t.DEFAULT_FLOAT_MINIMUM,n=t.DEFAULT_FLOAT_MAXIMUM,r=t.DEFAULT_INCLUDE_MINIMUM,o=t.DEFAULT_INCLUDE_MAXIMUM){const i=this._next()*(n-e)+e;if(r&&!o)return i;{const t=i+Number.EPSILON*i;return r&&o?t<n?t:n:!r&&o?t>e?t:e+Number.EPSILON:t<=e?e+Number.EPSILON:t>=n?n-Number.EPSILON:t}}nextBigInt(e=t.DEFAULT_BIGINT_MINIMUM,n=t.DEFAULT_BIGINT_MAXIMUM,r=t.DEFAULT_INCLUDE_MINIMUM,o=t.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(n)-BigInt(e)+_)/BigInt(Number.MAX_SAFE_INTEGER)+BigInt(e)+i}nextString(t,e){const n=e.length;let r="";for(let o=0;o<t;o++)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 o=0;for(let n=0;n<t.length;n++)if(o+=e[n],o>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}}t.DEFAULT_INCLUDE_MINIMUM=!0,t.DEFAULT_INCLUDE_MAXIMUM=!1,t.DEFAULT_FLOAT_MINIMUM=0,t.DEFAULT_FLOAT_MAXIMUM=1,t.DEFAULT_INTEGER_MINIMUM=0,t.DEFAULT_INTEGER_MAXIMUM=4294967296,t.DEFAULT_BIGINT_MINIMUM=0n,t.DEFAULT_BIGINT_MAXIMUM=18446744073709551616n;var e=class{constructor(t){this._engine=t}get engine(){return this._engine}};const n=void 0;return class extends e{pick(t,e,n){if(void 0!==t){let r,o={};if(void 0!==e){if("number"==typeof e)r=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}').`);o=e}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 t)return this._pickString(t,r,o);if(Array.isArray(t))return this._pickArray(t,r,o);if("object"==typeof t&&null!==t)return this._pickObject(t,r,o)}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:n;if(e>1){let n="";for(let r=0;r<e;r++)n+=this._engine.pickArray(t);return n}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:n;if(e>1){const n=[];for(let r=0;r<e;r++)n.push(this._engine.pickArray(t));return n}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 o=Object.keys(t);if(0===o.length)return"fallback"in r?r.fallback:n;if(e>1){const n=[];let r;for(let i=0;i<e;i++)r=this._engine.pickArray(o),n.push([r,t[r]]);return n}const i=this._engine.pickArray(o);return[i,t[i]]}}})); //# sourceMappingURL=core.min.js.map