mazey 3.6.7 → 3.7.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.md +53 -53
- package/lib/index.cjs.js +171 -76
- package/lib/index.d.ts +93 -50
- package/lib/index.d.ts.map +1 -1
- package/lib/index.esm.js +169 -77
- package/lib/mazey.min.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,7 +48,7 @@ Import from CDN.
|
|
|
48
48
|
<script type="text/javascript" src="//i.mazey.net/mazey/lib/mazey.min.js"></script>
|
|
49
49
|
<script>
|
|
50
50
|
const x = Infinity;
|
|
51
|
-
mazey.isNumber(x, {
|
|
51
|
+
mazey.isNumber(x, { isInfinityAsNumber: true }); // <=> typeof x === 'string' && !isNaN(x)
|
|
52
52
|
// Output: true
|
|
53
53
|
</script>
|
|
54
54
|
```
|
|
@@ -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
|
|
@@ -203,14 +203,14 @@ isNumber(123); // true
|
|
|
203
203
|
isNumber('123'); // false
|
|
204
204
|
// Default: NaN, Infinity is not Number
|
|
205
205
|
isNumber(Infinity); // false
|
|
206
|
-
isNumber(Infinity, {
|
|
206
|
+
isNumber(Infinity, { isInfinityAsNumber: true }); // true
|
|
207
207
|
isNumber(NaN); // false
|
|
208
|
-
isNumber(NaN, { isNaNAsNumber: true,
|
|
208
|
+
isNumber(NaN, { isNaNAsNumber: true, isInfinityAsNumber: true }); // true
|
|
209
209
|
```
|
|
210
210
|
|
|
211
211
|
<!-- @param {*} num 被判断的值
|
|
212
212
|
@param {boolean} options.isNaNAsNumber 是否 NaN 算数字(默认不算)
|
|
213
|
-
@param {boolean} options.
|
|
213
|
+
@param {boolean} options.isInfinityAsNumber 是否 无限 算数字(默认不算)
|
|
214
214
|
@returns {boolean} true 是数字
|
|
215
215
|
@category Util -->
|
|
216
216
|
|
|
@@ -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.1
|
|
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) {
|
|
@@ -4997,50 +4993,45 @@ function getFriendlyInterval(start, end, options) {
|
|
|
4997
4993
|
* isNumber('123'); // false
|
|
4998
4994
|
* // Default: NaN, Infinity is not Number
|
|
4999
4995
|
* isNumber(Infinity); // false
|
|
5000
|
-
* isNumber(Infinity, {
|
|
4996
|
+
* isNumber(Infinity, { isInfinityAsNumber: true }); // true
|
|
5001
4997
|
* isNumber(NaN); // false
|
|
5002
|
-
* isNumber(NaN, { isNaNAsNumber: true,
|
|
4998
|
+
* isNumber(NaN, { isNaNAsNumber: true, isInfinityAsNumber: true }); // true
|
|
5003
4999
|
* ```
|
|
5004
5000
|
*
|
|
5005
5001
|
* @param {*} num 被判断的值
|
|
5006
5002
|
* @param {boolean} options.isNaNAsNumber 是否 NaN 算数字(默认不算)
|
|
5007
|
-
* @param {boolean} options.
|
|
5003
|
+
* @param {boolean} options.isInfinityAsNumber 是否 无限 算数字(默认不算)
|
|
5008
5004
|
* @returns {boolean} true 是数字
|
|
5009
5005
|
* @category Util
|
|
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
|
-
|
|
5023
|
-
isUnFiniteAsNumber =
|
|
5024
|
-
var ret = true;
|
|
5025
|
-
// 数字类型
|
|
5011
|
+
var _a = options.isNaNAsNumber,
|
|
5012
|
+
isNaNAsNumber = _a === void 0 ? false : _a,
|
|
5013
|
+
_b = options.isInfinityAsNumber,
|
|
5014
|
+
isInfinityAsNumber = _b === void 0 ? false : _b,
|
|
5015
|
+
_c = options.isUnFiniteAsNumber,
|
|
5016
|
+
isUnFiniteAsNumber = _c === void 0 ? false : _c;
|
|
5026
5017
|
if (typeof num !== 'number') {
|
|
5027
|
-
|
|
5018
|
+
return false;
|
|
5028
5019
|
}
|
|
5029
|
-
|
|
5030
|
-
|
|
5031
|
-
// console.log('1333');
|
|
5032
|
-
ret = false;
|
|
5020
|
+
if (!(isInfinityAsNumber === true || isUnFiniteAsNumber === true) && !isFinite(num)) {
|
|
5021
|
+
return false;
|
|
5033
5022
|
}
|
|
5034
|
-
//
|
|
5035
|
-
if (
|
|
5036
|
-
|
|
5037
|
-
|
|
5023
|
+
// Be compatible with previous versions.
|
|
5024
|
+
// if (!isUnFiniteAsNumber && !isFinite(num)) {
|
|
5025
|
+
// return false;
|
|
5026
|
+
// }
|
|
5027
|
+
if (!isNaNAsNumber && isNaN(num)) {
|
|
5028
|
+
return false;
|
|
5038
5029
|
}
|
|
5039
|
-
return
|
|
5030
|
+
return true;
|
|
5040
5031
|
}
|
|
5041
5032
|
/**
|
|
5042
|
-
*
|
|
5043
|
-
*
|
|
5033
|
+
* 执行有效函数
|
|
5034
|
+
*
|
|
5044
5035
|
* @param {function} fn 等待被执行的未知是否有效的函数
|
|
5045
5036
|
* @category Util
|
|
5046
5037
|
*/
|
|
@@ -5078,7 +5069,7 @@ function doFn(fn) {
|
|
|
5078
5069
|
* ```
|
|
5079
5070
|
*
|
|
5080
5071
|
* @param {string} key 键
|
|
5081
|
-
* @returns {
|
|
5072
|
+
* @returns {void} 返回值
|
|
5082
5073
|
* @category Cache Data
|
|
5083
5074
|
*/
|
|
5084
5075
|
function setSessionStorage(key, value) {
|
|
@@ -5148,7 +5139,7 @@ function getSessionStorage(key) {
|
|
|
5148
5139
|
* ```
|
|
5149
5140
|
*
|
|
5150
5141
|
* @param {string} key 键
|
|
5151
|
-
* @returns {
|
|
5142
|
+
* @returns {void} 返回值
|
|
5152
5143
|
* @category Cache Data
|
|
5153
5144
|
*/
|
|
5154
5145
|
function setLocalStorage(key, value) {
|
|
@@ -5427,8 +5418,8 @@ function loadScript(url, options) {
|
|
|
5427
5418
|
});
|
|
5428
5419
|
}
|
|
5429
5420
|
/**
|
|
5430
|
-
*
|
|
5431
|
-
*
|
|
5421
|
+
* 获取时间戳
|
|
5422
|
+
*
|
|
5432
5423
|
* @category Util
|
|
5433
5424
|
*/
|
|
5434
5425
|
function mNow() {
|
|
@@ -5454,11 +5445,11 @@ function mNow() {
|
|
|
5454
5445
|
*/
|
|
5455
5446
|
function setCookie(name, value, days, domain) {
|
|
5456
5447
|
var domainParts, expires;
|
|
5457
|
-
|
|
5448
|
+
// let date: any;
|
|
5458
5449
|
if (days) {
|
|
5459
|
-
date = new Date();
|
|
5450
|
+
var date = new Date();
|
|
5460
5451
|
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
|
5461
|
-
expires = '; expires=' + date.
|
|
5452
|
+
expires = '; expires=' + date.toUTCString();
|
|
5462
5453
|
} else {
|
|
5463
5454
|
expires = '';
|
|
5464
5455
|
}
|
|
@@ -6103,14 +6094,50 @@ function getBrowserInfo() {
|
|
|
6103
6094
|
}
|
|
6104
6095
|
}
|
|
6105
6096
|
/**
|
|
6106
|
-
*
|
|
6107
|
-
*
|
|
6097
|
+
* 去除 HTML 标签
|
|
6098
|
+
*
|
|
6108
6099
|
* @param {string} str 带html标签的字符串
|
|
6109
6100
|
* @returns {string} 字符串
|
|
6110
6101
|
* @category Util
|
|
6111
6102
|
*/
|
|
6112
|
-
function clearHtml(str) {
|
|
6113
|
-
|
|
6103
|
+
function clearHtml(str, options) {
|
|
6104
|
+
if (options === void 0) {
|
|
6105
|
+
options = {};
|
|
6106
|
+
}
|
|
6107
|
+
var _a = options.removeNewLine,
|
|
6108
|
+
removeNewLine = _a === void 0 ? false : _a;
|
|
6109
|
+
var ret = '';
|
|
6110
|
+
if (str) {
|
|
6111
|
+
ret = str.replace(/<\/?.+?>/g, '');
|
|
6112
|
+
if (removeNewLine) {
|
|
6113
|
+
ret = ret.replace(/[\r\n]/g, '');
|
|
6114
|
+
}
|
|
6115
|
+
}
|
|
6116
|
+
return ret;
|
|
6117
|
+
// return str.replace(/<\/?.+?>/g, '').replace(/[\r\n]/g, '');
|
|
6118
|
+
}
|
|
6119
|
+
/**
|
|
6120
|
+
* Sanitizes user input to prevent XSS attacks
|
|
6121
|
+
*
|
|
6122
|
+
* @param input - The input string to sanitize
|
|
6123
|
+
* @returns The sanitized input string
|
|
6124
|
+
*/
|
|
6125
|
+
function sanitizeInput(input) {
|
|
6126
|
+
var regex = /[&<>"'/]/g;
|
|
6127
|
+
var replacements = {
|
|
6128
|
+
'&': '&',
|
|
6129
|
+
'<': '<',
|
|
6130
|
+
'>': '>',
|
|
6131
|
+
'"': '"',
|
|
6132
|
+
'\'': ''',
|
|
6133
|
+
'/': '/'
|
|
6134
|
+
};
|
|
6135
|
+
if (typeof input !== 'string') {
|
|
6136
|
+
console.error('Input must be a string');
|
|
6137
|
+
}
|
|
6138
|
+
return input.replace(regex, function (match) {
|
|
6139
|
+
return replacements[match];
|
|
6140
|
+
});
|
|
6114
6141
|
}
|
|
6115
6142
|
/**
|
|
6116
6143
|
* 截取字符串,中文算 2 个字节
|
|
@@ -6843,7 +6870,6 @@ function getUrlFileType(url) {
|
|
|
6843
6870
|
* The `src` attribute should contain `width` and/or `height` values in the format "width=100" or "height=100".
|
|
6844
6871
|
* If jQuery is available, this function uses jQuery to select the images. Otherwise, it uses pure JavaScript.
|
|
6845
6872
|
*
|
|
6846
|
-
*
|
|
6847
6873
|
* @example
|
|
6848
6874
|
* ```
|
|
6849
6875
|
* // Example images with `src` attributes containing `width` and/or `height` values
|
|
@@ -6863,18 +6889,21 @@ function setImgWidHeiBySrc() {
|
|
|
6863
6889
|
var $ = window.jQuery || window.$;
|
|
6864
6890
|
if ($) {
|
|
6865
6891
|
// Use jQuery to select all images on the page
|
|
6866
|
-
$('img')
|
|
6892
|
+
var images = $('img');
|
|
6893
|
+
if (!(images && images.length)) return false;
|
|
6894
|
+
images.each(function () {
|
|
6867
6895
|
var $this = $(this);
|
|
6868
6896
|
if (!$this) return;
|
|
6869
6897
|
// Get the `src` attribute of the image
|
|
6870
6898
|
var src = $this.attr('src');
|
|
6871
|
-
|
|
6899
|
+
var canMatch = src && typeof src === 'string' && src.length;
|
|
6900
|
+
if (!canMatch) return;
|
|
6872
6901
|
// Use regular expressions to extract the `width` and `height` values from the `src` attribute
|
|
6873
6902
|
var width = src.match(/width=([0-9]+[a-z%]*)/);
|
|
6874
6903
|
var height = src.match(/height=([0-9]+[a-z%]*)/);
|
|
6875
6904
|
// 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]);
|
|
6905
|
+
if (width && isNonEmptyArray(width) && width[1]) $this.width(width[1]);
|
|
6906
|
+
if (height && isNonEmptyArray(height) && height[1]) $this.height(height[1]);
|
|
6878
6907
|
});
|
|
6879
6908
|
return true;
|
|
6880
6909
|
} else {
|
|
@@ -6883,20 +6912,83 @@ function setImgWidHeiBySrc() {
|
|
|
6883
6912
|
if (images.length > 0) {
|
|
6884
6913
|
// Loop through each image and set its width and height based on the `src` attribute
|
|
6885
6914
|
Array.from(images).forEach(function (img) {
|
|
6915
|
+
var $this = img;
|
|
6916
|
+
if (!$this) return;
|
|
6886
6917
|
// Get the `src` attribute of the image
|
|
6887
|
-
var src =
|
|
6918
|
+
var src = $this.getAttribute('src');
|
|
6919
|
+
var canMatch = src && typeof src === 'string' && src.length;
|
|
6920
|
+
if (!canMatch) return;
|
|
6888
6921
|
// Use regular expressions to extract the `width` and `height` values from the `src` attribute
|
|
6889
|
-
var width = src
|
|
6890
|
-
var height = src
|
|
6922
|
+
var width = src.match(/width=([0-9]+[a-z%]*)/);
|
|
6923
|
+
var height = src.match(/height=([0-9]+[a-z%]*)/);
|
|
6891
6924
|
// 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])
|
|
6925
|
+
if (width && isNonEmptyArray(width) && width[1]) $this.style.width = width[1];
|
|
6926
|
+
if (height && isNonEmptyArray(height) && height[1]) $this.style.height = height[1];
|
|
6894
6927
|
});
|
|
6895
6928
|
return true;
|
|
6896
6929
|
}
|
|
6897
6930
|
}
|
|
6898
6931
|
return false;
|
|
6899
6932
|
}
|
|
6933
|
+
/**
|
|
6934
|
+
* Generate the inline style string from the given parameters, First parameter is the ClassNames, Second parameter is the style array.
|
|
6935
|
+
*
|
|
6936
|
+
* @example
|
|
6937
|
+
* ```js
|
|
6938
|
+
* console.log(genStyleString('a', [ 'color:red' ])); // '.a{color:red;}'
|
|
6939
|
+
* console.log(genStyleString('b', [ 'color:red', 'font-size:12px' ])); // '.b{color:red;font-size:12px;}'
|
|
6940
|
+
* ```
|
|
6941
|
+
*
|
|
6942
|
+
* @param {string} className
|
|
6943
|
+
* @param {Array} styleArray
|
|
6944
|
+
* @returns {string} Return the inline style string.
|
|
6945
|
+
* @category DOM
|
|
6946
|
+
*/
|
|
6947
|
+
function genStyleString(className, styleArray) {
|
|
6948
|
+
var style = '';
|
|
6949
|
+
if (styleArray && styleArray.length > 0) {
|
|
6950
|
+
// It's wrong. Last item will not include `;`.
|
|
6951
|
+
// style = styleArray.join(';');
|
|
6952
|
+
style = styleArray.join(';') + ';';
|
|
6953
|
+
}
|
|
6954
|
+
return ".".concat(className, "{").concat(style, "}");
|
|
6955
|
+
}
|
|
6956
|
+
/**
|
|
6957
|
+
* Load the image from the given url.
|
|
6958
|
+
* The target image will be loaded in the background, and the Promise status will change after the image is loaded.
|
|
6959
|
+
* If the image fails to load, the Promise status will change to `reject`.
|
|
6960
|
+
* If the image is loaded successfully, the Promise status will change to `resolve`.
|
|
6961
|
+
* If the image is loaded successfully, the Promise will return the image object.
|
|
6962
|
+
* If the image fails to load, the Promise will return the error object.
|
|
6963
|
+
* The method will help Web to preload the image. And the image will be cached by the browser.
|
|
6964
|
+
* We can use it to implement the lazy loading of images.
|
|
6965
|
+
* The method will not add the image to the DOM.
|
|
6966
|
+
*
|
|
6967
|
+
* @example
|
|
6968
|
+
* ```js
|
|
6969
|
+
* loadImage('https://example.com/example.png').then((img) => {
|
|
6970
|
+
* console.log(img);
|
|
6971
|
+
* }).catch((err) => {
|
|
6972
|
+
* console.log(err);
|
|
6973
|
+
* });
|
|
6974
|
+
* ```
|
|
6975
|
+
*
|
|
6976
|
+
* @param {string} url - The image url.
|
|
6977
|
+
* @returns {Promise} Return the Promise object.
|
|
6978
|
+
* @category Load Resource
|
|
6979
|
+
*/
|
|
6980
|
+
function loadImage(url) {
|
|
6981
|
+
return new Promise(function (resolve, reject) {
|
|
6982
|
+
var img = new Image();
|
|
6983
|
+
img.onload = function () {
|
|
6984
|
+
resolve(img);
|
|
6985
|
+
};
|
|
6986
|
+
img.onerror = function (err) {
|
|
6987
|
+
reject(err);
|
|
6988
|
+
};
|
|
6989
|
+
img.src = url;
|
|
6990
|
+
});
|
|
6991
|
+
}
|
|
6900
6992
|
|
|
6901
6993
|
exports.addClass = addClass;
|
|
6902
6994
|
exports.addEvent = addEvent;
|
|
@@ -6919,6 +7011,7 @@ exports.floatToPercent = floatToPercent;
|
|
|
6919
7011
|
exports.formatDate = formatDate;
|
|
6920
7012
|
exports.genCustomConsole = genCustomConsole;
|
|
6921
7013
|
exports.genHashCode = genHashCode;
|
|
7014
|
+
exports.genStyleString = genStyleString;
|
|
6922
7015
|
exports.generateRndNum = generateRndNum;
|
|
6923
7016
|
exports.generateUniqueNum = generateUniqueNum;
|
|
6924
7017
|
exports.getBrowserInfo = getBrowserInfo;
|
|
@@ -6948,12 +7041,14 @@ exports.isValidEmail = isValidEmail;
|
|
|
6948
7041
|
exports.isValidPhoneNumber = isValidPhoneNumber;
|
|
6949
7042
|
exports.isValidUrl = isValidUrl;
|
|
6950
7043
|
exports.loadCSS = loadCSS;
|
|
7044
|
+
exports.loadImage = loadImage;
|
|
6951
7045
|
exports.loadScript = loadScript;
|
|
6952
7046
|
exports.mNow = mNow;
|
|
6953
7047
|
exports.mTrim = mTrim;
|
|
6954
7048
|
exports.newLine = newLine;
|
|
6955
7049
|
exports.removeClass = removeClass;
|
|
6956
7050
|
exports.removeEvent = removeEvent;
|
|
7051
|
+
exports.sanitizeInput = sanitizeInput;
|
|
6957
7052
|
exports.setCookie = setCookie;
|
|
6958
7053
|
exports.setImgWidHeiBySrc = setImgWidHeiBySrc;
|
|
6959
7054
|
exports.setLocalStorage = setLocalStorage;
|