mazey 3.5.2 → 3.5.4

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 CHANGED
@@ -10,7 +10,7 @@ English | [简体中文](https://github.com/mazeyqian/mazey/blob/master/README_C
10
10
  [l-image]: https://img.shields.io/npm/l/mazey
11
11
  [l-url]: https://github.com/mazeyqian/mazey
12
12
 
13
- Mazey's functional library for daily front-end work. There are already many excellent libraries for front-end development, but creating a file named UtilsJS or CommonJS is generally used to supply common functions in projects. It's boring to copy similar functions among projects again and again. I will consistently update the library during my work life to save time.
13
+ Mazey's functional library for daily front-end work. There are already many excellent libraries for front-end development, but creating a file named UtilsJS or CommonJS is generally used to supply common functions in projects. It's boring to copy similar functions among projects again and again. Therefore, I will consistently update the library during my work life to save time.
14
14
 
15
15
  ## Install
16
16
 
@@ -45,59 +45,59 @@ Import from CDN.
45
45
  ```
46
46
  <script type="text/javascript" src="//i.mazey.net/mazey/lib/mazey.min.js"></script>
47
47
  <script>
48
- mazey.isNumber(Infinity, { isUnFiniteAsNumber: true }); // true
48
+ mazey.isNumber(Infinity, { isUnFiniteAsNumber: true }); // true
49
49
  </script>
50
50
  ```
51
51
 
52
52
  ## API Examples
53
53
 
54
- There ara some examples maintained by hand below. For more information, please check the [full documentation](https://i.mazey.net/mazey/docs/modules/_index_.html).
54
+ There ara some examples maintained by hand below. For more information, please check the [full documentation](https://mazey.cn/t/m).
55
55
 
56
56
  **Table of Contents**
57
57
 
58
- <!-- toc - begin -->
59
- - Generated with ❤️
60
- - [Load Resource](#load-resource)
61
- * [loadScript](#loadscript)
62
- * [loadCSS](#loadcss)
63
- * [windowLoaded](#windowloaded)
64
- - [Function](#function)
65
- * [debounce](#debounce)
66
- * [throttle](#throttle)
67
- * [isNumber](#isnumber)
68
- * [camelCaseToKebabCase](#camelcasetokebabcase)
69
- * [camelCase2Underscore](#camelcase2underscore)
70
- * [mTrim](#mtrim)
71
- * [deepCopyObject](#deepcopyobject)
72
- * [isJsonString](#isjsonstring)
73
- * [generateRndNum](#generaterndnum)
74
- * [formatDate](#formatdate)
75
- * [isValidData](#isvaliddata)
76
- - [DOM](#dom)
77
- * [Class](#class)
78
- * [addStyle](#addstyle)
79
- * [newLine](#newline)
80
- - [URL](#url)
81
- * [getQueryParam](#getqueryparam)
82
- * [getUrlParam](#geturlparam)
83
- * [updateQueryParam](#updatequeryparam)
84
- * [getHashQueryParam](#gethashqueryparam)
85
- * [getDomain](#getdomain)
86
- - [Cache Data](#cache-data)
87
- * [Storage](#storage)
88
- * [Cookie](#cookie)
89
- - [Calculate and Formula](#calculate-and-formula)
90
- * [inRate](#inrate)
91
- * [calLongestCommonSubstring](#callongestcommonsubstring)
92
- * [calLongestCommonSubsequence](#callongestcommonsubsequence)
93
- - [Browser Information](#browser-information)
94
- * [getBrowserInfo](#getbrowserinfo)
95
- - [Web Performance](#web-performance)
96
- * [getPerformance](#getperformance)
97
- - [Margin of Safety](#margin-of-safety)
98
- * [isSafePWAEnv](#issafepwaenv)
99
- - [Debug](#debug)
100
- * [genCustomConsole](#gencustomconsole)
58
+ <!-- toc - begin -->
59
+ - Generated with ❤️
60
+ - [Load Resource](#load-resource)
61
+ * [loadScript](#loadscript)
62
+ * [loadCSS](#loadcss)
63
+ * [windowLoaded](#windowloaded)
64
+ - [Util](#util)
65
+ * [isNumber](#isnumber)
66
+ * [generateRndNum](#generaterndnum)
67
+ * [isJsonString](#isjsonstring)
68
+ * [formatDate](#formatdate)
69
+ * [debounce](#debounce)
70
+ * [throttle](#throttle)
71
+ * [camelCaseToKebabCase](#camelcasetokebabcase)
72
+ * [camelCase2Underscore](#camelcase2underscore)
73
+ * [mTrim](#mtrim)
74
+ * [deepCopyObject](#deepcopyobject)
75
+ * [isValidData](#isvaliddata)
76
+ - [DOM](#dom)
77
+ * [addStyle](#addstyle)
78
+ * [newLine](#newline)
79
+ * [Class](#class)
80
+ - [URL](#url)
81
+ * [getQueryParam](#getqueryparam)
82
+ * [getUrlParam](#geturlparam)
83
+ * [getHashQueryParam](#gethashqueryparam)
84
+ * [getDomain](#getdomain)
85
+ * [updateQueryParam](#updatequeryparam)
86
+ - [Cache Data](#cache-data)
87
+ * [Storage](#storage)
88
+ * [Cookie](#cookie)
89
+ - [Calculate and Formula](#calculate-and-formula)
90
+ * [inRate](#inrate)
91
+ * [calLongestCommonSubstring](#callongestcommonsubstring)
92
+ * [calLongestCommonSubsequence](#callongestcommonsubsequence)
93
+ - [Browser Information](#browser-information)
94
+ * [getBrowserInfo](#getbrowserinfo)
95
+ - [Web Performance](#web-performance)
96
+ * [getPerformance](#getperformance)
97
+ - [Margin of Safety](#margin-of-safety)
98
+ * [isSafePWAEnv](#issafepwaenv)
99
+ - [Debug](#debug)
100
+ * [genCustomConsole](#gencustomconsole)
101
101
  <!-- toc - end -->
102
102
 
103
103
  ### Load Resource
@@ -134,7 +134,8 @@ loadScript(
134
134
  @param {function} options.callback -- 加载后回调函数
135
135
  @param {number} options.timeout -- 超时时长
136
136
  @param {boolean} options.isDefer -- 是否添加 defer 标签
137
- @returns {Promise<boolean>} -- true 成功 -->
137
+ @returns {Promise<boolean>} -- true 成功
138
+ @category Load Resource -->
138
139
 
139
140
  #### loadCSS
140
141
 
@@ -163,7 +164,8 @@ loadCSS(
163
164
 
164
165
  <!-- @param {string} url -- css资源路径
165
166
  @param {string} options.id -- link标签id
166
- @returns {Promise<boolean>} true -- 加载成功 -->
167
+ @returns {Promise<boolean>} true -- 加载成功
168
+ @category Load Resource -->
167
169
 
168
170
  #### windowLoaded
169
171
 
@@ -182,9 +184,87 @@ windowLoaded(30) // second
182
184
  ```
183
185
 
184
186
  <!-- @param {number} timeout 超时时间 / 单位:秒
185
- @returns {Promise<string>} document is loaded? 'complete' 'load' / 'timeout' -->
187
+ @returns {Promise<string>} document is loaded? 'complete' 'load' / 'timeout'
188
+ @category Load Resource -->
186
189
 
187
- ### Function
190
+ ### Util
191
+
192
+ #### isNumber
193
+
194
+ Check whether it is a right number.
195
+
196
+ <!-- ZH: 判断是否有效数字 -->
197
+
198
+ ```
199
+ isNumber(123); // true
200
+ isNumber('123'); // false
201
+ // Default: NaN, Infinity is not Number
202
+ isNumber(Infinity); // false
203
+ isNumber(Infinity, { isUnFiniteAsNumber: true }); // true
204
+ isNumber(NaN); // false
205
+ isNumber(NaN, { isNaNAsNumber: true, isUnFiniteAsNumber: true }); // true
206
+ ```
207
+
208
+ <!-- @param {*} num 被判断的值
209
+ @param {boolean} options.isNaNAsNumber 是否 NaN 算数字(默认不算)
210
+ @param {boolean} options.isUnFiniteAsNumber 是否 无限 算数字(默认不算)
211
+ @returns {boolean} true 是数字 -->
212
+
213
+ #### generateRndNum
214
+
215
+ Produce a random string of number, `generateRndNum(7)` => '7658495'.
216
+
217
+ ```
218
+ generateRndNum(4); // '9730'
219
+ generateRndNum(7); // '2262490'
220
+ ```
221
+
222
+ #### isJsonString
223
+
224
+ Check whether it is a valid JSON string.
225
+
226
+ Usage:
227
+
228
+ ```
229
+ isJsonString(`['a', 'b', 'c']`);
230
+ isJsonString(`["a", "b", "c"]`);
231
+ ```
232
+
233
+ Output:
234
+
235
+ ```
236
+ false
237
+ true
238
+ ```
239
+
240
+ <!-- @param {string} str The string to check.
241
+ @returns {boolean} Return the result of checking. -->
242
+
243
+ #### formatDate
244
+
245
+ Return the formatted date string in the given format.
246
+
247
+ Usage:
248
+
249
+ ```
250
+ console.log('Default formatDate value:', formatDate());
251
+ console.log('String formatDate value:', formatDate('Tue Jan 11 2022 14:12:26 GMT+0800 (China Standard Time)', 'yyyy-MM-dd hh:mm:ss'));
252
+ console.log('Number formatDate value:', formatDate(1641881235000, 'yyyy-MM-dd hh:mm:ss'));
253
+ console.log('Date formatDate value:', formatDate(new Date(2014, 1, 11), 'MM/dd/yyyy'));
254
+ ```
255
+
256
+ Output:
257
+
258
+ ```
259
+ Default formatDate value: 2023-01-11
260
+ String formatDate value: 2022-01-11 14:12:26
261
+ Number formatDate value: 2022-01-11 14:07:15
262
+ Date formatDate value: 02/11/2014
263
+ ```
264
+
265
+ <!-- @param {Date|number|string} dateIns Original Date
266
+ @param {string} format Format String
267
+ @returns {string} Return the formatted date string. -->
188
268
 
189
269
  #### debounce
190
270
 
@@ -212,27 +292,6 @@ const foo = throttle(() => {
212
292
 
213
293
  Reference: [Lodash](https://lodash.com/docs/4.17.15#throttle)
214
294
 
215
- #### isNumber
216
-
217
- Check whether it is a right number.
218
-
219
- <!-- ZH: 判断是否有效数字 -->
220
-
221
- ```
222
- isNumber(123); // true
223
- isNumber('123'); // false
224
- // Default: NaN, Infinity is not Number
225
- isNumber(Infinity); // false
226
- isNumber(Infinity, { isUnFiniteAsNumber: true }); // true
227
- isNumber(NaN); // false
228
- isNumber(NaN, { isNaNAsNumber: true, isUnFiniteAsNumber: true }); // true
229
- ```
230
-
231
- <!-- @param {*} num 被判断的值
232
- @param {boolean} options.isNaNAsNumber 是否 NaN 算数字(默认不算)
233
- @param {boolean} options.isUnFiniteAsNumber 是否 无限 算数字(默认不算)
234
- @returns {boolean} true 是数字 -->
235
-
236
295
  #### camelCaseToKebabCase
237
296
 
238
297
  Transfer CamelCase to KebabCase.
@@ -290,62 +349,6 @@ Output:
290
349
  <!-- @param {object} obj The value to clone.
291
350
  @returns {object} Returns the deep cloned value. -->
292
351
 
293
- #### isJsonString
294
-
295
- Check whether it is a valid JSON string.
296
-
297
- Usage:
298
-
299
- ```
300
- isJsonString(`['a', 'b', 'c']`);
301
- isJsonString(`["a", "b", "c"]`);
302
- ```
303
-
304
- Output:
305
-
306
- ```
307
- false
308
- true
309
- ```
310
-
311
- <!-- @param {string} str The string to check.
312
- @returns {boolean} Return the result of checking. -->
313
-
314
- #### generateRndNum
315
-
316
- Produce a random string of number, `generateRndNum(7)` => '7658495'.
317
-
318
- ```
319
- generateRndNum(4); // '9730'
320
- generateRndNum(7); // '2262490'
321
- ```
322
-
323
- #### formatDate
324
-
325
- Return the formatted date string in the given format.
326
-
327
- Usage:
328
-
329
- ```
330
- console.log('Default formatDate value:', formatDate());
331
- console.log('String formatDate value:', formatDate('Tue Jan 11 2022 14:12:26 GMT+0800 (China Standard Time)', 'yyyy-MM-dd hh:mm:ss'));
332
- console.log('Number formatDate value:', formatDate(1641881235000, 'yyyy-MM-dd hh:mm:ss'));
333
- console.log('Date formatDate value:', formatDate(new Date(2014, 1, 11), 'MM/dd/yyyy'));
334
- ```
335
-
336
- Output:
337
-
338
- ```
339
- Default formatDate value: 2023-01-11
340
- String formatDate value: 2022-01-11 14:12:26
341
- Number formatDate value: 2022-01-11 14:07:15
342
- Date formatDate value: 02/11/2014
343
- ```
344
-
345
- <!-- @param {Date|number|string} dateIns Original Date
346
- @param {string} format Format String
347
- @returns {string} Return the formatted date string. -->
348
-
349
352
  #### isValidData
350
353
 
351
354
  Determine the validity of the data.
@@ -385,21 +388,6 @@ isValidDataResC: false
385
388
 
386
389
  ### DOM
387
390
 
388
- #### Class
389
-
390
- Modify `class`.
391
-
392
- ```
393
- const dom = document.querySelector('#box');
394
-
395
- // Determine `class`
396
- hasClass(dom, 'test');
397
- // Add `class`
398
- addClass(dom, 'test');
399
- // Remove `class`
400
- removeClass(dom, 'test');
401
- ```
402
-
403
391
  #### addStyle
404
392
 
405
393
  Add `<style>` in `<head>`.
@@ -450,6 +438,21 @@ newLine('a\nb\nc'); // 'a<br />b<br />c'
450
438
  newLine('a\n\nbc'); // 'a<br /><br />bc'
451
439
  ```
452
440
 
441
+ #### Class
442
+
443
+ Modify `class`.
444
+
445
+ ```
446
+ const dom = document.querySelector('#box');
447
+
448
+ // Determine `class`
449
+ hasClass(dom, 'test');
450
+ // Add `class`
451
+ addClass(dom, 'test');
452
+ // Remove `class`
453
+ removeClass(dom, 'test');
454
+ ```
455
+
453
456
  ### URL
454
457
 
455
458
  #### getQueryParam
@@ -464,7 +467,8 @@ getQueryParam('t4'); // 4
464
467
  ```
465
468
 
466
469
  <!-- @param {string} param Query param.
467
- @returns {string} value -->
470
+ @returns {string} value
471
+ @category URL -->
468
472
 
469
473
  #### getUrlParam
470
474
 
@@ -477,30 +481,8 @@ getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4'); // 4
477
481
 
478
482
  <!-- @param {string} url URL string.
479
483
  @param {string} param Query param.
480
- @returns {string} value -->
481
-
482
- #### updateQueryParam
483
-
484
- Update the query param's value of the input URL.
485
-
486
- Usage:
487
-
488
- ```
489
- updateQueryParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3', 'three');
490
- updateQueryParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4', 'four');
491
- ```
492
-
493
- Output:
494
-
495
- ```
496
- http://example.com/?t1=1&t2=2&t3=three&t4=4
497
- http://example.com/?t1=1&t2=2&t3=3&t4=four
498
- ```
499
-
500
- <!-- @param {string} url URL string.
501
- @param {string} param Query param.
502
- @param {string} value Param's value.
503
- @returns {string} URL. -->
484
+ @returns {string} value
485
+ @category URL -->
504
486
 
505
487
  #### getHashQueryParam
506
488
 
@@ -523,7 +505,8 @@ Output:
523
505
  ```
524
506
 
525
507
  <!-- @param {string} param Query param.
526
- @returns {string} value -->
508
+ @returns {string} value
509
+ @category URL -->
527
510
 
528
511
  #### getDomain
529
512
 
@@ -544,7 +527,32 @@ example.com/test/thanks
544
527
  ```
545
528
 
546
529
  <!-- @param {string} url
547
- @param {array} rules Object.keys(location), ['href', 'protocol', 'host', 'hostname', 'port', 'pathname', 'search', 'hash'], ['hostname', 'pathname'] = 'km.mazey.net/plugins/servlet/mobile' -->
530
+ @param {array} rules Object.keys(location), ['href', 'protocol', 'host', 'hostname', 'port', 'pathname', 'search', 'hash'], ['hostname', 'pathname'] = 'km.mazey.net/plugins/servlet/mobile'
531
+ @category URL -->
532
+
533
+ #### updateQueryParam
534
+
535
+ Update the query param's value of the input URL.
536
+
537
+ Usage:
538
+
539
+ ```
540
+ updateQueryParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3', 'three');
541
+ updateQueryParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4', 'four');
542
+ ```
543
+
544
+ Output:
545
+
546
+ ```
547
+ http://example.com/?t1=1&t2=2&t3=three&t4=4
548
+ http://example.com/?t1=1&t2=2&t3=3&t4=four
549
+ ```
550
+
551
+ <!-- @param {string} url URL string.
552
+ @param {string} param Query param.
553
+ @param {string} value Param's value.
554
+ @returns {string} URL.
555
+ @category URL -->
548
556
 
549
557
  ### Cache Data
550
558
 
@@ -694,7 +702,7 @@ Results:
694
702
  | Supporter version | supporterVs | - |
695
703
  | Shell | shell | (Optional) wechat, qq_browser, qq_app, uc, 360, 2345, sougou, liebao, maxthon, bilibili |
696
704
  | Shell version | shellVs | (Optional) 20/... |
697
- | Apple device type | appleType | (Optional) iphone, ipad, ipod, iwatch |
705
+ | Apple device type | appleType | (Optional) ipad, iphone, ipod, iwatch |
698
706
 
699
707
  Example: Determine the environment of the mobile QQ.
700
708
 
@@ -703,7 +711,8 @@ const { system, shell } = getBrowserInfo();
703
711
  const isMobileQQ = ['android', 'ios'].includes(system) && ['qq_browser', 'qq_app'].includes(shell);
704
712
  ```
705
713
 
706
- <!-- @returns 浏览器信息 -->
714
+ <!-- @returns 浏览器信息
715
+ @category Browser Information -->
707
716
 
708
717
  ### Web Performance
709
718
 
@@ -716,9 +725,9 @@ Get page load time(PerformanceTiming).
716
725
  Usage:
717
726
 
718
727
  ```
719
- // `camelCase:true`(Default) Return hump data.
720
- // `camelCase:false` Return underline data.
721
- getPerformance(true)
728
+ // `camelCase:false` (Default) Return underline data.
729
+ // `camelCase:true` Return hump data.
730
+ getPerformance()
722
731
  .then(res => {
723
732
  console.log(JSON.stringify(res));
724
733
  })
@@ -728,7 +737,7 @@ getPerformance(true)
728
737
  Output:
729
738
 
730
739
  ```
731
- {"deviceType":"pc","network":"3g","unloadTime":0,"redirectTime":0,"dnsTime":0,"tcpTime":0,"responseTime":65,"downloadTime":1,"domreadyTime":369,"onloadTime":441,"whiteTime":94,"renderTime":441,"decodedBodySize":210,"encodedBodySize":210}
740
+ {"os":"ios","os_version":"13_2_3","device_type":"phone","network":"4g","unload_time":0,"redirect_time":0,"dns_time":0,"tcp_time":0,"response_time":289,"download_time":762,"first_paint_time":469,"first_contentful_paint_time":469,"domready_time":1318,"onload_time":2767,"white_time":299,"render_time":2768,"decoded_body_size":979570,"encoded_body_size":324938}
732
741
  ```
733
742
 
734
743
  Results:
@@ -747,7 +756,7 @@ Results:
747
756
  | SSL | ssl_time | (Optional) connectEnd - secureConnectionStart |
748
757
  | Download | download_time | (Optional) responseEnd - responseStart |
749
758
 
750
- <!-- @param {boolean} camelCase -- true(默认) 以驼峰形式返回数据 false 以下划线形式返回数据
759
+ <!-- @param {boolean} camelCase -- false(默认) 以下划线形式返回数据 true 以驼峰形式返回数据
751
760
  @returns {Promise<object>} 加载数据 -->
752
761
 
753
762
  ### Margin of Safety