mazey 3.5.4 → 3.6.1

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 DELETED
@@ -1,359 +0,0 @@
1
- ⚠️ Note: The document has expired, please check the latest [README.md](./README.md).
2
-
3
- # mazey
4
-
5
- [![NPM version][npm-image]][npm-url]
6
- [![l][l-image]][l-url]
7
-
8
- [npm-image]: ./images/mazey-v1.4.39.svg
9
- [npm-url]: https://www.npmjs.com/package/mazey/v/1.4.39
10
- [l-image]: https://img.shields.io/npm/l/mazey
11
- [l-url]: https://github.com/mazeyqian/mazey
12
-
13
- 后除的 JavaScript 函数库,所有的方法都在线上项目中使用过。
14
-
15
- **目录**
16
-
17
- - [mazey](#mazey)
18
- - [安装](#安装)
19
- - [使用](#使用)
20
- - [加载资源](#加载资源)
21
- - [存储数据](#存储数据)
22
- - [操作 DOM](#操作-dom)
23
- - [网站性能](#网站性能)
24
- - [公式计算](#公式计算)
25
- - [浏览器信息](#浏览器信息)
26
- - [安全边际](#安全边际)
27
- - [函数](#函数)
28
- - [调试](#调试)
29
- - [开发](#开发)
30
- - [参考](#参考)
31
-
32
- ## 安装
33
-
34
- 使用 [NPM](https://www.npmjs.com/package/mazey/v/1.4.39) 安装 mazey@1.4.39
35
-
36
- ```
37
- npm install mazey@1.4.39 --save
38
- ```
39
-
40
- ## 使用
41
-
42
- ### 加载资源
43
-
44
- 加载 JavaScript
45
-
46
- ```
47
- import { loadScript } from 'mazey';
48
-
49
- loadScript({
50
- url: 'http://www.mazey.net/js/plugin/jquery/jquery-2.1.1.min.js',
51
- id: 'iamid', // 可选,script 标签 ID,默认无 ID
52
- timeout: 5000, // 可选,超时时间,默认 5000
53
- isDefer: false, // 可选,defer,默认 false
54
- })
55
- .then(
56
- res => {
57
- console.log(`加载 JavaScript 成功: ${res}`);
58
- }
59
- )
60
- .catch(
61
- err => {
62
- console.error(`加载 JavaScript 失败: ${err}`)
63
- }
64
- );
65
- ```
66
-
67
- 加载 CSS
68
-
69
- ```
70
- import { loadCSS } from 'mazey';
71
-
72
- loadCSS({
73
- url: 'http://www.mazey.net/css/mazey-base.css',
74
- id: 'iamid', // 可选,link 标签 ID,默认无 ID
75
- })
76
- .then(
77
- res => {
78
- console.log(`加载 CSS 成功: ${res}`);
79
- }
80
- )
81
- .catch(
82
- err => {
83
- console.error(`加载 CSS 失败: ${err}`)
84
- }
85
- );
86
- ```
87
-
88
- 判断页面是否加载完成(兼容浏览器 `load` 事件已经触发过的情况)
89
-
90
- ```
91
- import { loadCSS } from 'mazey';
92
-
93
- windowLoaded({ timeout: 30 })
94
- .then(res => {
95
- console.log(`加载完成:${res}`); // 加载完成:load
96
- })
97
- .catch(err => {
98
- console.log(`加载超时或失败:${err}`);
99
- });
100
- ```
101
-
102
- ### 存储数据
103
-
104
- Storage(兼容 JSON 格式,自行转换)
105
-
106
- ```
107
- import { setSessionStorage, getSessionStorage, setLocalStorage, getLocalStorage } from 'mazey';
108
-
109
- setSessionStorage('test', '123');
110
- getSessionStorage('test'); // 123
111
- setLocalStorage('test', '123');
112
- getLocalStorage('test'); // 123
113
- ```
114
-
115
- Cookie
116
-
117
- ```
118
- import { setCookie, getCookie } from 'mazey';
119
-
120
- setCookie('test', '123', 30, 'mazey.net'); // key value day domain
121
- getCookie('test'); // 123
122
- ```
123
-
124
- ### 操作 DOM
125
-
126
- 修改 `class`
127
-
128
- ```
129
- import { hasClass, addClass, removeClass } from 'mazey';
130
-
131
- const dom = document.querySelector('#box');
132
-
133
- // 判断 class
134
- hasClass(dom, 'test');
135
- // 增加 class
136
- addClass(dom, 'test');
137
- // 删除 class
138
- removeClass(dom, 'test');
139
- ```
140
-
141
- 添加内联样式
142
-
143
- ```
144
- import { addInlineStyle } from 'mazey';
145
-
146
- // 添加有 id 的内联样式,重复添加会更新内联样式而不是新增
147
- addInlineStyle({
148
- inlineStyle: `
149
- body {
150
- background-color: #333;
151
- }
152
- `,
153
- id: 'test',
154
- });
155
- // <style id="test">
156
- // body {
157
- // background-color: #333;
158
- // }
159
- // </style>
160
-
161
- // 添加无 id 的内联样式,重复添加会新增内联样式
162
- addInlineStyle({
163
- inlineStyle: `
164
- body {
165
- background-color: #444;
166
- }
167
- `,
168
- });
169
- // <style>
170
- // body {
171
- // background-color: #444;
172
- // }
173
- // </style>
174
- ```
175
-
176
- 添加自定义滚动条(适用于用 `transform` 移动的元素)
177
-
178
- ```
179
- import { customScrollBarForTransformEle } from 'mazey';
180
-
181
- // 使用类名
182
- customScrollBarForTransformEle({
183
- containerClassName: 'i-am-container',
184
- imgBoxClassName: 'i-am-img-father-i-can-transform',
185
- imgClassName: 'i-am-img',
186
- });
187
-
188
- // 使用 Dom 对象
189
- customScrollBarForTransformEle({
190
- containerClassName: 'i-am-container',
191
- imgBoxDom: document.querySelector('.i-am-img-father-i-can-transform'),
192
- imgDom: document.querySelector('.i-am-img-father-i-can-transform'),
193
- });
194
-
195
- // 按需直接隐藏滚动条
196
- customScrollBarForTransformEle({
197
- containerClassName: 'i-am-container',
198
- action: 'hide',
199
- });
200
- ```
201
-
202
- 计算适配容器宽度的图片的尺寸、距离顶部的距离
203
-
204
- ```
205
- import { calcContainImageSizeAndPosition } from 'mazey';
206
-
207
- // 如果高度不足以占满容器,使其垂直居中;如果高度比容器长,由上向下铺开
208
- calcContainImageSizeAndPosition({ oriImageWidth: 300, oriImageHeight: 300, viewportWidth: 375, viewportHeight: 812 });
209
- // {"targetImageWidth":375,"targetImageHeight":375,"top":218.5,"wPer":1.25}
210
- ```
211
-
212
- ### 网站性能
213
-
214
- 获取加载时间
215
-
216
- ```
217
- import { getPerformance } from 'mazey';
218
-
219
- // camelCase:true(默认) 以驼峰形式返回数据 / false 以下划线形式返回数据
220
- getPerformance({ camelCase: true })
221
- .then(res => {
222
- console.log(JSON.stringify(res));
223
- // {"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}
224
- })
225
- .catch(console.error);
226
- ```
227
-
228
- | 指标 | 字段 | 计算⽅法 |
229
- | --- | --- | --- |
230
- | * DNS 查询时间 | dns_time | domainLookupEnd - domainLookupStart |
231
- | * 服务器连接时间 | tcp_time | connectEnd - connectStart |
232
- | * 服务器响应时间 | response_time | responseStart - requestStart |
233
- | * ⽩屏时间 | white_time | responseStart - navigationStart |
234
- | * DomReady 总时间 | domready_time | domContentLoadedEventStart - navigationStart |
235
- | * 页面加载时间 | onload_time | loadEventStart - navigationStart |
236
- | * EventEnd 总时间 | render_time | loadEventEnd -navigationStart |
237
- | 上个⽂档卸载时间 | unload_time | unloadEventEnd - unloadEventStart |
238
- | 重定向时间 | redirect_time | redirectEnd - redirectStart |
239
- | 客户端⽩屏时间 | custom_white_time | renderTiming - navigationStart |
240
- | SSL连接时间 | ssl_time | connectEnd - secureConnectionStart |
241
- | ⽹⻚下载时间 | download_time | responseEnd - responseStart |
242
- | FCP | first_contentful_paint_time | firstPaintTime |
243
-
244
- ### 公式计算
245
-
246
- 计算两个字符串的最长公共子串
247
-
248
- ```
249
- import { calLongestCommonSubstring } from 'mazey';
250
-
251
- calLongestCommonSubstring('fish', 'finish'); // 3
252
- ```
253
-
254
- 计算两个字符串的最长公共子序列
255
-
256
- ```
257
- import { calLongestCommonSubsequence } from 'mazey';
258
-
259
- calLongestCommonSubsequence('fish', 'finish'); // 4
260
- ```
261
-
262
- 命中概率(1% ~ 100%)
263
-
264
- ```
265
- import { inRate } from 'mazey';
266
-
267
- inRate(0.5); // 0.01 ~ 1 true / false
268
-
269
- // 测试准确性
270
- let trueCount = 0;
271
- let falseCount = 0;
272
- new Array(1000000).fill(0).forEach(() => {
273
- if (inRate(0.5)) {
274
- trueCount++;
275
- } else {
276
- falseCount++;
277
- }
278
- });
279
- console.log(trueCount, falseCount); // 499994 500006
280
- ```
281
-
282
- ### 浏览器信息
283
-
284
- ```
285
- import { getBrowserType } from 'mazey';
286
-
287
- getBrowserType(); // {"engine":"webkit","engineVs":"537.36","platform":"desktop","supporter":"chrome","supporterVs":"85.0.4183.121","system":"windows","systemVs":"10"}
288
- // 外壳和外壳版本 { shell: 'wechat', shellVs: '20' } shell: wechat qq uc 360 2345 sougou liebao maxthon
289
- ```
290
-
291
- ### 安全边际
292
-
293
- 判断是否是安全的 PWA 运行环境
294
-
295
- ```
296
- import { isSafePWAEnv } from 'mazey';
297
-
298
- isSafePWAEnv(); // true
299
- ```
300
-
301
- ### 函数
302
-
303
- 防抖
304
-
305
- ```
306
- import { debounce } from 'mazey';
307
-
308
- const foo = debounce(() => {
309
- console.log('执行 1 秒内再次执行无反应');
310
- }, 1000, { leading: true })
311
- ```
312
-
313
- 判断是否是有效数字
314
-
315
- ```
316
- import { isNumber } from 'mazey';
317
-
318
- isNumber(123); // true
319
- isNumber('123'); // false
320
- // 默认情况下 NaN、Infinity 不算有效数字
321
- isNumber(Infinity); // false
322
- isNumber(Infinity, { isUnFiniteAsNumber: true }); true
323
- isNumber(NaN); // false
324
- isNumber(NaN, { isNaNAsNumber: true, isUnFiniteAsNumber: true }); // true
325
- ```
326
-
327
- ### 调试
328
-
329
- 自定义控制台打印(`console`)
330
-
331
- ```
332
- import { genCustomConsole } from 'mazey';
333
-
334
- const myConsole = genCustomConsole({ prefix: 'MazeyLog:' });
335
- myConsole.log('I am string.'); // MazeyLog: I am string.
336
- myConsole.info('I am boolean.', true); // MazeyLog: I am boolean. true
337
- myConsole.info('I am number.', 123, 456); // MazeyLog: I am number. 123 456
338
- myConsole.info('I am object.', { a: 123, b: 456}); // MazeyLog: I am object. {a: 123, b: 456}
339
- ```
340
-
341
- [更多接口>>](https://mazey.cn/docs/mazey/modules/_index_.html)
342
-
343
- ## 开发
344
-
345
- ```
346
- # dev
347
- npm run dev
348
-
349
- # build
350
- npm run build
351
-
352
- # documentation
353
- npm run docs
354
- ```
355
-
356
- ## 参考
357
-
358
- 1. [@babel/preset-env conflicts with @babel/plugin-transform-runtime when using core-js@3](https://github.com/babel/babel/issues/10271#issuecomment-528379505)
359
- 2. [@babel/plugin-transform-runtime](https://babeljs.io/docs/en/babel-plugin-transform-runtime)
@@ -1,21 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <title>Test Page</title>
6
- <link rel="shortcut icon" type="image/png" href="https://mazey.cn/fav/logo-dark-circle-transparent-144x144.png">
7
- <style>
8
- .box {
9
- color: #333;
10
- }
11
- </style>
12
- </head>
13
- <body>
14
- <div class="container">
15
- <div class="box">
16
- <span>Box</span>
17
- </div>
18
- <div class="s"></div>
19
- </div>
20
- </body>
21
- </html>
package/examples/index.ts DELETED
@@ -1,121 +0,0 @@
1
- import {
2
- isNumber,
3
- getUrlParam,
4
- updateQueryParam,
5
- getHashQueryParam,
6
- getDomain,
7
- camelCaseToKebabCase,
8
- camelCase2Underscore,
9
- generateRndNum,
10
- isNonEmptyArray,
11
- getFileSize,
12
- isSupportWebp,
13
- genHashCode,
14
- debounce,
15
- genCustomConsole,
16
- formatDate,
17
- isValidData
18
- } from '../src/index';
19
-
20
- // Sync
21
- console.log(
22
- 'Is Infinity number?',
23
- isNumber(Infinity, { isUnFiniteAsNumber: true })
24
- );
25
- console.log(
26
- 'Is NaN number?',
27
- isNumber(NaN, { isNaNAsNumber: true, isUnFiniteAsNumber: true })
28
- );
29
- console.log('Did ttt exist?', getHashQueryParam('ttt'));
30
- console.log(
31
- 'What is t3?',
32
- getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3')
33
- );
34
- console.log(
35
- 'Can query param update successfully?',
36
- updateQueryParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3', 'three')
37
- );
38
- console.log(
39
- 'What is domain?',
40
- getDomain('http://example.com/?t1=1&t2=2&t3=3&t4=4')
41
- );
42
- console.log('Get KebabCase:', camelCaseToKebabCase('aBC'));
43
- console.log('Get Underscore:', camelCase2Underscore('bBC'));
44
- console.log('Get a string with random number:', generateRndNum(7));
45
- console.log('Is non-empty array?', isNonEmptyArray(['a', 123]));
46
- console.log('Get file size:', getFileSize(2000));
47
- console.log('Generate a Hash from a string:', genHashCode('123'));
48
- const genLog = genCustomConsole('GenLog:', {
49
- isClosed: false,
50
- showWrap: false,
51
- showDate: true,
52
- locales: 'zh-CN'
53
- });
54
- genLog.log('test?');
55
- // const s011101 = ;
56
- console.log('Default formatDate value:', formatDate());
57
- // 2022-01-11
58
- console.log(
59
- 'String formatDate value:',
60
- formatDate(
61
- 'Tue Jan 11 2022 14:12:26 GMT+0800 (China Standard Time)',
62
- 'yyyy-MM-dd hh:mm:ss'
63
- )
64
- );
65
- // 2022-01-11 14:12:26
66
- console.log(
67
- 'Number formatDate value:',
68
- formatDate(1641881235000, 'yyyy-MM-dd hh:mm:ss')
69
- );
70
- // 2022-01-11 14:07:15
71
- // (new Date(2014, 1, 11), 'MM/dd/yyyy')
72
- console.log(
73
- 'Date formatDate value:',
74
- formatDate(new Date(2014, 1, 11), 'MM/dd/yyyy')
75
- );
76
- // Date formatDate value: 02/11/2014
77
- const validData = {
78
- a: {
79
- b: {
80
- c: 413
81
- }
82
- }
83
- };
84
-
85
- const isValidDataResA = isValidData(validData, ['a', 'b', 'c'], 2333);
86
- const isValidDataResB = isValidData(validData, ['a', 'b', 'c'], 413);
87
- const isValidDataResC = isValidData(validData, ['d', 'd'], 413);
88
-
89
- console.log('isValidDataResA:', isValidDataResA);
90
- console.log('isValidDataResB:', isValidDataResB);
91
- console.log('isValidDataResC:', isValidDataResC);
92
-
93
- // isValidDataResA: false
94
- // isValidDataResB: true
95
- // isValidDataResC: false
96
-
97
- // Async
98
- (async () => {
99
- console.log('Detect webp support:', await isSupportWebp());
100
-
101
- // debounce
102
- console.log('Test debounce - begin');
103
- const c = debounce(
104
- () => {
105
- console.log('Test debounce - fun myself');
106
- },
107
- 3000,
108
- true
109
- );
110
- console.log('Test debounce - invoke first');
111
- c();
112
- setTimeout(() => {
113
- c();
114
- console.log('Test debounce - 2000ms second');
115
- }, 2000);
116
- setTimeout(() => {
117
- c();
118
- console.log('Test debounce - 10000ms third');
119
- }, 10000);
120
- console.log('Test debounce - end');
121
- })();
@@ -1 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="88" height="20" role="img" aria-label="npm: v1.4.39"><title>npm: v1.4.39</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="88" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="35" height="20" fill="#555"/><rect x="35" width="53" height="20" fill="#007ec6"/><rect width="88" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="185" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="250">npm</text><text x="185" y="140" transform="scale(.1)" fill="#fff" textLength="250">npm</text><text aria-hidden="true" x="605" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">v1.4.39</text><text x="605" y="140" transform="scale(.1)" fill="#fff" textLength="430">v1.4.39</text></g></svg>