simple2k13
Version:
some simple functions
3 lines • 9.85 kB
JavaScript
/*! For license information please see simple.min.js.LICENSE.txt */
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("simple",[],e):"object"==typeof exports?exports.simple=e():t.simple=e()}(self,(function(){return function(){"use strict";var t={d:function(e,n){for(var r in n)t.o(n,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:n[r]})},o:function(t,e){return Object.prototype.hasOwnProperty.call(t,e)}},e={};t.d(e,{Bc:function(){return o},sg:function(){return r},default:function(){return c}});const n={getItem:function(t,e=localStorage){const n=e.getItem(t);return n?JSON.parse(n):null},setItem:function(t,e,n=localStorage){n.setItem(t,JSON.stringify(e))},removeItem:function(t,e=localStorage){e.removeItem(t)}},r=function(t,e,n){let r,i,s,c,a,u,h=0,l=!1,d=!1,f=!0;const m=!e&&0!==e&&"function"==typeof window.requestAnimationFrame;if("function"!=typeof t)throw new TypeError("Expected a function");function p(e){const n=r,o=i;return r=i=void 0,h=e,s=t.apply(o,n),s}function g(t,e=0){return m?window.requestAnimationFrame(t):setTimeout(t,e)}function w(t){const n=t-(u||0);return void 0===u||n>=(e||0)||n<0||d&&t-h>=(c||0)}function b(){const t=Date.now();if(w(t))return y(t);a=g(b,function(t){const n=t-h,r=(e||0)-(t-(u||0));return d?Math.min(r,(c||0)-n):r}(t))}function y(t){return a=void 0,f&&r?p(t):(r=i=void 0,s)}function v(...t){const n=Date.now(),o=w(n);if(r=t,i=this,u=n,o){if(void 0===a)return function(t){return h=t,a=g(b,e),l?p(t):s}(u);if(d)return a=g(b,e),p(u)}return void 0===a&&(a=g(b,e)),s}return e=Number(e)||0,n&&"Object"===o(n)&&(l=Boolean(n.leading),d="maxWait"in n,c=d?Math.max(Number(n.maxWait)||0,e):c,f="trailing"in n?Boolean(n.trailing):f),v.cancel=function(){void 0!==a&&function(t){if(m)return window.cancelAnimationFrame(t);clearTimeout(t)}(a),h=0,r=u=i=a=void 0},v.flush=function(){return void 0===a?s:y(Date.now())},v.pending=function(){return void 0!==a},v},o=(t,e)=>{const n=typeof t,{lowercase:r=!1}=e||{};if("object"===n){const e=Object.prototype.toString.call(t).slice(8,-1);return r?e.toLowerCase():e}return n},i=(t,e="image/png")=>{const n=document.createElement("canvas"),r=n.getContext("2d");return n.height=t.height,n.width=t.width,r?.putImageData(t,0,0),n.toDataURL(e)},{rE:s}={rE:"1.2.2"},c={version:s,PromiseQueue:class{taskId;arr;func;limit;count;pendingTask;finishSResolve;constructor(){this.taskId=0,this.count=0,this.arr=[],this.limit=0,this.pendingTask=[],this.func=()=>({}),this.finishSResolve=()=>({})}init(t){const{arr:e=[],func:n,limit:r=6}=t||{};return e.length&&n?(this.arr.length&&this.pendingTask.push({arr:[...this.arr],func:this.func,limit:this.limit}),this.count=0,this.arr=[...e],this.limit=Math.min(r,e.length),this.func=n,this.loop(this.taskId),new Promise((t=>{this.finishSResolve=t}))):Promise.reject("arr and func is required")}async loop(t){if(t===this.taskId)if(0!==this.arr.length)this.count+=1,this.count<this.limit&&this.loop(this.taskId),await this.func(this.arr.shift()),t===this.taskId&&(this.count>=this.limit&&this.loop(this.taskId),this.count-=1);else if(this.count<=0){this.finishSResolve("finish");const{arr:t,func:e,limit:n}=this.pendingTask.shift()||{};t?.length&&e&&(this.taskId+=1,this.init({arr:t,func:e,limit:n}))}else setTimeout((()=>this.loop(this.taskId)),100)}cancel(){return this.taskId+=1,this.arr.length=0,this.pendingTask.length=0,Promise.resolve("cancel")}},ComputeStore:class{store;constructor(){this.store=new Map}get(t,...e){const n=JSON.stringify(e);if(!this.store.has(t)){const r=t(...e),o=new Map;return o.set(n,r),this.store.set(t,o),r}const r=this.store.get(t);if(!r?.has(n)){const o=t(...e);return r?.set(n,o),o}return r.get(n)}},Once:class{func;result;constructor(t){this.func=t,this.result}run(...t){return void 0!==this.result||(this.result=this.func(...t)),this.result}},IndexedDB:class{db;dbName;storeName;version;constructor(t,e,n=1){this.db=void 0,this.dbName=t,this.storeName=e,this.version=n}open(){return new Promise(((t,e)=>{const n=indexedDB||webkitIndexedDB||mozIndexedDB||msIndexedDB;if(!n){const t=new Error("IndexedDB is not supported");return console.warn(t),e(t)}const r=n.open(this.dbName,this.version);r.onsuccess=e=>{this.db=e.target?.result,t(e)},r.onerror=function(t){e(t),console.log("数据库打开失败...")},r.onupgradeneeded=t=>{const e=this.storeName,n=t.target?.result;n.objectStoreNames.contains(e)||n.createObjectStore(e)}}))}dispatch(t,e={},...n){const{mode:r="readwrite",more:o=!1}=e;return new Promise(((i,s)=>{try{const e=this.getObjectStore(r),c=e[t];if(!(t in e))return s(`${t} is undefined, reference resources:https://developer.mozilla.org/zh-CN/docs/Web/API/IDBObjectStore`);if("function"==typeof c){const r=e[t](...n);r.onsuccess=t=>{const n=t.target?.result;i(o?{res:r,event:t,result:n,objectStore:e,bd:this.db}:n)},r.onerror=s}else i(c)}catch(r){s({message:r,arguments:{func:t,options:e,args:n}})}}))}getObjectStore(t="readwrite"){if(!this.db)throw new Error("No database connection available , please use IndexedDB=>open to connect");return this.db.transaction([this.storeName],t).objectStore(this.storeName)}},objectToQuery:function t(e,n){if("Object"!==o(e))return"";const r=Object.keys(e);return r.reduce(((i,s,c)=>{const a=e[s];return 0!==c||n||(i+="?"),"Object"===o(a)?i+=t(a,n?`${n}[${s}]`:s):i+=n?`${n}[${s}]=${a}`:`${s}=${a}`,c!==r.length-1&&(i+="&"),i}),"")},parseError:function(t){return t instanceof Error?{message:t.message,stack:t.stack,name:t.name}:{message:String(t),stack:"Unknown error",name:"Unknown"}},queryToObject:function(t){t=t||location.search;const e={};if("function"==typeof URLSearchParams){const n=new URLSearchParams(t);for(const[t,r]of n)e[t]=r}else{const n=t.replace("?","").split("&");for(const t of n){const[n,r]=t.split("=");e[n]=r}}return e},arraysLaminated:(...t)=>{const e=[],n=t.length;for(let r=0;r<n;r++){const i=t[r];"Array"===o(i)&&i?.forEach(((t,o)=>{e[r+o*n]=t}))}return e.filter((t=>null!=t))},debounce:r,throttle:function(t,e=0,n){let i=!0,s=!0;if("function"!=typeof t)throw new TypeError("Expected a function");return n&&"Object"===o(n)&&(i="leading"in n?Boolean(n.leading):i,s="trailing"in n?Boolean(n.trailing):s),r(t,e,{leading:i,trailing:s,maxWait:e})},createElement:t=>{const{tagName:e="div",...n}=t||{},r=document.createElement(e);for(const[t,e]of Object.entries(n))r[t]=e;return r},isNull:t=>[void 0,'""','" "',"null","[]","{}"].includes(JSON.stringify(t)),isCanvasBlank:t=>{const e=document.createElement("canvas");return e.width=t.width,e.height=t.height,t.toDataURL()==e.toDataURL()},env:(t,e)=>{const{userAgent:n,platform:r}=navigator;return t=t||n,{isWin:(e=e||r).includes("Win"),isMac:e.includes("Mac"),isLinux:!!e.match(/Linux|X11/i),isIpad:!!t.match(/iPad/i),isMobile:!!t.match(/mobile|phone/i),isAndroid:!!t.match(/android/i),isIphone:!!t.match(/iphone/i),isWM:!!t.match(/windows mobile/i),isWeChat:!!t.match(/MicroMessenger/i)}},dataType:o,canvasDrawImage:(t,e,n={})=>{const{interceptors:r,crossOrigin:i="Anonymous",zoom:s=1,debug:c=!1}=n;return new Promise(((n,a)=>{try{const u=t?.getContext("2d"),h=t.width,l=t.height;let d=1;const f=new Image;f.crossOrigin=i,f.src=e,f.onload=()=>{let t=f.width,e=f.height;d=h/l>t/e?l/e:h/t,t=t*d*s,e=e*d*s,c&&console.log({cW:h,cH:l,scale:d,zoom:s,originIW:f.width,originIH:f.height,iW:t,iH:e});const[i,m,p,g]=[(h-t)/2,(l-e)/2,t,e];if("function"!=typeof r)u?.drawImage(f,i,m,p,g),n("success");else{const t=r();if("Promise"!==o(t))throw a(new Error("interceptors must be Promise"));t.then((t=>{t?(u?.drawImage(f,i,m,p,g),n("success")):a("cancel")}),(()=>{a("cancel")}))}},f.onerror=t=>{a(t)}}catch(t){a(t)}}))},ImageDataToDataURL:i,ImageDataToImage:(t,e)=>new Promise(((n,r)=>{const o=i(t,e),s=new Image;s.src=o,s.onload=()=>{n(s)},s.onerror=t=>{r(t)}})),ImageDataToArraybuffer:async(t,e)=>{try{const n=i(t,e),r=await fetch(n);return Promise.resolve(r.arrayBuffer())}catch(t){return Promise.reject(t)}},arrayBufferToJson:(t,e=!1)=>{try{if(TextDecoder){const e=new TextDecoder("utf-8");return JSON.parse(e.decode(new Uint8Array(t)))}const e=new Uint8Array(t),n=String.fromCharCode.apply(null,e);return JSON.parse(n)}catch(t){return void(e&&console.log(t))}},isDICM:t=>{if(!t||!t.byteLength||t.byteLength<=132)return!1;let e="";const n=new Uint8Array(t,128,4);for(let t=0;t<n.length;t++)e+=String.fromCharCode(n[t]);return"DICM"===e},importScript:(t,e,n={})=>{if(e&&(e=`script${e}`,document.querySelector(`#${e}`)))return;const r=document.createElement("script");r.setAttribute("type","text/javascript"),r.setAttribute("src",t),e&&r.setAttribute("id",e);for(const[t,e]of Object.entries(n))r.setAttribute(t,e);document.querySelector("head")?.appendChild(r)},httpRequest:(t,e={},n)=>new Promise(((r,o)=>{const i=new XMLHttpRequest;"function"==typeof n&&n(i),i.onreadystatechange=()=>{const{status:t,readyState:e}=i;if(0!==e&&1!==e)return 200!==t?o(i):4===e?r(i):void 0},i.onerror=()=>{o(i)};const{method:s="GET",async:c=!0,username:a,password:u,send:h,open:l,...d}=e;l&&console.log(new Error("don't config open ,please configure async、username、password")),i.open(s,t,c,a,u);for(const[t,e]of Object.entries(d)){if(!(t in i))return o(`${t} is undefined, reference resources: https://www.w3school.com.cn/xmldom/dom_http.asp`);if("function"==typeof i[t]){const{args:n}=e;n&&n.constructor===Array?i[t](...e):i[t](e)}else i[t]=e}i.send(h)})),copyText:t=>new Promise(((e,n)=>{const r=navigator.clipboard;if(!r){if(document.execCommand){const r=document.createElement("input");document.body.appendChild(r),r.setAttribute("value",t),r.select();try{return document.execCommand("Copy"),r.remove(),e("success")}catch(t){return r.remove(),n(t)}}return n("该浏览器不支持自动复制")}r.writeText(t).then((()=>{e("success")}),(t=>{n(t)}))})),storage:n};return e.default}()}));
//# sourceMappingURL=simple.min.js.map