mazey 3.3.11 → 3.4.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/src/index.ts CHANGED
@@ -5,8 +5,16 @@
5
5
  *
6
6
  * ZH: 计算两个字符串的最长公共子串
7
7
  *
8
+ * Usage:
9
+ *
10
+ * ```
11
+ * calLongestCommonSubstring('fish', 'finish');
12
+ * ```
13
+ *
14
+ * Output:
15
+ *
8
16
  * ```
9
- * calLongestCommonSubstring('fish', 'finish'); // 3
17
+ * 3
10
18
  * ```
11
19
  *
12
20
  * @param {string} aStr String
@@ -41,8 +49,16 @@ export function calLongestCommonSubstring(aStr: string, bStr: string): number {
41
49
  *
42
50
  * ZH: 计算两个字符串的最长公共子序列
43
51
  *
52
+ * Usage:
53
+ *
54
+ * ```
55
+ * calLongestCommonSubsequence('fish', 'finish');
56
+ * ```
57
+ *
58
+ * Output:
59
+ *
44
60
  * ```
45
- * calLongestCommonSubsequence('fish', 'finish'); // 4
61
+ * 4
46
62
  * ```
47
63
  *
48
64
  * @param {string} aStr 字符串
@@ -85,8 +101,15 @@ export function calLongestCommonSubsequence(
85
101
  }
86
102
 
87
103
  /**
88
- * @method getQueryParam
89
- * @description Get the query param's value of the current Web URL(`location.search`).
104
+ * Get the query param's value of the current Web URL(`location.search`).
105
+ *
106
+ * ```
107
+ * // http://example.com/?t1=1&t2=2&t3=3&t4=4#2333
108
+ * // ?t1=1&t2=2&t3=3&t4=4
109
+ * getQueryParam('t3'); // 3
110
+ * getQueryParam('t4'); // 4
111
+ * ```
112
+ *
90
113
  * @param {string} param Query param.
91
114
  * @returns {string} value
92
115
  */
@@ -101,8 +124,13 @@ export function getQueryParam(param: string): string {
101
124
  }
102
125
 
103
126
  /**
104
- * @method getUrlParam
105
- * @description Get the query param's value of the input URL.
127
+ * Get the query param's value of the input URL.
128
+ *
129
+ * ```
130
+ * getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3'); // 3
131
+ * getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4'); // 4
132
+ * ```
133
+ *
106
134
  * @param {string} url URL string.
107
135
  * @param {string} param Query param.
108
136
  * @returns {string} value
@@ -129,8 +157,22 @@ export function getUrlParam(url: string, param: string): string | string[] {
129
157
  }
130
158
 
131
159
  /**
132
- * @method updateQueryParam
133
- * @description Update the query param's value of the input URL.
160
+ * Update the query param's value of the input URL.
161
+ *
162
+ * Usage:
163
+ *
164
+ * ```
165
+ * updateQueryParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3', 'three');
166
+ * updateQueryParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4', 'four');
167
+ * ```
168
+ *
169
+ * Output:
170
+ *
171
+ * ```
172
+ * http://example.com/?t1=1&t2=2&t3=three&t4=4
173
+ * http://example.com/?t1=1&t2=2&t3=3&t4=four
174
+ * ```
175
+ *
134
176
  * @param {string} url URL string.
135
177
  * @param {string} param Query param.
136
178
  * @param {string} value Param's value.
@@ -151,8 +193,24 @@ export function updateQueryParam(
151
193
  }
152
194
 
153
195
  /**
154
- * @method getHashQueryParam
155
- * @description Get the hash query param's value of the current Web URL(`location.hash`).
196
+ * Get the hash query param's value of the current Web URL(`location.hash`).
197
+ *
198
+ * Usage:
199
+ *
200
+ * ```
201
+ * // http://example.com/?#2333?t1=1&t2=2&t3=3&t4=4
202
+ * // #2333?t1=1&t2=2&t3=3&t4=4
203
+ * getHashQueryParam('t3');
204
+ * getHashQueryParam('t4');
205
+ * ```
206
+ *
207
+ * Output:
208
+ *
209
+ * ```
210
+ * 3
211
+ * 4
212
+ * ```
213
+ *
156
214
  * @param {string} param Query param.
157
215
  * @returns {string} value
158
216
  */
@@ -167,8 +225,22 @@ export function getHashQueryParam(param: string): string {
167
225
  }
168
226
 
169
227
  /**
170
- * @method getDomain
171
- * @description Get the domain of URL, and other params.
228
+ * Get the domain of URL, and other params.
229
+ *
230
+ * Usage:
231
+ *
232
+ * ```
233
+ * getDomain('http://example.com/?t1=1&t2=2&t3=3&t4=4');
234
+ * getDomain('http://example.com/test/thanks?t1=1&t2=2&t3=3&t4=4', ['hostname', 'pathname']);
235
+ * ```
236
+ *
237
+ * Output:
238
+ *
239
+ * ```
240
+ * example.com
241
+ * example.com/test/thanks
242
+ * ```
243
+ *
172
244
  * @param {string} url
173
245
  * @param {array} rules Object.keys(location), ['href', 'protocol', 'host', 'hostname', 'port', 'pathname', 'search', 'hash'], ['hostname', 'pathname'] = 'km.mazey.net/plugins/servlet/mobile'
174
246
  */
@@ -182,8 +254,13 @@ export function getDomain(url: string, rules = ['hostname']): string {
182
254
  }
183
255
 
184
256
  /**
185
- * @method camelCaseToKebabCase
186
- * @description Transfer CamelCase to KebabCase.
257
+ * Transfer CamelCase to KebabCase.
258
+ *
259
+ * ```
260
+ * camelCaseToKebabCase('ABC'); // a-b-c
261
+ * camelCaseToKebabCase('aBC'); // a-b-c
262
+ * ```
263
+ *
187
264
  * @param {string} camelCase 'aBC' or 'ABC'
188
265
  * @returns {string} 'a-b-c'
189
266
  */
@@ -193,8 +270,13 @@ export function camelCaseToKebabCase(camelCase: string): string {
193
270
  }
194
271
 
195
272
  /**
196
- * @method camelCase2Underscore
197
- * @description Transfer CamelCase to Underscore.
273
+ * Transfer CamelCase to Underscore.
274
+ *
275
+ * ```
276
+ * camelCase2Underscore('ABC'); // a_b_c
277
+ * camelCase2Underscore('aBC'); // a_b_c
278
+ * ```
279
+ *
198
280
  * @param {string} camelCase 'aBC' or 'ABC'
199
281
  * @returns {string} 'a_b_c'
200
282
  */
@@ -204,8 +286,13 @@ export function camelCase2Underscore(camelCase: string): string {
204
286
  }
205
287
 
206
288
  /**
207
- * @method mTrim
208
- * @description Remove leading and trailing whitespace or specified characters from string.
289
+ * Remove leading and trailing whitespace or specified characters from string.
290
+ *
291
+ * ```
292
+ * mTrim(' 1 2 3 '); // '1 2 3'
293
+ * mTrim('abc '); // 'abc'
294
+ * ```
295
+ *
209
296
  * @param {string} str The string to trim.
210
297
  * @returns {string} Trimmed string.
211
298
  */
@@ -234,8 +321,22 @@ export function newLine(str: string): string {
234
321
  }
235
322
 
236
323
  /**
237
- * @method deepCopyObject
238
- * @description Clone Object deeply.
324
+ * Clone Object deeply.
325
+ *
326
+ * Usage:
327
+ *
328
+ * ```
329
+ * deepCopyObject(['a', 'b', 'c']);
330
+ * deepCopyObject('abc');
331
+ * ```
332
+ *
333
+ * Output:
334
+ *
335
+ * ```
336
+ * ['a', 'b', 'c']
337
+ * 'abc'
338
+ * ```
339
+ *
239
340
  * @param {object} obj The value to clone.
240
341
  * @returns {object} Returns the deep cloned value.
241
342
  */
@@ -244,10 +345,21 @@ export function deepCopyObject(obj: any): any {
244
345
  }
245
346
 
246
347
  /**
247
- * @method isJsonString
248
- * @description Check whether it is a valid JSON string.
249
- * @param {string} str The string to check.
250
- * @returns {boolean} Return the result of checking.
348
+ * Check whether it is a valid JSON string.
349
+ *
350
+ * Usage:
351
+ *
352
+ * ```
353
+ * isJsonString(`['a', 'b', 'c']`);
354
+ * isJsonString(`["a", "b", "c"]`);
355
+ * ```
356
+ *
357
+ * Output:
358
+ *
359
+ * ```
360
+ * false
361
+ * true
362
+ * ```
251
363
  */
252
364
  export function isJsonString(str: string): boolean {
253
365
  try {
@@ -369,6 +481,12 @@ export function removeClass(obj: any, cls: string): void {
369
481
  *
370
482
  * ZH: 节流
371
483
  *
484
+ * ```
485
+ * const foo = throttle(() => {
486
+ * console.log('The function will be invoked at most once per every wait 1000 milliseconds.');
487
+ * }, 1000, { leading: true });
488
+ * ```
489
+ *
372
490
  * Reference: [Lodash](https://lodash.com/docs/4.17.15#throttle)
373
491
  */
374
492
  export function throttle(
@@ -424,8 +542,6 @@ export function throttle(
424
542
  *
425
543
  * ZH: 去抖
426
544
  *
427
- * Usage:
428
- *
429
545
  * ```
430
546
  * const foo = debounce(() => {
431
547
  * console.log('The debounced function will only be invoked in 1000 milliseconds, the other invoking will disappear during the wait time.');
@@ -474,7 +590,7 @@ export function debounce(func: any, wait: number, immediate?: any): any {
474
590
  * @param {number/Date} start 开始时间戳 1585325367122
475
591
  * @param {number/Date} end 结束时间戳 1585325367122
476
592
  * @param {string} options.type 返回类型 d: 2(天) text: 2 天 4 时...
477
- * @returns {String/number} 取决于 type
593
+ * @returns {string/number} 取决于 type
478
594
  */
479
595
  export function friendlyInterval(
480
596
  start = 0,
@@ -511,8 +627,20 @@ export function friendlyInterval(
511
627
  }
512
628
 
513
629
  /**
514
- * @method isNumber
515
- * @description 判断是否有效数字
630
+ * EN: Check whether it is a right number.
631
+ *
632
+ * ZH: 判断是否有效数字
633
+ *
634
+ * ```
635
+ * isNumber(123); // true
636
+ * isNumber('123'); // false
637
+ * // Default: NaN, Infinity is not Number
638
+ * isNumber(Infinity); // false
639
+ * isNumber(Infinity, { isUnFiniteAsNumber: true }); // true
640
+ * isNumber(NaN); // false
641
+ * isNumber(NaN, { isNaNAsNumber: true, isUnFiniteAsNumber: true }); // true
642
+ * ```
643
+ *
516
644
  * @param {*} num 被判断的值
517
645
  * @param {boolean} options.isNaNAsNumber 是否 NaN 算数字(默认不算)
518
646
  * @param {boolean} options.isUnFiniteAsNumber 是否 无限 算数字(默认不算)
@@ -619,8 +747,29 @@ export function getLocalStorage(key: string): any {
619
747
  }
620
748
 
621
749
  /**
622
- * @method loadCSS
623
- * @description 动态加载css文件
750
+ * EN: Load a CSS file from the server.
751
+ *
752
+ * ZH: 动态加载 CSS 文件
753
+ *
754
+ * ```
755
+ * loadCSS(
756
+ * 'http://example.com/css/mazey-base.css',
757
+ * {
758
+ * id: 'iamid', // Optional, link ID, default none
759
+ * }
760
+ * )
761
+ * .then(
762
+ * res => {
763
+ * console.log(`Load CSS Success: ${res}`);
764
+ * }
765
+ * )
766
+ * .catch(
767
+ * err => {
768
+ * console.error(`Load CSS Fail: ${err.message}`)
769
+ * }
770
+ * );
771
+ * ```
772
+ *
624
773
  * @param {string} url -- css资源路径
625
774
  * @param {string} options.id -- link标签id
626
775
  * @returns {Promise<boolean>} true -- 加载成功
@@ -749,9 +898,32 @@ export function loadCSS(
749
898
  }
750
899
 
751
900
  /**
752
- * @method loadScript
753
- * @description 动态加载js文件
754
- * @param {string} url -- js资源路径
901
+ * EN: Load a JavaScript file from the server and execute it.
902
+ *
903
+ * ZH: 动态加载 JavaScript 文件
904
+ *
905
+ * ```
906
+ * loadScript(
907
+ * 'http://example.com/static/js/plugin-2.1.1.min.js',
908
+ * {
909
+ * id: 'iamid', // (Optional) script ID, default none
910
+ * timeout: 5000, // (Optional) timeout, default `5000`
911
+ * isDefer: false, // (Optional) defer, default `false`
912
+ * }
913
+ * )
914
+ * .then(
915
+ * res => {
916
+ * console.log(`Load JavaScript script: ${res}`);
917
+ * }
918
+ * )
919
+ * .catch(
920
+ * err => {
921
+ * console.error(`Load JavaScript script: ${err.message}`)
922
+ * }
923
+ * );
924
+ * ```
925
+ *
926
+ * @param {string} url -- JavaScript 资源路径
755
927
  * @param {string} options.id -- DOM ID
756
928
  * @param {function} options.callback -- 加载后回调函数
757
929
  * @param {number} options.timeout -- 超时时长
@@ -908,8 +1080,44 @@ interface WebPerformance {
908
1080
  }
909
1081
 
910
1082
  /**
911
- * @method getPerformance
912
- * @description 获取页面加载相关的各项数据
1083
+ * EN: Get page load time(PerformanceTiming).
1084
+ *
1085
+ * ZH: 获取页面加载相关的各项数据
1086
+ *
1087
+ * Usage:
1088
+ *
1089
+ * ```
1090
+ * // `camelCase:true`(Default) Return hump data.
1091
+ * // `camelCase:false` Return underline data.
1092
+ * getPerformance(true)
1093
+ * .then(res => {
1094
+ * console.log(JSON.stringify(res));
1095
+ * })
1096
+ * .catch(console.error);
1097
+ * ```
1098
+ *
1099
+ * Output:
1100
+ *
1101
+ * ```
1102
+ * {"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}
1103
+ * ```
1104
+ *
1105
+ * Results:
1106
+ *
1107
+ * | Index | Field | Description |
1108
+ * | --- | --- | --- |
1109
+ * | DNS lookup | dns_time | domainLookupEnd - domainLookupStart |
1110
+ * | Connection negotiation | tcp_time | connectEnd - connectStart |
1111
+ * | Requests and responses | response_time | responseStart - requestStart |
1112
+ * | White screen | white_time | responseStart - navigationStart |
1113
+ * | DomReady | domready_time | domContentLoadedEventStart - navigationStart |
1114
+ * | Onload | onload_time | loadEventStart - navigationStart |
1115
+ * | EventEnd | render_time | loadEventEnd -navigationStart |
1116
+ * | Unload | unload_time | (Optional) unloadEventEnd - unloadEventStart |
1117
+ * | Redirect | redirect_time | (Optional) redirectEnd - redirectStart |
1118
+ * | SSL | ssl_time | (Optional) connectEnd - secureConnectionStart |
1119
+ * | Download | download_time | (Optional) responseEnd - responseStart |
1120
+ *
913
1121
  * @param {boolean} camelCase -- true(默认) 以驼峰形式返回数据 false 以下划线形式返回数据
914
1122
  * @returns {Promise<object>} 加载数据
915
1123
  */
@@ -1164,8 +1372,38 @@ export function getPerformance(
1164
1372
  }
1165
1373
 
1166
1374
  /**
1167
- * @method inRate
1168
- * @description 百分位概率
1375
+ * EN: Hit probability (1% ~ 100%).
1376
+ *
1377
+ * ZH: 百分位概率
1378
+ *
1379
+ * Usage:
1380
+ *
1381
+ * ```
1382
+ * inRate(0.5); // 0.01 ~ 1 true/false
1383
+ * ```
1384
+ *
1385
+ * Output:
1386
+ *
1387
+ * ```
1388
+ * true
1389
+ * ```
1390
+ *
1391
+ * Example: Test the precision.
1392
+ *
1393
+ * ```
1394
+ * // Test
1395
+ * let trueCount = 0;
1396
+ * let falseCount = 0;
1397
+ * new Array(1000000).fill(0).forEach(() => {
1398
+ * if (inRate(0.5)) {
1399
+ * trueCount++;
1400
+ * } else {
1401
+ * falseCount++;
1402
+ * }
1403
+ * });
1404
+ * console.log(trueCount, falseCount); // 499994 500006
1405
+ * ```
1406
+ *
1169
1407
  * @param {number} rate -- 0.1 ~ 1 => 1% ~ 100%
1170
1408
  * @returns {boolean} true 命中
1171
1409
  */
@@ -1177,8 +1415,22 @@ export function inRate(rate: number): boolean {
1177
1415
  }
1178
1416
 
1179
1417
  /**
1180
- * @method isSafePWAEnv
1181
- * @description 判断是否是安全的 PWA 环境
1418
+ * EN: Determine if it is a secure PWA environment that it can run.
1419
+ *
1420
+ * ZH: 判断是否是安全的 PWA 环境
1421
+ *
1422
+ * Usage:
1423
+ *
1424
+ * ```
1425
+ * isSafePWAEnv();
1426
+ * ```
1427
+ *
1428
+ * Output:
1429
+ *
1430
+ * ```
1431
+ * true
1432
+ * ```
1433
+ *
1182
1434
  * @returns {boolean} true 是
1183
1435
  */
1184
1436
  export function isSafePWAEnv(): boolean {
@@ -1229,10 +1481,20 @@ export function isSafePWAEnv(): boolean {
1229
1481
  *
1230
1482
  * ZH: 返回浏览器信息 https://github.com/JowayYoung/juejin-code/blob/master/browser-type.js
1231
1483
  *
1484
+ * Usage:
1485
+ *
1486
+ * ```
1487
+ * getBrowserInfo();
1488
+ * ```
1489
+ *
1490
+ * Output:
1491
+ *
1232
1492
  * ```
1233
- * getBrowserInfo(); // {"engine":"webkit","engineVs":"537.36","platform":"desktop","supporter":"chrome","supporterVs":"85.0.4183.121","system":"windows","systemVs":"10"}
1493
+ * {"engine":"webkit","engineVs":"537.36","platform":"desktop","supporter":"chrome","supporterVs":"85.0.4183.121","system":"windows","systemVs":"10"}
1234
1494
  * ```
1235
1495
  *
1496
+ * Results:
1497
+ *
1236
1498
  * | Index | Field | Description |
1237
1499
  * | --- | --- | --- |
1238
1500
  * | System | system | android, ios, windows, macos, linux |
@@ -1243,7 +1505,7 @@ export function isSafePWAEnv(): boolean {
1243
1505
  * | Supporter | supporter | edge, opera, chrome, safari, firefox, iexplore |
1244
1506
  * | Supporter version | supporterVs | - |
1245
1507
  * | Shell | shell | (Optional) wechat, qq_browser, qq_app, uc, 360, 2345, sougou, liebao, maxthon, bilibili |
1246
- * | Shell version | shellVs | (Optional) |
1508
+ * | Shell version | shellVs | (Optional) 20/... |
1247
1509
  * | Apple device type | appleType | (Optional) iphone, ipad, ipod, iwatch |
1248
1510
  *
1249
1511
  * Example: Determine the environment of the mobile QQ.
@@ -1498,8 +1760,20 @@ export function cutCHSString(str: string, len: number, hasDot = false): string {
1498
1760
  }
1499
1761
 
1500
1762
  /**
1501
- * @method windowLoaded
1502
- * @description 页面加载完成
1763
+ * EN: Check whether the page is loaded successfully (Keepe the compatibility in case that browser's `load` event has been triggered).
1764
+ *
1765
+ * ZH: 页面加载完成
1766
+ *
1767
+ * ```
1768
+ * windowLoaded(30) // second
1769
+ * .then(res => {
1770
+ * console.log(`Load Success: ${res}`); // Load Success: load
1771
+ * })
1772
+ * .catch(err => {
1773
+ * console.log(`Load Timeout or Fail: ${err.message}`);
1774
+ * });
1775
+ * ```
1776
+ *
1503
1777
  * @param {number} timeout 超时时间 / 单位:秒
1504
1778
  * @returns {Promise<string>} document is loaded? 'complete' 'load' / 'timeout'
1505
1779
  */
@@ -1607,7 +1881,28 @@ export function addStyle(
1607
1881
  }
1608
1882
 
1609
1883
  /**
1610
- * 生成自定义控制台打印
1884
+ * EN: Custom console printing (`console`).
1885
+ *
1886
+ * ZH: 生成自定义控制台打印
1887
+ *
1888
+ * Usage:
1889
+ *
1890
+ * ```
1891
+ * const myConsole = genCustomConsole('MazeyLog:');
1892
+ * myConsole.log('I am string.');
1893
+ * myConsole.info('I am boolean.', true);
1894
+ * myConsole.info('I am number.', 123, 456);
1895
+ * myConsole.info('I am object.', { a: 123, b: 456});
1896
+ * ```
1897
+ *
1898
+ * Output:
1899
+ *
1900
+ * ```
1901
+ * MazeyLog: I am string.
1902
+ * MazeyLog: I am boolean. true
1903
+ * MazeyLog: I am number. 123 456
1904
+ * MazeyLog: I am object. {a: 123, b: 456}
1905
+ * ```
1611
1906
  *
1612
1907
  * @param {string} prefix 前缀
1613
1908
  * @param {string} locales A locale string.