-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1 lines (1 loc) · 3.56 KB
/
index.js
1
function r(r,e,t){return e=Math.ceil(e),t=Math.floor(t),e+Math.floor(r.call(null)*(t-e+1))}function e(r){return r>="0"&&r<="9"}function t(r){return r>="A"&&r<="Z"}function a(r){return r>="a"&&r<="z"}function n(r){return t(r)||a(r)}exports.ALPHABET="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",exports.ALPHA_NUMERIC="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",exports.LAST_ASCII=127,exports.LAST_ISO=255,exports.LOWER_ALPHABET="abcdefghijklmnopqrstuvwxyz",exports.NUMBERS="0123456789",exports.UPPER_ALPHABET="ABCDEFGHIJKLMNOPQRSTUVWXYZ",exports.charIsAlpha=n,exports.charIsAlphanumeric=function(r){return n(r)||e(r)},exports.charIsLowerAlpha=a,exports.charIsNumeric=e,exports.charIsUpperAlpha=t,exports.randstr=function(e){var t,a,n=function(e){var t=e||{random:Math.random,chars:[32,255],acceptable:void 0,replacer:void 0,includeControlChars:!1};if(t.random||(t.random=Math.random),"function"!=typeof t.random)throw new Error("Parameter 'random' must a function.");var a=null==t.length?r(t.random,0,100):t.length;if(Array.isArray(a)){var n=a.length;if(n<1)throw new Error("Parameter 'length' must have at least one number.");if(n>2)throw new Error("Parameter 'length' must have at most two numbers.");if(isNaN(a[0]))throw new Error("The first value of the parameter 'length' must be a number.");if(n>1){if(isNaN(a[1]))throw new Error("The second value of the parameter 'length' must be a number.")}else a.push(100);if(t.length=[Number(a[0]),Number(a[1])],t.length[0]<0)throw new Error("The first value of the parameter 'length' must be greater than equal to zero.");if(t.length[1]<0)throw new Error("The second value of the parameter 'length' must be greater than or equal to zero.");if(t.length[0]>t.length[1])throw new Error("The second value of the parameter 'length' must be less than the first value.")}else{if(isNaN(a))throw new Error("Parameter 'length' must be a number.");t.length=Number(a)}var o=null==t.chars?[32,255]:t.chars;if(Array.isArray(o)){if(2!==o.length)throw new Error("Parameter 'chars' must have two numbers.");if(isNaN(o[0]))throw new Error("The first value of the parameter 'chars' must be a number: ");if(isNaN(o[1]))throw new Error("The second value of the parameter 'chars' must be a number.");if(t.chars=[Number(o[0]),Number(o[1])],t.chars[0]<0)throw new Error("The first value of the parameter 'chars' must be greater than or equal zero.");if(t.chars[1]<0)throw new Error("The second value of the parameter 'chars' must be greater than or equal zero.");if(t.chars[0]>t.chars[1])throw new Error("The second value of the parameter 'chars' must be less than the first value.")}else{if("string"!=typeof o)throw new Error("Parameter 'chars' must be a string or an array.");if(o.length<1)throw new Error("Parameter 'chars' must be a string with at least 1 character.");t.chars=o}if(t.acceptable&&"function"!=typeof t.acceptable)throw new Error("Parameter 'acceptable' must a function.");if(null!=t.replacer&&"function"!=typeof t.replacer)throw new Error("Parameter 'replacer' must a function.");return t}(e);if(Array.isArray(n.length)){var o=n.length;t=o[0],a=o[1]}else t=a=n.length||0;var h="";if(0===a)return h;for(var s,l,u,c="string"==typeof n.chars,f=c?n.chars.length-1:0,i=!0===n.includeControlChars,m="function"==typeof n.acceptable,p="function"==typeof n.replacer,w=r(n.random,t,a),g=0,b=0;g<w&&b<w;++g){if(c){var v=r(n.random,0,f);s=n.chars.charCodeAt(v)}else{var E=n.chars;s=r(n.random,E[0],E[1])}!i&&((u=s)<0||u>=0&&u<=31||u>=127&&u<=159)?--g:(l=String.fromCharCode(s),!m||n.acceptable.call(null,l)?p&&b+(l=n.replacer.call(null,l)).length>w?--g:b=(h+=l).length:--g)}return h};