lgutils 1.3.5 → 1.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,152 +1,232 @@
1
- export declare const accAdd: (arg1: number, arg2: number) => number;
1
+ export declare const accAdd: (arg1: number, arg2: number) => number
2
2
 
3
- export declare const accDiv: (arg1: number, arg2: number) => number;
3
+ export declare const accDiv: (arg1: number, arg2: number) => number
4
4
 
5
- export declare const accMul: (arg1: number, arg2: number) => number;
5
+ export declare const accMul: (arg1: number, arg2: number) => number
6
6
 
7
- export declare const accSub: (arg1: number, arg2: number) => string;
7
+ export declare const accSub: (arg1: number, arg2: number) => string
8
8
 
9
- export declare const cutTimeFn: (n: number) => string;
9
+ export declare const cutTimeFn: (n: number) => string
10
10
 
11
- export declare const DATE_FORMAT = "YYYY/MM/DD";
11
+ export declare const DATE_FORMAT = 'YYYY/MM/DD'
12
12
 
13
- export declare function digitCnUppercase(n: number): string;
13
+ export declare function digitCnUppercase(n: number): string
14
14
 
15
- export declare const download: (data: string, strFileName?: string, strMimeType?: string, isImgUrl?: boolean | undefined) => true | undefined;
15
+ export declare const download: (
16
+ data: string,
17
+ strFileName?: string,
18
+ strMimeType?: string,
19
+ isImgUrl?: boolean | undefined
20
+ ) => true | undefined
16
21
 
17
- export declare const fileReaderToBase64: (target: any) => (cb: (p: any) => void) => void;
22
+ export declare const fileReaderToBase64: (
23
+ target: any
24
+ ) => (cb: (p: any) => void) => void
18
25
 
19
- export declare const format10k: (n: number) => string;
26
+ export declare const format10k: (n: number) => string
20
27
 
21
- export declare const format10kNil: (d: number | NIL) => string;
28
+ export declare const format10kNil: (d: number | NIL) => string
22
29
 
23
- export declare const FORMAT_DATE = "YYYY-MM-DD";
30
+ export declare const FORMAT_DATE = 'YYYY-MM-DD'
24
31
 
25
- export declare const FORMAT_TIME = "YYYY-MM-DD HH:mm:ss";
32
+ export declare const FORMAT_TIME = 'YYYY-MM-DD HH:mm:ss'
26
33
 
27
- export declare const formatCash: (n: number | {
28
- valueOf(): number;
29
- }) => string;
34
+ export declare const formatCash: (
35
+ n:
36
+ | number
37
+ | {
38
+ valueOf(): number
39
+ }
40
+ ) => string
30
41
 
31
- export declare const formatCash2: (num: number) => string;
42
+ export declare const formatCash2: (num: number) => string
32
43
 
33
- export declare const formatCashInt: (n: number | {
34
- valueOf(): number;
35
- }) => string;
44
+ export declare const formatCashInt: (
45
+ n:
46
+ | number
47
+ | {
48
+ valueOf(): number
49
+ }
50
+ ) => string
36
51
 
37
- export declare const formatCashIntNil: (d: number | {
38
- valueOf(): number;
39
- } | NIL) => string;
52
+ export declare const formatCashIntNil: (
53
+ d:
54
+ | number
55
+ | {
56
+ valueOf(): number
57
+ }
58
+ | NIL
59
+ ) => string
40
60
 
41
- export declare const formatCashNil: (d: number | {
42
- valueOf(): number;
43
- } | NIL) => string;
61
+ export declare const formatCashNil: (
62
+ d:
63
+ | number
64
+ | {
65
+ valueOf(): number
66
+ }
67
+ | NIL
68
+ ) => string
44
69
 
45
- export declare const formatCreditEnhanced: (yes: boolean) => string;
70
+ export declare const formatCreditEnhanced: (yes: boolean) => string
46
71
 
47
- export declare function formatDate(d: string | number, formatType?: string): string;
72
+ export declare function formatDate(
73
+ d: string | number,
74
+ formatType?: string
75
+ ): string
48
76
 
49
- export declare const formatDateNil: (d: string | number | null | undefined) => string;
77
+ export declare const formatDateNil: (
78
+ d: string | number | null | undefined
79
+ ) => string
50
80
 
51
- export declare function formatDuration(period: string): string;
81
+ export declare function formatDuration(period: string): string
52
82
 
53
- export declare const formatDurationNil: (d: string | null | undefined) => string;
83
+ export declare const formatDurationNil: (d: string | null | undefined) => string
54
84
 
55
- export declare const formatRatio: (n: number | {
56
- valueOf(): number;
57
- }) => string;
85
+ export declare const formatRatio: (
86
+ n:
87
+ | number
88
+ | {
89
+ valueOf(): number
90
+ }
91
+ ) => string
58
92
 
59
- export declare const formatRatioNil: (d: number | {
60
- valueOf(): number;
61
- } | NIL) => string;
93
+ export declare const formatRatioNil: (
94
+ d:
95
+ | number
96
+ | {
97
+ valueOf(): number
98
+ }
99
+ | NIL
100
+ ) => string
62
101
 
