@grandom/string-umd
Version:
Various UMD builds of a great random string generator.
13 lines (12 loc) • 4.2 kB
JavaScript
/*!
* @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.714Z
*/
!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).grandomStringCrypto=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,M=0;return n&&!r?(i=0,M=0):n&&r?M=1:!n&&r?i=1:(i=1,M=-1),Math.floor(this._next()*(t-e+M))+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,M=0n;return n&&!r?(i=0n,M=0n):n&&r?M=1n:!n&&r?i=1n:(i=1n,M=-1n),BigInt(Math.floor(this._next()*Number.MAX_SAFE_INTEGER))*(BigInt(t)-BigInt(e)+M)/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 M=i,E=class{constructor(e){this._engine=e}get engine(){return this._engine}};r.RandomEngine=M;var s=r.RandomGenerator=E,u=r;class I extends u.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 _=t(I);class l extends s{string(e,t,n){if(void 0!==e){let t=l.DEFAULT_LENGTH;if("number"==typeof e)t=e;else if("object"==typeof e&&null!==e)"number"==typeof e.length&&(t=e.length);else if("number"!=typeof e)throw new TypeError(`length must be a number, got: ${e} (typeof === "${typeof e}").`);if(t!=t)throw new RangeError(`length must be a non-NaN number, got: ${t}.`);if(t<0||t>Number.MAX_SAFE_INTEGER)throw new RangeError(`length must be >= 0 <= 2^53-1 (9,007,199,254,740,991), got ${t}.`);return 0===t?"":this._engine.nextString(Math.floor(t),l.ALPHANUMERIC)}return this._engine.nextString(l.DEFAULT_LENGTH,l.ALPHANUMERIC)}}l.DEFAULT_LENGTH=16,l.NUMBERS="0123456789",l.LETTERS="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",l.ALPHANUMERIC=l.LETTERS+l.NUMBERS;const a=new l(new _),U=a.string.bind(a);return Object.defineProperties(U,{CryptoEngine:{value:_},RandomString:{value:l}}),U}));
//# sourceMappingURL=crypto.min.js.map