lgutils 1.3.3 → 1.3.7

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,153 +1,232 @@
1
+ export declare const accAdd: (arg1: number, arg2: number) => number
1
2
 
2
- export declare const accAdd: (arg1: number, arg2: number) => number;
3
+ export declare const accDiv: (arg1: number, arg2: number) => number
3
4
 
4
- export declare const accDiv: (arg1: number, arg2: number) => number;
5
+ export declare const accMul: (arg1: number, arg2: number) => number
5
6
 
6
- export declare const accMul: (arg1: number, arg2: number) => number;
7
+ export declare const accSub: (arg1: number, arg2: number) => string
7
8
 
8
- export declare const accSub: (arg1: number, arg2: number) => string;
9
+ export declare const cutTimeFn: (n: number) => string
9
10
 
10
- export declare const cutTimeFn: (n: number) => string;
11
+ export declare const DATE_FORMAT = 'YYYY/MM/DD'
11
12
 
12
- export declare const DATE_FORMAT = "YYYY/MM/DD";
13
+ export declare function digitCnUppercase(n: number): string
13
14
 
14
- export declare function digitCnUppercase(n: number): string;
15
+ export declare const download: (
16
+ data: string,
17
+ strFileName?: string,
18
+ strMimeType?: string,
19
+ isImgUrl?: boolean | undefined
20
+ ) => true | undefined
15
21
 
16
- export declare const download: (data: string, strFileName?: string, strMimeType?: string, isImgUrl?: boolean | undefined) => boolean | undefined;
22
+ export declare const fileReaderToBase64: (
23
+ target: any
24
+ ) => (cb: (p: any) => void) => void
17
25
 
18
- export declare const fileReaderToBase64: (target: any) => (cb: (p: any) => void) => void;
26
+ export declare const format10k: (n: number) => string
19
27
 
20
- export declare const format10k: (n: number) => string;
28
+ export declare const format10kNil: (d: number | NIL) => string
21
29
 
22
- export declare const format10kNil: (d: number | NIL) => string;
30
+ export declare const FORMAT_DATE = 'YYYY-MM-DD'
23
31
 
24
- export declare const FORMAT_DATE = "YYYY-MM-DD";
32
+ export declare const FORMAT_TIME = 'YYYY-MM-DD HH:mm:ss'
25
33
 
26
- export declare const FORMAT_TIME = "YYYY-MM-DD HH:mm:ss";
34
+ export declare const formatCash: (
35
+ n:
36
+ | number
37
+ | {
38
+ valueOf(): number
39
+ }
40
+ ) => string
27
41
 
28
- export declare const formatCash: (n: number | {
29
- valueOf(): number;
30
- }) => string;
42
+ export declare const formatCash2: (num: number) => string
31
43
 
32
- export declare const formatCash2: (num: number) => string;
44
+ export declare const formatCashInt: (
45
+ n:
46
+ | number
47
+ | {
48
+ valueOf(): number
49
+ }
50
+ ) => string
33
51
 
34
- export declare const formatCashInt: (n: number | {
35
- valueOf(): number;
36
- }) => string;
52
+ export declare const formatCashIntNil: (
53
+ d:
54
+ | number
55
+ | {
56
+ valueOf(): number
57
+ }
58
+ | NIL
59
+ ) => string
37
60
 
38
- export declare const formatCashIntNil: (d: number | {
39
- valueOf(): number;
40
- } | NIL) => string;
61
+ export declare const formatCashNil: (
62
+ d:
63
+ | number
64
+ | {
65
+ valueOf(): number
66
+ }
67
+ | NIL
68
+ ) => string
41
69
 
42
- export declare const formatCashNil: (d: number | {
43
- valueOf(): number;
44
- } | NIL) => string;
70
+ export declare const formatCreditEnhanced: (yes: boolean) => string
45
71
 
46
- export declare const formatCreditEnhanced: (yes: boolean) => string;
72
+ export declare function formatDate(
73
+ d: string | number,
74
+ formatType?: string
75
+ ): string
47
76
 
48
- export declare function formatDate(d: string | number, formatType?: string): string;
77
+ export declare const formatDateNil: (
78
+ d: string | number | null | undefined
79
+ ) => string
49
80
 
50
- export declare const formatDateNil: (d: string | number | null | undefined) => string;
81
+ export declare function formatDuration(period: string): string
51
82
 
