ph-utils 0.2.20 → 0.2.22

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
@@ -3,15 +3,14 @@
3
3
  * @param form {object} Form 节点对象
4
4
  */
5
5
  export declare const formJson: <T>(form: HTMLFormElement) => T;
6
- interface QueryRes {
7
- [index: string]: number | boolean | string | string[];
8
- }
9
6
  /**
10
7
  * 获取 url query 参数 (get 请求的参数)
11
8
  * @param search 如果是 React 应用就需要传递 useLocation().search
12
9
  * @returns
13
10
  */
14
- export declare function query(search?: string): QueryRes;
11
+ export declare function query(search?: string): {
12
+ [index: string]: string;
13
+ };
15
14
  interface RandomOpts {
16
15
  /** 生成的随机数是整形还是小数形 */
17
16
  isInt?: boolean;
@@ -39,4 +38,18 @@ export declare function random(min: number, max: number, opts?: RandomOpts): num
39
38
  * @return string
40
39
  */
41
40
  export declare function formatMoney(number: number): string;
41
+ /**
42
+ * 函数节流 - 每隔单位时间,只执行一次
43
+ * @param cb 待节流的函数
44
+ * @param wait 间隔时间
45
+ * @returns
46
+ */
47
+ export declare function throttle<R extends any[], T>(fn: (...args: R) => T, wait?: number): (...args: R) => void;
48
+ /**
49
+ * 函数防抖 - 当重复触发某一个行为(事件时),只执行最后一次触发
50
+ * @param fn 防抖函数
51
+ * @param interval 间隔时间段
52
+ * @returns
53
+ */
54
+ export declare function debounce<R extends any[], T>(fn: (...args: R) => T, interval?: number): (...args: R) => void;
42
55
  export {};
package/lib/web.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * web(浏览器) 端工具类
3
3
  */
4
- import { isBlank, isBoolean, isNumeric } from './index_m';
4
+ import { isBlank } from './index_m';
5
5
  /**
6
6
  * 解析 Form 表单中的 input 元素的数据为 JSON 格式,key: input-name;value: input-value
7
7
  * @param form {object} Form 节点对象
@@ -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);
@@ -37,31 +38,22 @@ export function query(search) {
37
38
  if (isBlank(search)) {
38
39
  search = location.search;
39
40
  }
41
+ const searchParams = new URLSearchParams(search);
40
42
  let query = {};
41
- /*
42
- 使用正则表达式解析,主要利用 反向字符集
43
- */
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
- }
43
+ for (const [key, value] of searchParams) {
44
+ let oldValue = query[key];
45
+ let newValue = value;
53
46
  if (oldValue != null) {
54
47
  if (oldValue instanceof Array) {
55
48
  oldValue.push(value);
56
- value = oldValue;
49
+ newValue = oldValue;
57
50
  }
58
51
  else {
59
- value = [value, oldValue];
52
+ newValue = [value, oldValue];
60
53
  }
61
54
  }
62
- query[k] = value;
63
- return v;
64
- });
55
+ query[key] = newValue;
56
+ }
65
57
  return query;
66
58
  }
67
59
  export function random() {
@@ -108,5 +100,42 @@ export function formatMoney(number) {
108
100
  let usePrecision = numberStr.indexOf('.');
109
101
  let dotStr = usePrecision > 0 ? numberStr.slice(usePrecision + 1) : '00';
110
102
  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);
103
+ return (negative +
104
+ (mod ? base.slice(0, mod) + ',' : '') +
105
+ base.slice(mod).replace(/(\d{3})(?=\d)/g, '$1,') +
106
+ '.' +
107
+ dotStr);
108
+ }
109
+ /**
110
+ * 函数节流 - 每隔单位时间,只执行一次
111
+ * @param cb 待节流的函数
112
+ * @param wait 间隔时间
113
+ * @returns
114
+ */
115
+ export function throttle(fn, wait = 500) {
116
+ // 上一次的请求时间
117
+ let last = 0;
118
+ return (...args) => {
119
+ // 当前时间戳
120
+ const now = Date.now();
121
+ if (now - last > wait) {
122
+ fn(...args);
123
+ last = now;
124
+ }
125
+ };
126
+ }
127
+ /**
128
+ * 函数防抖 - 当重复触发某一个行为(事件时),只执行最后一次触发
129
+ * @param fn 防抖函数
130
+ * @param interval 间隔时间段
131
+ * @returns
132
+ */
133
+ export function debounce(fn, interval = 500) {
134
+ let _t = -1;
135
+ return (...args) => {
136
+ clearTimeout(_t);
137
+ _t = setTimeout(() => {
138
+ fn(...args);
139
+ }, interval);
140
+ };
112
141
  }
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.22",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https//gitee.com/towardly/ph.git",