63
- export declare const getBase64: (file: File) => Promise<unknown>;
102
+ export declare const getBase64: (file: File) => Promise<unknown>
64
103
 
65
- export declare const getItem: (key: string) => any;
104
+ export declare const getItem: (key: string) => any
66
105
 
67
- export declare const getQueryObject: (url?: string) => any;
106
+ export declare const getQueryObject: (url?: string) => any
68
107
 
69
- export declare const getSearchParams: (search: string) => URLSearchParams;
108
+ export declare const getSearchParams: (search: string) => URLSearchParams
70
109
 
71
- export declare const getValOfArr: (_array: number[]) => {
72
- min: any;
73
- max: any;
74
- sum: number;
75
- average: number;
76
- mull: number;
77
- };
110
+ export declare const getValOfArr: (
111
+ _array: number[]
112
+ ) => {
113
+ min: any
114
+ max: any
115
+ sum: number
116
+ average: number
117
+ mull: number
118
+ }
119
+
120
+ export declare const getValueWithNil: (val: any) => any
78
121
 
79
- export declare const getValueWithNil: (val: any) => any;
80
-
81
- export declare const inBrowser: boolean;
122
+ export declare const inBrowser: boolean
82
123
 
83
124
  declare type IndexType = {
84
- [x: string]: any;
85
- };
86
-
87
- export declare const isArray: (value: any) => boolean;
88
-
89
- export declare const isBlob: (value: any) => any;
90
-
91
- export declare const isCardNo: (cardNum: any) => boolean;
125
+ [x: string]: any
126
+ }
92
127
 
93
- export declare const isDate: (value: any) => boolean;
128
+ export declare const isArray: (value: any) => boolean
94
129
 
95
- export declare const isEmail: (emailStr: string) => boolean;
130
+ export declare const isBlob: (value: any) => any
96
131
 
97
- export declare const isFile: (value: any) => any;
132
+ export declare const isCardNo: (cardNum: any) => boolean
98
133
 
99
- export declare const isNil: (val: any) => val is NIL;
134
+ export declare const isDate: (value: any) => boolean
100
135
 
101
- export declare const isNull: (value: any) => boolean;
136
+ export declare const isEmail: (emailStr: string) => boolean
102
137
 
103
- export declare const isObject: (value: any) => boolean;
138
+ export declare const isFile: (value: any) => any
104
139
 
105
- export declare const isPhone: (phone: any) => boolean;
140
+ export declare const isNil: (val: any) => val is NIL
106
141
 
107
- export declare const isSMSCode: (code: any) => any;
142
+ export declare const isNull: (value: any) => boolean
108
143
 
109
- export declare const isUndefined: (value: any) => boolean;
144
+ export declare const isObject: (value: any) => boolean
110
145
 
111
- export declare const log: (...rest: any) => void;
146
+ export declare const isPhone: (phone: any) => boolean
112
147
 
113
- declare type NIL = undefined | null | '';
148
+ export declare const isSMSCode: (code: any) => any
114
149
 
115
- export declare const NIL_STRING = "\u2014\u2014";
150
+ export declare const isUndefined: (value: any) => boolean
116
151
 
117
- export declare function normalizeFormValues<T extends IndexType>(values: T): any;
118
-
119
- export declare function normalizeSearchParams(params: IndexType): IndexType;
120
-
121
- export declare const os: () => {
122
- isTablet: boolean;
123
- isPhone: boolean;
124
- isAndroid: boolean;
125
- isPc: boolean;
126
- };
152
+ declare interface IWSObj {
153
+ open?: (params?: any) => any
154
+ msg: (params: any) => any
155
+ error?: (params?: any) => any
156
+ disconnect?: (params?: any) => any
157
+ close?: (params?: any) => any
158
+ }
127
159
 
128
- export declare const playAudio: (mp3Id: string) => void;
160
+ export declare const log: (...rest: any) => void
129
161
 
130
- export declare const random: (min: number, max: number) => number;
162
+ declare type NIL = undefined | null | ''
131
163
 
132
- export declare const randomString: (len?: number) => string;
164
+ export declare const NIL_STRING = '\u2014\u2014'
133
165
 
134
- export declare const removeAll: () => any;
166
+ export declare function normalizeFormValues<T extends IndexType>(values: T): any
135
167
 
136
- export declare const removeItem: (key: string) => any;
168
+ export declare function normalizeSearchParams(params: IndexType): IndexType
137
169
 
