ph-utils 0.2.21 → 0.2.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/lib/web.d.ts +3 -4
  2. package/lib/web.js +13 -24
  3. package/package.json +1 -1
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;
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 节点对象
@@ -38,32 +38,21 @@ export function query(search) {
38
38
  if (isBlank(search)) {
39
39
  search = location.search;
40
40
  }
41
+ const searchParams = new URLSearchParams(search);
41
42
  let query = {};
42
- /*
43
- 使用正则表达式解析,主要利用 反向字符集
44
- */
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);
51
- }
52
- else if (isNumeric(value)) {
53
- value = Number(value);
43
+ for (const [key, value] of searchParams) {
44
+ let oldValue = query[key];
45
+ let newValue = value;
46
+ if (oldValue != null) {
47
+ if (oldValue instanceof Array) {
48
+ oldValue.push(value);
49
+ newValue = oldValue;
54
50
  }
55
- if (oldValue != null) {
56
- if (oldValue instanceof Array) {
57
- oldValue.push(value);
58
- value = oldValue;
59
- }
60
- else {
61
- value = [value, oldValue];
62
- }
51
+ else {
52
+ newValue = [value, oldValue];
63
53
  }
64
- query[k] = value;
65
- return v;
66
- });
54
+ }
55
+ query[key] = newValue;
67
56
  }
68
57
  return query;
69
58
  }
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.21",
8
+ "version": "0.2.22",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https//gitee.com/towardly/ph.git",