@mrtujiawei/utils
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -1,1 +0,1 @@ | ||
!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:()=>D,DateTimeTool:()=>k,InvalidIndexError:()=>n,InvalidSizeError:()=>r,LinkList:()=>j,LocalData:()=>N,Lock:()=>o,Logger:()=>U,Pagination:()=>z,Queue:()=>h,QueueEmptyError:()=>i,ResponsibilityChain:()=>u,Stack:()=>a,StackEmptyError:()=>s,TYPES:()=>p,TaskQueue:()=>l,addZero:()=>g,debounce:()=>y,identify:()=>w,isPlainObject:()=>x,isPromise:()=>E,objectToUrlParams:()=>v,retry:()=>S,sleep:()=>d,throttle:()=>T,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 S(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 T(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))}}const k=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)}},D=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=[]}},N=class{constructor(t="tujiawei"){this.prefix=t}getString(t){return localStorage.getItem(this.prefix+t)}get(t){let e=this.getString(t);try{e=JSON.parse(e)}catch(t){console.warn("无法转换")}return e}set(t,e){localStorage.setItem(this.prefix+t,JSON.stringify(e))}clear(){localStorage.clear()}remove(t){localStorage.removeItem(this.prefix+t)}},z=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 I{constructor(t,e,s){this.value=t,this.prev=e,this.next=s}}const F=t=>t,L=t=>void 0!==t;class A{constructor(){this.head=new I,this.tail=new I,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 A;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=F){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 A;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()&&!L(e))throw new TypeError("Reduce of empty array with no initial value");return this.forEach(((s,i)=>{e=0!=i||L(e)?t(e,s,i):s})),e}reduceRight(t,e){let s=this.size()-1,i=this.tail.prev;if(!L(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=L(e)?e:this.size())>-1?e:this.size()-e;let s=new A;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}}}}}const j=A;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 M{constructor(){this.level=P.OFF,this.listenHandle=[]}formatMessage(t,e){return`[${P[t]}] ${k.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)e.push(`${s} => ${this.getParameters([t[s]])}`);return e.push("]"),e.join("")}if(p.OBJECT==e){let e=[];e.push("{");for(let s in t)e.push(`${s} => ${this.getParameters([t[s]])}`);e.push("}")}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)))}}M.LOG_LEVEL=P,M.instance=new M;const U=M;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:()=>k,DateTimeTool:()=>R,InvalidIndexError:()=>n,InvalidSizeError:()=>r,LinkList:()=>j,LocalData:()=>z,Lock:()=>o,Logger:()=>$,Pagination:()=>I,Queue:()=>h,QueueEmptyError:()=>i,ResponsibilityChain:()=>u,Stack:()=>a,StackEmptyError:()=>s,TYPES:()=>p,TaskQueue:()=>l,addZero:()=>g,debounce:()=>y,identify:()=>w,isInteger:()=>D,isPlainObject:()=>x,isPromise:()=>E,objectToUrlParams:()=>v,retry:()=>S,sleep:()=>d,throttle:()=>T,toString:()=>f,trailing:()=>N,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 S(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 T(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):N(t,e.timeout)}function N(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())}const R=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)}},k=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=[]}},z=class{constructor(t="tujiawei"){this.prefix=t}getString(t){return localStorage.getItem(this.prefix+t)}get(t){let e=this.getString(t);try{e=JSON.parse(e)}catch(t){console.warn("无法转换")}return e}set(t,e){localStorage.setItem(this.prefix+t,JSON.stringify(e))}clear(){localStorage.clear()}remove(t){localStorage.removeItem(this.prefix+t)}},I=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 L{constructor(t,e,s){this.value=t,this.prev=e,this.next=s}}const F=t=>t,A=t=>void 0!==t;class P{constructor(){this.head=new L,this.tail=new L,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 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=F){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 L(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(((s,i)=>{e=0!=i||A(e)?t(e,s,i):s})),e}reduceRight(t,e){let s=this.size()-1,i=this.tail.prev;if(!A(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=A(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 L(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 j=P;var M;!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"}(M||(M={}));class U{constructor(){this.level=M.OFF,this.listenHandle=[]}formatMessage(t,e){return`[${M[t]}] ${R.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]])}, `);1<e.length&&(e[e.length-1]=e[e.length-1].slice(0,-2)),e.push("}")}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=M.TRACE;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}info(...t){const e=M.INFO;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}debug(...t){const e=M.DEBUG;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}warn(...t){const e=M.WARN;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}error(...t){const e=M.ERROR;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}fatal(...t){const e=M.FATAL;e>=this.level&&this.publish(this.formatMessage(e,this.getParameters(t)))}}U.LOG_LEVEL=M,U.instance=new U;const $=U;return e})()})); |
{ | ||
"name": "@mrtujiawei/utils", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "把自己写的工具函数打包发布到npm上", | ||
@@ -5,0 +5,0 @@ "types": "src/types.d.ts", |
import DateTimeTool from './DateTimeTool'; | ||
import { TYPES, toString, } from './utils'; | ||
import { TYPES, toString, isInteger, } from './utils'; | ||
export enum LOG_LEVEL { ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF, }; | ||
@@ -64,5 +64,11 @@ type Listener = (message: string) => void; | ||
for (let prop in param) { | ||
result.push(`${prop} => ${this.getParameters([param[prop]])}`); | ||
if (isInteger(prop)) { | ||
result.push(`${this.getParameters([param[prop]])}, `); | ||
} else { | ||
result.push(`${prop}: ${this.getParameters([param[prop]])}, `); | ||
} | ||
} | ||
result.push(`length: ${param.length}`); | ||
result.push(']'); | ||
@@ -77,4 +83,9 @@ | ||
for (let prop in param) { | ||
result.push(`${prop} => ${this.getParameters([param[prop]])}`); | ||
result.push(`${prop} => ${this.getParameters([param[prop]])}, `); | ||
} | ||
// 移调最后多余的, | ||
if (1 < result.length) { | ||
result[result.length - 1] = result[result.length - 1].slice(0, -2); | ||
} | ||
result.push('}'); | ||
@@ -81,0 +92,0 @@ } |
@@ -216,1 +216,12 @@ const _toString = Object.prototype.toString; | ||
} | ||
/** | ||
* 是否是数字 | ||
*/ | ||
export function isInteger(number: any) { | ||
const type = toString(number); | ||
if (TYPES.NULL == type || TYPES.UNDEFINED == type) { | ||
return false; | ||
} | ||
return /^[0-9]+$/.test(number.toString()); | ||
} |
@@ -1,2 +0,2 @@ | ||
import { throttle, debounce, retry, sleep, } from '../src/index'; | ||
import { isInteger, throttle, debounce, retry, sleep, } from '../src/index'; | ||
@@ -179,1 +179,13 @@ test('Debounce should trigger one time', () => { | ||
}); | ||
test('IsInteger test', () => { | ||
expect(isInteger(1)).toBe(true); | ||
expect(isInteger('a')).toBe(false); | ||
expect(isInteger('1.1')).toBe(false); | ||
expect(isInteger(true)).toBe(false); | ||
expect(isInteger(void 0)).toBe(false); | ||
expect(isInteger(null)).toBe(false); | ||
expect(isInteger(function () {})).toBe(false); | ||
expect(isInteger(Symbol(123))).toBe(false); | ||
expect(isInteger({})).toBe(false); | ||
}); |
67412
2179