52
- export declare function formatDuration(period: string): string;
83
+ export declare const formatDurationNil: (d: string | null | undefined) => string
53
84
 
54
- export declare const formatDurationNil: (d: string | null | undefined) => string;
85
+ export declare const formatRatio: (
86
+ n:
87
+ | number
88
+ | {
89
+ valueOf(): number
90
+ }
91
+ ) => string
55
92
 
56
- export declare const formatRatio: (n: number | {
57
- valueOf(): number;
58
- }) => string;
93
+ export declare const formatRatioNil: (
94
+ d:
95
+ | number
96
+ | {
97
+ valueOf(): number
98
+ }
99
+ | NIL
100
+ ) => string
59
101
 
60
- export declare const formatRatioNil: (d: number | {
61
- valueOf(): number;
62
- } | NIL) => string;
102
+ export declare const getBase64: (file: File) => Promise<unknown>
63
103
 
64
- export declare const getBase64: (file: File) => Promise<unknown>;
104
+ export declare const getItem: (key: string) => any
65
105
 
66
- export declare const getItem: (key: string) => any;
106
+ export declare const getQueryObject: (url?: string) => any
67
107
 
68
- export declare const getQueryObject: (url?: string) => any;
108
+ export declare const getSearchParams: (search: string) => URLSearchParams
69
109
 
70
- export declare const getSearchParams: (search: string) => URLSearchParams;
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
71
121
 
72
- export declare const getValOfArr: (_array: number[]) => {
73
- min: any;
74
- max: any;
75
- sum: number;
76
- average: number;
77
- mull: number;
78
- };
79
-
80
- export declare const getValueWithNil: (val: any) => any;
81
-
82
- export declare const inBrowser: boolean;
122
+ export declare const inBrowser: boolean
83
123
 
84
124
  declare type IndexType = {
85
- [x: string]: any;
86
- };
87
-
88
- export declare const isArray: (value: any) => boolean;
89
-
90
- export declare const isBlob: (value: any) => any;
91
-
92
- export declare const isCardNo: (cardNum: any) => boolean;
93
-
94
- export declare const isDate: (value: any) => boolean;
95
-
96
- export declare const isEmail: (emailStr: string) => boolean;
125
+ [x: string]: any
126
+ }
97
127
 
98
- export declare const isFile: (value: any) => any;
128
+ export declare const isArray: (value: any) => boolean
99
129
 
100
- export declare const isNil: (val: any) => val is NIL;
130
+ export declare const isBlob: (value: any) => any
101
131
 
102
- export declare const isNull: (value: any) => boolean;
132
+ export declare const isCardNo: (cardNum: any) => boolean
103
133
 
104
- export declare const isObject: (value: any) => boolean;
134
+ export declare const isDate: (value: any) => boolean
105
135
 
106
- export declare const isPhone: (phone: any) => boolean;
136
+ export declare const isEmail: (emailStr: string) => boolean
107
137
 
108
- export declare const isSMSCode: (code: any) => any;
138
+ export declare const isFile: (value: any) => any
109
139
 
110
- export declare const isUndefined: (value: any) => boolean;
140
+ export declare const isNil: (val: any) => val is NIL
111
141
 
112
- export declare const log: (...rest: any) => void;
142
+ export declare const isNull: (value: any) => boolean
113
143
 
114
- declare type NIL = undefined | null | '';
144
+ export declare const isObject: (value: any) => boolean
115
145
 
116
- export declare const NIL_STRING = "\u2014\u2014";
146
+ export declare const isPhone: (phone: any) => boolean
117
147
 
118
- export declare function normalizeFormValues<T extends IndexType>(values: T): any;
148
+ export declare const isSMSCode: (code: any) => any
119
149
 
120
- export declare function normalizeSearchParams(params: IndexType): IndexType;
150
+ export declare const isUndefined: (value: any) => boolean
121
151
 
122
- export declare const os: () => {
123
- isTablet: boolean;
124
- isPhone: boolean;
125
- isAndroid: boolean;
126
- isPc: boolean;
127
- };
128
-
129
- export declare const playAudio: (mp3Id: string) => void;
130
-
131
- export declare const random: (min: number, max: number) => number;
132
-
133
- export declare const randomString: (len?: number) => string;
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
+ }
134
159
 
135
- export declare const removeAll: () => any;
160
+ export declare const log: (...rest: any) => void
136
161
 
