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/lib/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * mazey v3.6.7
2
+ * mazey v3.7.0
3
3
  * (c) 2018-2023 Mazey Chu
4
4
  * Released under the MIT License.
5
5
  */
@@ -4358,34 +4358,32 @@ function getQueryParam(param) {
4358
4358
  return '';
4359
4359
  }
4360
4360
  /**
4361
- * Get the query param's value of the input URL.
4361
+ * Returns the value of the specified query parameter in the input URL.
4362
4362
  *
4363
+ * @example
4363
4364
  * ```
4364
- * getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3'); // 3
4365
- * getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4'); // 4
4365
+ * getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3'); // Returns '3'
4366
+ * getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4'); // Returns '4'
4366
4367
  * ```
4367
4368
  *
4368
- * @param {string} url URL string.
4369
- * @param {string} param Query param.
4370
- * @returns {string} value
4369
+ * @param {string} url The URL string.
4370
+ * @param {string} param The query parameter to retrieve the value for.
4371
+ * @returns {string|string[]} The value of the specified query parameter, or an empty string if the parameter is not found.
4371
4372
  * @category URL
4372
4373
  */
4373
4374
  function getUrlParam(url, param) {
4374
4375
  var result = {};
4375
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
4376
- url.replace(/\??(\w+)=([^&]*)&?/g, function (a, k, v) {
4376
+ url.replace(/\??(\w+)=([^&]*)&?/g, function (_, k, v) {
4377
4377
  if (result[k] !== undefined) {
4378
4378
  var t = result[k];
4379
+ // Cast `t` to `string[]` to ensure that `concat` receives an array
4379
4380
  result[k] = [].concat(t, v);
4380
4381
  } else {
4381
4382
  result[k] = v;
4382
4383
  }
4384
+ // Return an empty string to satisfy the signature of the replace method
4385
+ return '';
4383
4386
  });
4384
- // if (param === undefined) {
4385
- // return result;
4386
- // } else {
4387
- // return result[param] || '';
4388
- // }
4389
4387
  return result[param] || '';
4390
4388
  }
4391
4389
  /**
@@ -4667,8 +4665,8 @@ function generateRndNum(n) {
4667
4665
  return ret;
4668
4666
  }
4669
4667
  /**
4670
- * @method generateUniqueNum
4671
- * @description 根据时间生成唯一标志的数字 mGenerateUniqueNum() => 1538324722364123。
4668
+ * 根据时间生成唯一标志的数字 mGenerateUniqueNum() => 1538324722364123
4669
+ *
4672
4670
  * @param {number} n 随机数的长度
4673
4671
  * @category Util
4674
4672
  */
@@ -4682,8 +4680,8 @@ function generateUniqueNum(n) {
4682
4680
  return now + rnd;
4683
4681
  }
4684
4682
  /**
4685
- * @method floatToPercent
4686
- * @description 浮点数转为百分比 0.2 => 20%。
4683
+ * 浮点数转为百分比 0.2 => 20%
4684
+ *
4687
4685
  * @param {number} num 浮点数
4688
4686
  * @param {number} fixSize 保留几位浮点数
4689
4687
  * @category Util
@@ -4701,8 +4699,8 @@ function floatToPercent(num, fixSize) {
4701
4699
  return "".concat(ret, "%");
4702
4700
  }
4703
4701
  /**
4704
- * @method floatFixed
4705
- * @description 浮点数保留指定位。
4702
+ * 浮点数保留指定位
4703
+ *
4706
4704
  * @category Util
4707
4705
  */
4708
4706
  function floatFixed(num, size) {
@@ -4712,9 +4710,9 @@ function floatFixed(num, size) {
4712
4710
  return parseFloat(num).toFixed(size);
4713
4711
  }
4714
4712
  /**
4715
- * @method cancelBubble
4716
- * @description 阻止冒泡。
4717
- * @category Util
4713
+ * 阻止冒泡
4714
+ *
4715
+ * @category Event
4718
4716
  */
4719
4717
  function cancelBubble(e) {
4720
4718
  var ev = e || window.event;
@@ -4825,8 +4823,6 @@ function throttle(func, wait, options) {
4825
4823
  options = {};
4826
4824
  }
4827
4825
  options = Object.assign({}, options);
4828
- // timeout: setTimeout Handle
4829
- // previous: 上次时间戳
4830
4826
  var context = null;
4831
4827
  var args = null;
4832
4828
  var timeout = null;
@@ -4836,7 +4832,7 @@ function throttle(func, wait, options) {
4836
4832
  var later = function later() {
4837
4833
  previous = options.leading === false ? 0 : mNow();
4838
4834
  timeout = null;
4839
- result = func.apply(context, args);
4835
+ result = func.apply(this, args);
4840
4836
  if (!timeout) {
4841
4837
  context = args = null;
4842
4838
  }
@@ -4864,7 +4860,7 @@ function throttle(func, wait, options) {
4864
4860
  context = args = null;
4865
4861
  }
4866
4862
  } else if (!timeout && options.trailing !== false) {
4867
- timeout = setTimeout(later, remaining);
4863
+ timeout = setTimeout(later.bind(context), remaining);
4868
4864
  }
4869
4865
  return result;
4870
4866
  };
@@ -4887,7 +4883,7 @@ function debounce(func, wait, immediate) {
4887
4883
  var timeout = null;
4888
4884
  var timestamp = null;
4889
4885
  var args = null;
4890
- var result = [null][0];
4886
+ var result = null;
4891
4887
  var later = function later() {
4892
4888
  var last = mNow() - timestamp;
4893
4889
  if (last < wait && last >= 0) {
@@ -5006,37 +5002,26 @@ function getFriendlyInterval(start, end, options) {
5006
5002
  */
5007
5003
  function isNumber(num, options) {
5008
5004
  if (options === void 0) {
5009
- options = {
5010
- isNaNAsNumber: false,
5011
- isUnFiniteAsNumber: false
5012
- };
5005
+ options = {};
5013
5006
  }
5014
- var _a = Object.assign({
5015
- isNaNAsNumber: false,
5016
- isUnFiniteAsNumber: false
5017
- }, options),
5018
- isNaNAsNumber = _a.isNaNAsNumber,
5019
- isUnFiniteAsNumber = _a.isUnFiniteAsNumber;
5020
- var ret = true;
5021
- // 数字类型
5007
+ var _a = options.isNaNAsNumber,
5008
+ isNaNAsNumber = _a === void 0 ? false : _a,
5009
+ _b = options.isFiniteAsNumber,
5010
+ isFiniteAsNumber = _b === void 0 ? false : _b;
5022
5011
  if (typeof num !== 'number') {
5023
- ret = false;
5012
+ return false;
5024
5013
  }
5025
- // 无限值
5026
- if (isUnFiniteAsNumber === false && !isFinite(num)) {
5027
- // console.log('1333');
5028
- ret = false;
5014
+ if (!isFiniteAsNumber && !isFinite(num)) {
5015
+ return false;
5029
5016
  }
5030
- // NaN
5031
- if (isNaNAsNumber === false && isNaN(num)) {
5032
- // console.log('2333');
5033
- ret = false;
5017
+ if (!isNaNAsNumber && isNaN(num)) {
5018
+ return false;
5034
5019
  }
5035
- return ret;
5020
+ return true;
5036
5021
  }
5037
5022
  /**
5038
- * @method doFn
5039
- * @description 执行有效函数
5023
+ * 执行有效函数
5024
+ *
5040
5025
  * @param {function} fn 等待被执行的未知是否有效的函数
5041
5026
  * @category Util
5042
5027
  */
@@ -5074,7 +5059,7 @@ function doFn(fn) {
5074
5059
  * ```
5075
5060
  *
5076
5061
  * @param {string} key 键
5077
- * @returns {any} 返回值
5062
+ * @returns {void} 返回值
5078
5063
  * @category Cache Data
5079
5064
  */
5080
5065
  function setSessionStorage(key, value) {
@@ -5144,7 +5129,7 @@ function getSessionStorage(key) {
5144
5129
  * ```
5145
5130
  *
5146
5131
  * @param {string} key 键
5147
- * @returns {any} 返回值
5132
+ * @returns {void} 返回值
5148
5133
  * @category Cache Data
5149
5134
  */
5150
5135
  function setLocalStorage(key, value) {
@@ -5423,8 +5408,8 @@ function loadScript(url, options) {
5423
5408
  });
5424
5409
  }
5425
5410
  /**
5426
- * @method mNow
5427
- * @description 获取时间戳
5411
+ * 获取时间戳
5412
+ *
5428
5413
  * @category Util
5429
5414
  */
5430
5415
  function mNow() {
@@ -5450,11 +5435,11 @@ function mNow() {
5450
5435
  */
5451
5436
  function setCookie(name, value, days, domain) {
5452
5437
  var domainParts, expires;
5453
- var date;
5438
+ // let date: any;
5454
5439
  if (days) {
5455
- date = new Date();
5440
+ var date = new Date();
5456
5441
  date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
5457
- expires = '; expires=' + date.toGMTString();
5442
+ expires = '; expires=' + date.toUTCString();
5458
5443
  } else {
5459
5444
  expires = '';
5460
5445
  }
@@ -6099,14 +6084,50 @@ function getBrowserInfo() {
6099
6084
  }
6100
6085
  }
6101
6086
  /**
6102
- * @method clearHtml
6103
- * @description 去除HTML标签
6087
+ * 去除 HTML 标签
6088
+ *
6104
6089
  * @param {string} str 带html标签的字符串
6105
6090
  * @returns {string} 字符串
6106
6091
  * @category Util
6107
6092
  */
6108
- function clearHtml(str) {
6109
- return str.replace(/<\/?.+?>/g, '').replace(/[\r\n]/g, '');
6093
+ function clearHtml(str, options) {
6094
+ if (options === void 0) {
6095
+ options = {};
6096
+ }
6097
+ var _a = options.removeNewLine,
6098
+ removeNewLine = _a === void 0 ? false : _a;
6099
+ var ret = '';
6100
+ if (str) {
6101
+ ret = str.replace(/<\/?.+?>/g, '');
6102
+ if (removeNewLine) {
6103
+ ret = ret.replace(/[\r\n]/g, '');
6104
+ }
6105
+ }
6106
+ return ret;
6107
+ // return str.replace(/<\/?.+?>/g, '').replace(/[\r\n]/g, '');
6108
+ }
6109
+ /**
6110
+ * Sanitizes user input to prevent XSS attacks
6111
+ *
6112
+ * @param input - The input string to sanitize
6113
+ * @returns The sanitized input string
6114
+ */
6115
+ function sanitizeInput(input) {
6116
+ var regex = /[&<>"'/]/g;
6117
+ var replacements = {
6118
+ '&': '&amp;',
6119
+ '<': '&lt;',
6120
+ '>': '&gt;',
6121
+ '"': '&quot;',
6122
+ '\'': '&#x27;',
6123
+ '/': '&#x2F;'
6124
+ };
6125
+ if (typeof input !== 'string') {
6126
+ console.error('Input must be a string');
6127
+ }
6128
+ return input.replace(regex, function (match) {
6129
+ return replacements[match];
6130
+ });
6110
6131
  }
6111
6132
  /**
6112
6133
  * 截取字符串,中文算 2 个字节
@@ -6509,15 +6530,12 @@ function isNonEmptyArray(arr) {
6509
6530
  function isValidData(data, attributes, validValue) {
6510
6531
  var ret = false;
6511
6532
  var foundRet = attributes.reduce(function (foundValue, curr) {
6512
- if (foundValue[curr]) {
6513
- foundValue = foundValue[curr];
6533
+ if (foundValue && Object.prototype.hasOwnProperty.call(foundValue, curr)) {
6534
+ return foundValue[curr];
6514
6535
  } else {
6515
6536
  return Object.create(null);
6516
6537
  }
6517
- // console.log('foundValue', foundValue);
6518
- return foundValue;
6519
6538
  }, data);
6520
- // console.log('foundRet', foundRet);
6521
6539
  if (foundRet === validValue) {
6522
6540
  ret = true;
6523
6541
  }
@@ -6839,7 +6857,6 @@ function getUrlFileType(url) {
6839
6857
  * The `src` attribute should contain `width` and/or `height` values in the format "width=100" or "height=100".
6840
6858
  * If jQuery is available, this function uses jQuery to select the images. Otherwise, it uses pure JavaScript.
6841
6859
  *
6842
- *
6843
6860
  * @example
6844
6861
  * ```
6845
6862
  * // Example images with `src` attributes containing `width` and/or `height` values
@@ -6859,18 +6876,21 @@ function setImgWidHeiBySrc() {
6859
6876
  var $ = window.jQuery || window.$;
6860
6877
  if ($) {
6861
6878
  // Use jQuery to select all images on the page
6862
- $('img').each(function () {
6879
+ var images = $('img');
6880
+ if (!(images && images.length)) return false;
6881
+ images.each(function () {
6863
6882
  var $this = $(this);
6864
6883
  if (!$this) return;
6865
6884
  // Get the `src` attribute of the image
6866
6885
  var src = $this.attr('src');
6867
- if (!src) return;
6886
+ var canMatch = src && typeof src === 'string' && src.length;
6887
+ if (!canMatch) return;
6868
6888
  // Use regular expressions to extract the `width` and `height` values from the `src` attribute
6869
6889
  var width = src.match(/width=([0-9]+[a-z%]*)/);
6870
6890
  var height = src.match(/height=([0-9]+[a-z%]*)/);
6871
6891
  // Set the width and height of the image using jQuery's `width()` and `height()` methods
6872
- if (width && width[1]) $this.width(width[1]);
6873
- if (height && height[1]) $this.height(height[1]);
6892
+ if (width && isNonEmptyArray(width) && width[1]) $this.width(width[1]);
6893
+ if (height && isNonEmptyArray(height) && height[1]) $this.height(height[1]);
6874
6894
  });
6875
6895
  return true;
6876
6896
  } else {
@@ -6879,19 +6899,82 @@ function setImgWidHeiBySrc() {
6879
6899
  if (images.length > 0) {
6880
6900
  // Loop through each image and set its width and height based on the `src` attribute
6881
6901
  Array.from(images).forEach(function (img) {
6902
+ var $this = img;
6903
+ if (!$this) return;
6882
6904
  // Get the `src` attribute of the image
6883
- var src = img.getAttribute('src');
6905
+ var src = $this.getAttribute('src');
6906
+ var canMatch = src && typeof src === 'string' && src.length;
6907
+ if (!canMatch) return;
6884
6908
  // Use regular expressions to extract the `width` and `height` values from the `src` attribute
6885
- var width = src ? src.match(/width=([0-9]+[a-z%]*)/) : null;
6886
- var height = src ? src.match(/height=([0-9]+[a-z%]*)/) : null;
6909
+ var width = src.match(/width=([0-9]+[a-z%]*)/);
6910
+ var height = src.match(/height=([0-9]+[a-z%]*)/);
6887
6911
  // Set the width and height of the image using the `style.width` and `style.height` properties
6888
- if (width && width[1]) img.style.width = width[1];
6889
- if (height && height[1]) img.style.height = height[1];
6912
+ if (width && isNonEmptyArray(width) && width[1]) $this.style.width = width[1];
6913
+ if (height && isNonEmptyArray(height) && height[1]) $this.style.height = height[1];
6890
6914
  });
6891
6915
  return true;
6892
6916
  }
6893
6917
  }
6894
6918
  return false;
6895
6919
  }
6920
+ /**
6921
+ * Generate the inline style string from the given parameters, First parameter is the ClassNames, Second parameter is the style array.
6922
+ *
6923
+ * @example
6924
+ * ```js
6925
+ * console.log(genStyleString('a', [ 'color:red' ])); // '.a{color:red;}'
6926
+ * console.log(genStyleString('b', [ 'color:red', 'font-size:12px' ])); // '.b{color:red;font-size:12px;}'
6927
+ * ```
6928
+ *
6929
+ * @param {string} className
6930
+ * @param {Array} styleArray
6931
+ * @returns {string} Return the inline style string.
6932
+ * @category DOM
6933
+ */
6934
+ function genStyleString(className, styleArray) {
6935
+ var style = '';
6936
+ if (styleArray && styleArray.length > 0) {
6937
+ // It's wrong. Last item will not include `;`.
6938
+ // style = styleArray.join(';');
6939
+ style = styleArray.join(';') + ';';
6940
+ }
6941
+ return ".".concat(className, "{").concat(style, "}");
6942
+ }
6943
+ /**
6944
+ * Load the image from the given url.
6945
+ * The target image will be loaded in the background, and the Promise status will change after the image is loaded.
6946
+ * If the image fails to load, the Promise status will change to `reject`.
6947
+ * If the image is loaded successfully, the Promise status will change to `resolve`.
6948
+ * If the image is loaded successfully, the Promise will return the image object.
6949
+ * If the image fails to load, the Promise will return the error object.
6950
+ * The method will help Web to preload the image. And the image will be cached by the browser.
6951
+ * We can use it to implement the lazy loading of images.
6952
+ * The method will not add the image to the DOM.
6953
+ *
6954
+ * @example
6955
+ * ```js
6956
+ * loadImage('https://example.com/example.png').then((img) => {
6957
+ * console.log(img);
6958
+ * }).catch((err) => {
6959
+ * console.log(err);
6960
+ * });
6961
+ * ```
6962
+ *
6963
+ * @param {string} url - The image url.
6964
+ * @returns {Promise} Return the Promise object.
6965
+ * @category Load Resource
6966
+ */
6967
+ function loadImage(url) {
6968
+ return new Promise(function (resolve, reject) {
6969
+ var img = new Image();
6970
+ img.onload = function () {
6971
+ resolve(img);
6972
+ };
6973
+ img.onerror = function (err) {
6974
+ reject(err);
6975
+ };
6976
+ img.src = url;
6977
+ });
6978
+ }
6896
6979
 
6897
- export { addClass, addEvent, addStyle, calLongestCommonSubsequence, calLongestCommonSubstring, camelCase2Underscore, camelCaseToKebabCase, cancelBubble, clearHtml, convert10To26, convertCamelToKebab, convertCamelToUnder, cutCHSString, debounce, deepCopyObject, doFn, floatFixed, floatToPercent, formatDate, genCustomConsole, genHashCode, generateRndNum, generateUniqueNum, getBrowserInfo, getCookie, getDefineListeners, getDomain, getFileSize, getFriendlyInterval, getHashQueryParam, getLocalStorage, getPerformance, getQueryParam, getSessionStorage, getUrlFileType, getUrlParam, hasClass, inRate, invokeEvent, isJSONString, isJsonString, isNonEmptyArray, isNumber, isSafePWAEnv, isSupportWebp, isValidData, isValidEmail, isValidPhoneNumber, isValidUrl, loadCSS, loadScript, mNow, mTrim, newLine, removeClass, removeEvent, setCookie, setImgWidHeiBySrc, setLocalStorage, setSessionStorage, throttle, truncateZHString, updateQueryParam, windowLoaded, zAxiosIsValidRes };
6980
+ export { addClass, addEvent, addStyle, calLongestCommonSubsequence, calLongestCommonSubstring, camelCase2Underscore, camelCaseToKebabCase, cancelBubble, clearHtml, convert10To26, convertCamelToKebab, convertCamelToUnder, cutCHSString, debounce, deepCopyObject, doFn, floatFixed, floatToPercent, formatDate, genCustomConsole, genHashCode, genStyleString, generateRndNum, generateUniqueNum, getBrowserInfo, getCookie, getDefineListeners, getDomain, getFileSize, getFriendlyInterval, getHashQueryParam, getLocalStorage, getPerformance, getQueryParam, getSessionStorage, getUrlFileType, getUrlParam, hasClass, inRate, invokeEvent, isJSONString, isJsonString, isNonEmptyArray, isNumber, isSafePWAEnv, isSupportWebp, isValidData, isValidEmail, isValidPhoneNumber, isValidUrl, loadCSS, loadImage, loadScript, mNow, mTrim, newLine, removeClass, removeEvent, sanitizeInput, setCookie, setImgWidHeiBySrc, setLocalStorage, setSessionStorage, throttle, truncateZHString, updateQueryParam, windowLoaded, zAxiosIsValidRes };