mazey 3.3.6 → 3.3.7

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_CN.md CHANGED
@@ -1,335 +1,335 @@
1
- # mazey
2
-
3
- [![NPM version][npm-image]][npm-url]
4
- [![l][l-image]][l-url]
5
-
6
- [npm-image]: ./images/mazey-v1.4.39.svg
7
- [npm-url]: https://www.npmjs.com/package/mazey/v/1.4.39
8
- [l-image]: https://img.shields.io/npm/l/mazey
9
- [l-url]: https://github.com/mazeyqian/mazey
10
-
11
- 后除的前端函数库
12
-
13
- ## Install
14
-
15
- You can get mazey via [npm](https://www.npmjs.com/package/mazey/v/1.4.39).
16
-
17
- ```
18
- npm install mazey@1.4.39 --save
19
- ```
20
-
21
- ## Usage
22
-
23
- ### 加载资源
24
-
25
- 加载 JavaScript
26
-
27
- ```
28
- import { loadScript } from 'mazey';
29
-
30
- 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
35
- })
36
- .then(
37
- res => {
38
- console.log(`加载 JavaScript 成功: ${res}`);
39
- }
40
- )
41
- .catch(
42
- err => {
43
- console.error(`加载 JavaScript 失败: ${err}`)
44
- }
45
- );
46
- ```
47
-
48
- 加载 CSS
49
-
50
- ```
51
- import { loadCSS } from 'mazey';
52
-
53
- loadCSS({
54
- url: 'http://www.mazey.net/css/mazey-base.css',
55
- id: 'iamid', // 可选,link 标签 ID,默认无 ID
56
- })
57
- .then(
58
- res => {
59
- console.log(`加载 CSS 成功: ${res}`);
60
- }
61
- )
62
- .catch(
63
- err => {
64
- console.error(`加载 CSS 失败: ${err}`)
65
- }
66
- );
67
- ```
68
-
69
- 判断页面是否加载完成(兼容浏览器 `load` 事件已经触发过的情况)
70
-
71
- ```
72
- import { loadCSS } from 'mazey';
73
-
74
- windowLoaded({ timeout: 30 })
75
- .then(res => {
76
- console.log(`加载完成:${res}`); // 加载完成:load
77
- })
78
- .catch(err => {
79
- console.log(`加载超时或失败:${err}`);
80
- });
81
- ```
82
-
83
- ### 存储数据
84
-
85
- Storage(兼容 JSON 格式,自行转换)
86
-
87
- ```
88
- import { setSessionStorage, getSessionStorage, setLocalStorage, getLocalStorage } from 'mazey';
89
-
90
- setSessionStorage('test', '123');
91
- getSessionStorage('test'); // 123
92
- setLocalStorage('test', '123');
93
- getLocalStorage('test'); // 123
94
- ```
95
-
96
- Cookie
97
-
98
- ```
99
- import { setCookie, getCookie } from 'mazey';
100
-
101
- setCookie('test', '123', 30, 'mazey.net'); // key value day domain
102
- getCookie('test'); // 123
103
- ```
104
-
105
- ### 操作 DOM
106
-
107
- 修改 `class`
108
-
109
- ```
110
- import { hasClass, addClass, removeClass } from 'mazey';
111
-
112
- const dom = document.querySelector('#box');
113
-
114
- // 判断 class
115
- hasClass(dom, 'test');
116
- // 增加 class
117
- addClass(dom, 'test');
118
- // 删除 class
119
- removeClass(dom, 'test');
120
- ```
121
-
122
- 添加内联样式
123
-
124
- ```
125
- import { addInlineStyle } from 'mazey';
126
-
127
- // 添加有 id 的内联样式,重复添加会更新内联样式而不是新增
128
- addInlineStyle({
129
- inlineStyle: `
130
- body {
131
- background-color: #333;
132
- }
133
- `,
134
- id: 'test',
135
- });
136
- // <style id="test">
137
- // body {
138
- // background-color: #333;
139
- // }
140
- // </style>
141
-
142
- // 添加无 id 的内联样式,重复添加会新增内联样式
143
- addInlineStyle({
144
- inlineStyle: `
145
- body {
146
- background-color: #444;
147
- }
148
- `,
149
- });
150
- // <style>
151
- // body {
152
- // background-color: #444;
153
- // }
154
- // </style>
155
- ```
156
-
157
- 添加自定义滚动条(适用于用 `transform` 移动的元素)
158
-
159
- ```
160
- import { customScrollBarForTransformEle } from 'mazey';
161
-
162
- // 使用类名
163
- customScrollBarForTransformEle({
164
- containerClassName: 'i-am-container',
165
- imgBoxClassName: 'i-am-img-father-i-can-transform',
166
- imgClassName: 'i-am-img',
167
- });
168
-
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
- });
175
-
176
- // 按需直接隐藏滚动条
177
- customScrollBarForTransformEle({
178
- containerClassName: 'i-am-container',
179
- action: 'hide',
180
- });
181
- ```
182
-
183
- 计算适配容器宽度的图片的尺寸、距离顶部的距离
184
-
185
- ```
186
- import { calcContainImageSizeAndPosition } from 'mazey';
187
-
188
- // 如果高度不足以占满容器,使其垂直居中;如果高度比容器长,由上向下铺开
189
- calcContainImageSizeAndPosition({ oriImageWidth: 300, oriImageHeight: 300, viewportWidth: 375, viewportHeight: 812 });
190
- // {"targetImageWidth":375,"targetImageHeight":375,"top":218.5,"wPer":1.25}
191
- ```
192
-
193
- ### 网站性能
194
-
195
- 获取加载时间
196
-
197
- ```
198
- import { getPerformance } from 'mazey';
199
-
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);
207
- ```
208
-
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 |
224
-
225
- ### 公式计算
226
-
227
- 计算两个字符串的最长公共子串
228
-
229
- ```
230
- import { calLongestCommonSubstring } from 'mazey';
231
-
232
- calLongestCommonSubstring('fish', 'finish'); // 3
233
- ```
234
-
235
- 计算两个字符串的最长公共子序列
236
-
237
- ```
238
- import { calLongestCommonSubsequence } from 'mazey';
239
-
240
- calLongestCommonSubsequence('fish', 'finish'); // 4
241
- ```
242
-
243
- 命中概率(1% ~ 100%)
244
-
245
- ```
246
- import { inRate } from 'mazey';
247
-
248
- inRate(0.5); // 0.01 ~ 1 true / false
249
-
250
- // 测试准确性
251
- let trueCount = 0;
252
- let falseCount = 0;
253
- new Array(1000000).fill(0).forEach(() => {
254
- if (inRate(0.5)) {
255
- trueCount++;
256
- } else {
257
- falseCount++;
258
- }
259
- });
260
- console.log(trueCount, falseCount); // 499994 500006
261
- ```
262
-
263
- ### 浏览器信息
264
-
265
- ```
266
- import { getBrowserType } from 'mazey';
267
-
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
270
- ```
271
-
272
- ### 安全边际
273
-
274
- 判断是否是安全的 PWA 运行环境
275
-
276
- ```
277
- import { isSafePWAEnv } from 'mazey';
278
-
279
- isSafePWAEnv(); // true
280
- ```
281
-
282
- ### 函数
283
-
284
- 防抖
285
-
286
- ```
287
- import { debounce } from 'mazey';
288
-
289
- const foo = debounce(() => {
290
- console.log('执行 1 秒内再次执行无反应');
291
- }, 1000, { leading: true })
292
- ```
293
-
294
- 判断是否是有效数字
295
-
296
- ```
297
- import { isNumber } from 'mazey';
298
-
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
306
- ```
307
-
308
- ### 调试
309
-
310
- 自定义控制台打印(`console`)
311
-
312
- ```
313
- import { genCustomConsole } from 'mazey';
314
-
315
- const myConsole = genCustomConsole({ prefix: 'MazeyLog:' });
316
- myConsole.log('I am string.'); // MazeyLog: I am string.
317
- myConsole.info('I am boolean.', true); // MazeyLog: I am boolean. true
318
- myConsole.info('I am number.', 123, 456); // MazeyLog: I am number. 123 456
319
- myConsole.info('I am object.', { a: 123, b: 456}); // MazeyLog: I am object. {a: 123, b: 456}
320
- ```
321
-
322
- [更多接口>>](https://mazey.cn/docs/mazey/modules/_index_.html)
323
-
324
- ## Develop
325
-
326
- ```
327
- # dev
328
- npm run dev
329
-
330
- # build
331
- npm run build
332
-
333
- # documentation
334
- npm run docs
1
+ # mazey
2
+
3
+ [![NPM version][npm-image]][npm-url]
4
+ [![l][l-image]][l-url]
5
+
6
+ [npm-image]: ./images/mazey-v1.4.39.svg
7
+ [npm-url]: https://www.npmjs.com/package/mazey/v/1.4.39
8
+ [l-image]: https://img.shields.io/npm/l/mazey
9
+ [l-url]: https://github.com/mazeyqian/mazey
10
+
11
+ 后除的前端函数库
12
+
13
+ ## Install
14
+
15
+ You can get mazey via [npm](https://www.npmjs.com/package/mazey/v/1.4.39).
16
+
17
+ ```
18
+ npm install mazey@1.4.39 --save
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### 加载资源
24
+
25
+ 加载 JavaScript
26
+
27
+ ```
28
+ import { loadScript } from 'mazey';
29
+
30
+ 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
35
+ })
36
+ .then(
37
+ res => {
38
+ console.log(`加载 JavaScript 成功: ${res}`);
39
+ }
40
+ )
41
+ .catch(
42
+ err => {
43
+ console.error(`加载 JavaScript 失败: ${err}`)
44
+ }
45
+ );
46
+ ```
47
+
48
+ 加载 CSS
49
+
50
+ ```
51
+ import { loadCSS } from 'mazey';
52
+
53
+ loadCSS({
54
+ url: 'http://www.mazey.net/css/mazey-base.css',
55
+ id: 'iamid', // 可选,link 标签 ID,默认无 ID
56
+ })
57
+ .then(
58
+ res => {
59
+ console.log(`加载 CSS 成功: ${res}`);
60
+ }
61
+ )
62
+ .catch(
63
+ err => {
64
+ console.error(`加载 CSS 失败: ${err}`)
65
+ }
66
+ );
67
+ ```
68
+
69
+ 判断页面是否加载完成(兼容浏览器 `load` 事件已经触发过的情况)
70
+
71
+ ```
72
+ import { loadCSS } from 'mazey';
73
+
74
+ windowLoaded({ timeout: 30 })
75
+ .then(res => {
76
+ console.log(`加载完成:${res}`); // 加载完成:load
77
+ })
78
+ .catch(err => {
79
+ console.log(`加载超时或失败:${err}`);
80
+ });
81
+ ```
82
+
83
+ ### 存储数据
84
+
85
+ Storage(兼容 JSON 格式,自行转换)
86
+
87
+ ```
88
+ import { setSessionStorage, getSessionStorage, setLocalStorage, getLocalStorage } from 'mazey';
89
+
90
+ setSessionStorage('test', '123');
91
+ getSessionStorage('test'); // 123
92
+ setLocalStorage('test', '123');
93
+ getLocalStorage('test'); // 123
94
+ ```
95
+
96
+ Cookie
97
+
98
+ ```
99
+ import { setCookie, getCookie } from 'mazey';
100
+
101
+ setCookie('test', '123', 30, 'mazey.net'); // key value day domain
102
+ getCookie('test'); // 123
103
+ ```
104
+
105
+ ### 操作 DOM
106
+
107
+ 修改 `class`
108
+
109
+ ```
110
+ import { hasClass, addClass, removeClass } from 'mazey';
111
+
112
+ const dom = document.querySelector('#box');
113
+
114
+ // 判断 class
115
+ hasClass(dom, 'test');
116
+ // 增加 class
117
+ addClass(dom, 'test');
118
+ // 删除 class
119
+ removeClass(dom, 'test');
120
+ ```
121
+
122
+ 添加内联样式
123
+
124
+ ```
125
+ import { addInlineStyle } from 'mazey';
126
+
127
+ // 添加有 id 的内联样式,重复添加会更新内联样式而不是新增
128
+ addInlineStyle({
129
+ inlineStyle: `
130
+ body {
131
+ background-color: #333;
132
+ }
133
+ `,
134
+ id: 'test',
135
+ });
136
+ // <style id="test">
137
+ // body {
138
+ // background-color: #333;
139
+ // }
140
+ // </style>
141
+
142
+ // 添加无 id 的内联样式,重复添加会新增内联样式
143
+ addInlineStyle({
144
+ inlineStyle: `
145
+ body {
146
+ background-color: #444;
147
+ }
148
+ `,
149
+ });
150
+ // <style>
151
+ // body {
152
+ // background-color: #444;
153
+ // }
154
+ // </style>
155
+ ```
156
+
157
+ 添加自定义滚动条(适用于用 `transform` 移动的元素)
158
+
159
+ ```
160
+ import { customScrollBarForTransformEle } from 'mazey';
161
+
162
+ // 使用类名
163
+ customScrollBarForTransformEle({
164
+ containerClassName: 'i-am-container',
165
+ imgBoxClassName: 'i-am-img-father-i-can-transform',
166
+ imgClassName: 'i-am-img',
167
+ });
168
+
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
+ });
175
+
176
+ // 按需直接隐藏滚动条
177
+ customScrollBarForTransformEle({
178
+ containerClassName: 'i-am-container',
179
+ action: 'hide',
180
+ });
181
+ ```
182
+
183
+ 计算适配容器宽度的图片的尺寸、距离顶部的距离
184
+
185
+ ```
186
+ import { calcContainImageSizeAndPosition } from 'mazey';
187
+
188
+ // 如果高度不足以占满容器,使其垂直居中;如果高度比容器长,由上向下铺开
189
+ calcContainImageSizeAndPosition({ oriImageWidth: 300, oriImageHeight: 300, viewportWidth: 375, viewportHeight: 812 });
190
+ // {"targetImageWidth":375,"targetImageHeight":375,"top":218.5,"wPer":1.25}
191
+ ```
192
+
193
+ ### 网站性能
194
+
195
+ 获取加载时间
196
+
197
+ ```
198
+ import { getPerformance } from 'mazey';
199
+
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);
207
+ ```
208
+
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 |
224
+
225
+ ### 公式计算
226
+
227
+ 计算两个字符串的最长公共子串
228
+
229
+ ```
230
+ import { calLongestCommonSubstring } from 'mazey';
231
+
232
+ calLongestCommonSubstring('fish', 'finish'); // 3
233
+ ```
234
+
235
+ 计算两个字符串的最长公共子序列
236
+
237
+ ```
238
+ import { calLongestCommonSubsequence } from 'mazey';
239
+
240
+ calLongestCommonSubsequence('fish', 'finish'); // 4
241
+ ```
242
+
243
+ 命中概率(1% ~ 100%)
244
+
245
+ ```
246
+ import { inRate } from 'mazey';
247
+
248
+ inRate(0.5); // 0.01 ~ 1 true / false
249
+
250
+ // 测试准确性
251
+ let trueCount = 0;
252
+ let falseCount = 0;
253
+ new Array(1000000).fill(0).forEach(() => {
254
+ if (inRate(0.5)) {
255
+ trueCount++;
256
+ } else {
257
+ falseCount++;
258
+ }
259
+ });
260
+ console.log(trueCount, falseCount); // 499994 500006
261
+ ```
262
+
263
+ ### 浏览器信息
264
+
265
+ ```
266
+ import { getBrowserType } from 'mazey';
267
+
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
270
+ ```
271
+
272
+ ### 安全边际
273
+
274
+ 判断是否是安全的 PWA 运行环境
275
+
276
+ ```
277
+ import { isSafePWAEnv } from 'mazey';
278
+
279
+ isSafePWAEnv(); // true
280
+ ```
281
+
282
+ ### 函数
283
+
284
+ 防抖
285
+
286
+ ```
287
+ import { debounce } from 'mazey';
288
+
289
+ const foo = debounce(() => {
290
+ console.log('执行 1 秒内再次执行无反应');
291
+ }, 1000, { leading: true })
292
+ ```
293
+
294
+ 判断是否是有效数字
295
+
296
+ ```
297
+ import { isNumber } from 'mazey';
298
+
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
306
+ ```
307
+
308
+ ### 调试
309
+
310
+ 自定义控制台打印(`console`)
311
+
312
+ ```
313
+ import { genCustomConsole } from 'mazey';
314
+
315
+ const myConsole = genCustomConsole({ prefix: 'MazeyLog:' });
316
+ myConsole.log('I am string.'); // MazeyLog: I am string.
317
+ myConsole.info('I am boolean.', true); // MazeyLog: I am boolean. true
318
+ myConsole.info('I am number.', 123, 456); // MazeyLog: I am number. 123 456
319
+ myConsole.info('I am object.', { a: 123, b: 456}); // MazeyLog: I am object. {a: 123, b: 456}
320
+ ```
321
+
322
+ [更多接口>>](https://mazey.cn/docs/mazey/modules/_index_.html)
323
+
324
+ ## Develop
325
+
326
+ ```
327
+ # dev
328
+ npm run dev
329
+
330
+ # build
331
+ npm run build
332
+
333
+ # documentation
334
+ npm run docs
335
335
  ```