mazey 3.5.3 → 3.6.0
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/README.md +226 -178
- package/README_CN.md +4 -2
- package/lib/index.cjs.js +3619 -2756
- package/lib/index.d.ts +286 -42
- package/lib/index.d.ts.map +1 -1
- package/lib/index.esm.js +3614 -2757
- package/lib/mazey.min.js +2 -2
- package/package.json +4 -3
- package/scripts/env.sh +2 -0
- package/src/index.ts +343 -48
- package/src/preview.js +0 -44
package/src/index.ts
CHANGED
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
* @param {string} aStr String
|
|
23
23
|
* @param {string} bStr String
|
|
24
24
|
* @returns {number} Length
|
|
25
|
+
* @category Calculate and Formula
|
|
25
26
|
*/
|
|
26
27
|
export function calLongestCommonSubstring(aStr: string, bStr: string): number {
|
|
27
28
|
const aLen = aStr.length;
|
|
@@ -66,6 +67,7 @@ export function calLongestCommonSubstring(aStr: string, bStr: string): number {
|
|
|
66
67
|
* @param {string} aStr 字符串
|
|
67
68
|
* @param {string} bStr 字符串
|
|
68
69
|
* @returns {number} 长度
|
|
70
|
+
* @category Calculate and Formula
|
|
69
71
|
*/
|
|
70
72
|
export function calLongestCommonSubsequence(
|
|
71
73
|
aStr: string,
|
|
@@ -114,6 +116,7 @@ export function calLongestCommonSubsequence(
|
|
|
114
116
|
*
|
|
115
117
|
* @param {string} param Query param.
|
|
116
118
|
* @returns {string} value
|
|
119
|
+
* @category URL
|
|
117
120
|
*/
|
|
118
121
|
export function getQueryParam(param: string): string {
|
|
119
122
|
const reg = new RegExp('(^|&)' + param + '=([^&]*)(&|$)');
|
|
@@ -136,6 +139,7 @@ export function getQueryParam(param: string): string {
|
|
|
136
139
|
* @param {string} url URL string.
|
|
137
140
|
* @param {string} param Query param.
|
|
138
141
|
* @returns {string} value
|
|
142
|
+
* @category URL
|
|
139
143
|
*/
|
|
140
144
|
export function getUrlParam(url: string, param: string): string | string[] {
|
|
141
145
|
const result: {
|
|
@@ -179,6 +183,7 @@ export function getUrlParam(url: string, param: string): string | string[] {
|
|
|
179
183
|
* @param {string} param Query param.
|
|
180
184
|
* @param {string} value Param's value.
|
|
181
185
|
* @returns {string} URL.
|
|
186
|
+
* @category URL
|
|
182
187
|
*/
|
|
183
188
|
export function updateQueryParam(
|
|
184
189
|
url: string,
|
|
@@ -215,6 +220,7 @@ export function updateQueryParam(
|
|
|
215
220
|
*
|
|
216
221
|
* @param {string} param Query param.
|
|
217
222
|
* @returns {string} value
|
|
223
|
+
* @category URL
|
|
218
224
|
*/
|
|
219
225
|
export function getHashQueryParam(param: string): string {
|
|
220
226
|
const hashs = location.hash.split('?');
|
|
@@ -245,6 +251,7 @@ export function getHashQueryParam(param: string): string {
|
|
|
245
251
|
*
|
|
246
252
|
* @param {string} url
|
|
247
253
|
* @param {array} rules Object.keys(location), ['href', 'protocol', 'host', 'hostname', 'port', 'pathname', 'search', 'hash'], ['hostname', 'pathname'] = 'km.mazey.net/plugins/servlet/mobile'
|
|
254
|
+
* @category URL
|
|
248
255
|
*/
|
|
249
256
|
export function getDomain(url: string, rules = ['hostname']): string {
|
|
250
257
|
const aEl: any = document.createElement('a');
|
|
@@ -265,6 +272,7 @@ export function getDomain(url: string, rules = ['hostname']): string {
|
|
|
265
272
|
*
|
|
266
273
|
* @param {string} camelCase 'aBC' or 'ABC'
|
|
267
274
|
* @returns {string} 'a-b-c'
|
|
275
|
+
* @category Util
|
|
268
276
|
*/
|
|
269
277
|
export function camelCaseToKebabCase(camelCase: string): string {
|
|
270
278
|
const kebabCase = camelCase.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
@@ -281,6 +289,7 @@ export function camelCaseToKebabCase(camelCase: string): string {
|
|
|
281
289
|
*
|
|
282
290
|
* @param {string} camelCase 'aBC' or 'ABC'
|
|
283
291
|
* @returns {string} 'a_b_c'
|
|
292
|
+
* @category Util
|
|
284
293
|
*/
|
|
285
294
|
export function camelCase2Underscore(camelCase: string): string {
|
|
286
295
|
const kebabCase = camelCase.replace(/([A-Z])/g, '_$1').toLowerCase();
|
|
@@ -297,6 +306,7 @@ export function camelCase2Underscore(camelCase: string): string {
|
|
|
297
306
|
*
|
|
298
307
|
* @param {string} str The string to trim.
|
|
299
308
|
* @returns {string} Trimmed string.
|
|
309
|
+
* @category Util
|
|
300
310
|
*/
|
|
301
311
|
export function mTrim(str: string): string {
|
|
302
312
|
str = str.replace(/^\s+/, ''); // 去除头部空格
|
|
@@ -309,10 +319,16 @@ export function mTrim(str: string): string {
|
|
|
309
319
|
}
|
|
310
320
|
|
|
311
321
|
/**
|
|
312
|
-
*
|
|
313
|
-
*
|
|
322
|
+
* Make a newline of HTML.
|
|
323
|
+
*
|
|
324
|
+
* ```
|
|
325
|
+
* newLine('a\nb\nc'); // 'a<br />b<br />c'
|
|
326
|
+
* newLine('a\n\nbc'); // 'a<br /><br />bc'
|
|
327
|
+
* ```
|
|
328
|
+
*
|
|
314
329
|
* @param {string} str The string to make a newline.
|
|
315
330
|
* @returns {string} A newline with `br`.
|
|
331
|
+
* @category DOM
|
|
316
332
|
*/
|
|
317
333
|
export function newLine(str: string): string {
|
|
318
334
|
if (!str) {
|
|
@@ -341,6 +357,7 @@ export function newLine(str: string): string {
|
|
|
341
357
|
*
|
|
342
358
|
* @param {object} obj The value to clone.
|
|
343
359
|
* @returns {object} Returns the deep cloned value.
|
|
360
|
+
* @category Util
|
|
344
361
|
*/
|
|
345
362
|
export function deepCopyObject(obj: any): any {
|
|
346
363
|
return JSON.parse(JSON.stringify(obj));
|
|
@@ -362,6 +379,10 @@ export function deepCopyObject(obj: any): any {
|
|
|
362
379
|
* false
|
|
363
380
|
* true
|
|
364
381
|
* ```
|
|
382
|
+
*
|
|
383
|
+
* @param {string} str The string to check.
|
|
384
|
+
* @returns {boolean} Return the result of checking.
|
|
385
|
+
* @category Util
|
|
365
386
|
*/
|
|
366
387
|
export function isJsonString(str: string): boolean {
|
|
367
388
|
try {
|
|
@@ -375,10 +396,16 @@ export function isJsonString(str: string): boolean {
|
|
|
375
396
|
}
|
|
376
397
|
|
|
377
398
|
/**
|
|
378
|
-
*
|
|
379
|
-
*
|
|
399
|
+
* Produce a random string of number, `generateRndNum(7)` => '7658495'.
|
|
400
|
+
*
|
|
401
|
+
* ```
|
|
402
|
+
* generateRndNum(4); // '9730'
|
|
403
|
+
* generateRndNum(7); // '2262490'
|
|
404
|
+
* ```
|
|
405
|
+
*
|
|
380
406
|
* @param {number} n Length
|
|
381
407
|
* @returns {string} Return the random string.
|
|
408
|
+
* @category Util
|
|
382
409
|
*/
|
|
383
410
|
export function generateRndNum(n = 5): string {
|
|
384
411
|
let ret = '';
|
|
@@ -438,7 +465,20 @@ export function cancelBubble(e: any): void {
|
|
|
438
465
|
}
|
|
439
466
|
|
|
440
467
|
/**
|
|
441
|
-
*
|
|
468
|
+
* Modify `class`.
|
|
469
|
+
*
|
|
470
|
+
* ```
|
|
471
|
+
* const dom = document.querySelector('#box');
|
|
472
|
+
*
|
|
473
|
+
* // Determine `class`
|
|
474
|
+
* hasClass(dom, 'test');
|
|
475
|
+
* // Add `class`
|
|
476
|
+
* addClass(dom, 'test');
|
|
477
|
+
* // Remove `class`
|
|
478
|
+
* removeClass(dom, 'test');
|
|
479
|
+
* ```
|
|
480
|
+
*
|
|
481
|
+
* @category DOM
|
|
442
482
|
*/
|
|
443
483
|
export function hasClass(obj: any, cls: string): boolean {
|
|
444
484
|
const oriCls = obj.className; // 获取对象的class值
|
|
@@ -452,7 +492,20 @@ export function hasClass(obj: any, cls: string): boolean {
|
|
|
452
492
|
}
|
|
453
493
|
|
|
454
494
|
/**
|
|
455
|
-
*
|
|
495
|
+
* Modify `class`.
|
|
496
|
+
*
|
|
497
|
+
* ```
|
|
498
|
+
* const dom = document.querySelector('#box');
|
|
499
|
+
*
|
|
500
|
+
* // Determine `class`
|
|
501
|
+
* hasClass(dom, 'test');
|
|
502
|
+
* // Add `class`
|
|
503
|
+
* addClass(dom, 'test');
|
|
504
|
+
* // Remove `class`
|
|
505
|
+
* removeClass(dom, 'test');
|
|
506
|
+
* ```
|
|
507
|
+
*
|
|
508
|
+
* @category DOM
|
|
456
509
|
*/
|
|
457
510
|
export function addClass(obj: any, cls: string): void {
|
|
458
511
|
const oriCls = obj.className;
|
|
@@ -466,7 +519,20 @@ export function addClass(obj: any, cls: string): void {
|
|
|
466
519
|
}
|
|
467
520
|
|
|
468
521
|
/**
|
|
469
|
-
*
|
|
522
|
+
* Modify `class`.
|
|
523
|
+
*
|
|
524
|
+
* ```
|
|
525
|
+
* const dom = document.querySelector('#box');
|
|
526
|
+
*
|
|
527
|
+
* // Determine `class`
|
|
528
|
+
* hasClass(dom, 'test');
|
|
529
|
+
* // Add `class`
|
|
530
|
+
* addClass(dom, 'test');
|
|
531
|
+
* // Remove `class`
|
|
532
|
+
* removeClass(dom, 'test');
|
|
533
|
+
* ```
|
|
534
|
+
*
|
|
535
|
+
* @category DOM
|
|
470
536
|
*/
|
|
471
537
|
export function removeClass(obj: any, cls: string): void {
|
|
472
538
|
const oriCls = obj.className;
|
|
@@ -490,6 +556,8 @@ export function removeClass(obj: any, cls: string): void {
|
|
|
490
556
|
* ```
|
|
491
557
|
*
|
|
492
558
|
* Reference: [Lodash](https://lodash.com/docs/4.17.15#throttle)
|
|
559
|
+
*
|
|
560
|
+
* @category Util
|
|
493
561
|
*/
|
|
494
562
|
export function throttle(
|
|
495
563
|
func: any,
|
|
@@ -503,9 +571,6 @@ export function throttle(
|
|
|
503
571
|
let args: any = null;
|
|
504
572
|
let timeout: any = null;
|
|
505
573
|
let [result, previous] = [null, 0];
|
|
506
|
-
// if (!options) {
|
|
507
|
-
// options = {};
|
|
508
|
-
// }
|
|
509
574
|
const later = function() {
|
|
510
575
|
previous = options.leading === false ? 0 : mNow();
|
|
511
576
|
timeout = null;
|
|
@@ -549,6 +614,8 @@ export function throttle(
|
|
|
549
614
|
* console.log('The debounced function will only be invoked in 1000 milliseconds, the other invoking will disappear during the wait time.');
|
|
550
615
|
* }, 1000, true);
|
|
551
616
|
* ```
|
|
617
|
+
*
|
|
618
|
+
* @category Util
|
|
552
619
|
*/
|
|
553
620
|
export function debounce(func: any, wait: number, immediate?: any): any {
|
|
554
621
|
let context: any = null;
|
|
@@ -647,6 +714,7 @@ export function friendlyInterval(
|
|
|
647
714
|
* @param {boolean} options.isNaNAsNumber 是否 NaN 算数字(默认不算)
|
|
648
715
|
* @param {boolean} options.isUnFiniteAsNumber 是否 无限 算数字(默认不算)
|
|
649
716
|
* @returns {boolean} true 是数字
|
|
717
|
+
* @category Util
|
|
650
718
|
*/
|
|
651
719
|
export function isNumber(
|
|
652
720
|
num: any,
|
|
@@ -691,10 +759,30 @@ export function doFn(fn: any, ...params: any[]): any {
|
|
|
691
759
|
}
|
|
692
760
|
|
|
693
761
|
/**
|
|
694
|
-
*
|
|
695
|
-
*
|
|
762
|
+
* EN: Handle Storage (Keep fit for JSON, it can tansfer format automatically).
|
|
763
|
+
*
|
|
764
|
+
* ZH: 存储/获取数据到 sessionStorage/localStorage
|
|
765
|
+
*
|
|
766
|
+
* ```
|
|
767
|
+
* setSessionStorage('test', '123');
|
|
768
|
+
* getSessionStorage('test'); // 123
|
|
769
|
+
* setLocalStorage('test', '123');
|
|
770
|
+
* getLocalStorage('test'); // 123
|
|
771
|
+
*
|
|
772
|
+
* // or package in usage
|
|
773
|
+
* const projectName = 'mazey';
|
|
774
|
+
* function mSetLocalStorage (key, value) {
|
|
775
|
+
* return setLocalStorage(`${projectName}_${key}`, value);
|
|
776
|
+
* }
|
|
777
|
+
*
|
|
778
|
+
* function mGetLocalStorage (key) {
|
|
779
|
+
* return getLocalStorage(`${projectName}_${key}`);
|
|
780
|
+
* }
|
|
781
|
+
* ```
|
|
782
|
+
*
|
|
696
783
|
* @param {string} key 键
|
|
697
|
-
* @
|
|
784
|
+
* @returns {any} 返回值
|
|
785
|
+
* @category Cache Data
|
|
698
786
|
*/
|
|
699
787
|
export function setSessionStorage(key: string, value: any = null): void {
|
|
700
788
|
if (key) {
|
|
@@ -703,10 +791,30 @@ export function setSessionStorage(key: string, value: any = null): void {
|
|
|
703
791
|
}
|
|
704
792
|
|
|
705
793
|
/**
|
|
706
|
-
*
|
|
707
|
-
*
|
|
794
|
+
* EN: Handle Storage (Keep fit for JSON, it can tansfer format automatically).
|
|
795
|
+
*
|
|
796
|
+
* ZH: 存储/获取数据到 sessionStorage/localStorage
|
|
797
|
+
*
|
|
798
|
+
* ```
|
|
799
|
+
* setSessionStorage('test', '123');
|
|
800
|
+
* getSessionStorage('test'); // 123
|
|
801
|
+
* setLocalStorage('test', '123');
|
|
802
|
+
* getLocalStorage('test'); // 123
|
|
803
|
+
*
|
|
804
|
+
* // or package in usage
|
|
805
|
+
* const projectName = 'mazey';
|
|
806
|
+
* function mSetLocalStorage (key, value) {
|
|
807
|
+
* return setLocalStorage(`${projectName}_${key}`, value);
|
|
808
|
+
* }
|
|
809
|
+
*
|
|
810
|
+
* function mGetLocalStorage (key) {
|
|
811
|
+
* return getLocalStorage(`${projectName}_${key}`);
|
|
812
|
+
* }
|
|
813
|
+
* ```
|
|
814
|
+
*
|
|
708
815
|
* @param {string} key 键
|
|
709
|
-
* @returns {
|
|
816
|
+
* @returns {any} 返回值
|
|
817
|
+
* @category Cache Data
|
|
710
818
|
*/
|
|
711
819
|
export function getSessionStorage(key: string): any {
|
|
712
820
|
let ret = null;
|
|
@@ -720,10 +828,30 @@ export function getSessionStorage(key: string): any {
|
|
|
720
828
|
}
|
|
721
829
|
|
|
722
830
|
/**
|
|
723
|
-
*
|
|
724
|
-
*
|
|
831
|
+
* EN: Handle Storage (Keep fit for JSON, it can tansfer format automatically).
|
|
832
|
+
*
|
|
833
|
+
* ZH: 存储/获取数据到 sessionStorage/localStorage
|
|
834
|
+
*
|
|
835
|
+
* ```
|
|
836
|
+
* setSessionStorage('test', '123');
|
|
837
|
+
* getSessionStorage('test'); // 123
|
|
838
|
+
* setLocalStorage('test', '123');
|
|
839
|
+
* getLocalStorage('test'); // 123
|
|
840
|
+
*
|
|
841
|
+
* // or package in usage
|
|
842
|
+
* const projectName = 'mazey';
|
|
843
|
+
* function mSetLocalStorage (key, value) {
|
|
844
|
+
* return setLocalStorage(`${projectName}_${key}`, value);
|
|
845
|
+
* }
|
|
846
|
+
*
|
|
847
|
+
* function mGetLocalStorage (key) {
|
|
848
|
+
* return getLocalStorage(`${projectName}_${key}`);
|
|
849
|
+
* }
|
|
850
|
+
* ```
|
|
851
|
+
*
|
|
725
852
|
* @param {string} key 键
|
|
726
|
-
* @
|
|
853
|
+
* @returns {any} 返回值
|
|
854
|
+
* @category Cache Data
|
|
727
855
|
*/
|
|
728
856
|
export function setLocalStorage(key: string, value: any = null): void {
|
|
729
857
|
if (key) {
|
|
@@ -732,10 +860,30 @@ export function setLocalStorage(key: string, value: any = null): void {
|
|
|
732
860
|
}
|
|
733
861
|
|
|
734
862
|
/**
|
|
735
|
-
*
|
|
736
|
-
*
|
|
863
|
+
* EN: Handle Storage (Keep fit for JSON, it can tansfer format automatically).
|
|
864
|
+
*
|
|
865
|
+
* ZH: 存储/获取数据到 sessionStorage/localStorage
|
|
866
|
+
*
|
|
867
|
+
* ```
|
|
868
|
+
* setSessionStorage('test', '123');
|
|
869
|
+
* getSessionStorage('test'); // 123
|
|
870
|
+
* setLocalStorage('test', '123');
|
|
871
|
+
* getLocalStorage('test'); // 123
|
|
872
|
+
*
|
|
873
|
+
* // or package in usage
|
|
874
|
+
* const projectName = 'mazey';
|
|
875
|
+
* function mSetLocalStorage (key, value) {
|
|
876
|
+
* return setLocalStorage(`${projectName}_${key}`, value);
|
|
877
|
+
* }
|
|
878
|
+
*
|
|
879
|
+
* function mGetLocalStorage (key) {
|
|
880
|
+
* return getLocalStorage(`${projectName}_${key}`);
|
|
881
|
+
* }
|
|
882
|
+
* ```
|
|
883
|
+
*
|
|
737
884
|
* @param {string} key 键
|
|
738
|
-
* @returns {
|
|
885
|
+
* @returns {any} 返回值
|
|
886
|
+
* @category Cache Data
|
|
739
887
|
*/
|
|
740
888
|
export function getLocalStorage(key: string): any {
|
|
741
889
|
let ret = null;
|
|
@@ -775,6 +923,7 @@ export function getLocalStorage(key: string): any {
|
|
|
775
923
|
* @param {string} url -- css资源路径
|
|
776
924
|
* @param {string} options.id -- link标签id
|
|
777
925
|
* @returns {Promise<boolean>} true -- 加载成功
|
|
926
|
+
* @category Load Resource
|
|
778
927
|
*/
|
|
779
928
|
export function loadCSS(
|
|
780
929
|
url: string,
|
|
@@ -931,6 +1080,7 @@ export function loadCSS(
|
|
|
931
1080
|
* @param {number} options.timeout -- 超时时长
|
|
932
1081
|
* @param {boolean} options.isDefer -- 是否添加 defer 标签
|
|
933
1082
|
* @returns {Promise<boolean>} -- true 成功
|
|
1083
|
+
* @category Load Resource
|
|
934
1084
|
*/
|
|
935
1085
|
export function loadScript(
|
|
936
1086
|
url: string,
|
|
@@ -1012,8 +1162,16 @@ export function mNow(): number {
|
|
|
1012
1162
|
}
|
|
1013
1163
|
|
|
1014
1164
|
/**
|
|
1015
|
-
*
|
|
1016
|
-
*
|
|
1165
|
+
* EN: Handle Cookie.
|
|
1166
|
+
*
|
|
1167
|
+
* ZH: 设置/获取 Cookie
|
|
1168
|
+
*
|
|
1169
|
+
* ```
|
|
1170
|
+
* setCookie('test', '123', 30, 'example.com'); // key value day domain
|
|
1171
|
+
* getCookie('test'); // 123
|
|
1172
|
+
* ```
|
|
1173
|
+
*
|
|
1174
|
+
* @category Cache Data
|
|
1017
1175
|
*/
|
|
1018
1176
|
export function setCookie(
|
|
1019
1177
|
name: string,
|
|
@@ -1061,8 +1219,16 @@ export function setCookie(
|
|
|
1061
1219
|
}
|
|
1062
1220
|
|
|
1063
1221
|
/**
|
|
1064
|
-
*
|
|
1065
|
-
*
|
|
1222
|
+
* EN: Handle Cookie.
|
|
1223
|
+
*
|
|
1224
|
+
* ZH: 设置/获取 Cookie
|
|
1225
|
+
*
|
|
1226
|
+
* ```
|
|
1227
|
+
* setCookie('test', '123', 30, 'example.com'); // key value day domain
|
|
1228
|
+
* getCookie('test'); // 123
|
|
1229
|
+
* ```
|
|
1230
|
+
*
|
|
1231
|
+
* @category Cache Data
|
|
1066
1232
|
*/
|
|
1067
1233
|
export function getCookie(name: string): string {
|
|
1068
1234
|
const nameEQ = name + '=';
|
|
@@ -1077,6 +1243,9 @@ export function getCookie(name: string): string {
|
|
|
1077
1243
|
return '';
|
|
1078
1244
|
}
|
|
1079
1245
|
|
|
1246
|
+
/**
|
|
1247
|
+
* @hidden
|
|
1248
|
+
*/
|
|
1080
1249
|
interface WebPerformance {
|
|
1081
1250
|
[key: string]: string | number;
|
|
1082
1251
|
}
|
|
@@ -1122,6 +1291,7 @@ interface WebPerformance {
|
|
|
1122
1291
|
*
|
|
1123
1292
|
* @param {boolean} camelCase -- false(默认) 以下划线形式返回数据 true 以驼峰形式返回数据
|
|
1124
1293
|
* @returns {Promise<object>} 加载数据
|
|
1294
|
+
* @category Web Performance
|
|
1125
1295
|
*/
|
|
1126
1296
|
export function getPerformance(
|
|
1127
1297
|
camelCase = false
|
|
@@ -1408,6 +1578,7 @@ export function getPerformance(
|
|
|
1408
1578
|
*
|
|
1409
1579
|
* @param {number} rate -- 0.1 ~ 1 => 1% ~ 100%
|
|
1410
1580
|
* @returns {boolean} true 命中
|
|
1581
|
+
* @category Calculate and Formula
|
|
1411
1582
|
*/
|
|
1412
1583
|
export function inRate(rate: number): boolean {
|
|
1413
1584
|
if (Math.random() < rate) {
|
|
@@ -1417,7 +1588,7 @@ export function inRate(rate: number): boolean {
|
|
|
1417
1588
|
}
|
|
1418
1589
|
|
|
1419
1590
|
/**
|
|
1420
|
-
* EN: Determine if it is a secure PWA environment that it can run.
|
|
1591
|
+
* EN: Detect the margin of Safety. Determine if it is a secure PWA environment that it can run.
|
|
1421
1592
|
*
|
|
1422
1593
|
* ZH: 判断是否是安全的 PWA 环境
|
|
1423
1594
|
*
|
|
@@ -1434,6 +1605,7 @@ export function inRate(rate: number): boolean {
|
|
|
1434
1605
|
* ```
|
|
1435
1606
|
*
|
|
1436
1607
|
* @returns {boolean} true 是
|
|
1608
|
+
* @category Browser Information
|
|
1437
1609
|
*/
|
|
1438
1610
|
export function isSafePWAEnv(): boolean {
|
|
1439
1611
|
// 判断是否支持 async await
|
|
@@ -1497,18 +1669,18 @@ export function isSafePWAEnv(): boolean {
|
|
|
1497
1669
|
*
|
|
1498
1670
|
* Results:
|
|
1499
1671
|
*
|
|
1500
|
-
* |
|
|
1501
|
-
* | --- | --- | --- |
|
|
1502
|
-
* | System |
|
|
1503
|
-
* | System version |
|
|
1504
|
-
* | Platform |
|
|
1505
|
-
* | Engine |
|
|
1506
|
-
* | Engine version |
|
|
1507
|
-
* | Supporter |
|
|
1508
|
-
* | Supporter version |
|
|
1509
|
-
* | Shell |
|
|
1510
|
-
* | Shell version |
|
|
1511
|
-
* | Apple device type |
|
|
1672
|
+
* | Attribute | Description | Type | Values |
|
|
1673
|
+
* | --- | --- | --- | --- |
|
|
1674
|
+
* | **system** | System | string | android, ios, windows, macos, linux |
|
|
1675
|
+
* | systemVs | System version | string | windows: 2000, xp, 2003, vista, 7, 8, 8.1, 10 <br />macos: ... |
|
|
1676
|
+
* | platform | Platform | string | desktop, mobile |
|
|
1677
|
+
* | engine | Engine | string | webkit, gecko, presto, trident |
|
|
1678
|
+
* | engineVs | Engine version | string | - |
|
|
1679
|
+
* | supporter | Supporter | string | edge, opera, chrome, safari, firefox, iexplore |
|
|
1680
|
+
* | supporterVs | Supporter version | string | - |
|
|
1681
|
+
* | shell | Shell | string | (Optional) wechat, qq_browser, qq_app, uc, 360, 2345, sougou, liebao, maxthon, bilibili |
|
|
1682
|
+
* | shellVs | Shell version | string | (Optional) 20/... |
|
|
1683
|
+
* | appleType | Apple device type | string | (Optional) ipad, iphone, ipod, iwatch |
|
|
1512
1684
|
*
|
|
1513
1685
|
* Example: Determine the environment of the mobile QQ.
|
|
1514
1686
|
*
|
|
@@ -1518,6 +1690,7 @@ export function isSafePWAEnv(): boolean {
|
|
|
1518
1690
|
* ```
|
|
1519
1691
|
*
|
|
1520
1692
|
* @returns 浏览器信息
|
|
1693
|
+
* @category Browser Information
|
|
1521
1694
|
*/
|
|
1522
1695
|
export function getBrowserInfo(): {
|
|
1523
1696
|
engine: string; // webkit gecko presto trident
|
|
@@ -1560,10 +1733,10 @@ export function getBrowserInfo(): {
|
|
|
1560
1733
|
system = 'android'; // android系统
|
|
1561
1734
|
} else if (testUa(/ios|iphone|ipad|ipod|iwatch/g)) {
|
|
1562
1735
|
system = 'ios'; // ios系统
|
|
1563
|
-
if (testUa(/
|
|
1564
|
-
appleType = 'iphone';
|
|
1565
|
-
} else if (testUa(/ipad/g)) {
|
|
1736
|
+
if (testUa(/ipad/g)) {
|
|
1566
1737
|
appleType = 'ipad';
|
|
1738
|
+
} else if (testUa(/iphone/g)) {
|
|
1739
|
+
appleType = 'iphone';
|
|
1567
1740
|
} else if (testUa(/iwatch/g)) {
|
|
1568
1741
|
appleType = 'iwatch';
|
|
1569
1742
|
} else if (testUa(/ipod/g)) {
|
|
@@ -1778,6 +1951,7 @@ export function cutCHSString(str: string, len: number, hasDot = false): string {
|
|
|
1778
1951
|
*
|
|
1779
1952
|
* @param {number} timeout 超时时间 / 单位:秒
|
|
1780
1953
|
* @returns {Promise<string>} document is loaded? 'complete' 'load' / 'timeout'
|
|
1954
|
+
* @category Load Resource
|
|
1781
1955
|
*/
|
|
1782
1956
|
export function windowLoaded(timeout = 90): Promise<string | Error> {
|
|
1783
1957
|
let loaded: (value: string) => void = () => undefined;
|
|
@@ -1801,7 +1975,7 @@ export function windowLoaded(timeout = 90): Promise<string | Error> {
|
|
|
1801
1975
|
/**
|
|
1802
1976
|
* EN: Add `<style>` in `<head>`.
|
|
1803
1977
|
*
|
|
1804
|
-
* ZH: 添加样式标签; style: 样式标签内的字符串; id: `<style>` 标签的 `id`; 返回:
|
|
1978
|
+
* ZH: 添加样式标签; style: 样式标签内的字符串; id: `<style>` 标签的 `id`; 返回: 添加成功/失败
|
|
1805
1979
|
*
|
|
1806
1980
|
* Example 1: Add the `<style>` with `id`, and repeated invoking will update the content instead of adding a new one.
|
|
1807
1981
|
*
|
|
@@ -1829,20 +2003,18 @@ export function windowLoaded(timeout = 90): Promise<string | Error> {
|
|
|
1829
2003
|
* addStyle(
|
|
1830
2004
|
* `
|
|
1831
2005
|
* body {
|
|
1832
|
-
* background-color: #
|
|
2006
|
+
* background-color: #444;
|
|
1833
2007
|
* }
|
|
1834
2008
|
* `
|
|
1835
2009
|
* );
|
|
1836
2010
|
* // <style>
|
|
1837
2011
|
* // body {
|
|
1838
|
-
* // background-color: #
|
|
2012
|
+
* // background-color: #444;
|
|
1839
2013
|
* // }
|
|
1840
2014
|
* // </style>
|
|
1841
2015
|
* ```
|
|
1842
2016
|
*
|
|
1843
|
-
* @
|
|
1844
|
-
* @param options.id `id` in `<style>`
|
|
1845
|
-
* @returns Success/Fail
|
|
2017
|
+
* @category DOM
|
|
1846
2018
|
*/
|
|
1847
2019
|
export function addStyle(
|
|
1848
2020
|
style: string,
|
|
@@ -1911,6 +2083,7 @@ export function addStyle(
|
|
|
1911
2083
|
* @param {function} logFn The function with Log.
|
|
1912
2084
|
* @param {function} errorFn The function with Error.
|
|
1913
2085
|
* @returns {object} 新实例
|
|
2086
|
+
* @category Debug
|
|
1914
2087
|
*/
|
|
1915
2088
|
export function genCustomConsole(
|
|
1916
2089
|
prefix = '',
|
|
@@ -2102,6 +2275,7 @@ export function isNonEmptyArray(arr: any[]): boolean {
|
|
|
2102
2275
|
* @param {string[]} attributes Data Attributes
|
|
2103
2276
|
* @param {any} validValue Given Value for verifying.
|
|
2104
2277
|
* @returns {boolean} Return TRUE if the data is valid.
|
|
2278
|
+
* @category Util
|
|
2105
2279
|
*/
|
|
2106
2280
|
export function isValidData(
|
|
2107
2281
|
data: any,
|
|
@@ -2208,6 +2382,7 @@ export function genHashCode(str: string): number {
|
|
|
2208
2382
|
* @param {Date|number|string} dateIns Original Date
|
|
2209
2383
|
* @param {string} format Format String
|
|
2210
2384
|
* @returns {string} Return the formatted date string.
|
|
2385
|
+
* @category Util
|
|
2211
2386
|
*/
|
|
2212
2387
|
export function formatDate(
|
|
2213
2388
|
dateIns?: Date | number | string,
|
|
@@ -2246,3 +2421,123 @@ export function formatDate(
|
|
|
2246
2421
|
});
|
|
2247
2422
|
return tempFormat;
|
|
2248
2423
|
}
|
|
2424
|
+
|
|
2425
|
+
/**
|
|
2426
|
+
* Get event container.
|
|
2427
|
+
*
|
|
2428
|
+
* @category Event
|
|
2429
|
+
* @hidden
|
|
2430
|
+
*/
|
|
2431
|
+
export function getDefineListeners(): DefineListeners {
|
|
2432
|
+
let defineListeners = window.MAZEY_DEFINE_LISTENERS;
|
|
2433
|
+
if (typeof defineListeners !== 'object') {
|
|
2434
|
+
defineListeners = {};
|
|
2435
|
+
window.MAZEY_DEFINE_LISTENERS = defineListeners;
|
|
2436
|
+
}
|
|
2437
|
+
return defineListeners;
|
|
2438
|
+
}
|
|
2439
|
+
|
|
2440
|
+
/**
|
|
2441
|
+
* Add event.
|
|
2442
|
+
*
|
|
2443
|
+
* @param type
|
|
2444
|
+
* @param fn
|
|
2445
|
+
* @category Event
|
|
2446
|
+
*/
|
|
2447
|
+
export function addEvent(type: string, fn: any): void {
|
|
2448
|
+
const defineListeners = getDefineListeners();
|
|
2449
|
+
if (typeof defineListeners[type] === 'undefined') {
|
|
2450
|
+
defineListeners[type] = [];
|
|
2451
|
+
}
|
|
2452
|
+
if (typeof fn === 'function') {
|
|
2453
|
+
defineListeners[type].push(fn);
|
|
2454
|
+
}
|
|
2455
|
+
}
|
|
2456
|
+
|
|
2457
|
+
/**
|
|
2458
|
+
* Invoke event.
|
|
2459
|
+
*
|
|
2460
|
+
* @param type
|
|
2461
|
+
* @category Event
|
|
2462
|
+
*/
|
|
2463
|
+
export function invokeEvent(type: string): void {
|
|
2464
|
+
const defineListeners = getDefineListeners();
|
|
2465
|
+
const arrayEvent = defineListeners[type];
|
|
2466
|
+
if (arrayEvent instanceof Array) {
|
|
2467
|
+
for (let i = 0, length = arrayEvent.length; i < length; i++) {
|
|
2468
|
+
if (typeof arrayEvent[i] === 'function') {
|
|
2469
|
+
arrayEvent[i]({
|
|
2470
|
+
type: type
|
|
2471
|
+
});
|
|
2472
|
+
}
|
|
2473
|
+
}
|
|
2474
|
+
}
|
|
2475
|
+
}
|
|
2476
|
+
|
|
2477
|
+
/**
|
|
2478
|
+
* Remove event.
|
|
2479
|
+
*
|
|
2480
|
+
* @param type
|
|
2481
|
+
* @param fn
|
|
2482
|
+
* @category Event
|
|
2483
|
+
*/
|
|
2484
|
+
export function removeEvent(type: string, fn: any): void {
|
|
2485
|
+
const defineListeners = getDefineListeners();
|
|
2486
|
+
const arrayEvent = defineListeners[type];
|
|
2487
|
+
if (typeof type === 'string' && arrayEvent instanceof Array) {
|
|
2488
|
+
if (typeof fn === 'function') {
|
|
2489
|
+
for (let i = 0, length = arrayEvent.length; i < length; i++) {
|
|
2490
|
+
if (arrayEvent[i] === fn) {
|
|
2491
|
+
defineListeners[type].splice(i, 1);
|
|
2492
|
+
break;
|
|
2493
|
+
}
|
|
2494
|
+
}
|
|
2495
|
+
} else {
|
|
2496
|
+
delete defineListeners[type];
|
|
2497
|
+
}
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
2500
|
+
|
|
2501
|
+
/**
|
|
2502
|
+
* Check if the given string is a valid url.
|
|
2503
|
+
*
|
|
2504
|
+
* @example
|
|
2505
|
+
* ```js
|
|
2506
|
+
* console.log(isUrl('https://www.google.com')); // true
|
|
2507
|
+
* console.log(isUrl('https://www.google.com/')); // true
|
|
2508
|
+
* console.log(isUrl('https://www.google.com/?q=hello')); // true
|
|
2509
|
+
* console.log(isUrl('https://www.google.com/?q=hello#world')); // true
|
|
2510
|
+
* console.log(isUrl('https://www.google.com/#world')); // true
|
|
2511
|
+
* console.log(isUrl('https://www.google.com/#')); // true
|
|
2512
|
+
* console.log(isUrl('https://www.google.com/#?q=hello')); // true
|
|
2513
|
+
* console.log(isUrl('https://www.google.com/#?q=hello#world')); // true
|
|
2514
|
+
* ```
|
|
2515
|
+
*
|
|
2516
|
+
* @param url
|
|
2517
|
+
* @returns {boolean} Return true if the given url is a valid url.
|
|
2518
|
+
* @category URL
|
|
2519
|
+
*/
|
|
2520
|
+
export function isValidUrl(url: string): boolean {
|
|
2521
|
+
const reg = /[a-zA-Z0-9]+:\/\/[-a-zA-Z0-9@:%._+~#=]{1,256}\b([-a-zA-Z0-9\u4E00-\u9FA5()!@:%_+.~#?&//=]*)/gm;
|
|
2522
|
+
return reg.test(url);
|
|
2523
|
+
}
|
|
2524
|
+
|
|
2525
|
+
/**
|
|
2526
|
+
* Check if the given string is a mobile phone number.
|
|
2527
|
+
*
|
|
2528
|
+
* @example
|
|
2529
|
+
* ```js
|
|
2530
|
+
* console.log(isMobile('13800138000')); // true
|
|
2531
|
+
* console.log(isMobile('1380013800')); // false
|
|
2532
|
+
* console.log(isMobile('138001380000')); // false
|
|
2533
|
+
* console.log(isMobile('1380013800a')); // false
|
|
2534
|
+
* ```
|
|
2535
|
+
*
|
|
2536
|
+
* @param mobile
|
|
2537
|
+
* @returns {boolean} Return true if the given string is a mobile phone number.
|
|
2538
|
+
* @category Util
|
|
2539
|
+
*/
|
|
2540
|
+
export function isValidPhoneNumber(mobile: string): boolean {
|
|
2541
|
+
const reg = /^1[3456789]\d{9}$/;
|
|
2542
|
+
return reg.test(mobile);
|
|
2543
|
+
}
|