mazey 1.4.39 → 2.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2017 Mazey
3
+ Copyright (c) 2022 Mazey
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ English | [简体中文](./README_CN.md)
2
+
1
3
  # Mazey
2
4
 
3
5
  [![NPM version][npm-image]][npm-url]
@@ -20,111 +22,197 @@ npm install mazey --save
20
22
 
21
23
  ## Usage
22
24
 
23
- ### 加载资源
25
+ ### Load Resource
24
26
 
25
- 加载 JavaScript
27
+ #### Load JavaScript
26
28
 
27
29
  ```
28
30
  import { loadScript } from 'mazey';
29
31
 
30
32
  loadScript({
31
- url: 'http://www.mazey.net/js/plugin/jquery/jquery-2.1.1.min.js',
32
- id: 'iamid', // 可选,script 标签 ID,默认无 ID
33
- timeout: 5000, // 可选,超时时间,默认 5000
34
- isDefer: false, // 可选,defer,默认 false
33
+ url: 'http://example.com/js/plugin/jquery/jquery-2.1.1.min.js',
34
+ id: 'iamid', // Optional, script ID, default none
35
+ timeout: 5000, // Optional, timeout, default `5000`
36
+ isDefer: false, // Optional, defer, default `false`
35
37
  })
36
38
  .then(
37
39
  res => {
38
- console.log(`加载 JavaScript 成功: ${res}`);
40
+ console.log(`Load JavaScript Success: ${res}`);
39
41
  }
40
42
  )
41
43
  .catch(
42
44
  err => {
43
- console.error(`加载 JavaScript 失败: ${err}`)
45
+ console.error(`Load JavaScript Fail: ${err}`)
44
46
  }
45
47
  );
46
48
  ```
47
49
 
48
- 加载 CSS
50
+ #### Load CSS
49
51
 
50
52
  ```
51
53
  import { loadCSS } from 'mazey';
52
54
 
53
55
  loadCSS({
54
- url: 'http://www.mazey.net/css/mazey-base.css',
55
- id: 'iamid', // 可选,link 标签 ID,默认无 ID
56
+ url: 'http://example.com/css/mazey-base.css',
57
+ id: 'iamid', // Optional, link ID, default none
56
58
  })
57
59
  .then(
58
60
  res => {
59
- console.log(`加载 CSS 成功: ${res}`);
61
+ console.log(`Load CSS Success: ${res}`);
60
62
  }
61
63
  )
62
64
  .catch(
63
65
  err => {
64
- console.error(`加载 CSS 失败: ${err}`)
66
+ console.error(`Load CSS Fail: ${err}`)
65
67
  }
66
68
  );
67
69
  ```
68
70
 
