ph-utils 0.2.21 → 0.2.23

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.js CHANGED
@@ -1,23 +1,27 @@
1
+ "use strict";
1
2
  /**
2
3
  * node 和 web 通用的工具类
3
4
  */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.throttle = exports.BaseError = exports.isBoolean = exports.isNumeric = exports.shieldMobile = exports.isBlank = void 0;
4
7
  /**
5
8
  * 验证字符串是否为空
6
9
  * @param str 待验证的字符串
7
10
  * @param ignoreWhitespace 是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
8
11
  */
9
- export function isBlank(str, ignoreWhitespace = true) {
12
+ function isBlank(str, ignoreWhitespace = true) {
10
13
  if (str == null) {
11
14
  return true;
12
15
  }
13
16
  return (ignoreWhitespace ? str.trim().length : str.length) === 0;
14
17
  }
18
+ exports.isBlank = isBlank;
15
19
  /**
16
20
  * 屏蔽手机号,中间部分用 * 展示
17
21
  * @param mobile 待屏蔽的手机号
18
22
  * @returns 屏蔽后的手机号,例如:123 **** 1234
19
23
  */
20
- export function shieldMobile(mobile) {
24
+ function shieldMobile(mobile) {
21
25
  let x1 = Math.floor(mobile.length / 2);
22
26
  let x2 = Math.ceil(x1 / 2);
23
27
  let shields = [' '];
@@ -27,6 +31,7 @@ export function shieldMobile(mobile) {
27
31
  shields.push(' ');
28
32
  return (mobile.substring(0, x2) + shields.join('') + mobile.substring(x2 + x1 - 1));
29
33
  }
34
+ exports.shieldMobile = shieldMobile;
30
35
  /**
31
36
  * 验证参数是否是数字
32
37
  * @param str 待验证的字符串
@@ -35,24 +40,31 @@ export function shieldMobile(mobile) {
35
40
  * @param numericParam.isFloat 是否是小数
36
41
  * @returns true 是数字, false 不是数字
37
42
  */
38
- export function isNumeric(str, numericParam) {
43
+ function isNumeric(str, numericParam) {
39
44
  numericParam = { isPositive: false, isFloat: true, ...(numericParam || {}) };
40
45
  let symbol = numericParam.isPositive ? '[+]?' : '[+-]?';
41
46
  let main = numericParam.isFloat ? '([0-9]*[.])?[0-9]+' : '[0-9]+';
42
47
  return new RegExp('^' + symbol + main + '$').test(str);
43
48
  }
49
+ exports.isNumeric = isNumeric;
44
50
  /**
45
51
  * 验证参数是否是Boolean 类型
46
52
  * @param str 待验证的字符串
47
53
  * @returns
48
54
  */
49
- export function isBoolean(str) {
55
+ function isBoolean(str) {
50
56
  return ['true', 'false'].indexOf(str) >= 0;
51
57
  }
58
+ exports.isBoolean = isBoolean;
52
59
  /**
53
60
  * 带有错误名称标记的错误类型
54
61
  */
55
- export class BaseError extends Error {
62
+ class BaseError extends Error {
63
+ /**
64
+ * 错误名称,类似于 Java 中的不同的 Exception[NullPointerException];
65
+ * 增加 name 字段,表明不同的错误,当需要根据不同的错误执行不同的处理的时候,会很有用
66
+ */
67
+ name;
56
68
  constructor() {
57
69
  if (arguments.length === 1) {
58
70
  super(arguments[0]);
@@ -64,13 +76,14 @@ export class BaseError extends Error {
64
76
  }
65
77
  }
66
78
  }
79
+ exports.BaseError = BaseError;
67
80
  /**
68
81
  * 创建一个节流函数,在 wait 秒内最多执行 func 一次的函数。
69
82
  * @param func 要节流的函数
70
83
  * @param wait 需要节流的毫秒
71
84
  * @returns
72
85
  */
73
- export function throttle(func, wait = 300) {
86
+ function throttle(func, wait = 300) {
74
87
  let t = -1;
75
88
  return function (...args) {
76
89
  const self = this;
@@ -80,3 +93,4 @@ export function throttle(func, wait = 300) {
80
93
  }, wait);
81
94
  };
82
95
  }
96
+ 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;
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.23",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https//gitee.com/towardly/ph.git",