mazey 4.11.0 → 4.11.2

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,4 +1,4 @@
1
- English | [简体中文](https://github.com/chengchuu/mazey/blob/main/README_ZH.md)
1
+ English | [简体中文](https://github.com/chengchuu/mazey/blob/main/README.zh-CN.md)
2
2
 
3
3
  # Mazey
4
4
 
@@ -0,0 +1,363 @@
1
+ <!-- markdownlint-disable MD041 -->
2
+
3
+ ⚠️ Note: The document has expired, please check the latest [README.md](https://github.com/chengchuu/mazey/blob/release/v4/README.md).
4
+
5
+ ⚠️ 注意: 此文档只适用于 v1.4.39 版本,[查看 v4.x 版本的文档](https://github.com/chengchuu/mazey/blob/release/v4/README.md)
6
+
7
+ <!-- omit from toc -->
8
+ # Mazey
9
+
10
+ 除除的 JavaScript 函数库,所有的方法都在线上项目中使用过。
11
+
12
+ <!-- omit from toc -->
13
+ ## 目录
14
+
15
+ - [安装](#安装)
16
+ - [使用](#使用)
17
+ - [加载资源](#加载资源)
18
+ - [存储数据](#存储数据)
19
+ - [操作 DOM](#操作-dom)
20
+ - [网站性能](#网站性能)
21
+ - [公式计算](#公式计算)
22
+ - [浏览器信息](#浏览器信息)
23
+ - [安全边际](#安全边际)
24
+ - [函数](#函数)
25
+ - [调试](#调试)
26
+ - [开发](#开发)
27
+ - [参考](#参考)
28
+
29
+ ## 安装
30
+
31
+ 使用 [npm](https://www.npmjs.com/package/mazey/v/1.4.39) 安装 `mazey@1.4.39`:
32
+
33
+ ```bash
34
+ npm install mazey@1.4.39 --save
35
+ ```
36
+
37
+ ## 使用
38
+
39
+ ### 加载资源
40
+
41
+ 加载 JavaScript:
42
+
43
+ ```javascript
44
+ import { loadScript } from "mazey";
45
+
46
+ loadScript({
47
+ url: "https://example.com/path/example.js",
48
+ id: "iamid", // 可选,script 标签 ID,默认无 ID
49
+ timeout: 5000, // 可选,超时时间,默认 5000
50
+ isDefer: false, // 可选,defer,默认 false
51
+ })
52
+ .then(
53
+ res => {
54
+ console.log(`加载 JavaScript 成功: ${res}`);
55
+ }
56
+ )
57
+ .catch(
58
+ err => {
59
+ console.error(`加载 JavaScript 失败: ${err}`)
60
+ }
61
+ );
62
+ ```
63
+
64
+ 加载 CSS:
65
+
66
+ ```javascript
67
+ import { loadCSS } from "mazey";
68
+
69
+ loadCSS({
70
+ url: "https://example.com/path/example.css",
71
+ id: "iamid", // 可选,link 标签 ID,默认无 ID
72
+ })
73
+ .then(
74
+ res => {
75
+ console.log(`加载 CSS 成功: ${res}`);
76
+ }
77
+ )
78
+ .catch(
79
+ err => {
80
+ console.error(`加载 CSS 失败: ${err}`)
81
+ }
82
+ );
83
+ ```
84
+
85
+ 判断页面是否加载完成(兼容浏览器 `load` 事件已经触发过的情况):
86
+
87
+ ```javascript
88
+ import { loadCSS } from "mazey";
89
+
90
+ windowLoaded({ timeout: 30 })
91
+ .then(res => {
92
+ console.log(`加载完成:${res}`); // 加载完成:load
93
+ })
94
+ .catch(err => {
95
+ console.log(`加载超时或失败:${err}`);
96
+ });
97
+ ```
98
+
99
+ ### 存储数据
100
+
101
+ Storage(兼容 JSON 格式,自动转换):
102
+
103
+ ```javascript
104
+ import { setSessionStorage, getSessionStorage, setLocalStorage, getLocalStorage } from "mazey";
105
+
106
+ setSessionStorage("test", "123");
107
+ getSessionStorage("test"); // 123
108
+ setLocalStorage("test", "123");
109
+ getLocalStorage("test"); // 123
110
+ ```
111
+
112
+ Cookie:
113
+
114
+ ```javascript
115
+ import { setCookie, getCookie } from "mazey";
116
+
117
+ setCookie("test", "123", 30, "example.com"); // key value day domain
118
+ getCookie("test"); // 123
119
+ ```
120
+
121
+ ### 操作 DOM
122
+
123
+ 修改 `class`:
124
+
125
+ ```javascript
126
+ import { hasClass, addClass, removeClass } from "mazey";
127
+
128
+ const dom = document.querySelector("#box");
129
+
130
+ // 判断 class
131
+ hasClass(dom, "test");
132
+ // 增加 class
133
+ addClass(dom, "test");
134
+ // 删除 class
135
+ removeClass(dom, "test");
136
+ ```
137
+
138
+ 添加内联样式:
139
+
140
+ ```javascript
141
+ import { addInlineStyle } from "mazey";
142
+
143
+ // 添加有 id 的内联样式,重复添加会更新内联样式而不是新增
144
+ addInlineStyle({
145
+ inlineStyle: `
146
+ body {
147
+ background-color: #333;
148
+ }
149
+ `,
150
+ id: "test",
151
+ });
152
+ // <style id="test">
153
+ // body {
154
+ // background-color: #333;
155
+ // }
156
+ // </style>
157
+
158
+ // 添加无 id 的内联样式,重复添加会新增内联样式
159
+ addInlineStyle({
160
+ inlineStyle: `
161
+ body {
162
+ background-color: #444;
163
+ }
164
+ `,
165
+ });
166
+ // <style>
167
+ // body {
168
+ // background-color: #444;
169
+ // }
170
+ // </style>
171
+ ```
172
+
173
+ 添加自定义滚动条(适用于用 `transform` 移动的元素):
174
+
175
+ ```javascript
176
+ import { customScrollBarForTransformEle } from "mazey";
177
+
178
+ // 使用类名
179
+ customScrollBarForTransformEle({
180
+ containerClassName: "i-am-container",
181
+ imgBoxClassName: "i-am-img-father-i-can-transform",
182
+ imgClassName: "i-am-img",
183
+ });
184
+
185
+ // 使用 Dom 对象
186
+ customScrollBarForTransformEle({
187
+ containerClassName: "i-am-container",
188
+ imgBoxDom: document.querySelector(".i-am-img-father-i-can-transform"),
189
+ imgDom: document.querySelector(".i-am-img-father-i-can-transform"),
190
+ });
191
+
192
+ // 按需直接隐藏滚动条
193
+ customScrollBarForTransformEle({
194
+ containerClassName: "i-am-container",
195
+ action: "hide",
196
+ });
197
+ ```
198
+
199
+ 计算适配容器宽度的图片的尺寸、距离顶部的距离:
200
+
201
+ ```javascript
202
+ import { calcContainImageSizeAndPosition } from "mazey";
203
+
204
+ // 如果高度不足以占满容器,使其垂直居中;如果高度比容器长,由上向下铺开
205
+ calcContainImageSizeAndPosition({ oriImageWidth: 300, oriImageHeight: 300, viewportWidth: 375, viewportHeight: 812 });
206
+ // {"targetImageWidth":375,"targetImageHeight":375,"top":218.5,"wPer":1.25}
207
+ ```
208
+
209
+ ### 网站性能
210
+
211
+ 获取加载时间:
212
+
213
+ ```javascript
214
+ import { getPerformance } from "mazey";
215
+
216
+ // camelCase:true(默认) 以驼峰形式返回数据 / false 以下划线形式返回数据
217
+ getPerformance({ camelCase: true })
218
+ .then(res => {
219
+ console.log(JSON.stringify(res));
220
+ // {"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}
221
+ })
222
+ .catch(console.error);
223
+ ```
224
+
225
+ | 指标 | 字段 | 计算⽅法 |
226
+ | --- | --- | --- |
227
+ | * DNS 查询时间 | dns_time | domainLookupEnd - domainLookupStart |
228
+ | * 服务器连接时间 | tcp_time | connectEnd - connectStart |
229
+ | * 服务器响应时间 | response_time | responseStart - requestStart |
230
+ | * ⽩屏时间 | white_time | responseStart - navigationStart |
231
+ | * DomReady 总时间 | domready_time | domContentLoadedEventStart - navigationStart |
232
+ | * 页面加载时间 | onload_time | loadEventStart - navigationStart |
233
+ | * EventEnd 总时间 | render_time | loadEventEnd -navigationStart |
234
+ | 上个⽂档卸载时间 | unload_time | unloadEventEnd - unloadEventStart |
235
+ | 重定向时间 | redirect_time | redirectEnd - redirectStart |
236
+ | 客户端⽩屏时间 | custom_white_time | renderTiming - navigationStart |
237
+ | SSL连接时间 | ssl_time | connectEnd - secureConnectionStart |
238
+ | ⽹⻚下载时间 | download_time | responseEnd - responseStart |
239
+ | FCP | first_contentful_paint_time | firstPaintTime |
240
+
241
+ ### 公式计算
242
+
243
+ 计算两个字符串的最长公共子串:
244
+
245
+ ```javascript
246
+ import { calLongestCommonSubstring } from "mazey";
247
+
248
+ calLongestCommonSubstring("fish", "finish"); // 3
249
+ ```
250
+
251
+ 计算两个字符串的最长公共子序列:
252
+
253
+ ```javascript
254
+ import { calLongestCommonSubsequence } from "mazey";
255
+
256
+ calLongestCommonSubsequence("fish", "finish"); // 4
257
+ ```
258
+
259
+ 命中概率(1% ~ 100%):
260
+
261
+ ```javascript
262
+ import { inRate } from "mazey";
263
+
264
+ inRate(0.5); // 0.01 ~ 1 true / false
265
+
266
+ // 测试准确性
267
+ let trueCount = 0;
268
+ let falseCount = 0;
269
+ new Array(1000000).fill(0).forEach(() => {
270
+ if (inRate(0.5)) {
271
+ trueCount++;
272
+ } else {
273
+ falseCount++;
274
+ }
275
+ });
276
+ console.log(trueCount, falseCount); // 499994 500006
277
+ ```
278
+
279
+ ### 浏览器信息
280
+
281
+ ```javascript
282
+ import { getBrowserType } from "mazey";
283
+
284
+ getBrowserType(); // {"engine":"webkit","engineVs":"537.36","platform":"desktop","supporter":"chrome","supporterVs":"85.0.4183.121","system":"windows","systemVs":"10"}
285
+ // 外壳和外壳版本 { shell: "wechat", shellVs: "20" } shell: wechat qq uc 360 2345 sougou liebao maxthon
286
+ ```
287
+
288
+ ### 安全边际
289
+
290
+ 判断是否是安全的 PWA 运行环境:
291
+
292
+ ```javascript
293
+ import { isSafePWAEnv } from "mazey";
294
+
295
+ isSafePWAEnv(); // true
296
+ ```
297
+
298
+ ### 函数
299
+
300
+ 防抖:
301
+
302
+ ```javascript
303
+ import { debounce } from "mazey";
304
+
305
+ const foo = debounce(() => {
306
+ console.log("执行 1 秒内再次执行无反应");
307
+ }, 1000, { leading: true })
308
+ ```
309
+
310
+ 判断是否是有效数字:
311
+
312
+ ```javascript
313
+ import { isNumber } from "mazey";
314
+
315
+ isNumber(123); // true
316
+ isNumber("123"); // false
317
+ // 默认情况下 NaN、Infinity 不算有效数字
318
+ isNumber(Infinity); // false
319
+ isNumber(Infinity, { isUnFiniteAsNumber: true }); true
320
+ isNumber(NaN); // false
321
+ isNumber(NaN, { isNaNAsNumber: true, isUnFiniteAsNumber: true }); // true
322
+ ```
323
+
324
+ ### 调试
325
+
326
+ 自定义控制台打印(`console`):
327
+
328
+ ```javascript
329
+ import { genCustomConsole } from "mazey";
330
+
331
+ const myConsole = genCustomConsole({ prefix: "MazeyLog:" });
332
+ myConsole.log("I am string."); // MazeyLog: I am string.
333
+ myConsole.info("I am boolean.", true); // MazeyLog: I am boolean. true
334
+ myConsole.info("I am number.", 123, 456); // MazeyLog: I am number. 123 456
335
+ myConsole.info("I am object.", { a: 123, b: 456}); // MazeyLog: I am object. {a: 123, b: 456}
336
+ ```
337
+
338
+ [更多接口>>](https://mazey.cn/t/m)
339
+
340
+ ## 开发
341
+
342
+ Development:
343
+
344
+ ```bash
345
+ npm run dev
346
+ ```
347
+
348
+ Build:
349
+
350
+ ```bash
351
+ npm run build
352
+ ```
353
+
354
+ Document:
355
+
356
+ ```bash
357
+ npm run docs
358
+ ```
359
+
360
+ ## 参考
361
+
362
+ 1. [@babel/preset-env conflicts with @babel/plugin-transform-runtime when using core-js@3](https://github.com/babel/babel/issues/10271#issuecomment-528379505)
363
+ 2. [@babel/plugin-transform-runtime](https://babeljs.io/docs/en/babel-plugin-transform-runtime)
package/lib/index.cjs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Mazey v4.11.0 https://github.com/chengchuu/mazey
2
+ * Mazey v4.11.2 https://github.com/chengchuu/mazey
3
3
  * (c) 2018-2026 Cheng
4
4
  * Released under the MIT License.
5
5
  */