mazey 3.1.0 → 3.2.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/LICENSE +21 -21
- package/README.md +496 -496
- package/README_CN.md +334 -334
- package/index.js +3 -3
- package/lib/index.cjs.js +1 -1
- package/lib/index.d.ts +102 -63
- package/lib/index.d.ts.map +1 -1
- package/lib/index.esm.js +1 -1
- package/lib/mazey.min.js +1 -1
- package/package.json +109 -108
- package/scripts/release.js +28 -28
- package/src/example.html +21 -21
- package/src/example.ts +49 -49
- package/src/index.ts +1426 -1384
package/README.md
CHANGED
|
@@ -1,496 +1,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 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
|
|
214
|
-
|
|
215
|
-
Case 1: Add the
|
|
216
|
-
|
|
217
|
-
```
|
|
218
|
-
import {
|
|
219
|
-
|
|
220
|
-
|
|
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
|
|
238
|
-
|
|
239
|
-
```
|
|
240
|
-
import {
|
|
241
|
-
|
|
242
|
-
|
|
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 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
|
+
```
|