@mrtujiawei/utils
Advanced tools
Comparing version 1.1.5 to 1.1.6
@@ -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.r(e),t.d(e,{CountDown:()=>F,DateTimeTool:()=>I,InvalidIndexError:()=>n,InvalidSizeError:()=>r,LinkList:()=>U,Lock:()=>o,Logger:()=>C,Pagination:()=>L,Queue:()=>h,QueueEmptyError:()=>i,ResponsibilityChain:()=>u,Stack:()=>a,StackEmptyError:()=>s,TYPES:()=>p,TaskQueue:()=>l,addZero:()=>g,debounce:()=>y,hiddenMobile:()=>N,identify:()=>w,isInteger:()=>D,isPlainObject:()=>x,isPromise:()=>E,objectToUrlParams:()=>v,retry:()=>T,reverseRange:()=>k,sleep:()=>d,throttle:()=>S,toString:()=>f,trailing:()=>R,trim:()=>m,urlParamsToObject:()=>b});class s extends Error{constructor(t="Stack is empty"){super(t)}}class i extends Error{constructor(t="Queue is empty"){super(t)}}class r extends Error{constructor(t="Size is invalid"){super(t)}}class n extends Error{constructor(t="Index is invalid"){super(t)}}const a=class{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;return this.stack.pop()}peak(){if(this.isEmpty())throw new s;return this.stack[this.stack.length-1]}},h=class{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 i;return this.queue.shift()}},o=class{constructor(t=1){this.queue=new h,this.size=0,this.maxSize=1,this.setSize(t)}setSize(t=1){if(t<=0)throw new r;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 r;this.queue.isEmpty()?this.size--:this.queue.dequeue()()}},l=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()}))}},u=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}},c=Object.prototype.toString,f=t=>c.call(t);class p{}async function d(t=0){return new Promise((e=>{setTimeout(e,1e3*t)}))}function m(t){if(typeof t==typeof{})for(let e in t)t[e]=m(t[e]);else"string"==typeof t&&(t=t.trim());return t}function g(t,e=2){return String(t).padStart(e,"0")}function v(t){return Object.entries(t).map((([t,e])=>`${t}=${e}`)).join("&")}function b(t){return t.substr(1).split("&").map((t=>t.split("="))).reduce(((t,[e,s])=>(t[decodeURIComponent(e)]=decodeURIComponent(s),t)),{})}function x(t){if("object"!=typeof t)return!1;for(const e in t)return!1;return!0}function y(t,e){let s;return function(...i){s&&clearTimeout(s),s=setTimeout((()=>{t(...i),s=null}),e)}}function E(t){return t&&p.FUNCTION==f(t.then)&&p.FUNCTION==f(t.catch)}function w(t){return t}function T(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 E(s)?s.then(w).catch((t=>r(t,...e))):s}catch(t){return r(t,...e)}}return n(...s)}}p.UNDEFINED=f(void 0),p.NULL=f(null),p.STRING=f(""),p.NUMBER=f(0),p.BOOLEAN=f(!1),p.FUNCTION=f(Function.prototype),p.SYMBOL="[object Symbol]",p.OBJECT=f({}),p.ARRAY=f([]),p.DATE=f(new Date),p.ERROR=f(new Error);const O={timeout:500,leading:!1};function S(t,e=O){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 D(t){const e=f(t);return p.NULL!=e&&p.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,s){let i=(s-e>>1)+e;for(let r=e;r<i;r++)z(t,r,s-r-1+e)}function z(t,e,s){let i=t[e];t[e]=t[s],t[s]=i}const I=class{static timeFormat(t,e=":"){return[t.getHours(),t.getMinutes(),t.getSeconds()].map((t=>g(t))).join(e)}static dateFormat(t,e="-"){return[t.getFullYear(),t.getMonth()+1,t.getDate()].map((t=>g(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)}},F=class{constructor(t){this.message="",this.callbacks=[],this.message=t}async start(t=60,e=0,s=1){for(let i=t;i>e;i--)this.publish(i,!1),await d(s);this.publish(this.message,!0)}publish(t,e){this.callbacks.forEach((s=>{s({message:t,done:e})}))}subscribe(t){this.callbacks.push(t)}unsubscribe(t){let e=this.callbacks.findIndex((e=>e==t));-1!=e&&this.callbacks.splice(e,1)}clear(){this.callbacks=[]}},L=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)}};class A{constructor(t,e,s){this.value=t,this.prev=e,this.next=s}}const P=t=>t,j=t=>void 0!==t;class M{constructor(){this.head=new A,this.tail=new A,this.length=0,this.clear()}checkIndex(t){if(t<0||t>=this.size())throw new n}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 M;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=P){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 M;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 A(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()&&!j(e))throw new TypeError("Reduce of empty array with no initial value");return this.forEach(((s,i)=>{e=0!=i||j(e)?t(e,s,i):s})),e}reduceRight(t,e){let s=this.size()-1,i=this.tail.prev;if(!j(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=j(e)?e:this.size())>-1?e:this.size()-e;let s=new M;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 A(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}}}}}const U=M;var $;!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"}($||($={}));class B{constructor(){this.level=$.OFF,this.listenHandle=[]}formatMessage(t,e){return`[${$[t]}] ${I.dateTimeFormat(new Date)} ${e}`}getParameters(t){return t.map((t=>{let e=f(t);if([p.FUNCTION,p.STRING,p.UNDEFINED,p.NULL].includes(e))return String(t);if("undefined"!=typeof Symbol&&p.SYMBOL==e)return t.toString();if(p.NUMBER==e)return t;if(p.ERROR==e)return`${t.message}: \n${t.stack}`;if(p.DATE==e)return`date: ${t.getTime()}`;if(p.ARRAY==e){let e=[];e.push("[");for(let s in t)D(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(p.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=$.TRACE;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}info(...t){const e=$.INFO;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}debug(...t){const e=$.DEBUG;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}warn(...t){const e=$.WARN;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}error(...t){const e=$.ERROR;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}fatal(...t){const e=$.FATAL;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}}B.LOG_LEVEL=$,B.instance=new B;const C=B;return e})()})); | ||
!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.r(e),t.d(e,{CountDown:()=>F,DateTimeTool:()=>I,InvalidIndexError:()=>n,InvalidSizeError:()=>r,LinkList:()=>U,Lock:()=>o,Logger:()=>C,Pagination:()=>L,Queue:()=>h,QueueEmptyError:()=>i,ResponsibilityChain:()=>u,Stack:()=>a,StackEmptyError:()=>s,TYPES:()=>p,TaskQueue:()=>l,addZero:()=>g,debounce:()=>y,hiddenMobile:()=>N,identify:()=>w,isInteger:()=>D,isPlainObject:()=>x,isPromise:()=>E,objectToUrlParams:()=>v,retry:()=>T,reverseRange:()=>k,sleep:()=>d,throttle:()=>S,toString:()=>f,trailing:()=>R,trim:()=>m,urlParamsToObject:()=>b});class s extends Error{constructor(t="Stack is empty"){super(t)}}class i extends Error{constructor(t="Queue is empty"){super(t)}}class r extends Error{constructor(t="Size is invalid"){super(t)}}class n extends Error{constructor(t="Index is invalid"){super(t)}}const a=class{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;return this.stack.pop()}peak(){if(this.isEmpty())throw new s;return this.stack[this.stack.length-1]}},h=class{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 i;return this.queue.shift()}},o=class{constructor(t=1){this.queue=new h,this.size=0,this.maxSize=1,this.setSize(t)}setSize(t=1){if(t<=0)throw new r;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 r;this.queue.isEmpty()?this.size--:this.queue.dequeue()()}},l=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()}))}},u=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}},c=Object.prototype.toString,f=t=>c.call(t);class p{}async function d(t=0){return new Promise((e=>{setTimeout(e,1e3*t)}))}function m(t){if(typeof t==typeof{})for(let e in t)t[e]=m(t[e]);else"string"==typeof t&&(t=t.trim());return t}function g(t,e=2){return String(t).padStart(e,"0")}function v(t){return Object.entries(t).map((([t,e])=>`${t}=${e}`)).join("&")}function b(t){return t.substr(1).split("&").map((t=>t.split("="))).reduce(((t,[e,s])=>(t[decodeURIComponent(e)]=decodeURIComponent(s),t)),{})}function x(t){if("object"!=typeof t)return!1;for(const e in t)return!1;return!0}function y(t,e){let s;return function(...i){s&&clearTimeout(s),s=setTimeout((()=>{t(...i),s=null}),e)}}function E(t){return t&&p.FUNCTION==f(t.then)&&p.FUNCTION==f(t.catch)}function w(t){return t}function T(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 E(s)?s.then(w).catch((t=>r(t,...e))):s}catch(t){return r(t,...e)}}return n(...s)}}p.UNDEFINED=f(void 0),p.NULL=f(null),p.STRING=f(""),p.NUMBER=f(0),p.BOOLEAN=f(!1),p.FUNCTION=f(Function.prototype),p.SYMBOL="[object Symbol]",p.OBJECT=f({}),p.ARRAY=f([]),p.DATE=f(new Date),p.ERROR=f(new Error);const O={timeout:500,leading:!1};function S(t,e=O){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 D(t){const e=f(t);return p.NULL!=e&&p.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,s){let i=(s-e>>1)+e;for(let r=e;r<i;r++)z(t,r,s-r-1+e)}function z(t,e,s){let i=t[e];t[e]=t[s],t[s]=i}const I=class{static timeFormat(t,e=":"){return[t.getHours(),t.getMinutes(),t.getSeconds()].map((t=>g(t))).join(e)}static dateFormat(t,e="-"){return[t.getFullYear(),t.getMonth()+1,t.getDate()].map((t=>g(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)}},F=class{constructor(t){this.message="",this.callbacks=[],this.message=t}publish(t,e){this.callbacks.forEach((s=>{s({message:t,done:e})}))}async start(t=60,e=0,s=1){for(let i=t;i>e;i--)this.publish(i,!1),await d(s);this.publish(this.message,!0)}subscribe(t){this.callbacks.push(t)}unsubscribe(t){let e=this.callbacks.findIndex((e=>e==t));-1!=e&&this.callbacks.splice(e,1)}clear(){this.callbacks=[]}},L=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)}};class A{constructor(t,e,s){this.value=t,this.prev=e,this.next=s}}const P=t=>t,j=t=>void 0!==t;class M{constructor(){this.head=new A,this.tail=new A,this.length=0,this.clear()}checkIndex(t){if(t<0||t>=this.size())throw new n}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 M;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=P){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 M;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 A(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()&&!j(e))throw new TypeError("Reduce of empty array with no initial value");return this.forEach(((s,i)=>{e=0!=i||j(e)?t(e,s,i):s})),e}reduceRight(t,e){let s=this.size()-1,i=this.tail.prev;if(!j(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=j(e)?e:this.size())>-1?e:this.size()-e;let s=new M;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 A(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}}}}}const U=M;var $;!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"}($||($={}));class B{constructor(){this.level=$.OFF,this.listenHandle=[]}formatMessage(t,e){return`[${$[t]}] ${I.dateTimeFormat(new Date)} ${e}`}getParameters(t){return t.map((t=>{let e=f(t);if([p.FUNCTION,p.STRING,p.UNDEFINED,p.NULL].includes(e))return String(t);if("undefined"!=typeof Symbol&&p.SYMBOL==e)return t.toString();if(p.NUMBER==e)return t;if(p.ERROR==e)return`${t.message}: \n${t.stack}`;if(p.DATE==e)return`date: ${t.getTime()}`;if(p.ARRAY==e){let e=[];e.push("[");for(let s in t)D(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(p.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=$.TRACE;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}info(...t){const e=$.INFO;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}debug(...t){const e=$.DEBUG;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}warn(...t){const e=$.WARN;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}error(...t){const e=$.ERROR;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}fatal(...t){const e=$.FATAL;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}}B.LOG_LEVEL=$,B.instance=new B;const C=B;return e})()})); | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@mrtujiawei/utils", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"description": "把自己写的工具函数打包发布到npm上", | ||
@@ -5,0 +5,0 @@ "types": "src/types.d.ts", |
@@ -1,2 +0,4 @@ | ||
export declare class Stack<T> { | ||
import './types/CountDown.d'; | ||
declare class Stack<T> { | ||
get size(): number; | ||
@@ -9,3 +11,3 @@ isEmpty(): boolean; | ||
export declare class Queue<T> { | ||
declare class Queue<T> { | ||
get size(): number; | ||
@@ -17,3 +19,3 @@ isEmpty(): boolean; | ||
export declare class Lock { | ||
declare class Lock { | ||
async lock(): Promise<void> | ||
@@ -28,3 +30,3 @@ unload():void; | ||
export declare class TaskQueue { | ||
declare class TaskQueue { | ||
constructor(options: TaskQueueOptions); | ||
@@ -35,3 +37,3 @@ setOptions(options: TaskQueueOptions): void; | ||
export declare class DateTimeTool { | ||
declare class DateTimeTool { | ||
static timeFormat(date: Date, delimiter?: string): string; | ||
@@ -46,3 +48,3 @@ static dateFormat(date: Date, delimiter?: string): string; | ||
export declare class ResponsibilityChain { | ||
declare class ResponsibilityChain { | ||
add(callback: Function): void; | ||
@@ -52,5 +54,23 @@ doAction(...args: any[]): any; | ||
export declare class CountDown { | ||
async stsart(from?: number, to?: number, timeout?: number): Promise<void>; | ||
subscribe(callback: Function): void; | ||
type countDownCallbackContent = { | ||
message: string, | ||
done: boolean, | ||
}; | ||
type countDownCallback = (content: countDownCallbackContent) => void; | ||
declare class ICountDown { | ||
/** | ||
* 定义结束消息是什么 | ||
*/ | ||
constructor(message: string); | ||
/** | ||
* 开始倒计时 | ||
*/ | ||
async start(from: number = 60, to: number = 0, timeout: number = 1): Promise<void>; | ||
/** | ||
* 添加订阅 | ||
*/ | ||
subscribe(callback: countDownCallback): void; | ||
unsubscribe(callback: Function): void; | ||
@@ -63,3 +83,3 @@ clear(): void; | ||
export default class Logger { | ||
class Logger { | ||
static LOG_LEVEL: LOG_LEVEL; | ||
@@ -78,28 +98,12 @@ static getLogger: Logger; | ||
export declare class StackEmptyError extends Error { | ||
declare class StackEmptyError extends Error { | ||
constructor(message?: string); | ||
} | ||
export declare class QueueEmptyError extends Error { | ||
declare class QueueEmptyError extends Error { | ||
constructor(message?: string); | ||
} | ||
export declare class InvalidSizeError extends Error { | ||
declare class InvalidSizeError extends Error { | ||
constructor(message?: string); | ||
} | ||
export declare namespace utils { | ||
Lock; | ||
Stack; | ||
Queue, | ||
Logger, | ||
CountDown; | ||
TaskQueue, | ||
DateTimeTool; | ||
ResponsibilityChain; | ||
StackEmptyError; | ||
QueueEmptyError; | ||
InvalidSizeError; | ||
} |
import { sleep, } from './utils'; | ||
import { ICountDown, } from '../types.d'; | ||
@@ -7,9 +8,6 @@ /** | ||
*/ | ||
class CountDown { | ||
class CountDown implements ICountDown { | ||
private message: string = ''; | ||
private callbacks: Function[] = []; | ||
/** | ||
* 定义结束消息是什么 | ||
*/ | ||
constructor(message: string) { | ||
@@ -20,13 +18,2 @@ this.message = message; | ||
/** | ||
* 开始倒计时 | ||
*/ | ||
async start(from: number = 60, to: number = 0, timeout: number = 1): Promise<void> { | ||
for (let i = from; i > to; i--) { | ||
this.publish(i, false); | ||
await sleep(timeout); | ||
} | ||
this.publish(this.message, true); | ||
} | ||
/** | ||
* 发布每次变化 | ||
@@ -43,5 +30,10 @@ */ | ||
/** | ||
* 添加订阅 | ||
*/ | ||
async start(from: number = 60, to: number = 0, timeout: number = 1): Promise<void> { | ||
for (let i = from; i > to; i--) { | ||
this.publish(i, false); | ||
await sleep(timeout); | ||
} | ||
this.publish(this.message, true); | ||
} | ||
subscribe(callback: Function): void { | ||
@@ -48,0 +40,0 @@ this.callbacks.push(callback); |
Sorry, the diff of this file is not supported yet
129015
56
2658