138
- export declare const setItem: (key: string, value: any) => any;
139
-
140
- export declare const setProxyObj: (data: object, getFn: () => void, setFn: () => void) => object;
141
-
142
- export declare const sortObj: <T extends keyof D, D extends {
143
- [key: string]: any;
144
- }>(obj: D[], property: T, type?: 'asc' | 'desc') => D[];
145
-
146
- export declare const sumObj: <T extends keyof D, D extends {
147
- [key: string]: any;
148
- }>(obj: D[], key: T) => number;
149
-
150
- export declare const trimString: (str: string) => string;
151
-
152
- export { }
170
+ export declare const os: () => {
171
+ isTablet: boolean
172
+ isPhone: boolean
173
+ isAndroid: boolean
174
+ isPc: boolean
175
+ }
176
+
177
+ export declare const playAudio: (mp3Id: string) => void
178
+
179
+ export declare const random: (min: number, max: number) => number
180
+
181
+ export declare const randomString: (len?: number) => string
182
+
183
+ export declare const removeAll: () => any
184
+
185
+ export declare const removeItem: (key: string) => any
186
+
187
+ export declare const setItem: (key: string, value: any) => any
188
+
189
+ export declare const setProxyObj: (
190
+ data: object,
191
+ getFn: () => void,
192
+ setFn: () => void
193
+ ) => object
194
+
195
+ export declare const sortObj: <
196
+ T extends keyof D,
197
+ D extends {
198
+ [key: string]: any
199
+ }
200
+ >(
201
+ obj: D[],
202
+ property: T,
203
+ type?: 'asc' | 'desc'
204
+ ) => D[]
205
+
206
+ export declare const sumObj: <
207
+ T extends keyof D,
208
+ D extends {
209
+ [key: string]: any
210
+ }
211
+ >(
212
+ obj: D[],
213
+ key: T
214
+ ) => number
215
+
216
+ export declare const trimString: (str: string) => string
217
+
218
+ export declare class WS {
219
+ socket: any
220
+ timer: any
221
+ ioUrl: string
222
+ private TIME_OUT
223
+ constructor(ioUrl: string, token: string)
224
+ init(obj: IWSObj): void
225
+ close(): void
226
+ start(): void
227
+ send(msg: AnyObj): void
228
+ reset(): void
229
+ getSocketState(): any
230
+ }
231
+
232
+ export {}
@@ -453,10 +453,74 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
453
453
  return true;
454
454
  };
455
455
 
456
+ class WS {
457
+ constructor(ioUrl, token) {
458
+ this.socket = null;
459
+ this.timer = null;
460
+ this.ioUrl = '';
461
+ this.TIME_OUT = 10000;
462
+ const that = this;
463
+ that.ioUrl = ioUrl;
464
+ that.socket = new WebSocket(ioUrl, token);
465
+ }
466
+ init(obj) {
467
+ const that = this;
468
+ that.socket.addEventListener('open', () => {
469
+ log('ws connect!');
470
+ that.start();
471
+ if (obj.open)
472
+ obj.open();
473
+ }, false);
474
+ that.socket.addEventListener('disconnect', () => {
475
+ console.info('[ws info]: disconnect and reconnect... ');
476
+ if (obj.disconnect)
477
+ obj.disconnect();
478
+ that.socket = new WebSocket(that.ioUrl);
479
+ }, false);
480
+ that.socket.addEventListener('error', (err) => {
481
+ console.info(`---------------`);
482
+ console.info('[ws error]: ');
483
+ console.info(err);
484
+ console.info(`---------------`);
485
+ if (obj.error)
486
+ obj.error();
487
+ }, false);
488
+ that.socket.addEventListener('close', obj.close, false);
489
+ that.socket.addEventListener('message', (res) => {
490
+ that.reset();
491
+ obj.msg(res);
492
+ }, false);
493
+ }
494
+ close() {
495
+ if (this.timer)
496
+ clearTimeout(this.timer);
497
+ console.info('[ws close]: close!');
498
+ this.socket.close();
499
+ }
500
+ start() {
501
+ const that = this;
502
+ that.timer = setTimeout(() => {
503
+ that.socket.send('ws HeartBeat!');
504
+ }, that.TIME_OUT);
505
+ }
506
+ send(msg) {
507
+ const that = this;
508
+ that.socket.send(msg);
509
+ }
510
+ reset() {
511
+ clearTimeout(this.timer);
512
+ this.start();
513
+ }
514
+ getSocketState() {
515
+ return this.socket.readyState;
516
+ }
517
+ }
518
+
456
519
  exports.DATE_FORMAT = DATE_FORMAT;
457
520
  exports.FORMAT_DATE = FORMAT_DATE;
458
521
  exports.FORMAT_TIME = FORMAT_TIME;
459
522
  exports.NIL_STRING = NIL_STRING;
523
+ exports.WS = WS;
460
524
  exports.accAdd = accAdd;
461
525
  exports.accDiv = accDiv;
