mazey 1.4.35 → 1.4.39

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,21 +1,21 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2017 Mazey
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Mazey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md CHANGED
@@ -1,283 +1,335 @@
1
- # Mazey
2
-
3
- [![NPM version][npm-image]][npm-url]
4
- [![l][l-image]][l-url]
5
-
6
- [npm-image]: https://img.shields.io/npm/v/mazey
7
- [npm-url]: https://npmjs.org/package/mazey
8
- [l-image]: https://img.shields.io/npm/l/mazey
9
- [l-url]: https://github.com/mazeyqian/mazey
10
-
11
- Mazey's library for front end.
12
-
13
- ## Install
14
-
15
- You can get Mazey via [npm](https://www.npmjs.com/package/mazey).
16
-
17
- ```
18
- npm install mazey --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
- })
33
- .then(
34
- res => {
35
- console.log(`加载 JavaScript: ${res}`);
36
- }
37
- )
38
- .catch(
39
- err => {
40
- console.error(`加载 JavaScript Error: ${err}`)
41
- }
42
- );
43
- ```
44
-
45
- 加载 CSS
46
-
47
- ```
48
- import { loadCSS } from 'mazey';
49
-
50
- loadCSS({
51
- url: 'http://www.mazey.net/css/mazey-base.css'
52
- })
53
- .then(
54
- res => {
55
- console.log(`加载 CSS: ${res}`);
56
- }
57
- )
58
- .catch(
59
- err => {
60
- console.error(`加载 CSS Error: ${err}`)
61
- }
62
- );
63
- ```
64
-
65
- 判断页面是否加载完成(兼容浏览器 `load` 事件已经触发过的情况)
66
-
67
- ```
68
- import { loadCSS } from 'mazey';
69
-
70
- windowLoaded({ timeout: 30 })
71
- .then(res => {
72
- console.log(`加载完成:${res}`); // 加载完成:load
73
- })
74
- .catch(err => {
75
- console.log(`加载超时或失败:${err}`);
76
- });
77
- ```
78
-
79
- ### 存储数据
80
-
81
- Storage
82
-
83
- ```
84
- import { setSessionStorage, getSessionStorage, setLocalStorage, getLocalStorage } from 'mazey';
85
-
86
- setSessionStorage('test', '123');
87
- getSessionStorage('test'); // 123
88
- setLocalStorage('test', '123');
89
- getLocalStorage('test'); // 123
90
- ```
91
-
92
- Cookie
93
-
94
- ```
95
- import { setCookie, getCookie } from 'mazey';
96
-
97
- setCookie('test', '123', 30, 'mazey.net'); // key value day domain
98
- getCookie('test'); // 123
99
- ```
100
-
101
- ### 操作 DOM
102
-
103
- 修改 `class`
104
-
105
- ```
106
- import { hasClass, addClass, removeClass } from 'mazey';
107
-
108
- const dom = document.querySelector('#box');
109
-
110
- // 判断 class
111
- hasClass(dom, 'test');
112
- // 增加 class
113
- addClass(dom, 'test');
114
- // 删除 class
115
- removeClass(dom, 'test');
116
- ```
117
-
118
- 添加内联样式
119
-
120
- ```
121
- import { addInlineStyle } from 'mazey';
122
-
123
- // 添加有 id 的内联样式,重复添加会更新内联样式而不是新增
124
- addInlineStyle({
125
- inlineStyle: `
126
- body {
127
- background-color: #333;
128
- }
129
- `,
130
- id: 'test',
131
- });
132
-
133
- // <style id="test">
134
- // body {
135
- // background-color: #333;
136
- // }
137
- // </style>
138
-
139
- // 添加无 id 的内联样式,重复添加会新增内联样式
140
- addInlineStyle({
141
- inlineStyle: `
142
- body {
143
- background-color: #444;
144
- }
145
- `,
146
- });
147
-
148
- // <style>
149
- // body {
150
- // background-color: #444;
151
- // }
152
- // </style>
153
- ```
154
-
155
- ### 网站性能
156
-
157
- 获取加载时间
158
-
159
- ```
160
- import { getPerformance } from 'mazey';
161
-
162
- // camelCase:true(默认) 以驼峰形式返回数据 / false 以下划线形式返回数据
163
- getPerformance({ camelCase: true })
164
- .then(res => {
165
- console.log(JSON.stringify(res));
166
- // {"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}
167
- })
168
- .catch(console.error);
169
- ```
170
-
171
- | 指标 | 字段 | 计算⽅法 |
172
- | --- | --- | --- |
173
- | * DNS 查询时间 | dns_time | domainLookupEnd - domainLookupStart |
174
- | * 服务器连接时间 | tcp_time | connectEnd - connectStart |
175
- | * 服务器响应时间 | response_time | responseStart - requestStart |
176
- | * ⽩屏时间 | white_time | responseStart - navigationStart |
177
- | * DomReady 总时间 | domready_time | domContentLoadedEventStart - navigationStart |
178
- | * 页面加载时间 | onload_time | loadEventStart - navigationStart |
179
- | * EventEnd 总时间 | render_time | loadEventEnd -navigationStart |
180
- | 上个⽂档卸载时间 | unload_time | unloadEventEnd - unloadEventStart |
181
- | 重定向时间 | redirect_time | redirectEnd - redirectStart |
182
- | 客户端⽩屏时间 | custom_white_time | renderTiming - navigationStart |
183
- | SSL连接时间 | ssl_time | connectEnd - secureConnectionStart |
184
- | ⽹⻚下载时间 | download_time | responseEnd - responseStart |
185
- | FCP | first_contentful_paint_time | firstPaintTime |
186
-
187
- ### 公式计算
188
-
189
- 计算两个字符串的最长公共子串
190
-
191
- ```
192
- import { calLongestCommonSubstring } from 'mazey';
193
-
194
- calLongestCommonSubstring('fish', 'finish'); // 3
195
- ```
196
-
197
- 计算两个字符串的最长公共子序列
198
-
199
- ```
200
- import { calLongestCommonSubsequence } from 'mazey';
201
-
202
- calLongestCommonSubsequence('fish', 'finish'); // 4
203
- ```
204
-
205
- 命中概率(1% ~ 100%)
206
-
207
- ```
208
- import { inRate } from 'mazey';
209
-
210
- inRate(0.5); // 0.01 ~ 1 true / false
211
-
212
- // 测试准确性
213
- let trueCount = 0;
214
- let falseCount = 0;
215
- new Array(1000000).fill(0).forEach(() => {
216
- if (inRate(0.5)) {
217
- trueCount++;
218
- } else {
219
- falseCount++;
220
- }
221
- });
222
- console.log(trueCount, falseCount); // 499994 500006
223
- ```
224
-
225
- ### 浏览器信息
226
-
227
- ```
228
- import { getBrowserType } from 'mazey';
229
-
230
- getBrowserType(); // {"engine":"webkit","engineVs":"537.36","platform":"desktop","supporter":"chrome","supporterVs":"85.0.4183.121","system":"windows","systemVs":"10"}
231
- // 外壳和外壳版本 { shell: 'wechat', shellVs: '20' } shell: wechat qq uc 360 2345 sougou liebao maxthon
232
- ```
233
-
234
- ### 安全边际
235
-
236
- 判断是否是安全的 PWA 运行环境
237
-
238
- ```
239
- import { isSafePWAEnv } from 'mazey';
240
-
241
- isSafePWAEnv(); // true
242
- ```
243
-
244
- ### 函数
245
-
246
- 防抖
247
-
248
- ```
249
- import { debounce } from 'mazey';
250
-
251
- const foo = debounce(() => {
252
- console.log('执行 1 秒内再次执行无反应');
253
- }, 1000, { leading: true })
254
- ```
255
-
256
- 判断是否是有效数字
257
-
258
- ```
259
- import { isNumber } from 'mazey';
260
-
261
- isNumber(123); // true
262
- isNumber('123'); // false
263
- // 默认情况下 NaN、Infinity 不算有效数字
264
- isNumber(Infinity); // false
265
- isNumber(Infinity, { isUnFiniteAsNumber: true }); true
266
- isNumber(NaN); // false
267
- isNumber(NaN, { isNaNAsNumber: true, isUnFiniteAsNumber: true }); // true
268
- ```
269
-
270
- [更多接口>>](https://mazey.cn/docs/mazey/modules/_index_.html)
271
-
272
- ## Develop
273
-
274
- ```
275
- # dev
276
- npm run dev
277
-
278
- # build
279
- npm run build
280
-
281
- # documentation
282
- npm run docs
1
+ # Mazey
2
+
3
+ [![NPM version][npm-image]][npm-url]
4
+ [![l][l-image]][l-url]
5
+
6
+ [npm-image]: https://img.shields.io/npm/v/mazey
7
+ [npm-url]: https://npmjs.org/package/mazey
8
+ [l-image]: https://img.shields.io/npm/l/mazey
9
+ [l-url]: https://github.com/mazeyqian/mazey
10
+
11
+ Mazey's library for front end.
12
+
13
+ ## Install
14
+
15
+ You can get Mazey via [npm](https://www.npmjs.com/package/mazey).
16
+
17
+ ```
18
+ npm install mazey --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
283
335
  ```