UNPKG

@xlou/webtools

Version:

Frontend Development Tools

3 lines (2 loc) 5.14 kB
class e{constructor(e){e&&(this.key=e)}key="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";encode(e){let t,r,o,n,i,a,l,f="",c=0;for(e=this.utf8_encode(e);c<e.length;)t=e.charCodeAt(c++),r=e.charCodeAt(c++),o=e.charCodeAt(c++),n=t>>2,i=(3&t)<<4|r>>4,a=(15&r)<<2|o>>6,l=63&o,isNaN(r)?a=l=64:isNaN(o)&&(l=64),f=f+this.key.charAt(n)+this.key.charAt(i)+this.key.charAt(a)+this.key.charAt(l);return f}decode(e){let t,r,o,n,i,a,l,f="",c=0;for(e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");c<e.length;)n=this.key.indexOf(e.charAt(c++)),i=this.key.indexOf(e.charAt(c++)),a=this.key.indexOf(e.charAt(c++)),l=this.key.indexOf(e.charAt(c++)),t=n<<2|i>>4,r=(15&i)<<4|a>>2,o=(3&a)<<6|l,f+=String.fromCharCode(t),64!=a&&(f+=String.fromCharCode(r)),64!=l&&(f+=String.fromCharCode(o));return f=this.utf8_decode(f).replace(/\u0000/g,""),f}utf8_encode(e){e=e.replace(/\r\n/g,"\n");let t="";for(let r=0;r<e.length;r++){let o=e.charCodeAt(r);o<128?t+=String.fromCharCode(o):o>127&&o<2048?(t+=String.fromCharCode(o>>6|192),t+=String.fromCharCode(63&o|128)):(t+=String.fromCharCode(o>>12|224),t+=String.fromCharCode(o>>6&63|128),t+=String.fromCharCode(63&o|128))}return t}utf8_decode(e){let t="",r=0,o=0,n=0,i=0;for(;r<e.length;)o=e.charCodeAt(r),o<128?(t+=String.fromCharCode(o),r++):o>191&&o<224?(n=e.charCodeAt(r+1),t+=String.fromCharCode((31&o)<<6|63&n),r+=2):(n=e.charCodeAt(r+1),i=e.charCodeAt(r+2),t+=String.fromCharCode((15&o)<<12|(63&n)<<6|63&i),r+=3);return t}}function t(e,r=new Set){if("object"!=typeof e||null===e)return e;if(r.has(e))return e;let o;switch(r.add(e),e.constructor){case Array:o=[];for(let n of e)o.push(t(n,r));break;case Set:o=new Set,e.forEach((e=>{o.add(t(e,r))}));break;case Map:o=new Map,e.forEach(((e,n)=>{o.set(t(n,r),t(e,r))}));break;default:o={};for(let n in e)o[n]=t(e[n],r)}return o}function r(e,t,r){let o={};if(!t)return Object.assign(o,e);let n=t.split(",");if(void 0===r&&(r=!0),r)for(let t of n)t=t.trim(),e[t]&&(o[t]=e[t]);else{Object.assign(o,e);for(let e of n)delete o[e]}return o}function o(e=window.location.href){let t={},r=e.indexOf("?");if(-1===r)return t;let o=e.indexOf("#");(-1===o||r>o)&&(o=void 0);let n=e.slice(r+1,o);if(""===n)return t;let i=n.split("&");for(let e of i){if(!e)continue;let r=e.split("=");t[decodeURIComponent(r[0])]=decodeURIComponent(r[1]||"")}return t}function n(e,t=!1){let r=[];for(let t in e)null!==e[t]&&void 0!==e[t]||(e[t]=""),r.push(encodeURIComponent(t)+"="+encodeURIComponent(e[t]));let o=r.join("&");return o&&t?"?"+o:o}function i(e,t){if(void 0===e)return;let r=Number(e);if(isNaN(r))throw"argument for toFixed error";if(r>Math.pow(10,21))return String(r);let o=Number(t);if(void 0===t||0==o)return String(Math.round(r));if(isNaN(o))throw"The argument of C.toFixed must be a number";if(o>20||o<0)throw"The second argument of C.toFixed must be between 0 and 20";let n=String(r),i=n.split(".");if(i.length<2){n+=".";for(let e=0;e<o;e++)n+="0";return n}let a=i[0],l=i[1];if(l.length==o)return n;if(l.length<o){for(let e=0;e<o-l.length;e++)n+="0";return n}n=a+"."+l.slice(0,o);let f=l.slice(o,o+1);if(parseInt(f,10)>=5){let e=10**o;n=(parseFloat(n)*e+1)/e,n=n.toFixed(o)}return n}function a(e){const{document:t}=window,r=t.createElement("form"),{data:o}=e;delete e.data;for(let t in e)e[t]&&(r[t]=e[t]);r.style.display="none";for(let e in o){const n=t.createElement("input");n.setAttribute("type","hidden"),n.setAttribute("name",e),n.value=o[e],r.appendChild(n)}t.body.appendChild(r),r.submit()}function l(e){return new Promise(((t,r)=>{const o=new XMLHttpRequest;o.onload=e=>{t(o.response)},o.onerror=e=>{r(e)},o.open("GET",e,!0),o.send()}))}function f(e){return new Promise(((t,r)=>{const o=new XMLHttpRequest;o.onload=e=>{t(JSON.parse(o.response))},o.onerror=e=>{r(e)},o.open("GET",e,!0),o.send()}))}function c(e){let t=localStorage.getItem(e);if("string"==typeof t)try{t=JSON.parse(t)}catch(e){}return t}function d(e,t){if("object"==typeof t&&null!==t)try{t=JSON.stringify(t)}catch(e){}localStorage.setItem(e,t)}function s(){return Math.floor(1e14*Math.random()).toString(36)+Date.now().toString(36)}function u(e){let t=e.toLowerCase(),r=[];if(/^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/.test(t)){if(4===t.length){let e="#";for(let r=1;r<4;r+=1)e+=t.slice(r,r+1).concat(t.slice(r,r+1));t=e}for(let e=1;e<7;e+=2)r.push(parseInt("0x"+t.slice(e,e+2)));return r}if(/^(rgb\(|RGB\()[\s\S]+\)/.test(t))return r=t.replace(/( |\(|\)|rgb|RGB)+/g,"").split(","),r.map(Number)}function h(e,t="text/plain"){let r=null;return r="[object Blob]"===Object.prototype.toString.call(e)?new ClipboardItem({[t]:e}):Array.isArray(e)?new ClipboardItem({[t]:new Blob(e,{type:t})}):new ClipboardItem({[t]:new Blob([e],{type:t})}),navigator.clipboard.write([r])}var p={Base64:e,deepCopy:t,filterObject:r,getQuery:o,queryString:n,toFixed:i,formSubmit:a,readText:l,readJSON:f,getStore:c,setStore:d,unid:s,colorRGB:u,clipboardWrite:h};export{e as Base64,h as clipboardWrite,u as colorRGB,t as deepCopy,p as default,r as filterObject,a as formSubmit,o as getQuery,c as getStore,n as queryString,f as readJSON,l as readText,d as setStore,i as toFixed,s as unid}; //# sourceMappingURL=webtools.min.js.map