@mrtujiawei/utils
Advanced tools
Comparing version 1.2.3 to 1.2.4
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.utils=e():t.utils=e()}(this,(function(){return(()=>{"use strict";var t={d:(e,s)=>{for(var i in s)t.o(s,i)&&!t.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:s[i]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.d(e,{default:()=>G});var s={};t.r(s),t.d(s,{TYPES:()=>c,addZero:()=>d,debounce:()=>E,hiddenMobile:()=>T,identify:()=>b,isInteger:()=>S,isPlainObject:()=>v,isPromise:()=>y,objectToUrlParams:()=>g,retry:()=>x,reverseRange:()=>N,sleep:()=>p,throttle:()=>L,toString:()=>u,trailing:()=>R,trim:()=>f,urlParamsToObject:()=>m});class i{constructor(){this.stack=[]}get size(){return this.stack.length}isEmpty(){return 0==this.size}push(...t){t.forEach((t=>{this.stack.push(t)}))}pop(){if(this.isEmpty())throw new i.StackEmptyError;return this.stack.pop()}peak(){if(this.isEmpty())throw new i.StackEmptyError;return this.stack[this.stack.length-1]}}i.StackEmptyError=class extends Error{constructor(t="Stack is empty"){super(t)}};const r=i;class n{constructor(){this.queue=[]}get size(){return this.queue.length}isEmpty(){return 0==this.size}enqueue(...t){t.forEach((t=>{this.queue.push(t)}))}dequeue(){if(this.isEmpty())throw new n.QueueEmptyError;return this.queue.shift()}}n.QueueEmptyError=class extends Error{constructor(t="Queue is empty"){super(t)}};const a=n;class h{constructor(t=1){this.queue=new a,this.size=0,this.maxSize=1,this.setSize(t)}setSize(t=1){if(t<=0)throw new h.InvalidSizeError;this.maxSize=t}async lock(){if(this.size++,!(this.size<=this.maxSize))return new Promise((t=>{this.queue.enqueue(t)}))}unLock(){if(this.size<=0)throw new h.InvalidSizeError;this.size--,this.queue.isEmpty()||this.queue.dequeue()()}}h.InvalidSizeError=class extends Error{constructor(t="Size is invalid"){super(t)}};const o=h,l=Object.prototype.toString,u=t=>l.call(t);class c{}async function p(t=0){return new Promise((e=>{setTimeout(e,1e3*t)}))}function f(t){if(typeof t==typeof{})for(let e in t)t[e]=f(t[e]);else"string"==typeof t&&(t=t.trim());return t}function d(t,e=2){return String(t).padStart(e,"0")}function g(t){return Object.entries(t).map((([t,e])=>`${t}=${e}`)).join("&")}function m(t){return t.substr(1).split("&").map((t=>t.split("="))).reduce(((t,[e,s])=>(t[decodeURIComponent(e)]=decodeURIComponent(s),t)),{})}function v(t){if("object"!=typeof t)return!1;for(const e in t)return!1;return!0}function E(t,e){let s;return function(...i){s&&clearTimeout(s),s=setTimeout((()=>{t(...i),s=null}),e)}}function y(t){return t&&c.FUNCTION==u(t.then)&&c.FUNCTION==u(t.catch)}function b(t){return t}function x(t,e=2){return function(...s){let i=0;function r(t,...s){if(i++<e)return n(...s);throw new Error(t.message)}function n(...e){try{let s=t(...e);return y(s)?s.then(b).catch((t=>r(t,...e))):s}catch(t){return r(t,...e)}}return n(...s)}}c.UNDEFINED=u(void 0),c.NULL=u(null),c.STRING=u(""),c.NUMBER=u(0),c.BOOLEAN=u(!1),c.FUNCTION=u(Function.prototype),c.SYMBOL="[object Symbol]",c.OBJECT=u({}),c.ARRAY=u([]),c.DATE=u(new Date),c.ERROR=u(new Error);const w={timeout:500,leading:!1};function L(t,e=w){return e.leading?function(t,e){let s,i=null,r=!1;function n(a){i?r=!0:(i=setTimeout((()=>{r?(r=!1,i=null,n(a)):(i&&clearTimeout(i),i=null)}),e),t.apply(a,s))}return function(...t){s=t,n(this)}}(t,e.timeout):R(t,e.timeout)}function R(t,e){let s,i=null;return function(...r){s=r,i||(i=setTimeout((()=>{i=null,t.apply(this,s)}),e))}}function S(t){const e=u(t);return c.NULL!=e&&c.UNDEFINED!=e&&/^[0-9]+$/.test(t.toString())}function T(t){return(t=t||"").replace(/\d{1,4}(?=(\d{4}$))/,(t=>"".padStart(t.length,"*")))}function N(t,e,s){let i=(s-e>>1)+e;for(let r=e;r<i;r++)k(t,r,s-r-1+e)}function k(t,e,s){let i=t[e];t[e]=t[s],t[s]=i}const D=class{static timeFormat(t,e=":"){return[t.getHours(),t.getMinutes(),t.getSeconds()].map((t=>d(t))).join(e)}static dateFormat(t,e="-"){return[t.getFullYear(),t.getMonth()+1,t.getDate()].map((t=>d(t))).join(e)}static dateTimeFormat(t,e="-",s=":"){return`${this.dateFormat(t,e)} ${this.timeFormat(t,s)}`}static getNthDayBefore(t){const e=new Date,s=new Date;return s.setTime(s.getTime()-864e5*t),[s,e]}static getNthHourBefore(t){const e=new Date,s=new Date;return s.setTime(s.getTime()-36e5*t),[s,e]}static getNthMonthBefore(t=1){const e=new Date,s=new Date;return s.setMonth(s.getMonth()-t),[s,e]}static toDayBegin(t){t.setHours(0),t.setMinutes(0),t.setSeconds(0),t.setMilliseconds(0)}},O={start:60,end:60,timeout:1};class I{constructor(t,e,s){this.value=t,this.prev=e,this.next=s}}const z=t=>t,F=t=>void 0!==t;class A extends Error{constructor(t="Index is invalid"){super(t)}}class P{constructor(){this.head=new I,this.tail=new I,this.length=0,this.clear()}checkIndex(t){if(t<0||t>=this.size())throw new P.InvalidIndexError}clear(){this.head.next=this.tail,this.tail.prev=this.head,this.length=0}concat(t){if(t){let e=t.head.next;this.tail.prev.next=e,e.prev=this.tail.prev,this.tail=t.tail,this.length+=t.size()}return this}contains(t){return-1!=this.indexOf(t)}filter(t){let e=new P;return this.forEach(((e,s,i)=>{t(e,s,i)&&i.push(e)})),e}find(t){for(let e=0,s=this.head.next;s!=this.tail;s=s.next,e++)if(t(s.value,e,this))return s.value}findIndex(t){for(let e=0,s=this.head.next;s!=this.tail;s=s.next,e++)if(t(s.value,e++,this))return e;return-1}forEach(t){for(let e=0,s=this.head.next;s!=this.tail;s=s.next,e++)t(s.value,e,this)}get(t){this.checkIndex(t);let e=this.head.next;for(let s=0;s<t;e=e.next,s++);return e.value}includes(t){return-1!=this.indexOf(t)}indexOf(t){return this.findIndex((e=>e==t))}isEmpty(){return 0==this.size()}join(t="",e=z){if("function"!=typeof e)throw new TypeError("transfer is not a function");let s=[];return this.forEach(((t,i)=>{s.push(e(t,i,this))})),s.join(t)}lastIndexOf(t){for(let e=this.tail.prev,s=this.size()-1;e!=this.head;e=e.prev,s--)if(t==e.value)return s;return-1}map(t){let e=new P;return this.forEach(((s,i)=>{e.push(t(s,i,this))})),e}pop(){if(!this.size())return;this.length--;let t=this.tail.prev;return t.prev.next=this.tail,this.tail.prev=t.prev,t.value}push(t){let e=new I(t);e.next=this.tail,e.prev=this.tail.prev,this.tail.prev.next=e,this.tail.prev=e,this.length++}reduce(t,e){if(!this.size()&&!F(e))throw new TypeError("Reduce of empty array with no initial value");return this.forEach(((s,i)=>{e=0!=i||F(e)?t(e,s,i):s})),e}reduceRight(t,e){let s=this.size()-1,i=this.tail.prev;if(!F(e)){if(!this.size())throw new TypeError("Reduce of empty array with no initial value");e=i.value,i=i.prev,s--}for(;i!=this.head;i=i.prev,s--)e=t(e,i.value,s);return e}remove(t){this.checkIndex(t);for(let e=0,s=this.head.next;s!=this.tail;s=s.next,e++)if(e==t)return s.prev.next=s.next,s.next.prev=s.prev,this.length--,s.value}reverse(){let t=this.head.next,e=this.tail;for(this.clear();t!=e;)this.unshift(t.value),t=t.next;return this}set(t,e){this.checkIndex(t);for(let s=0,i=this.head.next;i!=this.tail;i=i.next,s++)if(s==t){i.value=e;break}}shift(){if(this.size()){let t=this.head.next;return this.head.next=t.next,t.next.prev=this.head,this.length--,t.value}}size(){return this.length}slice(t=0,e){t=t||0,e=(e=F(e)?e:this.size())>-1?e:this.size()-e;let s=new P;return this.forEach(((i,r)=>{r>=t&&r<e&&s.push(i)})),s}some(t){return-1!=this.findIndex(t)}sort(t){for(let e=this.head.next;e!=this.tail;e=e.next){let s=e;for(let i=e.next;i!=this.tail;i=i.next)t(s.value,i.value)>0&&(s=i);[e.value,s.value]=[s.value,e.value]}return this}toString(){return this.join()}toArray(){return this.reduce(((t,e)=>(t.push(e),t)),[])}unshift(t){let e=new I(t);e.next=this.head.next,e.prev=this.head,this.head.next=e,this.length++}[Symbol.iterator](){let t=this.head.next,e=this.tail;return{next(){let s,i=t==e;return i||(s=t.value,t=t.next),{value:s,done:i}}}}}P.InvalidIndexError=A;const M=P;var j;!function(t){t[t.ALL=0]="ALL",t[t.TRACE=1]="TRACE",t[t.DEBUG=2]="DEBUG",t[t.INFO=3]="INFO",t[t.WARN=4]="WARN",t[t.ERROR=5]="ERROR",t[t.FATAL=6]="FATAL",t[t.OFF=7]="OFF"}(j||(j={}));class U{constructor(){this.level=j.OFF,this.listenHandle=[]}formatMessage(t,e){return`[${j[t]}] ${D.dateTimeFormat(new Date)} ${e}`}getParameters(t){return t.map((t=>{let e=u(t);if([c.FUNCTION,c.STRING,c.UNDEFINED,c.NULL].includes(e))return String(t);if("undefined"!=typeof Symbol&&c.SYMBOL==e)return t.toString();if(c.NUMBER==e)return t;if(c.ERROR==e)return`${t.message}: \n${t.stack}`;if(c.DATE==e)return`date: ${t.getTime()}`;if(c.ARRAY==e){let e=[];e.push("[");for(let s in t)S(s)?e.push(`${this.getParameters([t[s]])}, `):e.push(`${s}: ${this.getParameters([t[s]])}, `);return e.push(`length: ${t.length}`),e.push("]"),e.join("")}if(c.OBJECT==e){let e=[];e.push("{");for(let s in t)e.push(`${s} => ${this.getParameters([t[s]])}, `);return 1<e.length&&(e[e.length-1]=e[e.length-1].slice(0,-2)),e.push("}"),e.join("")}try{return JSON.stringify(t)}catch(e){return t.toString()}})).join(" ")}publish(t){this.listenHandle.forEach((e=>{try{e(t)}catch(t){console.log(t)}}))}static getLogger(){return this.instance}setLevel(t){this.level=t}subscribe(t){this.listenHandle.push(t)}unsubscribe(t){for(let e=0;e<this.listenHandle.length;e++)if(this.listenHandle[e]==t){this.listenHandle.splice(e,1);break}}trace(...t){const e=j.TRACE;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}info(...t){const e=j.INFO;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}debug(...t){const e=j.DEBUG;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}warn(...t){const e=j.WARN;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}error(...t){const e=j.ERROR;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}fatal(...t){const e=j.FATAL;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}}U.LOG_LEVEL=j,U.instance=new U;const C=U;class ${constructor(t,e=[]){if(!(t instanceof Function))throw new $.CompareInvalidError;this.compare=t,this.heap=e,this.buildHeap()}buildHeap(){for(let t=(this.heap.length>>1)-1;t>=0;t--)this.shiftDown(t)}shiftDown(t){let e=this.heap,s=e.length,i=e[t],r=s>>1;for(;t<r;){let r=1+(t<<1),n=e[r],a=r+1;if(a<s&&0<this.compare(n,e[a])&&(n=e[a],r=a),0>=this.compare(i,n))break;e[t]=n,t=r}e[t]=i}shiftUp(t){let e=this.heap,s=e[t],i=t-1>>1;for(;0<=i&&0<this.compare(e[i],s);)e[t]=e[i],i=(t=i)-1>>1;e[t]=s}get size(){return this.heap.length}isEmpty(){return 0==this.size}peak(){if(this.isEmpty())throw new $.HeapEmptyError;return this.heap[0]}insert(t){this.heap.push(t),this.shiftUp(this.size-1)}remove(){if(this.isEmpty())throw new $.HeapEmptyError;let t=this.heap[0],e=this.heap.pop();return this.isEmpty()||(this.heap[0]=e,this.shiftDown(0)),t}}$.CompareInvalidError=class extends Error{constructor(t="Param Compare is not a function."){super(t)}},$.HeapEmptyError=class extends Error{constructor(t="Heap is empty"){super(t)}};const H=$;class B{constructor(t){this.value=t}getValue(){return this.value}setValue(t){this.value=t}getLeft(){return this.left}setLeft(t){this.left=t}getRight(){return this.right}setRight(t){this.right=t}}class q{constructor(t,e=[]){if(!t)throw new q.CompareInvalidError;this.compare=t,e.forEach((t=>{this.append(t)}))}append(t){let e=new B(t);this.root=this.appendNode(this.root,e)}appendNode(t,e){if(!t)return e;let s=this.compare(t.getValue(),e.getValue());if(0==s)throw new q.DuplicateValueError;if(0>s){let s=t.getRight();t.setRight(this.appendNode(s,e))}else if(0<s){let s=t.getLeft();t.setLeft(this.appendNode(s,e))}return t}remove(t){this.root=this.removeNode(this.root,t)}removeNode(t,e){if(!t)return null;let s=this.compare(t.getValue(),e);if(0==s)return t.getRight()?this.appendNode(t.getLeft(),t.getRight()):t.getLeft();if(0>s){let s=t.getRight();t.setRight(this.removeNode(s,e))}else if(0<s){let s=t.getLeft();t.setLeft(this.removeNode(s,e))}return t}getMin(){let t=this.root;if(!t)return null;for(;t;){let e=t.getLeft();if(!e)break;t=e}return t.getValue()}getMax(){let t=this.root;if(!t)return null;for(;t;){let e=t.getRight();if(!e)break;t=e}return t.getValue()}clear(){this.root=null}inorderTraversal(t){const e=s=>{s&&(e(s.getLeft()),t(s.getValue()),e(s.getRight()))};e(this.root)}toArray(){let t=[];return this.inorderTraversal((e=>{t.push(e)})),t}}q.CompareInvalidError=class extends Error{constructor(t="Param Compare is not a function."){super(t)}},q.DuplicateValueError=class extends Error{constructor(t="Value is exist."){super(t)}};const V=q,G={...s,Lock:o,Heap:H,Stack:r,Queue:a,Events:class{constructor(){this.eventListeners=new Map}getListeners(t){return this.eventListeners.get(t)||[]}setListeners(t,e){0==e.length?this.eventListeners.delete(t):this.eventListeners.set(t,e)}on(t,e){let s=this.getListeners(t);return s.push(e),this.setListeners(t,s),e}once(t,e){let s=this.getListeners(t),i=(...s)=>{try{e.apply(this,s)}catch(t){this.emit("error",t)}this.off(t,i)};return s.push(i),this.setListeners(t,s),i}off(t,e){let s=this.getListeners(t),i=s.findIndex((t=>t==e));-1!=i&&(s.splice(i,1),this.setListeners(t,s))}emit(t,...e){let s=this.getListeners(t);"error"==t&&0==s.length&&console.error(...e),s.forEach((t=>{try{t.apply(this,e)}catch(t){this.emit("error",t)}}))}},Logger:C,LinkList:M,CountDown:class{constructor(t){this.message="",this.callbacks=[],this.message=t}async start(t=O){for(let e=t.start;e>t.end;e--)this.publish(e,!1),await p(t.timeout);this.publish(this.message,!0)}publish(t,e){this.callbacks.forEach((s=>{s({message:t,done:e})}))}subscribe(t){return this.callbacks.push(t),t}unsubscribe(t){let e=this.callbacks.findIndex((e=>e==t));return-1!=e&&(this.callbacks.splice(e,1),!0)}clear(){this.callbacks=[]}},TaskQueue:class{constructor(t){this.access=new o(1),this.timeout=6e4,this.handler=function(){},this.setOptions(t)}setOptions(t){t.timeout&&(this.timeout=t.timeout),t.handler&&(this.handler=t.handler)}async push(...t){try{return await this.getAccess(),await this.handler(...t)}finally{this.access.unLock()}}getAccess(){return new Promise((async(t,e)=>{this.timeout&&setTimeout((()=>{e("timeout")}),this.timeout),await this.access.lock(),t()}))}},Pagination:class{constructor(t){this.callbacks=[],this.tableData=t}setOrder(t="order"){this.tableData.list.forEach(((e,s)=>{e[t]=s+1}))}sort(t,e="desc"){"asc"==e?this.tableData.list.sort(((e,s)=>e[t]-s[t])):this.tableData.list.sort(((e,s)=>s[t]-e[t]))}to(t){t=t||this.tableData.pageNum,this.tableData.pageNum=t;let{pageSize:e}=this.tableData,s=(t-1)*e,i=s+e,r=this.tableData.list.slice(s,i);return this.publish({...this.tableData,list:r}),r}setPageSize(t){this.tableData.pageSize=t}publish(t){this.callbacks.forEach((e=>e(t)))}subscribe(t){this.callbacks.push(t)}},DateTimeTool:D,BinarySearchTree:V,ResponsibilityChain:class{constructor(){this.chain=[]}add(t){this.chain.push(t)}doAction(...t){let e;for(let s=0;s<this.chain.length&&(e=this.chain[s](...t),!e);s++);return e}}};return e.default})()})); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.utils=e():t.utils=e()}(this,(function(){return(()=>{"use strict";var t={d:(e,r)=>{for(var s in r)t.o(r,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:r[s]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.d(e,{default:()=>Q});var r={};t.r(r),t.d(r,{TYPES:()=>c,addZero:()=>d,debounce:()=>E,hiddenMobile:()=>N,identify:()=>y,isInteger:()=>S,isPlainObject:()=>v,isPromise:()=>w,objectToUrlParams:()=>g,retry:()=>b,reverseRange:()=>k,sleep:()=>p,throttle:()=>R,toString:()=>u,trailing:()=>L,trim:()=>f,urlParamsToObject:()=>m});class s{constructor(){this.stack=[]}get size(){return this.stack.length}isEmpty(){return 0==this.size}push(...t){t.forEach((t=>{this.stack.push(t)}))}pop(){if(this.isEmpty())throw new s.StackEmptyError;return this.stack.pop()}peak(){if(this.isEmpty())throw new s.StackEmptyError;return this.stack[this.stack.length-1]}}s.StackEmptyError=class extends Error{constructor(t="Stack is empty"){super(t)}};const i=s;class n{constructor(){this.queue=[]}get size(){return this.queue.length}isEmpty(){return 0==this.size}enqueue(...t){t.forEach((t=>{this.queue.push(t)}))}dequeue(){if(this.isEmpty())throw new n.QueueEmptyError;return this.queue.shift()}}n.QueueEmptyError=class extends Error{constructor(t="Queue is empty"){super(t)}};const a=n;class o{constructor(t=1){this.queue=new a,this.size=0,this.maxSize=1,this.setSize(t)}setSize(t=1){if(t<=0)throw new o.InvalidSizeError;this.maxSize=t}async lock(){if(this.size++,!(this.size<=this.maxSize))return new Promise((t=>{this.queue.enqueue(t)}))}unLock(){if(this.size<=0)throw new o.InvalidSizeError;this.size--,this.queue.isEmpty()||this.queue.dequeue()()}}o.InvalidSizeError=class extends Error{constructor(t="Size is invalid"){super(t)}};const h=o,l=Object.prototype.toString,u=t=>l.call(t);class c{}async function p(t=0){return new Promise((e=>{setTimeout(e,1e3*t)}))}function f(t){if(typeof t==typeof{})for(let e in t)t[e]=f(t[e]);else"string"==typeof t&&(t=t.trim());return t}function d(t,e=2){return String(t).padStart(e,"0")}function g(t){return Object.entries(t).map((([t,e])=>`${t}=${e}`)).join("&")}function m(t){return t.substr(1).split("&").map((t=>t.split("="))).reduce(((t,[e,r])=>(t[decodeURIComponent(e)]=decodeURIComponent(r),t)),{})}function v(t){if("object"!=typeof t)return!1;for(const e in t)return!1;return!0}function E(t,e){let r;return function(...s){r&&clearTimeout(r),r=setTimeout((()=>{t(...s),r=null}),e)}}function w(t){return t&&c.FUNCTION==u(t.then)&&c.FUNCTION==u(t.catch)}function y(t){return t}function b(t,e=2){return function(...r){let s=0;function i(t,...r){if(s++<e)return n(...r);throw new Error(t.message)}function n(...e){try{let r=t(...e);return w(r)?r.then(y).catch((t=>i(t,...e))):r}catch(t){return i(t,...e)}}return n(...r)}}c.UNDEFINED=u(void 0),c.NULL=u(null),c.STRING=u(""),c.NUMBER=u(0),c.BOOLEAN=u(!1),c.FUNCTION=u(Function.prototype),c.SYMBOL="[object Symbol]",c.OBJECT=u({}),c.ARRAY=u([]),c.DATE=u(new Date),c.ERROR=u(new Error);const x={timeout:500,leading:!1};function R(t,e=x){return e.leading?function(t,e){let r,s=null,i=!1;function n(a){s?i=!0:(s=setTimeout((()=>{i?(i=!1,s=null,n(a)):(s&&clearTimeout(s),s=null)}),e),t.apply(a,r))}return function(...t){r=t,n(this)}}(t,e.timeout):L(t,e.timeout)}function L(t,e){let r,s=null;return function(...i){r=i,s||(s=setTimeout((()=>{s=null,t.apply(this,r)}),e))}}function S(t){const e=u(t);return c.NULL!=e&&c.UNDEFINED!=e&&/^[0-9]+$/.test(t.toString())}function N(t){return(t=t||"").replace(/\d{1,4}(?=(\d{4}$))/,(t=>"".padStart(t.length,"*")))}function k(t,e,r){let s=(r-e>>1)+e;for(let i=e;i<s;i++)T(t,i,r-i-1+e)}function T(t,e,r){let s=t[e];t[e]=t[r],t[r]=s}const D=class{static timeFormat(t,e=":"){return[t.getHours(),t.getMinutes(),t.getSeconds()].map((t=>d(t))).join(e)}static dateFormat(t,e="-"){return[t.getFullYear(),t.getMonth()+1,t.getDate()].map((t=>d(t))).join(e)}static dateTimeFormat(t,e="-",r=":"){return`${this.dateFormat(t,e)} ${this.timeFormat(t,r)}`}static getNthDayBefore(t){const e=new Date,r=new Date;return r.setTime(r.getTime()-864e5*t),[r,e]}static getNthHourBefore(t){const e=new Date,r=new Date;return r.setTime(r.getTime()-36e5*t),[r,e]}static getNthMonthBefore(t=1){const e=new Date,r=new Date;return r.setMonth(r.getMonth()-t),[r,e]}static toDayBegin(t){t.setHours(0),t.setMinutes(0),t.setSeconds(0),t.setMilliseconds(0)}},O={start:60,end:60,timeout:1};class I{constructor(t,e,r){this.value=t,this.prev=e,this.next=r}}const z=t=>t,A=t=>void 0!==t;class j extends Error{constructor(t="Index is invalid"){super(t)}}class F{constructor(){this.head=new I,this.tail=new I,this.length=0,this.clear()}checkIndex(t){if(t<0||t>=this.size())throw new F.InvalidIndexError}clear(){this.head.next=this.tail,this.tail.prev=this.head,this.length=0}concat(t){if(t){let e=t.head.next;this.tail.prev.next=e,e.prev=this.tail.prev,this.tail=t.tail,this.length+=t.size()}return this}contains(t){return-1!=this.indexOf(t)}filter(t){let e=new F;return this.forEach(((e,r,s)=>{t(e,r,s)&&s.push(e)})),e}find(t){for(let e=0,r=this.head.next;r!=this.tail;r=r.next,e++)if(t(r.value,e,this))return r.value}findIndex(t){for(let e=0,r=this.head.next;r!=this.tail;r=r.next,e++)if(t(r.value,e++,this))return e;return-1}forEach(t){for(let e=0,r=this.head.next;r!=this.tail;r=r.next,e++)t(r.value,e,this)}get(t){this.checkIndex(t);let e=this.head.next;for(let r=0;r<t;e=e.next,r++);return e.value}includes(t){return-1!=this.indexOf(t)}indexOf(t){return this.findIndex((e=>e==t))}isEmpty(){return 0==this.size()}join(t="",e=z){if("function"!=typeof e)throw new TypeError("transfer is not a function");let r=[];return this.forEach(((t,s)=>{r.push(e(t,s,this))})),r.join(t)}lastIndexOf(t){for(let e=this.tail.prev,r=this.size()-1;e!=this.head;e=e.prev,r--)if(t==e.value)return r;return-1}map(t){let e=new F;return this.forEach(((r,s)=>{e.push(t(r,s,this))})),e}pop(){if(!this.size())return;this.length--;let t=this.tail.prev;return t.prev.next=this.tail,this.tail.prev=t.prev,t.value}push(t){let e=new I(t);e.next=this.tail,e.prev=this.tail.prev,this.tail.prev.next=e,this.tail.prev=e,this.length++}reduce(t,e){if(!this.size()&&!A(e))throw new TypeError("Reduce of empty array with no initial value");return this.forEach(((r,s)=>{e=0!=s||A(e)?t(e,r,s):r})),e}reduceRight(t,e){let r=this.size()-1,s=this.tail.prev;if(!A(e)){if(!this.size())throw new TypeError("Reduce of empty array with no initial value");e=s.value,s=s.prev,r--}for(;s!=this.head;s=s.prev,r--)e=t(e,s.value,r);return e}remove(t){this.checkIndex(t);for(let e=0,r=this.head.next;r!=this.tail;r=r.next,e++)if(e==t)return r.prev.next=r.next,r.next.prev=r.prev,this.length--,r.value}reverse(){let t=this.head.next,e=this.tail;for(this.clear();t!=e;)this.unshift(t.value),t=t.next;return this}set(t,e){this.checkIndex(t);for(let r=0,s=this.head.next;s!=this.tail;s=s.next,r++)if(r==t){s.value=e;break}}shift(){if(this.size()){let t=this.head.next;return this.head.next=t.next,t.next.prev=this.head,this.length--,t.value}}size(){return this.length}slice(t=0,e){t=t||0,e=(e=A(e)?e:this.size())>-1?e:this.size()-e;let r=new F;return this.forEach(((s,i)=>{i>=t&&i<e&&r.push(s)})),r}some(t){return-1!=this.findIndex(t)}sort(t){for(let e=this.head.next;e!=this.tail;e=e.next){let r=e;for(let s=e.next;s!=this.tail;s=s.next)t(r.value,s.value)>0&&(r=s);[e.value,r.value]=[r.value,e.value]}return this}toString(){return this.join()}toArray(){return this.reduce(((t,e)=>(t.push(e),t)),[])}unshift(t){let e=new I(t);e.next=this.head.next,e.prev=this.head,this.head.next=e,this.length++}[Symbol.iterator](){let t=this.head.next,e=this.tail;return{next(){let r,s=t==e;return s||(r=t.value,t=t.next),{value:r,done:s}}}}}F.InvalidIndexError=j;const M=F;var P;!function(t){t[t.ALL=0]="ALL",t[t.TRACE=1]="TRACE",t[t.DEBUG=2]="DEBUG",t[t.INFO=3]="INFO",t[t.WARN=4]="WARN",t[t.ERROR=5]="ERROR",t[t.FATAL=6]="FATAL",t[t.OFF=7]="OFF"}(P||(P={}));class C{constructor(){this.level=P.OFF,this.listenHandle=[]}formatMessage(t,e){return`[${P[t]}] ${D.dateTimeFormat(new Date)} ${e}`}getParameters(t){return t.map((t=>{let e=u(t);if([c.FUNCTION,c.STRING,c.UNDEFINED,c.NULL].includes(e))return String(t);if("undefined"!=typeof Symbol&&c.SYMBOL==e)return t.toString();if(c.NUMBER==e)return t;if(c.ERROR==e)return`${t.message}: \n${t.stack}`;if(c.DATE==e)return`date: ${t.getTime()}`;if(c.ARRAY==e){let e=[];e.push("[");for(let r in t)S(r)?e.push(`${this.getParameters([t[r]])}, `):e.push(`${r}: ${this.getParameters([t[r]])}, `);return e.push(`length: ${t.length}`),e.push("]"),e.join("")}if(c.OBJECT==e){let e=[];e.push("{");for(let r in t)e.push(`${r} => ${this.getParameters([t[r]])}, `);return 1<e.length&&(e[e.length-1]=e[e.length-1].slice(0,-2)),e.push("}"),e.join("")}try{return JSON.stringify(t)}catch(e){return t.toString()}})).join(" ")}publish(t){this.listenHandle.forEach((e=>{try{e(t)}catch(t){console.log(t)}}))}static getLogger(){return this.instance}setLevel(t){this.level=t}subscribe(t){this.listenHandle.push(t)}unsubscribe(t){for(let e=0;e<this.listenHandle.length;e++)if(this.listenHandle[e]==t){this.listenHandle.splice(e,1);break}}trace(...t){const e=P.TRACE;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}info(...t){const e=P.INFO;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}debug(...t){const e=P.DEBUG;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}warn(...t){const e=P.WARN;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}error(...t){const e=P.ERROR;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}fatal(...t){const e=P.FATAL;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}}C.LOG_LEVEL=P,C.instance=new C;const U=C;class ${constructor(t,e=[]){if(!(t instanceof Function))throw new $.CompareInvalidError;this.compare=t,this.heap=e,this.buildHeap()}buildHeap(){for(let t=(this.heap.length>>1)-1;t>=0;t--)this.shiftDown(t)}shiftDown(t){let e=this.heap,r=e.length,s=e[t],i=r>>1;for(;t<i;){let i=1+(t<<1),n=e[i],a=i+1;if(a<r&&0<this.compare(n,e[a])&&(n=e[a],i=a),0>=this.compare(s,n))break;e[t]=n,t=i}e[t]=s}shiftUp(t){let e=this.heap,r=e[t],s=t-1>>1;for(;0<=s&&0<this.compare(e[s],r);)e[t]=e[s],s=(t=s)-1>>1;e[t]=r}get size(){return this.heap.length}isEmpty(){return 0==this.size}peak(){if(this.isEmpty())throw new $.HeapEmptyError;return this.heap[0]}insert(t){this.heap.push(t),this.shiftUp(this.size-1)}remove(){if(this.isEmpty())throw new $.HeapEmptyError;let t=this.heap[0],e=this.heap.pop();return this.isEmpty()||(this.heap[0]=e,this.shiftDown(0)),t}}$.CompareInvalidError=class extends Error{constructor(t="Param Compare is not a function."){super(t)}},$.HeapEmptyError=class extends Error{constructor(t="Heap is empty"){super(t)}};const B=$;class H{constructor(t){this.value=t}getValue(){return this.value}setValue(t){this.value=t}getLeft(){return this.left}setLeft(t){this.left=t}getRight(){return this.right}setRight(t){this.right=t}}class q{constructor(t,e=[]){if(!t)throw new q.CompareInvalidError;this.compare=t,e.forEach((t=>{this.append(t)}))}append(t){let e=new H(t);this.root=this.appendNode(this.root,e)}appendNode(t,e){if(!t)return e;let r=this.compare(t.getValue(),e.getValue());if(0==r)throw new q.DuplicateValueError;if(0>r){let r=t.getRight();t.setRight(this.appendNode(r,e))}else if(0<r){let r=t.getLeft();t.setLeft(this.appendNode(r,e))}return t}remove(t){this.root=this.removeNode(this.root,t)}removeNode(t,e){if(!t)return null;let r=this.compare(t.getValue(),e);if(0==r)return t.getRight()?this.appendNode(t.getLeft(),t.getRight()):t.getLeft();if(0>r){let r=t.getRight();t.setRight(this.removeNode(r,e))}else if(0<r){let r=t.getLeft();t.setLeft(this.removeNode(r,e))}return t}getMin(){let t=this.root;if(!t)return null;for(;t;){let e=t.getLeft();if(!e)break;t=e}return t.getValue()}getMax(){let t=this.root;if(!t)return null;for(;t;){let e=t.getRight();if(!e)break;t=e}return t.getValue()}clear(){this.root=null}inorderTraversal(t){const e=r=>{r&&(e(r.getLeft()),t(r.getValue()),e(r.getRight()))};e(this.root)}toArray(){let t=[];return this.inorderTraversal((e=>{t.push(e)})),t}}q.CompareInvalidError=class extends Error{constructor(t="Param Compare is not a function."){super(t)}},q.DuplicateValueError=class extends Error{constructor(t="Value is exist."){super(t)}};const V=q;class G{static checkLength(t){if(0>=t)throw new Error("Length is not valid")}static checkRange(t,e){if(e==t)throw new Error(`Number min:${t} is equal to max: ${e}`)}}G.getRandomNumber=(t,e)=>{G.checkRange(t,e);const r=Math.random()*(e-t);return t+Math.floor(r)},G.getRandomBoolean=(t=.5)=>t>Math.random(),G.getRandomUppercaseLetter=()=>{const t=G.getRandomNumber(65,91);return String.fromCharCode(t)},G.getRandomUppercaseString=t=>{G.checkLength(t);const e=new Array(t);for(let r=0;r<t;r++)e[r]=G.getRandomUppercaseLetter();return e.join("")},G.getRandomLowercaseLetter=()=>{const t=G.getRandomNumber(97,123);return String.fromCharCode(t)},G.getRandomLowercaseString=t=>{G.checkLength(t);const e=new Array(t);for(let r=0;r<t;r++)e[r]=G.getRandomLowercaseLetter();return e.join("")},G.getRandomAlphabetString=t=>{G.checkLength(t);const e=new Array(t);for(let r=0;r<t;r++)e[r]=G.getRandomBoolean()?G.getRandomUppercaseLetter():G.getRandomLowercaseLetter();return e.join("")},G.getRandomString=t=>{if(t<=0)throw new Error("String length is not valid");const e=new Array(t);for(let r=0;r<t;r++){let t=G.getRandomNumber(-1,2);-1==t?t=G.getRandomLowercaseLetter():0==t?t=G.getRandomNumber(0,10):1==t&&(t=G.getRandomUppercaseLetter()),e[r]=t}return e.join("")},G.getRandomID=()=>{const t=G.getRandomString(8).toLowerCase(),e=G.getRandomString(12).toLowerCase(),r=[];let s=(new Date).getTime();for(let t=0;t<3;t++){let t=s%65536;s-=t,s/=65536,r.push(d(t.toString(16),4))}return`${t}-${r.join("-")}-${e}`};const Y=G,Q={...r,Lock:h,Heap:B,Stack:i,Queue:a,Events:class{constructor(){this.eventListeners=new Map}getListeners(t){return this.eventListeners.get(t)||[]}setListeners(t,e){0==e.length?this.eventListeners.delete(t):this.eventListeners.set(t,e)}on(t,e){let r=this.getListeners(t);return r.push(e),this.setListeners(t,r),e}once(t,e){let r=this.getListeners(t),s=(...r)=>{try{e.apply(this,r)}catch(t){this.emit("error",t)}this.off(t,s)};return r.push(s),this.setListeners(t,r),s}off(t,e){let r=this.getListeners(t),s=r.findIndex((t=>t==e));-1!=s&&(r.splice(s,1),this.setListeners(t,r))}emit(t,...e){let r=this.getListeners(t);"error"==t&&0==r.length&&console.error(...e),r.forEach((t=>{try{t.apply(this,e)}catch(t){this.emit("error",t)}}))}},Logger:U,Random:Y,LinkList:M,CountDown:class{constructor(t){this.message="",this.callbacks=[],this.message=t}async start(t=O){for(let e=t.start;e>t.end;e--)this.publish(e,!1),await p(t.timeout);this.publish(this.message,!0)}publish(t,e){this.callbacks.forEach((r=>{r({message:t,done:e})}))}subscribe(t){return this.callbacks.push(t),t}unsubscribe(t){let e=this.callbacks.findIndex((e=>e==t));return-1!=e&&(this.callbacks.splice(e,1),!0)}clear(){this.callbacks=[]}},TaskQueue:class{constructor(t){this.access=new h(1),this.timeout=6e4,this.handler=function(){},this.setOptions(t)}setOptions(t){t.timeout&&(this.timeout=t.timeout),t.handler&&(this.handler=t.handler)}async push(...t){try{return await this.getAccess(),await this.handler(...t)}finally{this.access.unLock()}}getAccess(){return new Promise((async(t,e)=>{this.timeout&&setTimeout((()=>{e("timeout")}),this.timeout),await this.access.lock(),t()}))}},Pagination:class{constructor(t){this.callbacks=[],this.tableData=t}setOrder(t="order"){this.tableData.list.forEach(((e,r)=>{e[t]=r+1}))}sort(t,e="desc"){"asc"==e?this.tableData.list.sort(((e,r)=>e[t]-r[t])):this.tableData.list.sort(((e,r)=>r[t]-e[t]))}to(t){t=t||this.tableData.pageNum,this.tableData.pageNum=t;let{pageSize:e}=this.tableData,r=(t-1)*e,s=r+e,i=this.tableData.list.slice(r,s);return this.publish({...this.tableData,list:i}),i}setPageSize(t){this.tableData.pageSize=t}publish(t){this.callbacks.forEach((e=>e(t)))}subscribe(t){this.callbacks.push(t)}},DateTimeTool:D,BinarySearchTree:V,ResponsibilityChain:class{constructor(){this.chain=[]}add(t){this.chain.push(t)}doAction(...t){let e;for(let r=0;r<this.chain.length&&(e=this.chain[r](...t),!e);r++);return e}}};return e.default})()})); | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@mrtujiawei/utils", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "把自己写的工具函数打包发布到npm上", | ||
@@ -5,0 +5,0 @@ "types": "types/index.d.ts", |
@@ -14,5 +14,6 @@ import Stack from './utils/Stack'; | ||
import Events from './utils/Events'; | ||
import Random from './utils/Random'; | ||
import * as utils from './utils/utils'; | ||
export * from './utils/utils'; | ||
export { Lock, Heap, Stack, Queue, Events, Logger, LinkList, CountDown, TaskQueue, Pagination, DateTimeTool, BinarySearchTree, ResponsibilityChain, }; | ||
export { Lock, Heap, Stack, Queue, Events, Logger, Random, LinkList, CountDown, TaskQueue, Pagination, DateTimeTool, BinarySearchTree, ResponsibilityChain, }; | ||
declare const _default: { | ||
@@ -25,2 +26,3 @@ Lock: typeof Lock; | ||
Logger: typeof Logger; | ||
Random: typeof Random; | ||
LinkList: typeof LinkList; | ||
@@ -27,0 +29,0 @@ CountDown: typeof CountDown; |
Sorry, the diff of this file is not supported yet
118760
37
1155