462
526
  exports.accMul = accMul;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("qs"),e=require("d3-format"),r=require("lodash/omit"),o=require("moment");function a(t){return t&&"object"==typeof t&&"default"in t?t.default:t}var s=a(r),n=a(o);const i="undefined"!=typeof window,l=t=>t&&"number"==typeof t.size&&"string"==typeof t.type&&"function"==typeof t.slice,c=i?localStorage:{};let p={};const d=e.format(",.2f"),u=e.format(","),m=e.format(".2%"),f=g(d),h=g(u),x=g(m);function g(t){return e=>w(e)?S:t(e)}const y=t=>d(t/1e4),M=g(y),D=(b="增信",function(t){return t?b:"不"+b});var b;const w=t=>null==t||""===t,S="——";function Y(t,e="YYYY-MM-DD"){return n(new Date(t)).format(e)}const A=g(Y),R={d:"天",m:"个月",y:"年"};function O(t){return t?`${parseInt(t,10)}${R[t.slice(-1).toLowerCase()]}`:t}const N=g(O);exports.DATE_FORMAT="YYYY/MM/DD",exports.FORMAT_DATE="YYYY-MM-DD",exports.FORMAT_TIME="YYYY-MM-DD HH:mm:ss",exports.NIL_STRING=S,exports.accAdd=(t,e)=>{let r,o,a;try{r=t.toString().split(".")[1].length}catch(s){r=0}try{o=e.toString().split(".")[1].length}catch(s){o=0}return a=Math.pow(10,Math.max(r,o)),(t*a+e*a)/a},exports.accDiv=(t,e)=>{let r,o,a=0,s=0;try{a=t.toString().split(".")[1].length}catch(n){}try{s=e.toString().split(".")[1].length}catch(n){}return r=Number(t.toString().replace(".","")),o=Number(e.toString().replace(".","")),r/o*Math.pow(10,s-a)},exports.accMul=(t,e)=>{let r=0,o=t.toString(),a=e.toString();try{r+=o.split(".")[1].length}catch(s){}try{r+=a.split(".")[1].length}catch(s){}return Number(o.replace(".",""))*Number(a.replace(".",""))/Math.pow(10,r)},exports.accSub=function(t,e){let r,o,a,s;try{r=t.toString().split(".")[1].length}catch(n){r=0}try{o=e.toString().split(".")[1].length}catch(n){o=0}return a=Math.pow(10,Math.max(r,o)),s=r>=o?r:o,((t*a-e*a)/a).toFixed(s)},exports.cutTimeFn=t=>`00‘${0===t?"00":"0"+Math.floor(t/60)}’${0===t?"00":t%60>=10?t%60:"0"+t%60}`,exports.digitCnUppercase=function(t){var e=["角","分"],r=["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"],o=[["元","万","亿"],["","拾","佰","仟"]],a=t<0?"欠":"";t=Math.abs(t);for(var s="",n=0;n<e.length;n++)s+=(r[Math.floor(10*t*Math.pow(10,n))%10]+e[n]).replace(/零./,"");for(s=s||"整",t=Math.floor(t),n=0;n<o[0].length&&t>0;n++){for(var i="",l=0;l<o[1].length&&t>0;l++)i=r[t%10]+o[1][l]+i,t=Math.floor(t/10);s=i.replace(/(零.)*零$/,"").replace(/^$/,"零")+o[0][n]+s}return a+s.replace(/(零.)*零元/,"元").replace(/(零.)+/g,"零").replace(/^整$/,"零元整")},exports.download=(t,e="download",r="application/octet-stream",o)=>{const a=window;let s=r,n=t;const i=document,l=i.createElement("a"),c=t=>String(t),p=a.Blob||c(l),d=e;let u=null,m=null;const f=(e,r)=>{if("download"in l)return l.href=o?t:e,l.setAttribute("download",d),l.innerHTML="downloading...",i.body.appendChild(l),setTimeout((()=>{l.click(),i.body.removeChild(l),!0===r&&setTimeout((()=>{a.URL.revokeObjectURL(l.href)}),250)}),66),!0;const s=i.createElement("iframe");i.body.appendChild(s),r||(e="data:"+e.replace(/^data:([\w\/\-\+]+)/,"application/octet-stream")),s.src=e,setTimeout((()=>{i.body.removeChild(s)}),333)};if(String(n).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/))return f(n);try{u=n instanceof p?n:new p([n],{type:s})}catch(h){}if(a.URL)f(a.URL.createObjectURL(u),!0);else{if("string"==typeof u||u.constructor===c(l))try{return f("data:"+s+";base64,"+a.btoa(u))}catch(h){return f("data:"+s+","+encodeURIComponent(u))}m=new FileReader,m.onload=function(){f(this.result)},m.readAsDataURL(u)}return!0},exports.fileReaderToBase64=t=>e=>{const r=new FileReader;r.onload=t=>{e&&e(t.target.result)},r.readAsDataURL(t)},exports.format10k=y,exports.format10kNil=M,exports.formatCash=d,exports.formatCash2=t=>(Math.floor(100*t)/100).toFixed(2),exports.formatCashInt=u,exports.formatCashIntNil=h,exports.formatCashNil=f,exports.formatCreditEnhanced=D,exports.formatDate=Y,exports.formatDateNil=A,exports.formatDuration=O,exports.formatDurationNil=N,exports.formatRatio=m,exports.formatRatioNil=x,exports.getBase64=t=>new Promise(((e,r)=>{const o=new FileReader;o.readAsDataURL(t),o.onload=()=>e(o.result),o.onerror=t=>r(t)})),exports.getItem=t=>((p[t]||c.getItem(""+t))&&(p[t]=JSON.parse(c.getItem(""+t)||"null")),p[t]),exports.getQueryObject=(e=window.location.href)=>{const r=e.split("?")[1];return t.parse(r||"")},exports.getSearchParams=t=>new URLSearchParams(t),exports.getValOfArr=t=>{const e=t.length,r=t.reduce(((t,e)=>t+e),0),o=t.reduce(((t,e)=>t*e),1);return{min:Math.min.apply(null,t),max:Math.max.apply(null,t),sum:r,average:r/e,mull:o}},exports.getValueWithNil=t=>w(t)?S:t,exports.inBrowser=i,exports.isArray=t=>Array.isArray(t),exports.isBlob=l,exports.isCardNo=t=>/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/.test(t),exports.isDate=t=>t instanceof Date,exports.isEmail=t=>/^(.+)@(.+)\.(.+)$/.test(t),exports.isFile=t=>l(t)&&"string"==typeof t.name&&("object"==typeof t.lastModifiedDate||"number"==typeof t.lastModified),exports.isNil=w,exports.isNull=t=>null===t,exports.isObject=t=>t===Object(t),exports.isPhone=t=>/^[1](\d{10})$/.test(t),exports.isSMSCode=t=>t.match(/^\d{6}$/),exports.isUndefined=t=>void 0===t,exports.log=(...t)=>{console.group("====================="),console.info(...t),console.groupEnd()},exports.normalizeFormValues=function(t){return Object.entries(t).filter((([t,e])=>void 0!==e&&"all"!==e)).reduce(((t,[e,r])=>(Array.isArray(r)&&r.some((t=>n.isMoment(t)))?(r[0]&&(t.startDate=r[0].format("YYYY-MM-DD")),r[1]&&(t.endDate=r[1].format("YYYY-MM-DD"))):t[e]=n.isMoment(r)?r.format("YYYY-MM-DD"):r,t)),{})},exports.normalizeSearchParams=function(t){return(t.startDate||t.endDate)&&(t=Object.assign(Object.assign({},s(t,["startDate","endDate"])),{dateRange:[t.startDate&&n(t.startDate),t.endDate&&n(t.endDate)]})),Object.entries(t).forEach((([e,r])=>{"string"==typeof r&&r.includes(",")&&(t[e]="all")})),t},exports.os=()=>{if(!i)return{isTablet:!1,isPhone:!1,isAndroid:!1,isPc:!1};const t=navigator.userAgent,e=/(?:Windows Phone)/.test(t),r=/(?:SymbianOS)/.test(t)||e,o=/(?:Android)/.test(t),a=/(?:Firefox)/.test(t),s=/(?:iPad|PlayBook)/.test(t)||o&&!/(?:Mobile)/.test(t)||a&&/(?:Tablet)/.test(t),n=/(?:iPhone)/.test(t)&&!s;return{isTablet:s,isPhone:n,isAndroid:o,isPc:!n&&!o&&!r}},exports.playAudio=t=>{const e=document.querySelectorAll("audio");Array.from(e).forEach((t=>{t.pause(),t.currentTime=0})),document.querySelector(`#${t}`).play()},exports.random=(t,e)=>Math.round(Math.random()*(e-t)+t),exports.randomString=(t=32)=>{const e="ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",r=e.length;let o="";for(let a=0;a<t;a++)o+=e.charAt(Math.floor(Math.random()*r));return o},exports.removeAll=()=>(p={},c.clear()),exports.removeItem=t=>(delete p[t],c.removeItem(""+t)),exports.setItem=(t,e)=>(p[t]=e,c.setItem(""+t,JSON.stringify(e))),exports.setProxyObj=(t,e,r)=>new Proxy(t,{get:(t,r)=>(e(),Reflect.get(t,r)),set:(t,e,o)=>(r(),Reflect.set(t,e,o))}),exports.sortObj=(t,e,r="asc")=>t.sort(((t,o)=>"asc"===r?o[e]-t[e]:t[e]-o[e])),exports.sumObj=(t,e)=>t.reduce(((t,r)=>r[e]+t),0),exports.trimString=t=>t.replace(/(^\s*)|(\s*$)/g,"");
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("qs"),e=require("d3-format"),r=require("lodash/omit"),o=require("moment");function s(t){return t&&"object"==typeof t&&"default"in t?t.default:t}var n=s(r),a=s(o);const i=(...t)=>{console.group("====================="),console.info(...t),console.groupEnd()},c="undefined"!=typeof window,l=t=>t&&"number"==typeof t.size&&"string"==typeof t.type&&"function"==typeof t.slice,p=c?localStorage:{};let d={};const m=e.format(",.2f"),u=e.format(","),f=e.format(".2%"),h=M(m),x=M(u),g=M(f);function M(t){return e=>S(e)?Y:t(e)}const y=t=>m(t/1e4),b=M(y),D=(w="增信",function(t){return t?w:"不"+w});var w;const S=t=>null==t||""===t,Y="——";function A(t,e="YYYY-MM-DD"){return a(new Date(t)).format(e)}const T=M(A),R={d:"天",m:"个月",y:"年"};function v(t){return t?`${parseInt(t,10)}${R[t.slice(-1).toLowerCase()]}`:t}const O=M(v);exports.DATE_FORMAT="YYYY/MM/DD",exports.FORMAT_DATE="YYYY-MM-DD",exports.FORMAT_TIME="YYYY-MM-DD HH:mm:ss",exports.NIL_STRING=Y,exports.WS=class{constructor(t,e){this.socket=null,this.timer=null,this.ioUrl="",this.TIME_OUT=1e4;this.ioUrl=t,this.socket=new WebSocket(t,e)}init(t){const e=this;e.socket.addEventListener("open",(()=>{i("ws connect!"),e.start(),t.open&&t.open()}),!1),e.socket.addEventListener("disconnect",(()=>{console.info("[ws info]: disconnect and reconnect... "),t.disconnect&&t.disconnect(),e.socket=new WebSocket(e.ioUrl)}),!1),e.socket.addEventListener("error",(e=>{console.info("---------------"),console.info("[ws error]: "),console.info(e),console.info("---------------"),t.error&&t.error()}),!1),e.socket.addEventListener("close",t.close,!1),e.socket.addEventListener("message",(r=>{e.reset(),t.msg(r)}),!1)}close(){this.timer&&clearTimeout(this.timer),console.info("[ws close]: close!"),this.socket.close()}start(){const t=this;t.timer=setTimeout((()=>{t.socket.send("ws HeartBeat!")}),t.TIME_OUT)}send(t){this.socket.send(t)}reset(){clearTimeout(this.timer),this.start()}getSocketState(){return this.socket.readyState}},exports.accAdd=(t,e)=>{let r,o,s;try{r=t.toString().split(".")[1].length}catch(n){r=0}try{o=e.toString().split(".")[1].length}catch(n){o=0}return s=Math.pow(10,Math.max(r,o)),(t*s+e*s)/s},exports.accDiv=(t,e)=>{let r,o,s=0,n=0;try{s=t.toString().split(".")[1].length}catch(a){}try{n=e.toString().split(".")[1].length}catch(a){}return r=Number(t.toString().replace(".","")),o=Number(e.toString().replace(".","")),r/o*Math.pow(10,n-s)},exports.accMul=(t,e)=>{let r=0,o=t.toString(),s=e.toString();try{r+=o.split(".")[1].length}catch(n){}try{r+=s.split(".")[1].length}catch(n){}return Number(o.replace(".",""))*Number(s.replace(".",""))/Math.pow(10,r)},exports.accSub=function(t,e){let r,o,s,n;try{r=t.toString().split(".")[1].length}catch(a){r=0}try{o=e.toString().split(".")[1].length}catch(a){o=0}return s=Math.pow(10,Math.max(r,o)),n=r>=o?r:o,((t*s-e*s)/s).toFixed(n)},exports.cutTimeFn=t=>`00‘${0===t?"00":"0"+Math.floor(t/60)}’${0===t?"00":t%60>=10?t%60:"0"+t%60}`,exports.digitCnUppercase=function(t){var e=["角","分"],r=["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"],o=[["元","万","亿"],["","拾","佰","仟"]],s=t<0?"欠":"";t=Math.abs(t);for(var n="",a=0;a<e.length;a++)n+=(r[Math.floor(10*t*Math.pow(10,a))%10]+e[a]).replace(/零./,"");for(n=n||"整",t=Math.floor(t),a=0;a<o[0].length&&t>0;a++){for(var i="",c=0;c<o[1].length&&t>0;c++)i=r[t%10]+o[1][c]+i,t=Math.floor(t/10);n=i.replace(/(零.)*零$/,"").replace(/^$/,"零")+o[0][a]+n}return s+n.replace(/(零.)*零元/,"元").replace(/(零.)+/g,"零").replace(/^整$/,"零元整")},exports.download=(t,e="download",r="application/octet-stream",o)=>{const s=window;let n=r,a=t;const i=document,c=i.createElement("a"),l=t=>String(t),p=s.Blob||l(c),d=e;let m=null,u=null;const f=(e,r)=>{if("download"in c)return c.href=o?t:e,c.setAttribute("download",d),c.innerHTML="downloading...",i.body.appendChild(c),setTimeout((()=>{c.click(),i.body.removeChild(c),!0===r&&setTimeout((()=>{s.URL.revokeObjectURL(c.href)}),250)}),66),!0;const n=i.createElement("iframe");i.body.appendChild(n),r||(e="data:"+e.replace(/^data:([\w\/\-\+]+)/,"application/octet-stream")),n.src=e,setTimeout((()=>{i.body.removeChild(n)}),333)};if(String(a).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/))return f(a);try{m=a instanceof p?a:new p([a],{type:n})}catch(h){}if(s.URL)f(s.URL.createObjectURL(m),!0);else{if("string"==typeof m||m.constructor===l(c))try{return f("data:"+n+";base64,"+s.btoa(m))}catch(h){return f("data:"+n+","+encodeURIComponent(m))}u=new FileReader,u.onload=function(){f(this.result)},u.readAsDataURL(m)}return!0},exports.fileReaderToBase64=t=>e=>{const r=new FileReader;r.onload=t=>{e&&e(t.target.result)},r.readAsDataURL(t)},exports.format10k=y,exports.format10kNil=b,exports.formatCash=m,exports.formatCash2=t=>(Math.floor(100*t)/100).toFixed(2),exports.formatCashInt=u,exports.formatCashIntNil=x,exports.formatCashNil=h,exports.formatCreditEnhanced=D,exports.formatDate=A,exports.formatDateNil=T,exports.formatDuration=v,exports.formatDurationNil=O,exports.formatRatio=f,exports.formatRatioNil=g,exports.getBase64=t=>new Promise(((e,r)=>{const o=new FileReader;o.readAsDataURL(t),o.onload=()=>e(o.result),o.onerror=t=>r(t)})),exports.getItem=t=>((d[t]||p.getItem(""+t))&&(d[t]=JSON.parse(p.getItem(""+t)||"null")),d[t]),exports.getQueryObject=(e=window.location.href)=>{const r=e.split("?")[1];return t.parse(r||"")},exports.getSearchParams=t=>new URLSearchParams(t),exports.getValOfArr=t=>{const e=t.length,r=t.reduce(((t,e)=>t+e),0),o=t.reduce(((t,e)=>t*e),1);return{min:Math.min.apply(null,t),max:Math.max.apply(null,t),sum:r,average:r/e,mull:o}},exports.getValueWithNil=t=>S(t)?Y:t,exports.inBrowser=c,exports.isArray=t=>Array.isArray(t),exports.isBlob=l,exports.isCardNo=t=>/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/.test(t),exports.isDate=t=>t instanceof Date,exports.isEmail=t=>/^(.+)@(.+)\.(.+)$/.test(t),exports.isFile=t=>l(t)&&"string"==typeof t.name&&("object"==typeof t.lastModifiedDate||"number"==typeof t.lastModified),exports.isNil=S,exports.isNull=t=>null===t,exports.isObject=t=>t===Object(t),exports.isPhone=t=>/^[1](\d{10})$/.test(t),exports.isSMSCode=t=>t.match(/^\d{6}$/),exports.isUndefined=t=>void 0===t,exports.log=i,exports.normalizeFormValues=function(t){return Object.entries(t).filter((([t,e])=>void 0!==e&&"all"!==e)).reduce(((t,[e,r])=>(Array.isArray(r)&&r.some((t=>a.isMoment(t)))?(r[0]&&(t.startDate=r[0].format("YYYY-MM-DD")),r[1]&&(t.endDate=r[1].format("YYYY-MM-DD"))):t[e]=a.isMoment(r)?r.format("YYYY-MM-DD"):r,t)),{})},exports.normalizeSearchParams=function(t){return(t.startDate||t.endDate)&&(t=Object.assign(Object.assign({},n(t,["startDate","endDate"])),{dateRange:[t.startDate&&a(t.startDate),t.endDate&&a(t.endDate)]})),Object.entries(t).forEach((([e,r])=>{"string"==typeof r&&r.includes(",")&&(t[e]="all")})),t},exports.os=()=>{if(!c)return{isTablet:!1,isPhone:!1,isAndroid:!1,isPc:!1};const t=navigator.userAgent,e=/(?:Windows Phone)/.test(t),r=/(?:SymbianOS)/.test(t)||e,o=/(?:Android)/.test(t),s=/(?:Firefox)/.test(t),n=/(?:iPad|PlayBook)/.test(t)||o&&!/(?:Mobile)/.test(t)||s&&/(?:Tablet)/.test(t),a=/(?:iPhone)/.test(t)&&!n;return{isTablet:n,isPhone:a,isAndroid:o,isPc:!a&&!o&&!r}},exports.playAudio=t=>{const e=document.querySelectorAll("audio");Array.from(e).forEach((t=>{t.pause(),t.currentTime=0})),document.querySelector(`#${t}`).play()},exports.random=(t,e)=>Math.round(Math.random()*(e-t)+t),exports.randomString=(t=32)=>{const e="ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",r=e.length;let o="";for(let s=0;s<t;s++)o+=e.charAt(Math.floor(Math.random()*r));return o},exports.removeAll=()=>(d={},p.clear()),exports.removeItem=t=>(delete d[t],p.removeItem(""+t)),exports.setItem=(t,e)=>(d[t]=e,p.setItem(""+t,JSON.stringify(e))),exports.setProxyObj=(t,e,r)=>new Proxy(t,{get:(t,r)=>(e(),Reflect.get(t,r)),set:(t,e,o)=>(r(),Reflect.set(t,e,o))}),exports.sortObj=(t,e,r="asc")=>t.sort(((t,o)=>"asc"===r?o[e]-t[e]:t[e]-o[e])),exports.sumObj=(t,e)=>t.reduce(((t,r)=>r[e]+t),0),exports.trimString=t=>t.replace(/(^\s*)|(\s*$)/g,"");
@@ -444,4 +444,67 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
444
444
  return true;
