mazey 3.2.0 → 3.3.0

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