ph-utils 0.2.20 → 0.2.21

Sign up to get free protection for your applications and to get access to all the features.
package/lib/index.js CHANGED
@@ -80,29 +80,3 @@ export function throttle(func, wait = 300) {
80
80
  }, wait);
81
81
  };
82
82
  }
83
-
84
- }
85
- else {
86
- super(arguments[1]);
87
- this.name = arguments[0];
88
- }
89
- }
90
- }
91
- exports.BaseError = BaseError;
92
- /**
93
- * 创建一个节流函数,在 wait 秒内最多执行 func 一次的函数。
94
- * @param func 要节流的函数
95
- * @param wait 需要节流的毫秒
96
- * @returns
97
- */
98
- function throttle(func, wait = 300) {
99
- let t = -1;
100
- return function (...args) {
101
- const self = this;
102
- clearTimeout(t);
103
- t = setTimeout(() => {
104
- func.apply(self, args);
105
- }, wait);
106
- };
107
- }
108
- exports.throttle = throttle;
package/lib/web.d.ts CHANGED
@@ -39,4 +39,18 @@ export declare function random(min: number, max: number, opts?: RandomOpts): num
39
39
  * @return string
40
40
  */
41
41
  export declare function formatMoney(number: number): string;
42
+ /**
43
+ * 函数节流 - 每隔单位时间,只执行一次
44
+ * @param cb 待节流的函数
45
+ * @param wait 间隔时间
46
+ * @returns
47
+ */
48
+ export declare function throttle<R extends any[], T>(fn: (...args: R) => T, wait?: number): (...args: R) => void;
49
+ /**
50
+ * 函数防抖 - 当重复触发某一个行为(事件时),只执行最后一次触发
51
+ * @param fn 防抖函数
52
+ * @param interval 间隔时间段
53
+ * @returns
54
+ */
55
+ export declare function debounce<R extends any[], T>(fn: (...args: R) => T, interval?: number): (...args: R) => void;
42
56
  export {};
package/lib/web.js CHANGED
@@ -12,7 +12,8 @@ export const formJson = function (form) {
12
12
  for (let i = 0, len = elems.length; i < len; i++) {
13
13
  let item = elems[i];
14
14
  if (!isBlank(item.name)) {
15
- if ((item.tagName === 'INPUT' || item.tagName === 'TEXTAREA') && !isBlank(item.value)) {
15
+ if ((item.tagName === 'INPUT' || item.tagName === 'TEXTAREA') &&
16
+ !isBlank(item.value)) {
16
17
  let dataType = item.getAttribute('data-type');
17
18
  if (dataType === 'number') {
18
19
  value[item.name] = Number(item.value);
@@ -41,27 +42,29 @@ export function query(search) {
41
42
  /*
42
43
  使用正则表达式解析,主要利用 反向字符集
43
44
  */
44
- search.replace(/([^?&=]+)=([^&]+)/g, (_, k, v) => {
45
- let oldValue = query[k];
46
- let value = decodeURIComponent(v);
47
- if (isBoolean(value)) {
48
- value = Boolean(value);
49
- }
50
- else if (isNumeric(value)) {
51
- value = Number(value);
52
- }
53
- if (oldValue != null) {
54
- if (oldValue instanceof Array) {
55
- oldValue.push(value);
56
- value = oldValue;
45
+ if (search != null) {
46
+ search.replace(/([^?&=]+)=([^&]+)/g, (_, k, v) => {
47
+ let oldValue = query[k];
48
+ let value = decodeURIComponent(v);
49
+ if (isBoolean(value)) {
50
+ value = Boolean(value);
57
51
  }
58
- else {
59
- value = [value, oldValue];
52
+ else if (isNumeric(value)) {
53
+ value = Number(value);
60
54
  }
61
- }
62
- query[k] = value;
63
- return v;
64
- });
55
+ if (oldValue != null) {
56
+ if (oldValue instanceof Array) {
57
+ oldValue.push(value);
58
+ value = oldValue;
59
+ }
60
+ else {
61
+ value = [value, oldValue];
62
+ }
63
+ }
64
+ query[k] = value;
65
+ return v;
66
+ });
67
+ }
65
68
  return query;
66
69
  }
67
70
  export function random() {
@@ -108,5 +111,42 @@ export function formatMoney(number) {
108
111
  let usePrecision = numberStr.indexOf('.');
109
112
  let dotStr = usePrecision > 0 ? numberStr.slice(usePrecision + 1) : '00';
110
113
  dotStr = dotStr.length > 2 ? dotStr.slice(0, 2) : dotStr;
111
- return (negative + (mod ? base.slice(0, mod) + ',' : '') + base.slice(mod).replace(/(\d{3})(?=\d)/g, '$1,') + '.' + dotStr);
114
+ return (negative +
115
+ (mod ? base.slice(0, mod) + ',' : '') +
116
+ base.slice(mod).replace(/(\d{3})(?=\d)/g, '$1,') +
117
+ '.' +
118
+ dotStr);
119
+ }
120
+ /**
121
+ * 函数节流 - 每隔单位时间,只执行一次
122
+ * @param cb 待节流的函数
123
+ * @param wait 间隔时间
124
+ * @returns
125
+ */
126
+ export function throttle(fn, wait = 500) {
127
+ // 上一次的请求时间
128
+ let last = 0;
129
+ return (...args) => {
130
+ // 当前时间戳
131
+ const now = Date.now();
132
+ if (now - last > wait) {
133
+ fn(...args);
134
+ last = now;
135
+ }
136
+ };
137
+ }
138
+ /**
139
+ * 函数防抖 - 当重复触发某一个行为(事件时),只执行最后一次触发
140
+ * @param fn 防抖函数
141
+ * @param interval 间隔时间段
142
+ * @returns
143
+ */
144
+ export function debounce(fn, interval = 500) {
145
+ let _t = -1;
146
+ return (...args) => {
147
+ clearTimeout(_t);
148
+ _t = setTimeout(() => {
149
+ fn(...args);
150
+ }, interval);
151
+ };
112
152
  }
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "module": "lib/index_m.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "browser": "lib/index_m.js",
8
- "version": "0.2.20",
8
+ "version": "0.2.21",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https//gitee.com/towardly/ph.git",