mazey 3.3.6 → 3.3.8

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
@@ -1,535 +1,535 @@
1
- English | [简体中文](https://github.com/mazeyqian/mazey/blob/master/README_CN.md)
2
-
3
- # mazey
4
-
5
- [![NPM version][npm-image]][npm-url]
6
- [![l][l-image]][l-url]
7
-
8
- [npm-image]: https://img.shields.io/npm/v/mazey
9
- [npm-url]: https://npmjs.org/package/mazey
10
- [l-image]: https://img.shields.io/npm/l/mazey
11
- [l-url]: https://github.com/mazeyqian/mazey
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.
14
-
15
- ## Install
16
-
17
- Use mazey via [npm](https://www.npmjs.com/package/mazey).
18
-
19
- ```
20
- npm install mazey --save
21
- ```
22
-
23
- Use mazey from CDN.
24
-
25
- ```
26
- <script type="text/javascript" src="//i.mazey.net/mazey/lib/mazey.min.js"></script>
27
- ```
28
-
29
- Of course, you can also download this file and serve it yourself.
30
-
31
- ## Usage
32
-
33
- Example: Use a function to load JavaScript script.
34
-
35
- Import from [npm](https://www.npmjs.com/package/mazey).
36
-
37
- ```
38
- import { isNumber } from 'mazey';
39
-
40
- isNumber(Infinity, { isUnFiniteAsNumber: true }); // true
41
- ```
42
-
43
- Import from CDN.
44
-
45
- ```
46
- <script type="text/javascript" src="//i.mazey.net/mazey/lib/mazey.min.js"></script>
47
- <script>
48
- mazey.isNumber(Infinity, { isUnFiniteAsNumber: true }); // true
49
- </script>
50
- ```
51
-
52
- ## API Examples
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).
55
-
56
- **Table of Contents**
57
-
58
- <ul>
59
- <li><a href="#load-resource">Load Resource</a>
60
- <ul>
61
- <li><a href="#load-script">Load Script</a></li>
62
- <li><a href="#load-css">Load CSS</a></li>
63
- <li><a href="#check-load">Check Load</a></li>
64
- </ul>
65
- </li>
66
- <li><a href="#function">Function</a>
67
- <ul>
68
- <li><a href="#debounce">Debounce</a></li>
69
- <li><a href="#throttle">Throttle</a></li>
70
- <li><a href="#check-number">Check Number</a></li>
71
- <li><a href="#camel-case">Camel Case</a></li>
72
- <li><a href="#trim">Trim</a></li>
73
- <li><a href="#deep-clone">Deep Clone</a></li>
74
- <li><a href="#json">JSON</a></li>
75
- <li><a href="#random">Random</a></li>
76
- </ul>
77
- </li>
78
- <li><a href="#dom">DOM</a>
79
- <ul>
80
- <li><a href="#class">Class</a></li>
81
- <li><a href="#style">Style</a></li>
82
- <li><a href="#newline">Newline</a></li>
83
- </ul>
84
- </li>
85
- <li><a href="#url">URL</a>
86
- <ul>
87
- <li><a href="#query-param">Query Param</a></li>
88
- <li><a href="#update-param">Update Param</a></li>
89
- <li><a href="#hash-param">Hash Param</a></li>
90
- <li><a href="#domain">Domain</a></li>
91
- </ul>
92
- </li>
93
- <li><a href="#cache-data">Cache Data</a>
94
- <ul>
95
- <li><a href="#storage">Storage</a></li>
96
- <li><a href="#cookie">Cookie</a></li>
97
- </ul>
98
- </li>
99
- <li><a href="#calculate-formula">Calculate&amp;Formula</a>
100
- <ul>
101
- <li><a href="#rate">Rate</a></li>
102
- <li><a href="#algorithm">Algorithm</a></li>
103
- </ul>
104
- </li>
105
- <li><a href="#browser-information">Browser Information</a></li>
106
- <li><a href="#web-performance">Web Performance</a></li>
107
- <li><a href="#margin-of-safety">Margin of Safety</a></li>
108
- <li><a href="#debug">Debug</a>
109
- <ul>
110
- <li><a href="#print">Print</a></li>
111
- </ul>
112
- </li>
113
- </ul>
114
-
115
- ### Load Resource
116
-
117
- #### Load Script
118
-
119
- ```
120
- loadScript(
121
- 'http://example.com/static/js/plugin-2.1.1.min.js',
122
- {
123
- id: 'iamid', // (Optional) script ID, default none
124
- timeout: 5000, // (Optional) timeout, default `5000`
125
- isDefer: false, // (Optional) defer, default `false`
126
- }
127
- )
128
- .then(
129
- res => {
130
- console.log(`Load JavaScript script: ${res}`);
131
- }
132
- )
133
- .catch(
134
- err => {
135
- console.error(`Load JavaScript script: ${err.message}`)
136
- }
137
- );
138
- ```
139
-
140
- #### Load CSS
141
-
142
- ```
143
- loadCSS(
144
- 'http://example.com/css/mazey-base.css',
145
- {
146
- id: 'iamid', // Optional, link ID, default none
147
- }
148
- )
149
- .then(
150
- res => {
151
- console.log(`Load CSS Success: ${res}`);
152
- }
153
- )
154
- .catch(
155
- err => {
156
- console.error(`Load CSS Fail: ${err.message}`)
157
- }
158
- );
159
- ```
160
-
161
- #### Check Load
162
-
163
- Check whether the page is loaded successfully (Keepe the compatibility in case that browser's `load` event has been triggered).
164
-
165
- ```
166
- windowLoaded(30) // second
167
- .then(res => {
168
- console.log(`Load Success: ${res}`); // Load Success: load
169
- })
170
- .catch(err => {
171
- console.log(`Load Timeout or Fail: ${err.message}`);
172
- });
173
- ```
174
-
175
- ### Function
176
-
177
- #### Debounce
178
-
179
- ```
180
- const foo = debounce(() => {
181
- console.log('The debounced function will only be invoked in 1000 milliseconds, the other invoking will disappear during the wait time.');
182
- }, 1000, true);
183
- ```
184
-
185
- #### Throttle
186
-
187
- ```
188
- const foo = throttle(() => {
189
- console.log('The function will be invoked at most once per every wait 1000 milliseconds.');
190
- }, 1000, { leading: true });
191
- ```
192
-
193
- #### Check Number
194
-
195
- Check whether it is a right number.
196
-
197
- ```
198
- isNumber(123); // true
199
- isNumber('123'); // false
200
- // Default: NaN, Infinity is not Number
201
- isNumber(Infinity); // false
202
- isNumber(Infinity, { isUnFiniteAsNumber: true }); // true
203
- isNumber(NaN); // false
204
- isNumber(NaN, { isNaNAsNumber: true, isUnFiniteAsNumber: true }); // true
205
- ```
206
-
207
- #### Camel Case
208
-
209
- Transfer CamelCase to KebabCase.
210
-
211
- ```
212
- camelCaseToKebabCase('ABC'); // a-b-c
213
- camelCaseToKebabCase('aBC'); // a-b-c
214
- ```
215
-
216
- Transfer CamelCase to Underscore.
217
-
218
- ```
219
- camelCase2Underscore('ABC'); // a_b_c
220
- camelCase2Underscore('aBC'); // a_b_c
221
- ```
222
-
223
- #### Trim
224
-
225
- Remove leading and trailing whitespace or specified characters from string.
226
-
227
- ```
228
- mTrim(' 1 2 3 '); // '1 2 3'
229
- mTrim('abc '); // 'abc'
230
- ```
231
-
232
- #### Deep Clone
233
-
234
- Clone Object deeply.
235
-
236
- ```
237
- deepCopyObject(['a', 'b', 'c']); // ['a', 'b', 'c']
238
- deepCopyObject('abc'); // 'abc'
239
- ```
240
-
241
- #### JSON
242
-
243
- Check whether it is a valid JSON string.
244
-
245
- ```
246
- isJsonString(`['a', 'b', 'c']`); // false
247
- isJsonString(`["a", "b", "c"]`); // true
248
- ```
249
-
250
- #### Random
251
-
252
- Produce a random string of number, `generateRndNum(7)` => '7658495'.
253
-
254
- ```
255
- generateRndNum(4); // '9730'
256
- generateRndNum(7); // '2262490'
257
- ```
258
-
259
- ### DOM
260
-
261
- #### Class
262
-
263
- Modify `class`.
264
-
265
- ```
266
- const dom = document.querySelector('#box');
267
-
268
- // Determine `class`
269
- hasClass(dom, 'test');
270
- // Add `class`
271
- addClass(dom, 'test');
272
- // Remove `class`
273
- removeClass(dom, 'test');
274
- ```
275
-
276
- #### Style
277
-
278
- Add `<style>` in `<head>`.
279
-
280
- Case 1: Add the `<style>` with `id`, and repeated invoking will update the content instead of adding a new one.
281
-
282
- ```
283
- addStyle(
284
- `
285
- body {
286
- background-color: #333;
287
- }
288
- `,
289
- {
290
- id: 'test',
291
- }
292
- );
293
- // <style id="test">
294
- // body {
295
- // background-color: #333;
296
- // }
297
- // </style>
298
- ```
299
-
300
- Case 2: Add the `<style>` without `id`, and repeated invoking will adding a new one.
301
-
302
- ```
303
- addStyle(
304
- `
305
- body {
306
- background-color: #444;
307
- }
308
- `
309
- );
310
- // <style>
311
- // body {
312
- // background-color: #444;
313
- // }
314
- // </style>
315
- ```
316
-
317
- #### Newline
318
-
319
- Make a newline of HTML.
320
-
321
- ```
322
- newLine('a\nb\nc'); // 'a<br />b<br />c'
323
- newLine('a\n\nbc'); // 'a<br /><br />bc'
324
- ```
325
-
326
- ### URL
327
-
328
- #### Query Param
329
-
330
- Get the query param's value of the current Web URL(`location.search`).
331
-
332
- ```
333
- // http://example.com/?t1=1&t2=2&t3=3&t4=4#2333
334
- // ?t1=1&t2=2&t3=3&t4=4
335
- getQueryParam('t3'); // 3
336
- getQueryParam('t4'); // 4
337
- ```
338
-
339
- Get the query param's value of the input URL.
340
-
341
- ```
342
- getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3'); // 3
343
- getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4'); // 4
344
- ```
345
-
346
- #### Update Param
347
-
348
- Update the query param's value of the input URL.
349
-
350
- ```
351
- updateQueryParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3', 'three'); // http://example.com/?t1=1&t2=2&t3=three&t4=4
352
- updateQueryParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4', 'four'); // http://example.com/?t1=1&t2=2&t3=3&t4=four
353
- ```
354
-
355
- #### Hash Param
356
-
357
- Get the hash query param's value of the current Web URL(`location.hash`).
358
-
359
- ```
360
- // http://example.com/?#2333?t1=1&t2=2&t3=3&t4=4
361
- // #2333?t1=1&t2=2&t3=3&t4=4
362
- getHashQueryParam('t3'); // 3
363
- getHashQueryParam('t4'); // 4
364
- ```
365
-
366
- #### Domain
367
-
368
- Get the domain of URL, and other params.
369
-
370
- ```
371
- getDomain('http://example.com/?t1=1&t2=2&t3=3&t4=4'); // example.com
372
- getDomain('http://example.com/test/thanks?t1=1&t2=2&t3=3&t4=4', ['hostname', 'pathname']); // example.com/test/thanks
373
- ```
374
-
375
- ### Cache Data
376
-
377
- #### Storage
378
-
379
- Handle Storage (Keep fit for JSON, it can tansfer format automatically).
380
-
381
- ```
382
- setSessionStorage('test', '123');
383
- getSessionStorage('test'); // 123
384
- setLocalStorage('test', '123');
385
- getLocalStorage('test'); // 123
386
-
387
- // or package in usage
388
- const projectName = 'mazey';
389
- function mSetLocalStorage (key, value) {
390
- return setLocalStorage(`${projectName}_${key}`, value);
391
- }
392
-
393
- function mGetLocalStorage (key) {
394
- return getLocalStorage(`${projectName}_${key}`);
395
- }
396
- ```
397
-
398
- #### Cookie
399
-
400
- Handle Cookie.
401
-
402
- ```
403
- setCookie('test', '123', 30, 'example.com'); // key value day domain
404
- getCookie('test'); // 123
405
- ```
406
-
407
- ### Calculate&Formula
408
-
409
- #### Rate
410
-
411
- Hit probability (1% ~ 100%).
412
-
413
- ```
414
- inRate(0.5); // 0.01 ~ 1 true / false
415
-
416
- // Test
417
- let trueCount = 0;
418
- let falseCount = 0;
419
- new Array(1000000).fill(0).forEach(() => {
420
- if (inRate(0.5)) {
421
- trueCount++;
422
- } else {
423
- falseCount++;
424
- }
425
- });
426
- console.log(trueCount, falseCount); // 499994 500006
427
- ```
428
-
429
- #### Algorithm
430
-
431
- Computes the longest common substring of two strings.
432
-
433
- ```
434
- calLongestCommonSubstring('fish', 'finish'); // 3
435
- ```
436
-
437
- Computes the longest common subsequence of two strings.
438
-
439
- ```
440
- calLongestCommonSubsequence('fish', 'finish'); // 4
441
- ```
442
-
443
- ### Browser Information
444
-
445
- ```
446
- getBrowserInfo(); // {"engine":"webkit","engineVs":"537.36","platform":"desktop","supporter":"chrome","supporterVs":"85.0.4183.121","system":"windows","systemVs":"10"}
447
- // Shell and shell version { shell: 'wechat', shellVs: '20' } shell: wechat qq uc 360 2345 sougou liebao maxthon
448
- ```
449
-
450
- | Index | Field | Description |
451
- | --- | --- | --- |
452
- | * System | system | android, ios, windows, macos, linux |
453
- | * System version | systemVs | windows: 2000, xp, 2003, vista, 7, 8, 8.1, 10<br />macos: ... |
454
- | * Platform | platform | desktop, mobile |
455
- | * Engine | engine | webkit, gecko, presto, trident |
456
- | * Engine version | engineVs | - |
457
- | * Supporter | supporter | edge, opera, chrome, safari, firefox, iexplore |
458
- | * Supporter version | supporterVs | - |
459
- | Shell | shell | wechat, qq_browser, qq_app, uc, 360, 2345, sougou, liebao, maxthon, bilibili |
460
- | Shell version | shellVs | - |
461
- | Apple device type | appleType | iphone, ipad, ipod, iwatch |
462
-
463
- ### Web Performance
464
-
465
- Get page load time(PerformanceTiming).
466
-
467
- ```
468
- // `camelCase:true`(Default) Return hump data.
469
- // `camelCase:false` Return underline data.
470
- getPerformance(true)
471
- .then(res => {
472
- console.log(JSON.stringify(res));
473
- // {"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}
474
- })
475
- .catch(console.error);
476
- ```
477
-
478
- | Index | Field | Calculation |
479
- | --- | --- | --- |
480
- | * DNS lookup | dns_time | domainLookupEnd - domainLookupStart |
481
- | * Connection negotiation | tcp_time | connectEnd - connectStart |
482
- | * Requests and responses | response_time | responseStart - requestStart |
483
- | * White screen | white_time | responseStart - navigationStart |
484
- | * DomReady | domready_time | domContentLoadedEventStart - navigationStart |
485
- | * Onload | onload_time | loadEventStart - navigationStart |
486
- | * EventEnd | render_time | loadEventEnd -navigationStart |
487
- | Unload | unload_time | unloadEventEnd - unloadEventStart |
488
- | Redirect | redirect_time | redirectEnd - redirectStart |
489
- | SSL | ssl_time | connectEnd - secureConnectionStart |
490
- | Download | download_time | responseEnd - responseStart |
491
-
492
- ### Margin of Safety
493
-
494
- Determine if it is a secure PWA environment that it can run.
495
-
496
- ```
497
- isSafePWAEnv(); // true
498
- ```
499
-
500
- ### Debug
501
-
502
- #### Print
503
-
504
- Custom console printing (`console`).
505
-
506
- ```
507
- const myConsole = genCustomConsole('MazeyLog:');
508
- myConsole.log('I am string.'); // MazeyLog: I am string.
509
- myConsole.info('I am boolean.', true); // MazeyLog: I am boolean. true
510
- myConsole.info('I am number.', 123, 456); // MazeyLog: I am number. 123 456
511
- myConsole.info('I am object.', { a: 123, b: 456}); // MazeyLog: I am object. {a: 123, b: 456}
512
- ```
513
-
514
- ## Contributing
515
-
516
- ```
517
- # Install
518
- npm i --registry=https://registry.npmjs.org
519
-
520
- # Serve
521
- npm run dev
522
-
523
- # Build
524
- npm run build
525
-
526
- # Document
527
- npm run docs
528
-
529
- # Test
530
- npm run test
531
- ```
532
-
533
- ## License
534
-
535
- This software is released under the terms of the [MIT license](https://github.com/mazeyqian/mazey/blob/master/LICENSE).
1
+ English | [简体中文](https://github.com/mazeyqian/mazey/blob/master/README_CN.md)
2
+
3
+ # mazey
4
+
5
+ [![NPM version][npm-image]][npm-url]
6
+ [![l][l-image]][l-url]
7
+
8
+ [npm-image]: https://img.shields.io/npm/v/mazey
9
+ [npm-url]: https://npmjs.org/package/mazey
10
+ [l-image]: https://img.shields.io/npm/l/mazey
11
+ [l-url]: https://github.com/mazeyqian/mazey
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.
14
+
15
+ ## Install
16
+
17
+ Use mazey via [npm](https://www.npmjs.com/package/mazey).
18
+
19
+ ```
20
+ npm install mazey --save
21
+ ```
22
+
23
+ Use mazey from CDN.
24
+
25
+ ```
26
+ <script type="text/javascript" src="//i.mazey.net/mazey/lib/mazey.min.js"></script>
27
+ ```
28
+
29
+ Of course, you can also download this file and serve it yourself.
30
+
31
+ ## Usage
32
+
33
+ Example: Use a function to load JavaScript script.
34
+
35
+ Import from [npm](https://www.npmjs.com/package/mazey).
36
+
37
+ ```
38
+ import { isNumber } from 'mazey';
39
+
40
+ isNumber(Infinity, { isUnFiniteAsNumber: true }); // true
41
+ ```
42
+
43
+ Import from CDN.
44
+
45
+ ```
46
+ <script type="text/javascript" src="//i.mazey.net/mazey/lib/mazey.min.js"></script>
47
+ <script>
48
+ mazey.isNumber(Infinity, { isUnFiniteAsNumber: true }); // true
49
+ </script>
50
+ ```
51
+
52
+ ## API Examples
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).
55
+
56
+ **Table of Contents**
57
+
58
+ <ul>
59
+ <li><a href="#load-resource">Load Resource</a>
60
+ <ul>
61
+ <li><a href="#load-script">Load Script</a></li>
62
+ <li><a href="#load-css">Load CSS</a></li>
63
+ <li><a href="#check-load">Check Load</a></li>
64
+ </ul>
65
+ </li>
66
+ <li><a href="#function">Function</a>
67
+ <ul>
68
+ <li><a href="#debounce">Debounce</a></li>
69
+ <li><a href="#throttle">Throttle</a></li>
70
+ <li><a href="#check-number">Check Number</a></li>
71
+ <li><a href="#camel-case">Camel Case</a></li>
72
+ <li><a href="#trim">Trim</a></li>
73
+ <li><a href="#deep-clone">Deep Clone</a></li>
74
+ <li><a href="#json">JSON</a></li>
75
+ <li><a href="#random">Random</a></li>
76
+ </ul>
77
+ </li>
78
+ <li><a href="#dom">DOM</a>
79
+ <ul>
80
+ <li><a href="#class">Class</a></li>
81
+ <li><a href="#style">Style</a></li>
82
+ <li><a href="#newline">Newline</a></li>
83
+ </ul>
84
+ </li>
85
+ <li><a href="#url">URL</a>
86
+ <ul>
87
+ <li><a href="#query-param">Query Param</a></li>
88
+ <li><a href="#update-param">Update Param</a></li>
89
+ <li><a href="#hash-param">Hash Param</a></li>
90
+ <li><a href="#domain">Domain</a></li>
91
+ </ul>
92
+ </li>
93
+ <li><a href="#cache-data">Cache Data</a>
94
+ <ul>
95
+ <li><a href="#storage">Storage</a></li>
96
+ <li><a href="#cookie">Cookie</a></li>
97
+ </ul>
98
+ </li>
99
+ <li><a href="#calculate-formula">Calculate&amp;Formula</a>
100
+ <ul>
101
+ <li><a href="#rate">Rate</a></li>
102
+ <li><a href="#algorithm">Algorithm</a></li>
103
+ </ul>
104
+ </li>
105
+ <li><a href="#browser-information">Browser Information</a></li>
106
+ <li><a href="#web-performance">Web Performance</a></li>
107
+ <li><a href="#margin-of-safety">Margin of Safety</a></li>
108
+ <li><a href="#debug">Debug</a>
109
+ <ul>
110
+ <li><a href="#print">Print</a></li>
111
+ </ul>
112
+ </li>
113
+ </ul>
114
+
115
+ ### Load Resource
116
+
117
+ #### Load Script
118
+
119
+ ```
120
+ loadScript(
121
+ 'http://example.com/static/js/plugin-2.1.1.min.js',
122
+ {
123
+ id: 'iamid', // (Optional) script ID, default none
124
+ timeout: 5000, // (Optional) timeout, default `5000`
125
+ isDefer: false, // (Optional) defer, default `false`
126
+ }
127
+ )
128
+ .then(
129
+ res => {
130
+ console.log(`Load JavaScript script: ${res}`);
131
+ }
132
+ )
133
+ .catch(
134
+ err => {
135
+ console.error(`Load JavaScript script: ${err.message}`)
136
+ }
137
+ );
138
+ ```
139
+
140
+ #### Load CSS
141
+
142
+ ```
143
+ loadCSS(
144
+ 'http://example.com/css/mazey-base.css',
145
+ {
146
+ id: 'iamid', // Optional, link ID, default none
147
+ }
148
+ )
149
+ .then(
150
+ res => {
151
+ console.log(`Load CSS Success: ${res}`);
152
+ }
153
+ )
154
+ .catch(
155
+ err => {
156
+ console.error(`Load CSS Fail: ${err.message}`)
157
+ }
158
+ );
159
+ ```
160
+
161
+ #### Check Load
162
+
163
+ Check whether the page is loaded successfully (Keepe the compatibility in case that browser's `load` event has been triggered).
164
+
165
+ ```
166
+ windowLoaded(30) // second
167
+ .then(res => {
168
+ console.log(`Load Success: ${res}`); // Load Success: load
169
+ })
170
+ .catch(err => {
171
+ console.log(`Load Timeout or Fail: ${err.message}`);
172
+ });
173
+ ```
174
+
175
+ ### Function
176
+
177
+ #### Debounce
178
+
179
+ ```
180
+ const foo = debounce(() => {
181
+ console.log('The debounced function will only be invoked in 1000 milliseconds, the other invoking will disappear during the wait time.');
182
+ }, 1000, true);
183
+ ```
184
+
185
+ #### Throttle
186
+
187
+ ```
188
+ const foo = throttle(() => {
189
+ console.log('The function will be invoked at most once per every wait 1000 milliseconds.');
190
+ }, 1000, { leading: true });
191
+ ```
192
+
193
+ #### Check Number
194
+
195
+ Check whether it is a right number.
196
+
197
+ ```
198
+ isNumber(123); // true
199
+ isNumber('123'); // false
200
+ // Default: NaN, Infinity is not Number
201
+ isNumber(Infinity); // false
202
+ isNumber(Infinity, { isUnFiniteAsNumber: true }); // true
203
+ isNumber(NaN); // false
204
+ isNumber(NaN, { isNaNAsNumber: true, isUnFiniteAsNumber: true }); // true
205
+ ```
206
+
207
+ #### Camel Case
208
+
209
+ Transfer CamelCase to KebabCase.
210
+
211
+ ```
212
+ camelCaseToKebabCase('ABC'); // a-b-c
213
+ camelCaseToKebabCase('aBC'); // a-b-c
214
+ ```
215
+
216
+ Transfer CamelCase to Underscore.
217
+
218
+ ```
219
+ camelCase2Underscore('ABC'); // a_b_c
220
+ camelCase2Underscore('aBC'); // a_b_c
221
+ ```
222
+
223
+ #### Trim
224
+
225
+ Remove leading and trailing whitespace or specified characters from string.
226
+
227
+ ```
228
+ mTrim(' 1 2 3 '); // '1 2 3'
229
+ mTrim('abc '); // 'abc'
230
+ ```
231
+
232
+ #### Deep Clone
233
+
234
+ Clone Object deeply.
235
+
236
+ ```
237
+ deepCopyObject(['a', 'b', 'c']); // ['a', 'b', 'c']
238
+ deepCopyObject('abc'); // 'abc'
239
+ ```
240
+
241
+ #### JSON
242
+
243
+ Check whether it is a valid JSON string.
244
+
245
+ ```
246
+ isJsonString(`['a', 'b', 'c']`); // false
247
+ isJsonString(`["a", "b", "c"]`); // true
248
+ ```
249
+
250
+ #### Random
251
+
252
+ Produce a random string of number, `generateRndNum(7)` => '7658495'.
253
+
254
+ ```
255
+ generateRndNum(4); // '9730'
256
+ generateRndNum(7); // '2262490'
257
+ ```
258
+
259
+ ### DOM
260
+
261
+ #### Class
262
+
263
+ Modify `class`.
264
+
265
+ ```
266
+ const dom = document.querySelector('#box');
267
+
268
+ // Determine `class`
269
+ hasClass(dom, 'test');
270
+ // Add `class`
271
+ addClass(dom, 'test');
272
+ // Remove `class`
273
+ removeClass(dom, 'test');
274
+ ```
275
+
276
+ #### Style
277
+
278
+ Add `<style>` in `<head>`.
279
+
280
+ Case 1: Add the `<style>` with `id`, and repeated invoking will update the content instead of adding a new one.
281
+
282
+ ```
283
+ addStyle(
284
+ `
285
+ body {
286
+ background-color: #333;
287
+ }
288
+ `,
289
+ {
290
+ id: 'test',
291
+ }
292
+ );
293
+ // <style id="test">
294
+ // body {
295
+ // background-color: #333;
296
+ // }
297
+ // </style>
298
+ ```
299
+
300
+ Case 2: Add the `<style>` without `id`, and repeated invoking will adding a new one.
301
+
302
+ ```
303
+ addStyle(
304
+ `
305
+ body {
306
+ background-color: #444;
307
+ }
308
+ `
309
+ );
310
+ // <style>
311
+ // body {
312
+ // background-color: #444;
313
+ // }
314
+ // </style>
315
+ ```
316
+
317
+ #### Newline
318
+
319
+ Make a newline of HTML.
320
+
321
+ ```
322
+ newLine('a\nb\nc'); // 'a<br />b<br />c'
323
+ newLine('a\n\nbc'); // 'a<br /><br />bc'
324
+ ```
325
+
326
+ ### URL
327
+
328
+ #### Query Param
329
+
330
+ Get the query param's value of the current Web URL(`location.search`).
331
+
332
+ ```
333
+ // http://example.com/?t1=1&t2=2&t3=3&t4=4#2333
334
+ // ?t1=1&t2=2&t3=3&t4=4
335
+ getQueryParam('t3'); // 3
336
+ getQueryParam('t4'); // 4
337
+ ```
338
+
339
+ Get the query param's value of the input URL.
340
+
341
+ ```
342
+ getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3'); // 3
343
+ getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4'); // 4
344
+ ```
345
+
346
+ #### Update Param
347
+
348
+ Update the query param's value of the input URL.
349
+
350
+ ```
351
+ updateQueryParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3', 'three'); // http://example.com/?t1=1&t2=2&t3=three&t4=4
352
+ updateQueryParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4', 'four'); // http://example.com/?t1=1&t2=2&t3=3&t4=four
353
+ ```
354
+
355
+ #### Hash Param
356
+
357
+ Get the hash query param's value of the current Web URL(`location.hash`).
358
+
359
+ ```
360
+ // http://example.com/?#2333?t1=1&t2=2&t3=3&t4=4
361
+ // #2333?t1=1&t2=2&t3=3&t4=4
362
+ getHashQueryParam('t3'); // 3
363
+ getHashQueryParam('t4'); // 4
364
+ ```
365
+
366
+ #### Domain
367
+
368
+ Get the domain of URL, and other params.
369
+
370
+ ```
371
+ getDomain('http://example.com/?t1=1&t2=2&t3=3&t4=4'); // example.com
372
+ getDomain('http://example.com/test/thanks?t1=1&t2=2&t3=3&t4=4', ['hostname', 'pathname']); // example.com/test/thanks
373
+ ```
374
+
375
+ ### Cache Data
376
+
377
+ #### Storage
378
+
379
+ Handle Storage (Keep fit for JSON, it can tansfer format automatically).
380
+
381
+ ```
382
+ setSessionStorage('test', '123');
383
+ getSessionStorage('test'); // 123
384
+ setLocalStorage('test', '123');
385
+ getLocalStorage('test'); // 123
386
+
387
+ // or package in usage
388
+ const projectName = 'mazey';
389
+ function mSetLocalStorage (key, value) {
390
+ return setLocalStorage(`${projectName}_${key}`, value);
391
+ }
392
+
393
+ function mGetLocalStorage (key) {
394
+ return getLocalStorage(`${projectName}_${key}`);
395
+ }
396
+ ```
397
+
398
+ #### Cookie
399
+
400
+ Handle Cookie.
401
+
402
+ ```
403
+ setCookie('test', '123', 30, 'example.com'); // key value day domain
404
+ getCookie('test'); // 123
405
+ ```
406
+
407
+ ### Calculate&Formula
408
+
409
+ #### Rate
410
+
411
+ Hit probability (1% ~ 100%).
412
+
413
+ ```
414
+ inRate(0.5); // 0.01 ~ 1 true / false
415
+
416
+ // Test
417
+ let trueCount = 0;
418
+ let falseCount = 0;
419
+ new Array(1000000).fill(0).forEach(() => {
420
+ if (inRate(0.5)) {
421
+ trueCount++;
422
+ } else {
423
+ falseCount++;
424
+ }
425
+ });
426
+ console.log(trueCount, falseCount); // 499994 500006
427
+ ```
428
+
429
+ #### Algorithm
430
+
431
+ Computes the longest common substring of two strings.
432
+
433
+ ```
434
+ calLongestCommonSubstring('fish', 'finish'); // 3
435
+ ```
436
+
437
+ Computes the longest common subsequence of two strings.
438
+
439
+ ```
440
+ calLongestCommonSubsequence('fish', 'finish'); // 4
441
+ ```
442
+
443
+ ### Browser Information
444
+
445
+ ```
446
+ getBrowserInfo(); // {"engine":"webkit","engineVs":"537.36","platform":"desktop","supporter":"chrome","supporterVs":"85.0.4183.121","system":"windows","systemVs":"10"}
447
+ // Shell and shell version { shell: 'wechat', shellVs: '20' } shell: wechat qq uc 360 2345 sougou liebao maxthon
448
+ ```
449
+
450
+ | Index | Field | Description |
451
+ | --- | --- | --- |
452
+ | * System | system | android, ios, windows, macos, linux |
453
+ | * System version | systemVs | windows: 2000, xp, 2003, vista, 7, 8, 8.1, 10<br />macos: ... |
454
+ | * Platform | platform | desktop, mobile |
455
+ | * Engine | engine | webkit, gecko, presto, trident |
456
+ | * Engine version | engineVs | - |
457
+ | * Supporter | supporter | edge, opera, chrome, safari, firefox, iexplore |
458
+ | * Supporter version | supporterVs | - |
459
+ | Shell | shell | wechat, qq_browser, qq_app, uc, 360, 2345, sougou, liebao, maxthon, bilibili |
460
+ | Shell version | shellVs | - |
461
+ | Apple device type | appleType | iphone, ipad, ipod, iwatch |
462
+
463
+ ### Web Performance
464
+
465
+ Get page load time(PerformanceTiming).
466
+
467
+ ```
468
+ // `camelCase:true`(Default) Return hump data.
469
+ // `camelCase:false` Return underline data.
470
+ getPerformance(true)
471
+ .then(res => {
472
+ console.log(JSON.stringify(res));
473
+ // {"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}
474
+ })
475
+ .catch(console.error);
476
+ ```
477
+
478
+ | Index | Field | Calculation |
479
+ | --- | --- | --- |
480
+ | * DNS lookup | dns_time | domainLookupEnd - domainLookupStart |
481
+ | * Connection negotiation | tcp_time | connectEnd - connectStart |
482
+ | * Requests and responses | response_time | responseStart - requestStart |
483
+ | * White screen | white_time | responseStart - navigationStart |
484
+ | * DomReady | domready_time | domContentLoadedEventStart - navigationStart |
485
+ | * Onload | onload_time | loadEventStart - navigationStart |
486
+ | * EventEnd | render_time | loadEventEnd -navigationStart |
487
+ | Unload | unload_time | unloadEventEnd - unloadEventStart |
488
+ | Redirect | redirect_time | redirectEnd - redirectStart |
489
+ | SSL | ssl_time | connectEnd - secureConnectionStart |
490
+ | Download | download_time | responseEnd - responseStart |
491
+
492
+ ### Margin of Safety
493
+
494
+ Determine if it is a secure PWA environment that it can run.
495
+
496
+ ```
497
+ isSafePWAEnv(); // true
498
+ ```
499
+
500
+ ### Debug
501
+
502
+ #### Print
503
+
504
+ Custom console printing (`console`).
505
+
506
+ ```
507
+ const myConsole = genCustomConsole('MazeyLog:');
508
+ myConsole.log('I am string.'); // MazeyLog: I am string.
509
+ myConsole.info('I am boolean.', true); // MazeyLog: I am boolean. true
510
+ myConsole.info('I am number.', 123, 456); // MazeyLog: I am number. 123 456
511
+ myConsole.info('I am object.', { a: 123, b: 456}); // MazeyLog: I am object. {a: 123, b: 456}
512
+ ```
513
+
514
+ ## Contributing
515
+
516
+ ```
517
+ # Install
518
+ npm i --registry=https://registry.npmjs.org
519
+
520
+ # Serve
521
+ npm run dev
522
+
523
+ # Build
524
+ npm run build
525
+
526
+ # Document
527
+ npm run docs
528
+
529
+ # Test
530
+ npm run test
531
+ ```
532
+
533
+ ## License
534
+
535
+ This software is released under the terms of the [MIT license](https://github.com/mazeyqian/mazey/blob/master/LICENSE).