mingdeng-universal-utils 0.1.2 → 0.1.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mingdeng-universal-utils",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "JavaScript通用utils",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -305,19 +305,24 @@ export const BrowserInfo = {
305
305
 
306
306
  /**
307
307
  * 函数防抖
308
- * @param {Function} func 函数
309
- * @param {Number} wait 等待时间
308
+ * @param {Function} callback 函数
309
+ * @param {Number} timeout 等待时间
310
+ * @param {Boolean} immediate 决定触发时机
310
311
  * @return {Function}
311
312
  */
312
- const debounce = (func, wait) => {
313
- let timeout;
313
+ const debounce = (callback, timeout, immediate) => {
314
+ let timer;
314
315
  return function () {
315
- let context = this;
316
- let args = arguments;
317
- if (timeout) clearTimeout(timeout);
318
- timeout = setTimeout(() => {
319
- func.apply(context, args)
320
- }, wait);
316
+ const context = this; // 持有执行上下文
317
+ const args = arguments; // 记录传参
318
+ const later = function () {
319
+ timer = null; // 贤者时间过了,重振旗鼓,重置为初始状态
320
+ if (!immediate) callback.apply(context, args); // 设置为尾部调用才延时触发
321
+ }
322
+ const callNow = immediate && !timer; // 如果确认允许首部调用,且首次调用,那么本次立即触发
323
+ clearTimeout(timer); // 杀掉上次的计时器,重新计时
324
+ timer = setTimeout(later, timeout); // 重启一个计时器,过了贤者时间之后才触发
325
+ callNow && callback.apply(context, args); // 设置为首部调用立即触发
321
326
  }
322
327
  }
323
328
 
@@ -3,7 +3,7 @@
3
3
  * @param {*} s
4
4
  */
5
5
  export function isEmail(s) {
6
- return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
6
+ return /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(s)
7
7
  }
8
8
 
9
9
  /**