mazey 3.2.0 → 3.3.2

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