lgutils 1.3.2 → 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 +181 -94
- package/lib/lgutils.cjs.js +103 -21
- package/lib/lgutils.cjs.prod.js +1 -1
- package/lib/lgutils.esm-bundler.js +99 -22
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -1,145 +1,232 @@
|
|
|
1
|
+
export declare const accAdd: (arg1: number, arg2: number) => number
|
|
1
2
|
|
|
2
|
-
export declare const
|
|
3
|
+
export declare const accDiv: (arg1: number, arg2: number) => number
|
|
3
4
|
|
|
4
|
-
export declare const
|
|
5
|
+
export declare const accMul: (arg1: number, arg2: number) => number
|
|
5
6
|
|
|
6
|
-
export declare const
|
|
7
|
+
export declare const accSub: (arg1: number, arg2: number) => string
|
|
7
8
|
|
|
8
|
-
export declare const
|
|
9
|
+
export declare const cutTimeFn: (n: number) => string
|
|
9
10
|
|
|
10
|
-
export declare const
|
|
11
|
+
export declare const DATE_FORMAT = 'YYYY/MM/DD'
|
|
11
12
|
|
|
12
|
-
export declare
|
|
13
|
+
export declare function digitCnUppercase(n: number): string
|
|
13
14
|
|
|
14
|
-
export declare
|
|
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
|
|
22
|
+
export declare const fileReaderToBase64: (
|
|
23
|
+
target: any
|
|
24
|
+
) => (cb: (p: any) => void) => void
|
|
17
25
|
|
|
18
|
-
export declare const
|
|
26
|
+
export declare const format10k: (n: number) => string
|
|
19
27
|
|
|
20
|
-
export declare const
|
|
28
|
+
export declare const format10kNil: (d: number | NIL) => string
|
|
21
29
|
|
|
22
|
-
export declare const
|
|
30
|
+
export declare const FORMAT_DATE = 'YYYY-MM-DD'
|
|
23
31
|
|
|
24
|
-
export declare const
|
|
32
|
+
export declare const FORMAT_TIME = 'YYYY-MM-DD HH:mm:ss'
|
|
25
33
|
|
|
26
|
-
export declare const
|
|
34
|
+
export declare const formatCash: (
|
|
35
|
+
n:
|
|
36
|
+
| number
|
|
37
|
+
| {
|
|
38
|
+
valueOf(): number
|
|
39
|
+
}
|
|
40
|
+
) => string
|
|
27
41
|
|
|
28
|
-
export declare const
|
|
29
|
-
valueOf(): number;
|
|
30
|
-
}) => string;
|
|
42
|
+
export declare const formatCash2: (num: number) => string
|
|
31
43
|
|
|
32
|
-
export declare const
|
|
44
|
+
export declare const formatCashInt: (
|
|
45
|
+
n:
|
|
46
|
+
| number
|
|
47
|
+
| {
|
|
48
|
+
valueOf(): number
|
|
49
|
+
}
|
|
50
|
+
) => string
|
|
33
51
|
|
|
34
|
-
export declare const
|
|
35
|
-
|
|
36
|
-
|
|
52
|
+
export declare const formatCashIntNil: (
|
|
53
|
+
d:
|
|
54
|
+
| number
|
|
55
|
+
| {
|
|
56
|
+
valueOf(): number
|
|
57
|
+
}
|
|
58
|
+
| NIL
|
|
59
|
+
) => string
|
|
37
60
|
|
|
38
|
-
export declare const
|
|
39
|
-
|
|
40
|
-
|
|
61
|
+
export declare const formatCashNil: (
|
|
62
|
+
d:
|
|
63
|
+
| number
|
|
64
|
+
| {
|
|
65
|
+
valueOf(): number
|
|
66
|
+
}
|
|
67
|
+
| NIL
|
|
68
|
+
) => string
|
|
41
69
|
|
|
42
|
-
export declare const
|
|
43
|
-
valueOf(): number;
|
|
44
|
-
} | NIL) => string;
|
|
70
|
+
export declare const formatCreditEnhanced: (yes: boolean) => string
|
|
45
71
|
|
|
46
|
-
export declare
|
|
72
|
+
export declare function formatDate(
|
|
73
|
+
d: string | number,
|
|
74
|
+
formatType?: string
|
|
75
|
+
): string
|
|
47
76
|
|
|
48
|
-
export declare
|
|
77
|
+
export declare const formatDateNil: (
|
|
78
|
+
d: string | number | null | undefined
|
|
79
|
+
) => string
|
|
49
80
|
|
|
50
|
-
export declare
|
|
81
|
+
export declare function formatDuration(period: string): string
|
|
51
82
|
|
|
52
|
-
export declare
|
|
83
|
+
export declare const formatDurationNil: (d: string | null | undefined) => string
|
|
53
84
|
|
|
54
|
-
export declare const
|
|
85
|
+
export declare const formatRatio: (
|
|
86
|
+
n:
|
|
87
|
+
| number
|
|
88
|
+
| {
|
|
89
|
+
valueOf(): number
|
|
90
|
+
}
|
|
91
|
+
) => string
|
|
55
92
|
|
|
56
|
-
export declare const
|
|
57
|
-
|
|
58
|
-
|
|
93
|
+
export declare const formatRatioNil: (
|
|
94
|
+
d:
|
|
95
|
+
| number
|
|
96
|
+
| {
|
|
97
|
+
valueOf(): number
|
|
98
|
+
}
|
|
99
|
+
| NIL
|
|
100
|
+
) => string
|
|
59
101
|
|
|
60
|
-
export declare const
|
|
61
|
-
valueOf(): number;
|
|
62
|
-
} | NIL) => string;
|
|
102
|
+
export declare const getBase64: (file: File) => Promise<unknown>
|
|
63
103
|
|
|
64
|
-
export declare const
|
|
104
|
+
export declare const getItem: (key: string) => any
|
|
65
105
|
|
|
66
|
-
export declare const
|
|
106
|
+
export declare const getQueryObject: (url?: string) => any
|
|
67
107
|
|
|
68
|
-
export declare const
|
|
108
|
+
export declare const getSearchParams: (search: string) => URLSearchParams
|
|
69
109
|
|
|
70
|
-
export declare const
|
|
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
|
|
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
|
-
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export declare const isArray: (value: any) => boolean;
|
|
89
|
-
|
|
90
|
-
export declare const isBlob: (value: any) => any;
|
|
91
|
-
|
|
92
|
-
export declare const isDate: (value: any) => boolean;
|
|
125
|
+
[x: string]: any
|
|
126
|
+
}
|
|
93
127
|
|
|
94
|
-
export declare const
|
|
128
|
+
export declare const isArray: (value: any) => boolean
|
|
95
129
|
|
|
96
|
-
export declare const
|
|
130
|
+
export declare const isBlob: (value: any) => any
|
|
97
131
|
|
|
98
|
-
export declare const
|
|
132
|
+
export declare const isCardNo: (cardNum: any) => boolean
|
|
99
133
|
|
|
100
|
-
export declare const
|
|
134
|
+
export declare const isDate: (value: any) => boolean
|
|
101
135
|
|
|
102
|
-
export declare const
|
|
136
|
+
export declare const isEmail: (emailStr: string) => boolean
|
|
103
137
|
|
|
104
|
-
export declare const
|
|
138
|
+
export declare const isFile: (value: any) => any
|
|
105
139
|
|
|
106
|
-
declare
|
|
140
|
+
export declare const isNil: (val: any) => val is NIL
|
|
107
141
|
|
|
108
|
-
export declare const
|
|
142
|
+
export declare const isNull: (value: any) => boolean
|
|
109
143
|
|
|
110
|
-
export declare
|
|
144
|
+
export declare const isObject: (value: any) => boolean
|
|
111
145
|
|
|
112
|
-
export declare
|
|
146
|
+
export declare const isPhone: (phone: any) => boolean
|
|
113
147
|
|
|
114
|
-
export declare const
|
|
115
|
-
isTablet: boolean;
|
|
116
|
-
isPhone: boolean;
|
|
117
|
-
isAndroid: boolean;
|
|
118
|
-
isPc: boolean;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
export declare const playAudio: (mp3Id: string) => void;
|
|
122
|
-
|
|
123
|
-
export declare const random: (min: number, max: number) => number;
|
|
148
|
+
export declare const isSMSCode: (code: any) => any
|
|
124
149
|
|
|
125
|
-
export declare const
|
|
150
|
+
export declare const isUndefined: (value: any) => boolean
|
|
126
151
|
|
|
127
|
-
|
|
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
|
+
}
|
|
128
159
|
|
|
129
|
-
export declare const
|
|
160
|
+
export declare const log: (...rest: any) => void
|
|
130
161
|
|
|
131
|
-
|
|
162
|
+
declare type NIL = undefined | null | ''
|
|
132
163
|
|
|
133
|
-
export declare const
|
|
164
|
+
export declare const NIL_STRING = '\u2014\u2014'
|
|
134
165
|
|
|
135
|
-
export declare
|
|
136
|
-
[key: string]: any;
|
|
137
|
-
}>(obj: D[], property: T, type?: 'asc' | 'desc') => D[];
|
|
166
|
+
export declare function normalizeFormValues<T extends IndexType>(values: T): any
|
|
138
167
|
|
|
139
|
-
export declare
|
|
140
|
-
[key: string]: any;
|
|
141
|
-
}>(obj: D[], key: T) => number;
|
|
168
|
+
export declare function normalizeSearchParams(params: IndexType): IndexType
|
|
142
169
|
|
|
143
|
-
export declare const
|
|
144
|
-
|
|
145
|
-
|
|
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 {}
|
package/lib/lgutils.cjs.js
CHANGED
|
@@ -21,6 +21,20 @@ const inBrowser = typeof window !== 'undefined';
|
|
|
21
21
|
const trimString = (str) => {
|
|
22
22
|
return str.replace(/(^\s*)|(\s*$)/g, '');
|
|
23
23
|
};
|
|
24
|
+
const isEmail = (emailStr) => {
|
|
25
|
+
const emailPat = /^(.+)@(.+)\.(.+)$/;
|
|
26
|
+
return emailPat.test(emailStr);
|
|
27
|
+
};
|
|
28
|
+
const isCardNo = (cardNum) => {
|
|
29
|
+
const cardPat = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;
|
|
30
|
+
return cardPat.test(cardNum);
|
|
31
|
+
};
|
|
32
|
+
const isPhone = (phone) => {
|
|
33
|
+
return /^[1](\d{10})$/.test(phone);
|
|
34
|
+
};
|
|
35
|
+
const isSMSCode = (code) => {
|
|
36
|
+
return code.match(/^\d{6}$/);
|
|
37
|
+
};
|
|
24
38
|
const random = (min, max) => Math.round(Math.random() * (max - min) + min);
|
|
25
39
|
const playAudio = (mp3Id) => {
|
|
26
40
|
const audioArr = document.querySelectorAll('audio');
|
|
@@ -340,20 +354,19 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
|
|
|
340
354
|
const fn = strFileName;
|
|
341
355
|
let blob = null;
|
|
342
356
|
let fr = null;
|
|
343
|
-
const d2b = (u) => {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
};
|
|
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
|
+
// }
|
|
357
370
|
const saver = (url, winMode) => {
|
|
358
371
|
if ('download' in a) {
|
|
359
372
|
// html5 A[download]
|
|
@@ -391,9 +404,10 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
|
|
|
391
404
|
// }
|
|
392
405
|
// go ahead and download dataURLs right away
|
|
393
406
|
if (String(x).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/)) {
|
|
394
|
-
return
|
|
395
|
-
|
|
396
|
-
|
|
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
|
|
397
411
|
} // end if dataURL passed?
|
|
398
412
|
try {
|
|
399
413
|
blob =
|
|
@@ -410,10 +424,10 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
|
|
|
410
424
|
// blob = b.getBlob(m); // the blob
|
|
411
425
|
// }
|
|
412
426
|
}
|
|
413
|
-
if (navigator.msSaveBlob) {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
}
|
|
427
|
+
// if (navigator.msSaveBlob) {
|
|
428
|
+
// // IE10+ : (has Blob, but not a[download] or URL)
|
|
429
|
+
// return navigator.msSaveBlob(blob, fn)
|
|
430
|
+
// }
|
|
417
431
|
if (self.URL) {
|
|
418
432
|
// simple fast and modern way using Blob and URL:
|
|
419
433
|
// saver(self.URL.createObjectURL(blob), true);
|
|
@@ -439,10 +453,74 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
|
|
|
439
453
|
return true;
|
|
440
454
|
};
|
|
441
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
|
+
|
|
442
519
|
exports.DATE_FORMAT = DATE_FORMAT;
|
|
443
520
|
exports.FORMAT_DATE = FORMAT_DATE;
|
|
444
521
|
exports.FORMAT_TIME = FORMAT_TIME;
|
|
445
522
|
exports.NIL_STRING = NIL_STRING;
|
|
523
|
+
exports.WS = WS;
|
|
446
524
|
exports.accAdd = accAdd;
|
|
447
525
|
exports.accDiv = accDiv;
|
|
448
526
|
exports.accMul = accMul;
|
|
@@ -474,11 +552,15 @@ exports.getValueWithNil = getValueWithNil;
|
|
|
474
552
|
exports.inBrowser = inBrowser;
|
|
475
553
|
exports.isArray = isArray;
|
|
476
554
|
exports.isBlob = isBlob;
|
|
555
|
+
exports.isCardNo = isCardNo;
|
|
477
556
|
exports.isDate = isDate;
|
|
557
|
+
exports.isEmail = isEmail;
|
|
478
558
|
exports.isFile = isFile;
|
|
479
559
|
exports.isNil = isNil;
|
|
480
560
|
exports.isNull = isNull;
|
|
481
561
|
exports.isObject = isObject;
|
|
562
|
+
exports.isPhone = isPhone;
|
|
563
|
+
exports.isSMSCode = isSMSCode;
|
|
482
564
|
exports.isUndefined = isUndefined;
|
|
483
565
|
exports.log = log;
|
|
484
566
|
exports.normalizeFormValues = normalizeFormValues;
|
package/lib/lgutils.cjs.prod.js
CHANGED
|
@@ -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
|
|
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,"");
|
|
@@ -12,6 +12,20 @@ const inBrowser = typeof window !== 'undefined';
|
|
|
12
12
|
const trimString = (str) => {
|
|
13
13
|
return str.replace(/(^\s*)|(\s*$)/g, '');
|
|
14
14
|
};
|
|
15
|
+
const isEmail = (emailStr) => {
|
|
16
|
+
const emailPat = /^(.+)@(.+)\.(.+)$/;
|
|
17
|
+
return emailPat.test(emailStr);
|
|
18
|
+
};
|
|
19
|
+
const isCardNo = (cardNum) => {
|
|
20
|
+
const cardPat = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;
|
|
21
|
+
return cardPat.test(cardNum);
|
|
22
|
+
};
|
|
23
|
+
const isPhone = (phone) => {
|
|
24
|
+
return /^[1](\d{10})$/.test(phone);
|
|
25
|
+
};
|
|
26
|
+
const isSMSCode = (code) => {
|
|
27
|
+
return code.match(/^\d{6}$/);
|
|
28
|
+
};
|
|
15
29
|
const random = (min, max) => Math.round(Math.random() * (max - min) + min);
|
|
16
30
|
const playAudio = (mp3Id) => {
|
|
17
31
|
const audioArr = document.querySelectorAll('audio');
|
|
@@ -331,20 +345,19 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
|
|
|
331
345
|
const fn = strFileName;
|
|
332
346
|
let blob = null;
|
|
333
347
|
let fr = null;
|
|
334
|
-
const d2b = (u) => {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
};
|
|
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
|
+
// }
|
|
348
361
|
const saver = (url, winMode) => {
|
|
349
362
|
if ('download' in a) {
|
|
350
363
|
// html5 A[download]
|
|
@@ -382,9 +395,10 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
|
|
|
382
395
|
// }
|
|
383
396
|
// go ahead and download dataURLs right away
|
|
384
397
|
if (String(x).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/)) {
|
|
385
|
-
return
|
|
386
|
-
|
|
387
|
-
|
|
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
|
|
388
402
|
} // end if dataURL passed?
|
|
389
403
|
try {
|
|
390
404
|
blob =
|
|
@@ -401,10 +415,10 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
|
|
|
401
415
|
// blob = b.getBlob(m); // the blob
|
|
402
416
|
// }
|
|
403
417
|
}
|
|
404
|
-
if (navigator.msSaveBlob) {
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
}
|
|
418
|
+
// if (navigator.msSaveBlob) {
|
|
419
|
+
// // IE10+ : (has Blob, but not a[download] or URL)
|
|
420
|
+
// return navigator.msSaveBlob(blob, fn)
|
|
421
|
+
// }
|
|
408
422
|
if (self.URL) {
|
|
409
423
|
// simple fast and modern way using Blob and URL:
|
|
410
424
|
// saver(self.URL.createObjectURL(blob), true);
|
|
@@ -430,4 +444,67 @@ const download = (data, strFileName = 'download', strMimeType = 'application/oct
|
|
|
430
444
|
return true;
|
|
431
445
|
};
|
|
432
446
|
|
|
433
|
-
|
|
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.
|
|
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": "
|
|
38
|
+
"gitHead": "7a6605eedbd58da7e7ae5a13f7607b75cd53f618"
|
|
39
39
|
}
|