137
- export declare const removeItem: (key: string) => any;
162
+ declare type NIL = undefined | null | ''
138
163
 
139
- export declare const setItem: (key: string, value: any) => any;
164
+ export declare const NIL_STRING = '\u2014\u2014'
140
165
 
141
- export declare const setProxyObj: (data: object, getFn: () => void, setFn: () => void) => object;
166
+ export declare function normalizeFormValues<T extends IndexType>(values: T): any
142
167
 
143
- export declare const sortObj: <T extends keyof D, D extends {
144
- [key: string]: any;
145
- }>(obj: D[], property: T, type?: 'asc' | 'desc') => D[];
168
+ export declare function normalizeSearchParams(params: IndexType): IndexType
146
169
 
147
- export declare const sumObj: <T extends keyof D, D extends {
148
- [key: string]: any;
149
- }>(obj: D[], key: T) => number;
150
-
151
- export declare const trimString: (str: string) => string;
152
-
153
- 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 {}
@@ -354,20 +354,19 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
354
354
  const fn = strFileName;
355
355
  let blob = null;
356
356
  let fr = null;
357
- const d2b = (u) => {
358
- const p = u.split(/[:;,]/);
359
- const t = p[1];
360
- const dec = p[2] === 'base64' ? atob : decodeURIComponent;
361
- const bin = dec(p.pop());
362
- const mx = bin.length;
363
- let i = 0;
364
- const uia = new Uint8Array(mx);
365
- for (i; i < mx; ++i)
366
- uia[i] = bin.charCodeAt(i);
367
- return new B([uia], {
368
- type: t
369
- });
370
- };
357
+ // const d2b = (u: any) => {
358
+ // const p = u.split(/[:;,]/)
359
+ // const t = p[1]
360
+ // const dec = p[2] === 'base64' ? atob : decodeURIComponent
361
+ // const bin = dec(p.pop())
362
+ // const mx = bin.length
363
+ // let i = 0
364
+ // const uia = new Uint8Array(mx)
365
+ // for (i; i < mx; ++i) uia[i] = bin.charCodeAt(i)
366
+ // return new B([uia], {
367
+ // type: t
368
+ // })
369
+ // }
371
370
  const saver = (url, winMode) => {
372
371
  if ('download' in a) {
373
372
  // html5 A[download]
@@ -405,9 +404,10 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
405
404
  // }
406
405
  // go ahead and download dataURLs right away
407
406
  if (String(x).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/)) {
408
- return navigator.msSaveBlob // IE10 can't do a[download], only Blobs:
409
- ? navigator.msSaveBlob(d2b(x), fn)
410
- : saver(x); // everyone else can save dataURLs un-processed
407
+ return saver(x);
408
+ // return navigator?.msSaveBlob // IE10 can't do a[download], only Blobs:
409
+ // ? navigator?.msSaveBlob(d2b(x), fn)
410
+ // : saver(x) // everyone else can save dataURLs un-processed
411
411
  } // end if dataURL passed?
412
412
  try {
413
413
  blob =
@@ -424,10 +424,10 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
424
424
  // blob = b.getBlob(m); // the blob
425
425
  // }
426
426
  }
427
- if (navigator.msSaveBlob) {
428
- // IE10+ : (has Blob, but not a[download] or URL)
429
- return navigator.msSaveBlob(blob, fn);
430
- }
427
+ // if (navigator.msSaveBlob) {
428
+ // // IE10+ : (has Blob, but not a[download] or URL)
429
+ // return navigator.msSaveBlob(blob, fn)
430
+ // }
431
431
  if (self.URL) {
432
432
  // simple fast and modern way using Blob and URL:
433
433
  // saver(self.URL.createObjectURL(blob), true);
@@ -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 = token ? new WebSocket(ioUrl, token) : new WebSocket(ioUrl);
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"),m=e.format(","),u=e.format(".2%"),f=g(d),h=g(m),x=g(u);function g(t){return e=>w(e)?S:t(e)}const y=t=>d(t/1e4),M=g(y),b=(D="增信",function(t){return t?D:"不"+D});var D;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),v={d:"天",m:"个月",y:"年"};function R(t){return t?`${parseInt(t,10)}${v[t.slice(-1).toLowerCase()]}`:t}const O=g(R);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 m=null,u=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 navigator.msSaveBlob?navigator.msSaveBlob((t=>{const e=t.split(/[:;,]/),r=e[1],o=("base64"===e[2]?atob:decodeURIComponent)(e.pop()),a=o.length;let s=0;const n=new Uint8Array(a);for(;s<a;++s)n[s]=o.charCodeAt(s);return new p([n],{type:r})})(n),d):f(n);try{m=n instanceof p?n:new p([n],{type:s})}catch(h){}if(navigator.msSaveBlob)return navigator.msSaveBlob(m,d);if(a.URL)f(a.URL.createObjectURL(m),!0);else{if("string"==typeof m||m.constructor===c(l))try{return f("data:"+s+";base64,"+a.btoa(m))}catch(h){return f("data:"+s+","+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=M,exports.formatCash=d,exports.formatCash2=t=>(Math.floor(100*t)/100).toFixed(2),exports.formatCashInt=m,exports.formatCashIntNil=h,exports.formatCashNil=f,exports.formatCreditEnhanced=b,exports.formatDate=Y,exports.formatDateNil=A,exports.formatDuration=R,exports.formatDurationNil=O,exports.formatRatio=u,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),w=(D="增信",function(t){return t?D:"不"+D});var D;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 k=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=e?new WebSocket(t,e):new WebSocket(t)}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=w,exports.formatDate=A,exports.formatDateNil=T,exports.formatDuration=v,exports.formatDurationNil=k,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,"");
@@ -345,20 +345,19 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
345
345
  const fn = strFileName;
346
346
  let blob = null;
347
347
  let fr = null;
348
- const d2b = (u) => {
349
- const p = u.split(/[:;,]/);
350
- const t = p[1];
351
- const dec = p[2] === 'base64' ? atob : decodeURIComponent;
352
- const bin = dec(p.pop());
353
- const mx = bin.length;
354
- let i = 0;
355
- const uia = new Uint8Array(mx);
356
- for (i; i < mx; ++i)
357
- uia[i] = bin.charCodeAt(i);
358
- return new B([uia], {
359
- type: t
360
- });
361
- };
348
+ // const d2b = (u: any) => {
349
+ // const p = u.split(/[:;,]/)
350
+ // const t = p[1]
351
+ // const dec = p[2] === 'base64' ? atob : decodeURIComponent
352
+ // const bin = dec(p.pop())
353
+ // const mx = bin.length
354
+ // let i = 0
355
+ // const uia = new Uint8Array(mx)
356
+ // for (i; i < mx; ++i) uia[i] = bin.charCodeAt(i)
357
+ // return new B([uia], {
358
+ // type: t
359
+ // })
360
+ // }
362
361
  const saver = (url, winMode) => {
363
362
  if ('download' in a) {
364
363
  // html5 A[download]
@@ -396,9 +395,10 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
396
395
  // }
397
396
  // go ahead and download dataURLs right away
398
397
  if (String(x).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/)) {
399
- return navigator.msSaveBlob // IE10 can't do a[download], only Blobs:
400
- ? navigator.msSaveBlob(d2b(x), fn)
401
- : saver(x); // everyone else can save dataURLs un-processed
398
+ return saver(x);
399
+ // return navigator?.msSaveBlob // IE10 can't do a[download], only Blobs:
400
+ // ? navigator?.msSaveBlob(d2b(x), fn)
401
+ // : saver(x) // everyone else can save dataURLs un-processed
402
402
  } // end if dataURL passed?
403
403
  try {
404
404
  blob =
@@ -415,10 +415,10 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
415
415
  // blob = b.getBlob(m); // the blob
416
416
  // }
417
417
  }
418
- if (navigator.msSaveBlob) {
419
- // IE10+ : (has Blob, but not a[download] or URL)
420
- return navigator.msSaveBlob(blob, fn);
421
- }
418
+ // if (navigator.msSaveBlob) {
419
+ // // IE10+ : (has Blob, but not a[download] or URL)
420
+ // return navigator.msSaveBlob(blob, fn)
421
+ // }
422
422
  if (self.URL) {
423
423
  // simple fast and modern way using Blob and URL:
424
424
  // saver(self.URL.createObjectURL(blob), true);
@@ -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 = token ? new WebSocket(ioUrl, token) : new WebSocket(ioUrl);
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.3",
3
+ "version": "1.3.7",
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": "e94fc5bc6a95ca90393043a3ff8437bb493b1d0f"
38
+ "gitHead": "822648687042bfd31f3d37c64842bc9988f06fc6"
39
39
  }