69
- 判断页面是否加载完成(兼容浏览器 `load` 事件已经触发过的情况)
71
+ #### Check Load
72
+
73
+ Check whether the page is loaded successfully (Keepe the compatibility in case that browser's `load` event has been triggered).
70
74
 
71
75
  ```
72
- import { loadCSS } from 'mazey';
76
+ import { windowLoaded } from 'mazey';
73
77
 
74
- windowLoaded({ timeout: 30 })
78
+ windowLoaded({ timeout: 30 }) // second
75
79
  .then(res => {
76
- console.log(`加载完成:${res}`); // 加载完成:load
80
+ console.log(`Load Success: ${res}`); // Load Success: load
77
81
  })
78
82
  .catch(err => {
79
- console.log(`加载超时或失败:${err}`);
83
+ console.log(`Load Timeout or Fail: ${err}`);
80
84
  });
81
85
  ```
86
+ ### Function
82
87
 
83
- ### 存储数据
88
+ #### Debounce
84
89
 
85
- Storage(兼容 JSON 格式,自行转换)
90
+ ```
91
+ import { debounce } from 'mazey';
86
92
 
93
+ const foo = debounce(() => {
94
+ console.log('The debounced function will only be invoked in 1000 milliseconds, the other invoking will disappear during the wait time.');
95
+ }, 1000, { leading: true })
87
96
  ```
88
- import { setSessionStorage, getSessionStorage, setLocalStorage, getLocalStorage } from 'mazey';
89
97
 
90
- setSessionStorage('test', '123');
91
- getSessionStorage('test'); // 123
92
- setLocalStorage('test', '123');
93
- getLocalStorage('test'); // 123
98
+ #### Throttle
99
+
94
100
  ```
101
+ import { throttle } from 'mazey';
95
102
 
96
- Cookie
103
+ const foo = throttle(() => {
104
+ console.log('The function will be invoked at most once per every wait 1000 milliseconds.');
105
+ }, 1000, { leading: true })
106
+ ```
107
+
108
+ #### Check Number
109
+
110
+ Check whether it is a right number.
97
111
 
98
112
  ```
99
- import { setCookie, getCookie } from 'mazey';
113
+ import { isNumber } from 'mazey';
114
+
115
+ isNumber(123); // true
116
+ isNumber('123'); // false
117
+ // Default: NaN, Infinity is not Number
118
+ isNumber(Infinity); // false
119
+ isNumber(Infinity, { isUnFiniteAsNumber: true }); true
120
+ isNumber(NaN); // false
121
+ isNumber(NaN, { isNaNAsNumber: true, isUnFiniteAsNumber: true }); // true
122
+ ```
123
+
124
+ #### Camel Case
125
+
126
+ Transfer CamelCase to KebabCase.
127
+
128
+ ```
129
+ import { camelCaseToKebabCase } from 'mazey';
130
+
131
+ camelCaseToKebabCase('ABC'); // a-b-c
132
+ camelCaseToKebabCase('aBC'); // a-b-c
133
+ ```
134
+
135
+ Transfer CamelCase to Underscore.
136
+
137
+ ```
138
+ import { camelCase2Underscore } from 'mazey';
139
+
140
+ camelCase2Underscore('ABC'); // a_b_c
141
+ camelCase2Underscore('aBC'); // a_b_c
142
+ ```
143
+
144
+ #### Trim
145
+
146
+ Remove leading and trailing whitespace or specified characters from string.
147
+
148
+ ```
149
+ import { mTrim } from 'mazey';
150
+
151
+ mTrim(' 1 2 3 '); // '1 2 3'
152
+ mTrim('abc '); // 'abc'
153
+ ```
154
+
155
+ #### Deep Clone
156
+
157
+ Clone Object deeply.
158
+
159
+ ```
160
+ import { deepCopyObject } from 'mazey';
161
+
162
+ deepCopyObject(['a', 'b', 'c']); // ['a', 'b', 'c']
163
+ deepCopyObject('abc'); // 'abc'
164
+ ```
165
+
166
+ #### JSON
167
+
168
+ Check whether it is a valid JSON string.
169
+
170
+ ```
171
+ import { isJsonString } from 'mazey';
172
+
173
+ isJsonString(`['a', 'b', 'c']`); // false
174
+ isJsonString(`["a", "b", "c"]`); // true
175
+ ```
176
+
177
+ #### Random
178
+
179
+ Produce a random string of number, `generateRndNum(7)` => '7658495'.
100
180
 
101
- setCookie('test', '123', 30, 'mazey.net'); // key value day domain
102
- getCookie('test'); // 123
103
181
  ```
182
+ import { generateRndNum } from 'mazey';
183
+
184
+ generateRndNum(4); // '9730'
185
+ generateRndNum(7); // '2262490'
186
+ ```
187
+
188
+ ### DOM
104
189
 
105
- ### 操作 DOM
190
+ #### Class
106
191
 
107
- 修改 `class`
192
+ Modify `class`.
108
193
 
109
194
  ```
110
195
  import { hasClass, addClass, removeClass } from 'mazey';
111
196
 
112
197
  const dom = document.querySelector('#box');
113
198
 
114
- // 判断 class
199
+ // Determine `class`
115
200
  hasClass(dom, 'test');
116
- // 增加 class
201
+ // Add `class`
117
202
  addClass(dom, 'test');
118
- // 删除 class
203
+ // Remove `class`
119
204
  removeClass(dom, 'test');
120
205
  ```
121
206
 
122
- 添加内联样式
207
+ #### Style
208
+
209
+ Add inline-style.
210
+
211
+ Case 1: Add the inline style with `id`, and repeated invoking will update the content instead of adding a new one.
123
212
 
124
213
  ```
125
214
  import { addInlineStyle } from 'mazey';
126
215
 
127
- // 添加有 id 的内联样式,重复添加会更新内联样式而不是新增
128
216
  addInlineStyle({
129
217
  inlineStyle: `
130
218
  body {
@@ -138,8 +226,13 @@ addInlineStyle({
138
226
  // background-color: #333;
139
227
  // }
140
228
  // </style>
229
+ ```
230
+
231
+ Case 2: Add the inline style without `id`, and repeated invoking will adding a new one.
232
+
233
+ ```
234
+ import { addInlineStyle } from 'mazey';
141
235
 
142
- // 添加无 id 的内联样式,重复添加会新增内联样式
143
236
  addInlineStyle({
144
237
  inlineStyle: `
145
238
  body {
@@ -154,100 +247,124 @@ addInlineStyle({
154
247
  // </style>
155
248
  ```
156
249
 
157
- 添加自定义滚动条(适用于用 `transform` 移动的元素)
250
+ #### Newline
251
+
252
+ Make a newline of HTML.
158
253
 
159
254
  ```
160
- import { customScrollBarForTransformEle } from 'mazey';
255
+ import { newLine } from 'mazey';
161
256
 
162
- // 使用类名
163
- customScrollBarForTransformEle({
164
- containerClassName: 'i-am-container',
165
- imgBoxClassName: 'i-am-img-father-i-can-transform',
166
- imgClassName: 'i-am-img',
167
- });
257
+ newLine('a\nb\nc'); // 'a<br />b<br />c'
258
+ newLine('a\n\nbc'); // 'a<br /><br />bc'
259
+ ```
168
260
 
169
- // 使用 Dom 对象
170
- customScrollBarForTransformEle({
171
- containerClassName: 'i-am-container',
172
- imgBoxDom: document.querySelector('.i-am-img-father-i-can-transform'),
173
- imgDom: document.querySelector('.i-am-img-father-i-can-transform'),
174
- });
261
+ ### URL
262
+
263
+ #### Query Param
264
+
265
+ Get the query param's value of the current Web URL(`location.search`).
175
266
 
176
- // 按需直接隐藏滚动条
177
- customScrollBarForTransformEle({
178
- containerClassName: 'i-am-container',
179
- action: 'hide',
180
- });
181
267
  ```
268
+ import { getQueryParam } from 'mazey';
182
269
 
183
- 计算适配容器宽度的图片的尺寸、距离顶部的距离
270
+ // http://example.com/?t1=1&t2=2&t3=3&t4=4#2333
271
+ // ?t1=1&t2=2&t3=3&t4=4
272
+ getQueryParam('t3'); // 3
273
+ getQueryParam('t4'); // 4
274
+ ```
275
+
276
+ Get the query param's value of the input URL.
184
277
 
185
278
  ```
186
- import { calcContainImageSizeAndPosition } from 'mazey';
279
+ import { getUrlParam } from 'mazey';
187
280
 
188
- // 如果高度不足以占满容器,使其垂直居中;如果高度比容器长,由上向下铺开
189
- calcContainImageSizeAndPosition({ oriImageWidth: 300, oriImageHeight: 300, viewportWidth: 375, viewportHeight: 812 });
190
- // {"targetImageWidth":375,"targetImageHeight":375,"top":218.5,"wPer":1.25}
281
+ getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3'); // 3
282
+ getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4'); // 4
191
283
  ```
192
284
 
193
- ### 网站性能
285
+ #### Update Param
194
286
 
195
- 获取加载时间
287
+ Update the query param's value of the input URL.
196
288
 
197
289
  ```
198
- import { getPerformance } from 'mazey';
290
+ import { updateQueryParam } from 'mazey';
199
291
 
200
- // camelCase:true(默认) 以驼峰形式返回数据 / false 以下划线形式返回数据
201
- getPerformance({ camelCase: true })
202
- .then(res => {
203
- console.log(JSON.stringify(res));
204
- // {"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}
205
- })
206
- .catch(console.error);
292
+ 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
293
+ 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
207
294
  ```
208
295
 
209
- | 指标 | 字段 | 计算⽅法 |
210
- | --- | --- | --- |
211
- | * DNS 查询时间 | dns_time | domainLookupEnd - domainLookupStart |
212
- | * 服务器连接时间 | tcp_time | connectEnd - connectStart |
213
- | * 服务器响应时间 | response_time | responseStart - requestStart |
214
- | * ⽩屏时间 | white_time | responseStart - navigationStart |
215
- | * DomReady 总时间 | domready_time | domContentLoadedEventStart - navigationStart |
216
- | * 页面加载时间 | onload_time | loadEventStart - navigationStart |
217
- | * EventEnd 总时间 | render_time | loadEventEnd -navigationStart |
218
- | 上个⽂档卸载时间 | unload_time | unloadEventEnd - unloadEventStart |
219
- | 重定向时间 | redirect_time | redirectEnd - redirectStart |
220
- | 客户端⽩屏时间 | custom_white_time | renderTiming - navigationStart |
221
- | SSL连接时间 | ssl_time | connectEnd - secureConnectionStart |
222
- | ⽹⻚下载时间 | download_time | responseEnd - responseStart |
223
- | FCP | first_contentful_paint_time | firstPaintTime |
296
+ #### Hash Param
224
297
 
225
- ### 公式计算
298
+ Get the hash query param's value of the current Web URL(`location.hash`).
226
299
 
227
- 计算两个字符串的最长公共子串
300
+ ```
301
+ import { getHashQueryParam } from 'mazey';
228
302
 
303
+ // http://example.com/?#2333?t1=1&t2=2&t3=3&t4=4
304
+ // #2333?t1=1&t2=2&t3=3&t4=4
305
+ getHashQueryParam('t3'); // 3
306
+ getHashQueryParam('t4'); // 4
229
307
  ```
230
- import { calLongestCommonSubstring } from 'mazey';
231
308
 
232
- calLongestCommonSubstring('fish', 'finish'); // 3
309
+ #### Domain
310
+
311
+ Get the domain of URL, and other params.
312
+
313
+ ```
314
+ import { getDomain } from 'mazey';
315
+
316
+ getDomain('http://example.com/?t1=1&t2=2&t3=3&t4=4'); // example.com
317
+ getDomain('http://example.com/test/thanks?t1=1&t2=2&t3=3&t4=4', ['hostname', 'pathname']); // example.com/test/thanks
318
+ ```
319
+
320
+ ### Cache Data
321
+
322
+ #### Storage
323
+
324
+ Handle Storage (Keep fit for JSON, it can tansfer format automatically).
325
+
233
326
  ```
327
+ import { setSessionStorage, getSessionStorage, setLocalStorage, getLocalStorage } from 'mazey';
328
+
329
+ setSessionStorage('test', '123');
330
+ getSessionStorage('test'); // 123
331
+ setLocalStorage('test', '123');
332
+ getLocalStorage('test'); // 123
234
333
 
235
- 计算两个字符串的最长公共子序列
334
+ // or package in usage
335
+ const projectName = 'mazey';
336
+ function mSetLocalStorage (key, value) {
337
+ return setLocalStorage(`${projectName}_${key}`, value);
338
+ }
236
339
 
340
+ function mGetLocalStorage (key) {
341
+ return getLocalStorage(`${projectName}_${key}`);
342
+ }
237
343
  ```
238
- import { calLongestCommonSubsequence } from 'mazey';
239
344
 
240
- calLongestCommonSubsequence('fish', 'finish'); // 4
345
+ #### Cookie
346
+
347
+ Handle Cookie.
348
+
241
349
  ```
350
+ import { setCookie, getCookie } from 'mazey';
351
+
352
+ setCookie('test', '123', 30, 'example.com'); // key value day domain
353
+ getCookie('test'); // 123
354
+ ```
355
+
356
+ ### Calculate&Formula
357
+
358
+ #### Rate
242
359
 
243
- 命中概率(1% ~ 100%)
360
+ Hit probability (1% ~ 100%).
244
361
 
245
362
  ```
246
363
  import { inRate } from 'mazey';
247
364
 
248
365
  inRate(0.5); // 0.01 ~ 1 true / false
249
366
 
250
- // 测试准确性
367
+ // Test
251
368
  let trueCount = 0;
252
369
  let falseCount = 0;
253
370
  new Array(1000000).fill(0).forEach(() => {
@@ -260,54 +377,92 @@ new Array(1000000).fill(0).forEach(() => {
260
377
  console.log(trueCount, falseCount); // 499994 500006
261
378
  ```
262
379
 
263
- ### 浏览器信息
380
+ #### Algorithm
381
+
382
+ Computes the longest common substring of two strings.
264
383
 
265
384
  ```
266
- import { getBrowserType } from 'mazey';
385
+ import { calLongestCommonSubstring } from 'mazey';
267
386
 
268
- getBrowserType(); // {"engine":"webkit","engineVs":"537.36","platform":"desktop","supporter":"chrome","supporterVs":"85.0.4183.121","system":"windows","systemVs":"10"}
269
- // 外壳和外壳版本 { shell: 'wechat', shellVs: '20' } shell: wechat qq uc 360 2345 sougou liebao maxthon
387
+ calLongestCommonSubstring('fish', 'finish'); // 3
270
388
  ```
271
389
 
272
- ### 安全边际
390
+ Computes the longest common subsequence of two strings.
273
391
 
274
- 判断是否是安全的 PWA 运行环境
392
+ ```
393
+ import { calLongestCommonSubsequence } from 'mazey';
275
394
 
395
+ calLongestCommonSubsequence('fish', 'finish'); // 4
276
396
  ```
277
- import { isSafePWAEnv } from 'mazey';
278
397
 
279
- isSafePWAEnv(); // true
398
+ ### Browser Information
399
+
280
400
  ```
401
+ import { getBrowserInfo } from 'mazey';
281
402
 
282
- ### 函数
403
+ getBrowserInfo(); // {"engine":"webkit","engineVs":"537.36","platform":"desktop","supporter":"chrome","supporterVs":"85.0.4183.121","system":"windows","systemVs":"10"}
404
+ // Shell and shell version { shell: 'wechat', shellVs: '20' } shell: wechat qq uc 360 2345 sougou liebao maxthon
405
+ ```
283
406
 
284
- 防抖
407
+ | Index | Field | Description |
408
+ | --- | --- | --- |
409
+ | * System | system | android, ios, windows, macos, linux |
410
+ | * System version | systemVs | windows: 2000, xp, 2003, vista, 7, 8, 8.1, 10<br />macos: ... |
411
+ | * Platform | platform | desktop, mobile |
412
+ | * Engine | engine | webkit, gecko, presto, trident |
413
+ | * Engine version | engineVs | - |
414
+ | * Supporter | supporter | edge, opera, chrome, safari, firefox, iexplore |
415
+ | * Supporter version | supporterVs | - |
416
+ | Shell | shell | wechat, qq_browser, qq_app, uc, 360, 2345, sougou, liebao, maxthon, bilibili |
417
+ | Shell version | shellVs | - |
418
+ | Apple device type | appleType | iphone, ipad, ipod, iwatch |
419
+
420
+ ### Web Performance
421
+
422
+ Get page load time(PerformanceTiming).
285
423
 
286
424
  ```
287
- import { debounce } from 'mazey';
425
+ import { getPerformance } from 'mazey';
288
426
 
289
- const foo = debounce(() => {
290
- console.log('执行 1 秒内再次执行无反应');
291
- }, 1000, { leading: true })
427
+ // `camelCase:true`(Default) Return hump data.
428
+ // `camelCase:false` Return underline data.
429
+ getPerformance({ camelCase: true })
430
+ .then(res => {
431
+ console.log(JSON.stringify(res));
432
+ // {"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}
433
+ })
434
+ .catch(console.error);
292
435
  ```
293
436
 
294
- 判断是否是有效数字
437
+ | Index | Field | Calculation |
438
+ | --- | --- | --- |
439
+ | * DNS lookup | dns_time | domainLookupEnd - domainLookupStart |
440
+ | * Connection negotiation | tcp_time | connectEnd - connectStart |
441
+ | * Requests and responses | response_time | responseStart - requestStart |
442
+ | * White screen | white_time | responseStart - navigationStart |
443
+ | * DomReady | domready_time | domContentLoadedEventStart - navigationStart |
444
+ | * Onload | onload_time | loadEventStart - navigationStart |
445
+ | * EventEnd | render_time | loadEventEnd -navigationStart |
446
+ | Unload | unload_time | unloadEventEnd - unloadEventStart |
447
+ | Redirect | redirect_time | redirectEnd - redirectStart |
448
+ | SSL | ssl_time | connectEnd - secureConnectionStart |
449
+ | Download | download_time | responseEnd - responseStart |
450
+
451
+ ### Margin of Safety
452
+
453
+ Determine if it is a secure PWA environment that it can run.
295
454
 
296
455
  ```
297
- import { isNumber } from 'mazey';
456
+ import { isSafePWAEnv } from 'mazey';
298
457
 
299
- isNumber(123); // true
300
- isNumber('123'); // false
301
- // 默认情况下 NaN、Infinity 不算有效数字
302
- isNumber(Infinity); // false
303
- isNumber(Infinity, { isUnFiniteAsNumber: true }); true
304
- isNumber(NaN); // false
305
- isNumber(NaN, { isNaNAsNumber: true, isUnFiniteAsNumber: true }); // true
458
+ isSafePWAEnv(); // true
306
459
  ```
307
460
 
308
- ### 调试
461
+ ### Debug
309
462
 
310
- 自定义控制台打印(`console`)
463
+ #### Print
464
+
465
+ Custom console printing (`console`).
311
466
 
312
467
  ```
313
468
  import { genCustomConsole } from 'mazey';
@@ -319,7 +474,7 @@ myConsole.info('I am number.', 123, 456); // MazeyLog: I am number. 123 456
319
474
  myConsole.info('I am object.', { a: 123, b: 456}); // MazeyLog: I am object. {a: 123, b: 456}
320
475
  ```
321
476
 
322
- [更多接口>>](https://mazey.cn/docs/mazey/modules/_index_.html)
477
+ [More API>>](https://mazey.cn/docs/mazey/modules/_index_.html)
323
478
 
324
479
  ## Develop
325
480
 
@@ -332,4 +487,4 @@ npm run build
332
487
 
333
488
  # documentation
334
489
  npm run docs
335
- ```
490
+ ```