445
445
  };
446
446
 
447
- export { DATE_FORMAT, FORMAT_DATE, FORMAT_TIME, NIL_STRING, accAdd, accDiv, accMul, accSub, cutTimeFn, digitCnUppercase, download, fileReaderToBase64, format10k, format10kNil, formatCash, formatCash2, formatCashInt, formatCashIntNil, formatCashNil, formatCreditEnhanced, formatDate, formatDateNil, formatDuration, formatDurationNil, formatRatio, formatRatioNil, getBase64, getItem, getQueryObject, getSearchParams, getValOfArr, getValueWithNil, inBrowser, isArray, isBlob, isCardNo, isDate, isEmail, isFile, isNil, isNull, isObject, isPhone, isSMSCode, isUndefined, log, normalizeFormValues, normalizeSearchParams, os, playAudio, random, randomString, removeAll, removeItem, setItem, setProxyObj, sortObj, sumObj, trimString };
447
+ class WS {
448
+ constructor(ioUrl, token) {
449
+ this.socket = null;
450
+ this.timer = null;
451
+ this.ioUrl = '';
452
+ this.TIME_OUT = 10000;
453
+ const that = this;
454
+ that.ioUrl = ioUrl;
455
+ that.socket = new WebSocket(ioUrl, token);
456
+ }
457
+ init(obj) {
458
+ const that = this;
459
+ that.socket.addEventListener('open', () => {
460
+ log('ws connect!');
461
+ that.start();
462
+ if (obj.open)
463
+ obj.open();
464
+ }, false);
465
+ that.socket.addEventListener('disconnect', () => {
466
+ console.info('[ws info]: disconnect and reconnect... ');
467
+ if (obj.disconnect)
468
+ obj.disconnect();
469
+ that.socket = new WebSocket(that.ioUrl);
470
+ }, false);
471
+ that.socket.addEventListener('error', (err) => {
472
+ console.info(`---------------`);
473
+ console.info('[ws error]: ');
474
+ console.info(err);
475
+ console.info(`---------------`);
476
+ if (obj.error)
477
+ obj.error();
478
+ }, false);
479
+ that.socket.addEventListener('close', obj.close, false);
480
+ that.socket.addEventListener('message', (res) => {
481
+ that.reset();
482
+ obj.msg(res);
483
+ }, false);
484
+ }
485
+ close() {
486
+ if (this.timer)
487
+ clearTimeout(this.timer);
488
+ console.info('[ws close]: close!');
489
+ this.socket.close();
490
+ }
491
+ start() {
492
+ const that = this;
493
+ that.timer = setTimeout(() => {
494
+ that.socket.send('ws HeartBeat!');
495
+ }, that.TIME_OUT);
496
+ }
497
+ send(msg) {
498
+ const that = this;
499
+ that.socket.send(msg);
500
+ }
501
+ reset() {
502
+ clearTimeout(this.timer);
503
+ this.start();
504
+ }
505
+ getSocketState() {
506
+ return this.socket.readyState;
507
+ }
508
+ }
509
+
510
+ export { DATE_FORMAT, FORMAT_DATE, FORMAT_TIME, NIL_STRING, WS, accAdd, accDiv, accMul, accSub, cutTimeFn, digitCnUppercase, download, fileReaderToBase64, format10k, format10kNil, formatCash, formatCash2, formatCashInt, formatCashIntNil, formatCashNil, formatCreditEnhanced, formatDate, formatDateNil, formatDuration, formatDurationNil, formatRatio, formatRatioNil, getBase64, getItem, getQueryObject, getSearchParams, getValOfArr, getValueWithNil, inBrowser, isArray, isBlob, isCardNo, isDate, isEmail, isFile, isNil, isNull, isObject, isPhone, isSMSCode, isUndefined, log, normalizeFormValues, normalizeSearchParams, os, playAudio, random, randomString, removeAll, removeItem, setItem, setProxyObj, sortObj, sumObj, trimString };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lgutils",
3
- "version": "1.3.5",
3
+ "version": "1.3.6",
4
4
  "description": "lgutils",
5
5
  "main": "index.js",
6
6
  "module": "lib/lgutils.cjs.prod.js",
@@ -35,5 +35,5 @@
35
35
  "@types/moment": "^2.13.0",
36
36
  "@types/qs": "^6.9.0"
37
37
  },
38
- "gitHead": "35e3d5499dce983ef12a69119674dd64416567ba"
38
+ "gitHead": "7a6605eedbd58da7e7ae5a13f7607b75cd53f618"
39
39
  }