mazey 3.6.7 → 3.7.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/README.md +49 -49
- package/lib/index.cjs.js +164 -78
- package/lib/index.d.ts +91 -48
- package/lib/index.d.ts.map +1 -1
- package/lib/index.esm.js +162 -79
- package/lib/mazey.min.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,48 +59,48 @@ There ara some examples maintained by hand below. For more information, please c
|
|
|
59
59
|
|
|
60
60
|
**Table of Contents**
|
|
61
61
|
|
|
62
|
-
<!-- toc - begin -->
|
|
63
|
-
- Generated with ❤️
|
|
64
|
-
- [Load Resource](#load-resource)
|
|
65
|
-
* [loadScript](#loadscript)
|
|
66
|
-
* [loadCSS](#loadcss)
|
|
67
|
-
* [windowLoaded](#windowloaded)
|
|
68
|
-
- [Util](#util)
|
|
69
|
-
* [isNumber](#isnumber)
|
|
70
|
-
* [isJSONString](#isjsonstring)
|
|
71
|
-
* [isValidData](#isvaliddata)
|
|
72
|
-
* [generateRndNum](#generaterndnum)
|
|
73
|
-
* [formatDate](#formatdate)
|
|
74
|
-
* [debounce](#debounce)
|
|
75
|
-
* [throttle](#throttle)
|
|
76
|
-
* [convertCamelToKebab](#convertcameltokebab)
|
|
77
|
-
* [convertCamelToUnder](#convertcameltounder)
|
|
78
|
-
* [deepCopyObject](#deepcopyobject)
|
|
79
|
-
* [mTrim](#mtrim)
|
|
80
|
-
- [URL](#url)
|
|
81
|
-
* [getQueryParam](#getqueryparam)
|
|
82
|
-
* [getUrlParam](#geturlparam)
|
|
83
|
-
* [getHashQueryParam](#gethashqueryparam)
|
|
84
|
-
* [getDomain](#getdomain)
|
|
85
|
-
* [updateQueryParam](#updatequeryparam)
|
|
86
|
-
- [Cache Data](#cache-data)
|
|
87
|
-
* [Cookie](#cookie)
|
|
88
|
-
* [Storage](#storage)
|
|
89
|
-
- [DOM](#dom)
|
|
90
|
-
* [addStyle](#addstyle)
|
|
91
|
-
* [Class](#class)
|
|
92
|
-
* [newLine](#newline)
|
|
93
|
-
- [Calculate and Formula](#calculate-and-formula)
|
|
94
|
-
* [inRate](#inrate)
|
|
95
|
-
* [calLongestCommonSubstring](#callongestcommonsubstring)
|
|
96
|
-
* [calLongestCommonSubsequence](#callongestcommonsubsequence)
|
|
97
|
-
- [Browser Information](#browser-information)
|
|
98
|
-
* [getBrowserInfo](#getbrowserinfo)
|
|
99
|
-
* [isSafePWAEnv](#issafepwaenv)
|
|
100
|
-
- [Web Performance](#web-performance)
|
|
101
|
-
* [getPerformance](#getperformance)
|
|
102
|
-
- [Debug](#debug)
|
|
103
|
-
* [genCustomConsole](#gencustomconsole)
|
|
62
|
+
<!-- toc - begin -->
|
|
63
|
+
- Generated with ❤️
|
|
64
|
+
- [Load Resource](#load-resource)
|
|
65
|
+
* [loadScript](#loadscript)
|
|
66
|
+
* [loadCSS](#loadcss)
|
|
67
|
+
* [windowLoaded](#windowloaded)
|
|
68
|
+
- [Util](#util)
|
|
69
|
+
* [isNumber](#isnumber)
|
|
70
|
+
* [isJSONString](#isjsonstring)
|
|
71
|
+
* [isValidData](#isvaliddata)
|
|
72
|
+
* [generateRndNum](#generaterndnum)
|
|
73
|
+
* [formatDate](#formatdate)
|
|
74
|
+
* [debounce](#debounce)
|
|
75
|
+
* [throttle](#throttle)
|
|
76
|
+
* [convertCamelToKebab](#convertcameltokebab)
|
|
77
|
+
* [convertCamelToUnder](#convertcameltounder)
|
|
78
|
+
* [deepCopyObject](#deepcopyobject)
|
|
79
|
+
* [mTrim](#mtrim)
|
|
80
|
+
- [URL](#url)
|
|
81
|
+
* [getQueryParam](#getqueryparam)
|
|
82
|
+
* [getUrlParam](#geturlparam)
|
|
83
|
+
* [getHashQueryParam](#gethashqueryparam)
|
|
84
|
+
* [getDomain](#getdomain)
|
|
85
|
+
* [updateQueryParam](#updatequeryparam)
|
|
86
|
+
- [Cache Data](#cache-data)
|
|
87
|
+
* [Cookie](#cookie)
|
|
88
|
+
* [Storage](#storage)
|
|
89
|
+
- [DOM](#dom)
|
|
90
|
+
* [addStyle](#addstyle)
|
|
91
|
+
* [Class](#class)
|
|
92
|
+
* [newLine](#newline)
|
|
93
|
+
- [Calculate and Formula](#calculate-and-formula)
|
|
94
|
+
* [inRate](#inrate)
|
|
95
|
+
* [calLongestCommonSubstring](#callongestcommonsubstring)
|
|
96
|
+
* [calLongestCommonSubsequence](#callongestcommonsubsequence)
|
|
97
|
+
- [Browser Information](#browser-information)
|
|
98
|
+
* [getBrowserInfo](#getbrowserinfo)
|
|
99
|
+
* [isSafePWAEnv](#issafepwaenv)
|
|
100
|
+
- [Web Performance](#web-performance)
|
|
101
|
+
* [getPerformance](#getperformance)
|
|
102
|
+
- [Debug](#debug)
|
|
103
|
+
* [genCustomConsole](#gencustomconsole)
|
|
104
104
|
<!-- toc - end -->
|
|
105
105
|
|
|
106
106
|
### Load Resource
|
|
@@ -424,16 +424,16 @@ getQueryParam('t4'); // 4
|
|
|
424
424
|
|
|
425
425
|
#### getUrlParam
|
|
426
426
|
|
|
427
|
-
|
|
427
|
+
Returns the value of the specified query parameter in the input URL.
|
|
428
428
|
|
|
429
429
|
```
|
|
430
|
-
getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3'); // 3
|
|
431
|
-
getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4'); // 4
|
|
430
|
+
getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3'); // Returns '3'
|
|
431
|
+
getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4'); // Returns '4'
|
|
432
432
|
```
|
|
433
433
|
|
|
434
|
-
<!-- @param {string} url URL string.
|
|
435
|
-
@param {string} param
|
|
436
|
-
@returns {string} value
|
|
434
|
+
<!-- @param {string} url The URL string.
|
|
435
|
+
@param {string} param The query parameter to retrieve the value for.
|
|
436
|
+
@returns {string|string[]} The value of the specified query parameter, or an empty string if the parameter is not found.
|
|
437
437
|
@category URL -->
|
|
438
438
|
|
|
439
439
|
#### getHashQueryParam
|
|
@@ -553,7 +553,7 @@ function mGetLocalStorage (key) {
|
|
|
553
553
|
```
|
|
554
554
|
|
|
555
555
|
<!-- @param {string} key 键
|
|
556
|
-
@returns {
|
|
556
|
+
@returns {void} 返回值
|
|
557
557
|
@category Cache Data -->
|
|
558
558
|
|
|
559
559
|
### DOM
|
package/lib/index.cjs.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* mazey v3.
|
|
2
|
+
* mazey v3.7.0
|
|
3
3
|
* (c) 2018-2023 Mazey Chu
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -4362,34 +4362,32 @@ function getQueryParam(param) {
|
|
|
4362
4362
|
return '';
|
|
4363
4363
|
}
|
|
4364
4364
|
/**
|
|
4365
|
-
*
|
|
4365
|
+
* Returns the value of the specified query parameter in the input URL.
|
|
4366
4366
|
*
|
|
4367
|
+
* @example
|
|
4367
4368
|
* ```
|
|
4368
|
-
* getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3'); // 3
|
|
4369
|
-
* getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4'); // 4
|
|
4369
|
+
* getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3'); // Returns '3'
|
|
4370
|
+
* getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4'); // Returns '4'
|
|
4370
4371
|
* ```
|
|
4371
4372
|
*
|
|
4372
|
-
* @param {string} url URL string.
|
|
4373
|
-
* @param {string} param
|
|
4374
|
-
* @returns {string} value
|
|
4373
|
+
* @param {string} url The URL string.
|
|
4374
|
+
* @param {string} param The query parameter to retrieve the value for.
|
|
4375
|
+
* @returns {string|string[]} The value of the specified query parameter, or an empty string if the parameter is not found.
|
|
4375
4376
|
* @category URL
|
|
4376
4377
|
*/
|
|
4377
4378
|
function getUrlParam(url, param) {
|
|
4378
4379
|
var result = {};
|
|
4379
|
-
|
|
4380
|
-
url.replace(/\??(\w+)=([^&]*)&?/g, function (a, k, v) {
|
|
4380
|
+
url.replace(/\??(\w+)=([^&]*)&?/g, function (_, k, v) {
|
|
4381
4381
|
if (result[k] !== undefined) {
|
|
4382
4382
|
var t = result[k];
|
|
4383
|
+
// Cast `t` to `string[]` to ensure that `concat` receives an array
|
|
4383
4384
|
result[k] = [].concat(t, v);
|
|
4384
4385
|
} else {
|
|
4385
4386
|
result[k] = v;
|
|
4386
4387
|
}
|
|
4388
|
+
// Return an empty string to satisfy the signature of the replace method
|
|
4389
|
+
return '';
|
|
4387
4390
|
});
|
|
4388
|
-
// if (param === undefined) {
|
|
4389
|
-
// return result;
|
|
4390
|
-
// } else {
|
|
4391
|
-
// return result[param] || '';
|
|
4392
|
-
// }
|
|
4393
4391
|
return result[param] || '';
|
|
4394
4392
|
}
|
|
4395
4393
|
/**
|
|
@@ -4671,8 +4669,8 @@ function generateRndNum(n) {
|
|
|
4671
4669
|
return ret;
|
|
4672
4670
|
}
|
|
4673
4671
|
/**
|
|
4674
|
-
*
|
|
4675
|
-
*
|
|
4672
|
+
* 根据时间生成唯一标志的数字 mGenerateUniqueNum() => 1538324722364123
|
|
4673
|
+
*
|
|
4676
4674
|
* @param {number} n 随机数的长度
|
|
4677
4675
|
* @category Util
|
|
4678
4676
|
*/
|
|
@@ -4686,8 +4684,8 @@ function generateUniqueNum(n) {
|
|
|
4686
4684
|
return now + rnd;
|
|
4687
4685
|
}
|
|
4688
4686
|
/**
|
|
4689
|
-
*
|
|
4690
|
-
*
|
|
4687
|
+
* 浮点数转为百分比 0.2 => 20%
|
|
4688
|
+
*
|
|
4691
4689
|
* @param {number} num 浮点数
|
|
4692
4690
|
* @param {number} fixSize 保留几位浮点数
|
|
4693
4691
|
* @category Util
|
|
@@ -4705,8 +4703,8 @@ function floatToPercent(num, fixSize) {
|
|
|
4705
4703
|
return "".concat(ret, "%");
|
|
4706
4704
|
}
|
|
4707
4705
|
/**
|
|
4708
|
-
*
|
|
4709
|
-
*
|
|
4706
|
+
* 浮点数保留指定位
|
|
4707
|
+
*
|
|
4710
4708
|
* @category Util
|
|
4711
4709
|
*/
|
|
4712
4710
|
function floatFixed(num, size) {
|
|
@@ -4716,9 +4714,9 @@ function floatFixed(num, size) {
|
|
|
4716
4714
|
return parseFloat(num).toFixed(size);
|
|
4717
4715
|
}
|
|
4718
4716
|
/**
|
|
4719
|
-
*
|
|
4720
|
-
*
|
|
4721
|
-
* @category
|
|
4717
|
+
* 阻止冒泡
|
|
4718
|
+
*
|
|
4719
|
+
* @category Event
|
|
4722
4720
|
*/
|
|
4723
4721
|
function cancelBubble(e) {
|
|
4724
4722
|
var ev = e || window.event;
|
|
@@ -4829,8 +4827,6 @@ function throttle(func, wait, options) {
|
|
|
4829
4827
|
options = {};
|
|
4830
4828
|
}
|
|
4831
4829
|
options = Object.assign({}, options);
|
|
4832
|
-
// timeout: setTimeout Handle
|
|
4833
|
-
// previous: 上次时间戳
|
|
4834
4830
|
var context = null;
|
|
4835
4831
|
var args = null;
|
|
4836
4832
|
var timeout = null;
|
|
@@ -4840,7 +4836,7 @@ function throttle(func, wait, options) {
|
|
|
4840
4836
|
var later = function later() {
|
|
4841
4837
|
previous = options.leading === false ? 0 : mNow();
|
|
4842
4838
|
timeout = null;
|
|
4843
|
-
result = func.apply(
|
|
4839
|
+
result = func.apply(this, args);
|
|
4844
4840
|
if (!timeout) {
|
|
4845
4841
|
context = args = null;
|
|
4846
4842
|
}
|
|
@@ -4868,7 +4864,7 @@ function throttle(func, wait, options) {
|
|
|
4868
4864
|
context = args = null;
|
|
4869
4865
|
}
|
|
4870
4866
|
} else if (!timeout && options.trailing !== false) {
|
|
4871
|
-
timeout = setTimeout(later, remaining);
|
|
4867
|
+
timeout = setTimeout(later.bind(context), remaining);
|
|
4872
4868
|
}
|
|
4873
4869
|
return result;
|
|
4874
4870
|
};
|
|
@@ -4891,7 +4887,7 @@ function debounce(func, wait, immediate) {
|
|
|
4891
4887
|
var timeout = null;
|
|
4892
4888
|
var timestamp = null;
|
|
4893
4889
|
var args = null;
|
|
4894
|
-
var result =
|
|
4890
|
+
var result = null;
|
|
4895
4891
|
var later = function later() {
|
|
4896
4892
|
var last = mNow() - timestamp;
|
|
4897
4893
|
if (last < wait && last >= 0) {
|
|
@@ -5010,37 +5006,26 @@ function getFriendlyInterval(start, end, options) {
|
|
|
5010
5006
|
*/
|
|
5011
5007
|
function isNumber(num, options) {
|
|
5012
5008
|
if (options === void 0) {
|
|
5013
|
-
options = {
|
|
5014
|
-
isNaNAsNumber: false,
|
|
5015
|
-
isUnFiniteAsNumber: false
|
|
5016
|
-
};
|
|
5009
|
+
options = {};
|
|
5017
5010
|
}
|
|
5018
|
-
var _a =
|
|
5019
|
-
|
|
5020
|
-
|
|
5021
|
-
|
|
5022
|
-
isNaNAsNumber = _a.isNaNAsNumber,
|
|
5023
|
-
isUnFiniteAsNumber = _a.isUnFiniteAsNumber;
|
|
5024
|
-
var ret = true;
|
|
5025
|
-
// 数字类型
|
|
5011
|
+
var _a = options.isNaNAsNumber,
|
|
5012
|
+
isNaNAsNumber = _a === void 0 ? false : _a,
|
|
5013
|
+
_b = options.isFiniteAsNumber,
|
|
5014
|
+
isFiniteAsNumber = _b === void 0 ? false : _b;
|
|
5026
5015
|
if (typeof num !== 'number') {
|
|
5027
|
-
|
|
5016
|
+
return false;
|
|
5028
5017
|
}
|
|
5029
|
-
|
|
5030
|
-
|
|
5031
|
-
// console.log('1333');
|
|
5032
|
-
ret = false;
|
|
5018
|
+
if (!isFiniteAsNumber && !isFinite(num)) {
|
|
5019
|
+
return false;
|
|
5033
5020
|
}
|
|
5034
|
-
|
|
5035
|
-
|
|
5036
|
-
// console.log('2333');
|
|
5037
|
-
ret = false;
|
|
5021
|
+
if (!isNaNAsNumber && isNaN(num)) {
|
|
5022
|
+
return false;
|
|
5038
5023
|
}
|
|
5039
|
-
return
|
|
5024
|
+
return true;
|
|
5040
5025
|
}
|
|
5041
5026
|
/**
|
|
5042
|
-
*
|
|
5043
|
-
*
|
|
5027
|
+
* 执行有效函数
|
|
5028
|
+
*
|
|
5044
5029
|
* @param {function} fn 等待被执行的未知是否有效的函数
|
|
5045
5030
|
* @category Util
|
|
5046
5031
|
*/
|
|
@@ -5078,7 +5063,7 @@ function doFn(fn) {
|
|
|
5078
5063
|
* ```
|
|
5079
5064
|
*
|
|
5080
5065
|
* @param {string} key 键
|
|
5081
|
-
* @returns {
|
|
5066
|
+
* @returns {void} 返回值
|
|
5082
5067
|
* @category Cache Data
|
|
5083
5068
|
*/
|
|
5084
5069
|
function setSessionStorage(key, value) {
|
|
@@ -5148,7 +5133,7 @@ function getSessionStorage(key) {
|
|
|
5148
5133
|
* ```
|
|
5149
5134
|
*
|
|
5150
5135
|
* @param {string} key 键
|
|
5151
|
-
* @returns {
|
|
5136
|
+
* @returns {void} 返回值
|
|
5152
5137
|
* @category Cache Data
|
|
5153
5138
|
*/
|
|
5154
5139
|
function setLocalStorage(key, value) {
|
|
@@ -5427,8 +5412,8 @@ function loadScript(url, options) {
|
|
|
5427
5412
|
});
|
|
5428
5413
|
}
|
|
5429
5414
|
/**
|
|
5430
|
-
*
|
|
5431
|
-
*
|
|
5415
|
+
* 获取时间戳
|
|
5416
|
+
*
|
|
5432
5417
|
* @category Util
|
|
5433
5418
|
*/
|
|
5434
5419
|
function mNow() {
|
|
@@ -5454,11 +5439,11 @@ function mNow() {
|
|
|
5454
5439
|
*/
|
|
5455
5440
|
function setCookie(name, value, days, domain) {
|
|
5456
5441
|
var domainParts, expires;
|
|
5457
|
-
|
|
5442
|
+
// let date: any;
|
|
5458
5443
|
if (days) {
|
|
5459
|
-
date = new Date();
|
|
5444
|
+
var date = new Date();
|
|
5460
5445
|
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
|
5461
|
-
expires = '; expires=' + date.
|
|
5446
|
+
expires = '; expires=' + date.toUTCString();
|
|
5462
5447
|
} else {
|
|
5463
5448
|
expires = '';
|
|
5464
5449
|
}
|
|
@@ -6103,14 +6088,50 @@ function getBrowserInfo() {
|
|
|
6103
6088
|
}
|
|
6104
6089
|
}
|
|
6105
6090
|
/**
|
|
6106
|
-
*
|
|
6107
|
-
*
|
|
6091
|
+
* 去除 HTML 标签
|
|
6092
|
+
*
|
|
6108
6093
|
* @param {string} str 带html标签的字符串
|
|
6109
6094
|
* @returns {string} 字符串
|
|
6110
6095
|
* @category Util
|
|
6111
6096
|
*/
|
|
6112
|
-
function clearHtml(str) {
|
|
6113
|
-
|
|
6097
|
+
function clearHtml(str, options) {
|
|
6098
|
+
if (options === void 0) {
|
|
6099
|
+
options = {};
|
|
6100
|
+
}
|
|
6101
|
+
var _a = options.removeNewLine,
|
|
6102
|
+
removeNewLine = _a === void 0 ? false : _a;
|
|
6103
|
+
var ret = '';
|
|
6104
|
+
if (str) {
|
|
6105
|
+
ret = str.replace(/<\/?.+?>/g, '');
|
|
6106
|
+
if (removeNewLine) {
|
|
6107
|
+
ret = ret.replace(/[\r\n]/g, '');
|
|
6108
|
+
}
|
|
6109
|
+
}
|
|
6110
|
+
return ret;
|
|
6111
|
+
// return str.replace(/<\/?.+?>/g, '').replace(/[\r\n]/g, '');
|
|
6112
|
+
}
|
|
6113
|
+
/**
|
|
6114
|
+
* Sanitizes user input to prevent XSS attacks
|
|
6115
|
+
*
|
|
6116
|
+
* @param input - The input string to sanitize
|
|
6117
|
+
* @returns The sanitized input string
|
|
6118
|
+
*/
|
|
6119
|
+
function sanitizeInput(input) {
|
|
6120
|
+
var regex = /[&<>"'/]/g;
|
|
6121
|
+
var replacements = {
|
|
6122
|
+
'&': '&',
|
|
6123
|
+
'<': '<',
|
|
6124
|
+
'>': '>',
|
|
6125
|
+
'"': '"',
|
|
6126
|
+
'\'': ''',
|
|
6127
|
+
'/': '/'
|
|
6128
|
+
};
|
|
6129
|
+
if (typeof input !== 'string') {
|
|
6130
|
+
console.error('Input must be a string');
|
|
6131
|
+
}
|
|
6132
|
+
return input.replace(regex, function (match) {
|
|
6133
|
+
return replacements[match];
|
|
6134
|
+
});
|
|
6114
6135
|
}
|
|
6115
6136
|
/**
|
|
6116
6137
|
* 截取字符串,中文算 2 个字节
|
|
@@ -6513,15 +6534,12 @@ function isNonEmptyArray(arr) {
|
|
|
6513
6534
|
function isValidData(data, attributes, validValue) {
|
|
6514
6535
|
var ret = false;
|
|
6515
6536
|
var foundRet = attributes.reduce(function (foundValue, curr) {
|
|
6516
|
-
if (foundValue
|
|
6517
|
-
|
|
6537
|
+
if (foundValue && Object.prototype.hasOwnProperty.call(foundValue, curr)) {
|
|
6538
|
+
return foundValue[curr];
|
|
6518
6539
|
} else {
|
|
6519
6540
|
return Object.create(null);
|
|
6520
6541
|
}
|
|
6521
|
-
// console.log('foundValue', foundValue);
|
|
6522
|
-
return foundValue;
|
|
6523
6542
|
}, data);
|
|
6524
|
-
// console.log('foundRet', foundRet);
|
|
6525
6543
|
if (foundRet === validValue) {
|
|
6526
6544
|
ret = true;
|
|
6527
6545
|
}
|
|
@@ -6843,7 +6861,6 @@ function getUrlFileType(url) {
|
|
|
6843
6861
|
* The `src` attribute should contain `width` and/or `height` values in the format "width=100" or "height=100".
|
|
6844
6862
|
* If jQuery is available, this function uses jQuery to select the images. Otherwise, it uses pure JavaScript.
|
|
6845
6863
|
*
|
|
6846
|
-
*
|
|
6847
6864
|
* @example
|
|
6848
6865
|
* ```
|
|
6849
6866
|
* // Example images with `src` attributes containing `width` and/or `height` values
|
|
@@ -6863,18 +6880,21 @@ function setImgWidHeiBySrc() {
|
|
|
6863
6880
|
var $ = window.jQuery || window.$;
|
|
6864
6881
|
if ($) {
|
|
6865
6882
|
// Use jQuery to select all images on the page
|
|
6866
|
-
$('img')
|
|
6883
|
+
var images = $('img');
|
|
6884
|
+
if (!(images && images.length)) return false;
|
|
6885
|
+
images.each(function () {
|
|
6867
6886
|
var $this = $(this);
|
|
6868
6887
|
if (!$this) return;
|
|
6869
6888
|
// Get the `src` attribute of the image
|
|
6870
6889
|
var src = $this.attr('src');
|
|
6871
|
-
|
|
6890
|
+
var canMatch = src && typeof src === 'string' && src.length;
|
|
6891
|
+
if (!canMatch) return;
|
|
6872
6892
|
// Use regular expressions to extract the `width` and `height` values from the `src` attribute
|
|
6873
6893
|
var width = src.match(/width=([0-9]+[a-z%]*)/);
|
|
6874
6894
|
var height = src.match(/height=([0-9]+[a-z%]*)/);
|
|
6875
6895
|
// Set the width and height of the image using jQuery's `width()` and `height()` methods
|
|
6876
|
-
if (width && width[1]) $this.width(width[1]);
|
|
6877
|
-
if (height && height[1]) $this.height(height[1]);
|
|
6896
|
+
if (width && isNonEmptyArray(width) && width[1]) $this.width(width[1]);
|
|
6897
|
+
if (height && isNonEmptyArray(height) && height[1]) $this.height(height[1]);
|
|
6878
6898
|
});
|
|
6879
6899
|
return true;
|
|
6880
6900
|
} else {
|
|
@@ -6883,20 +6903,83 @@ function setImgWidHeiBySrc() {
|
|
|
6883
6903
|
if (images.length > 0) {
|
|
6884
6904
|
// Loop through each image and set its width and height based on the `src` attribute
|
|
6885
6905
|
Array.from(images).forEach(function (img) {
|
|
6906
|
+
var $this = img;
|
|
6907
|
+
if (!$this) return;
|
|
6886
6908
|
// Get the `src` attribute of the image
|
|
6887
|
-
var src =
|
|
6909
|
+
var src = $this.getAttribute('src');
|
|
6910
|
+
var canMatch = src && typeof src === 'string' && src.length;
|
|
6911
|
+
if (!canMatch) return;
|
|
6888
6912
|
// Use regular expressions to extract the `width` and `height` values from the `src` attribute
|
|
6889
|
-
var width = src
|
|
6890
|
-
var height = src
|
|
6913
|
+
var width = src.match(/width=([0-9]+[a-z%]*)/);
|
|
6914
|
+
var height = src.match(/height=([0-9]+[a-z%]*)/);
|
|
6891
6915
|
// Set the width and height of the image using the `style.width` and `style.height` properties
|
|
6892
|
-
if (width && width[1])
|
|
6893
|
-
if (height && height[1])
|
|
6916
|
+
if (width && isNonEmptyArray(width) && width[1]) $this.style.width = width[1];
|
|
6917
|
+
if (height && isNonEmptyArray(height) && height[1]) $this.style.height = height[1];
|
|
6894
6918
|
});
|
|
6895
6919
|
return true;
|
|
6896
6920
|
}
|
|
6897
6921
|
}
|
|
6898
6922
|
return false;
|
|
6899
6923
|
}
|
|
6924
|
+
/**
|
|
6925
|
+
* Generate the inline style string from the given parameters, First parameter is the ClassNames, Second parameter is the style array.
|
|
6926
|
+
*
|
|
6927
|
+
* @example
|
|
6928
|
+
* ```js
|
|
6929
|
+
* console.log(genStyleString('a', [ 'color:red' ])); // '.a{color:red;}'
|
|
6930
|
+
* console.log(genStyleString('b', [ 'color:red', 'font-size:12px' ])); // '.b{color:red;font-size:12px;}'
|
|
6931
|
+
* ```
|
|
6932
|
+
*
|
|
6933
|
+
* @param {string} className
|
|
6934
|
+
* @param {Array} styleArray
|
|
6935
|
+
* @returns {string} Return the inline style string.
|
|
6936
|
+
* @category DOM
|
|
6937
|
+
*/
|
|
6938
|
+
function genStyleString(className, styleArray) {
|
|
6939
|
+
var style = '';
|
|
6940
|
+
if (styleArray && styleArray.length > 0) {
|
|
6941
|
+
// It's wrong. Last item will not include `;`.
|
|
6942
|
+
// style = styleArray.join(';');
|
|
6943
|
+
style = styleArray.join(';') + ';';
|
|
6944
|
+
}
|
|
6945
|
+
return ".".concat(className, "{").concat(style, "}");
|
|
6946
|
+
}
|
|
6947
|
+
/**
|
|
6948
|
+
* Load the image from the given url.
|
|
6949
|
+
* The target image will be loaded in the background, and the Promise status will change after the image is loaded.
|
|
6950
|
+
* If the image fails to load, the Promise status will change to `reject`.
|
|
6951
|
+
* If the image is loaded successfully, the Promise status will change to `resolve`.
|
|
6952
|
+
* If the image is loaded successfully, the Promise will return the image object.
|
|
6953
|
+
* If the image fails to load, the Promise will return the error object.
|
|
6954
|
+
* The method will help Web to preload the image. And the image will be cached by the browser.
|
|
6955
|
+
* We can use it to implement the lazy loading of images.
|
|
6956
|
+
* The method will not add the image to the DOM.
|
|
6957
|
+
*
|
|
6958
|
+
* @example
|
|
6959
|
+
* ```js
|
|
6960
|
+
* loadImage('https://example.com/example.png').then((img) => {
|
|
6961
|
+
* console.log(img);
|
|
6962
|
+
* }).catch((err) => {
|
|
6963
|
+
* console.log(err);
|
|
6964
|
+
* });
|
|
6965
|
+
* ```
|
|
6966
|
+
*
|
|
6967
|
+
* @param {string} url - The image url.
|
|
6968
|
+
* @returns {Promise} Return the Promise object.
|
|
6969
|
+
* @category Load Resource
|
|
6970
|
+
*/
|
|
6971
|
+
function loadImage(url) {
|
|
6972
|
+
return new Promise(function (resolve, reject) {
|
|
6973
|
+
var img = new Image();
|
|
6974
|
+
img.onload = function () {
|
|
6975
|
+
resolve(img);
|
|
6976
|
+
};
|
|
6977
|
+
img.onerror = function (err) {
|
|
6978
|
+
reject(err);
|
|
6979
|
+
};
|
|
6980
|
+
img.src = url;
|
|
6981
|
+
});
|
|
6982
|
+
}
|
|
6900
6983
|
|
|
6901
6984
|
exports.addClass = addClass;
|
|
6902
6985
|
exports.addEvent = addEvent;
|
|
@@ -6919,6 +7002,7 @@ exports.floatToPercent = floatToPercent;
|
|
|
6919
7002
|
exports.formatDate = formatDate;
|
|
6920
7003
|
exports.genCustomConsole = genCustomConsole;
|
|
6921
7004
|
exports.genHashCode = genHashCode;
|
|
7005
|
+
exports.genStyleString = genStyleString;
|
|
6922
7006
|
exports.generateRndNum = generateRndNum;
|
|
6923
7007
|
exports.generateUniqueNum = generateUniqueNum;
|
|
6924
7008
|
exports.getBrowserInfo = getBrowserInfo;
|
|
@@ -6948,12 +7032,14 @@ exports.isValidEmail = isValidEmail;
|
|
|
6948
7032
|
exports.isValidPhoneNumber = isValidPhoneNumber;
|
|
6949
7033
|
exports.isValidUrl = isValidUrl;
|
|
6950
7034
|
exports.loadCSS = loadCSS;
|
|
7035
|
+
exports.loadImage = loadImage;
|
|
6951
7036
|
exports.loadScript = loadScript;
|
|
6952
7037
|
exports.mNow = mNow;
|
|
6953
7038
|
exports.mTrim = mTrim;
|
|
6954
7039
|
exports.newLine = newLine;
|
|
6955
7040
|
exports.removeClass = removeClass;
|
|
6956
7041
|
exports.removeEvent = removeEvent;
|
|
7042
|
+
exports.sanitizeInput = sanitizeInput;
|
|
6957
7043
|
exports.setCookie = setCookie;
|
|
6958
7044
|
exports.setImgWidHeiBySrc = setImgWidHeiBySrc;
|
|
6959
7045
|
exports.setLocalStorage = setLocalStorage;
|