ph-utils 0.2.19 → 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
@@ -1,27 +1,23 @@
1
- "use strict";
2
1
  /**
3
2
  * node 和 web 通用的工具类
4
3
  */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.throttle = exports.BaseError = exports.isBoolean = exports.isNumeric = exports.shieldMobile = exports.isBlank = void 0;
7
4
  /**
8
5
  * 验证字符串是否为空
9
6
  * @param str 待验证的字符串
10
7
  * @param ignoreWhitespace 是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
11
8
  */
12
- function isBlank(str, ignoreWhitespace = true) {
9
+ export function isBlank(str, ignoreWhitespace = true) {
13
10
  if (str == null) {
14
11
  return true;
15
12
  }
16
13
  return (ignoreWhitespace ? str.trim().length : str.length) === 0;
17
14
  }
18
- exports.isBlank = isBlank;
19
15
  /**
20
16
  * 屏蔽手机号,中间部分用 * 展示
21
17
  * @param mobile 待屏蔽的手机号
22
18
  * @returns 屏蔽后的手机号,例如:123 **** 1234
23
19
  */
24
- function shieldMobile(mobile) {
20
+ export function shieldMobile(mobile) {
25
21
  let x1 = Math.floor(mobile.length / 2);
26
22
  let x2 = Math.ceil(x1 / 2);
27
23
  let shields = [' '];
@@ -31,7 +27,6 @@ function shieldMobile(mobile) {
31
27
  shields.push(' ');
32
28
  return (mobile.substring(0, x2) + shields.join('') + mobile.substring(x2 + x1 - 1));
33
29
  }
34
- exports.shieldMobile = shieldMobile;
35
30
  /**
36
31
  * 验证参数是否是数字
37
32
  * @param str 待验证的字符串
@@ -40,31 +35,24 @@ exports.shieldMobile = shieldMobile;
40
35
  * @param numericParam.isFloat 是否是小数
41
36
  * @returns true 是数字, false 不是数字
42
37
  */
43
- function isNumeric(str, numericParam) {
38
+ export function isNumeric(str, numericParam) {
44
39
  numericParam = { isPositive: false, isFloat: true, ...(numericParam || {}) };
45
40
  let symbol = numericParam.isPositive ? '[+]?' : '[+-]?';
46
41
  let main = numericParam.isFloat ? '([0-9]*[.])?[0-9]+' : '[0-9]+';
47
42
  return new RegExp('^' + symbol + main + '$').test(str);
48
43
  }
49
- exports.isNumeric = isNumeric;
50
44
  /**
51
45
  * 验证参数是否是Boolean 类型
52
46
  * @param str 待验证的字符串
53
47
  * @returns
54
48
  */
55
- function isBoolean(str) {
49
+ export function isBoolean(str) {
56
50
  return ['true', 'false'].indexOf(str) >= 0;
57
51
  }
58
- exports.isBoolean = isBoolean;
59
52
  /**
60
53
  * 带有错误名称标记的错误类型
61
54
  */
62
- class BaseError extends Error {
63
- /**
64
- * 错误名称,类似于 Java 中的不同的 Exception[NullPointerException];
65
- * 增加 name 字段,表明不同的错误,当需要根据不同的错误执行不同的处理的时候,会很有用
66
- */
67
- name;
55
+ export class BaseError extends Error {
68
56
  constructor() {
69
57
  if (arguments.length === 1) {
70
58
  super(arguments[0]);
@@ -76,14 +64,13 @@ class BaseError extends Error {
76
64
  }
77
65
  }
78
66
  }
79
- exports.BaseError = BaseError;
80
67
  /**
81
68
  * 创建一个节流函数,在 wait 秒内最多执行 func 一次的函数。
82
69
  * @param func 要节流的函数
83
70
  * @param wait 需要节流的毫秒
84
71
  * @returns
85
72
  */
86
- function throttle(func, wait = 300) {
73
+ export function throttle(func, wait = 300) {
87
74
  let t = -1;
88
75
  return function (...args) {
89
76
  const self = this;
@@ -93,4 +80,3 @@ function throttle(func, wait = 300) {
93
80
  }, wait);
94
81
  };
95
82
  }
96
- exports.throttle = throttle;
@@ -15,6 +15,7 @@ export interface SchemaType {
15
15
  required?: boolean;
16
16
  type?: string | ((v: any) => void);
17
17
  rules: RuleType[];
18
+ message?: string;
18
19
  }
19
20
  /**
20
21
  * 数据验证器,除了进行数据验证外,还可以同时进行数据转化
@@ -43,4 +44,4 @@ declare class Validator {
43
44
  private _validateRule;
44
45
  private _parseStringRule;
45
46
  }
46
- export = Validator;
47
+ export default Validator;
package/lib/validator.js CHANGED
@@ -3,6 +3,7 @@
3
3
  /**
4
4
  * 数据验证器
5
5
  */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
6
7
  // 默认的错误提示信息
7
8
  const defaultMsgs = {
8
9
  mobile: '请输入正确的手机号',
@@ -58,16 +59,19 @@ class Validator {
58
59
  let rules = [];
59
60
  let rule = schema.rules;
60
61
  if (typeof rule === 'string') {
61
- rules = rules.concat(this._parseStringRule(rule));
62
+ rules = rules.concat(this._parseStringRule(rule, schema.message));
62
63
  }
63
64
  else if (rule instanceof Array) {
64
65
  for (let ruleItem of rule) {
65
66
  if (typeof ruleItem === 'string') {
66
- rules.push(...this._parseStringRule(ruleItem));
67
+ rules.push(...this._parseStringRule(ruleItem, schema.message));
67
68
  }
68
69
  else if (ruleItem instanceof RegExp ||
69
70
  typeof ruleItem === 'function') {
70
- rules.push({ rule: ruleItem, message: defaultMsg });
71
+ rules.push({
72
+ rule: ruleItem,
73
+ message: schema.message || defaultMsg,
74
+ });
71
75
  }
72
76
  else {
73
77
  if (typeof ruleItem.rule === 'string') {
@@ -103,7 +107,8 @@ class Validator {
103
107
  let errMsg = '';
104
108
  let errKey = '';
105
109
  for (let key in this.rules) {
106
- if ({}.hasOwnProperty.call(this.rules, key)) {
110
+ // eslint-disable-next-line no-prototype-builtins
111
+ if (this.rules.hasOwnProperty(key)) {
107
112
  errMsg = this._validateRule(this.rules[key], data[key], data);
108
113
  if (errMsg !== '') {
109
114
  errKey = key;
@@ -142,33 +147,31 @@ class Validator {
142
147
  let errMsg = '';
143
148
  for (let rule of rules) {
144
149
  // 如果数据为空,则判断是否是必填
145
- if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
146
- if (rule.rule === 'required') {
150
+ if (rule.rule === 'required') {
151
+ if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
147
152
  errMsg = rule.message;
148
153
  }
149
154
  }
150
- else {
151
- if (typeof rule.rule === 'function') {
152
- if (!rule.rule(value)) {
153
- errMsg = rule.message;
154
- }
155
- }
156
- else if (rule.sameKey != null) {
157
- if (data != null) {
158
- if (!ruleFns.same(value, data[rule.sameKey])) {
159
- errMsg = rule.message;
160
- }
161
- }
155
+ if (typeof rule.rule === 'function') {
156
+ if (!rule.rule(value)) {
157
+ errMsg = rule.message;
162
158
  }
163
- else if (rule.rule === 'required') {
164
- if (!ruleFns.pattern(ruleRegexs.required, String(value))) {
159
+ }
160
+ else if (rule.sameKey != null) {
161
+ if (data != null) {
162
+ if (!ruleFns.same(value, data[rule.sameKey])) {
165
163
  errMsg = rule.message;
166
164
  }
167
165
  }
168
- else {
169
- if (!ruleFns.pattern(rule.rule, String(value))) {
170
- errMsg = rule.message;
171
- }
166
+ }
167
+ else if (rule.rule === 'required') {
168
+ if (!ruleFns.pattern(ruleRegexs.required, String(value))) {
169
+ errMsg = rule.message;
170
+ }
171
+ }
172
+ else {
173
+ if (!ruleFns.pattern(rule.rule, String(value))) {
174
+ errMsg = rule.message;
172
175
  }
173
176
  }
174
177
  if (errMsg !== '') {
@@ -181,29 +184,32 @@ class Validator {
181
184
  let rules = [];
182
185
  let trule = rule.split('|');
183
186
  for (let r of trule) {
184
- let message = defaultMsg;
185
- let rrule;
187
+ let message = ruleErrMsg;
188
+ let rrule = null;
186
189
  let sameKey;
187
190
  if (ruleRegexs.same.test(r)) {
188
191
  let m = r.match(ruleRegexs.same);
189
192
  if (m != null) {
190
193
  rrule = ruleRegexs.same;
191
- sameKey = r.match(ruleRegexs.same)[1];
192
- message = defaultMsgs['same'];
194
+ let m = r.match(ruleRegexs.same);
195
+ if (m != null) {
196
+ sameKey = m[1];
197
+ }
198
+ message = message || defaultMsgs['same'];
193
199
  }
194
200
  }
195
201
  else if (rule === 'required') {
196
202
  rrule = 'required';
197
- message = ruleErrMsg || defaultMsgs.required;
203
+ message = message || ruleErrMsg || defaultMsgs.required;
204
+ // eslint-disable-next-line no-prototype-builtins
198
205
  }
199
- else if (Object.prototype.hasOwnProperty.call(ruleRegexs, r)) {
206
+ else if (ruleRegexs.hasOwnProperty(r)) {
200
207
  rrule = ruleRegexs[r];
201
- message = defaultMsgs[r] || defaultMsg;
208
+ message = message || defaultMsgs[r];
202
209
  }
203
- message = ruleErrMsg || message;
204
210
  rules.push({ rule: rrule, message: message, sameKey });
205
211
  }
206
212
  return rules;
207
213
  }
208
214
  }
209
- module.exports = Validator;
215
+ exports.default = Validator;
@@ -15,6 +15,7 @@ export interface SchemaType {
15
15
  required?: boolean;
16
16
  type?: string | ((v: any) => void);
17
17
  rules: RuleType[];
18
+ message?: string;
18
19
  }
19
20
  /**
20
21
  * 数据验证器,除了进行数据验证外,还可以同时进行数据转化
@@ -54,16 +54,19 @@ class Validator {
54
54
  let rules = [];
55
55
  let rule = schema.rules;
56
56
  if (typeof rule === 'string') {
57
- rules = rules.concat(this._parseStringRule(rule));
57
+ rules = rules.concat(this._parseStringRule(rule, schema.message));
58
58
  }
59
59
  else if (rule instanceof Array) {
60
60
  for (let ruleItem of rule) {
61
61
  if (typeof ruleItem === 'string') {
62
- rules.push(...this._parseStringRule(ruleItem));
62
+ rules.push(...this._parseStringRule(ruleItem, schema.message));
63
63
  }
64
64
  else if (ruleItem instanceof RegExp ||
65
65
  typeof ruleItem === 'function') {
66
- rules.push({ rule: ruleItem, message: defaultMsg });
66
+ rules.push({
67
+ rule: ruleItem,
68
+ message: schema.message || defaultMsg,
69
+ });
67
70
  }
68
71
  else {
69
72
  if (typeof ruleItem.rule === 'string') {
@@ -99,7 +102,8 @@ class Validator {
99
102
  let errMsg = '';
100
103
  let errKey = '';
101
104
  for (let key in this.rules) {
102
- if ({}.hasOwnProperty.call(this.rules, key)) {
105
+ // eslint-disable-next-line no-prototype-builtins
106
+ if (this.rules.hasOwnProperty(key)) {
103
107
  errMsg = this._validateRule(this.rules[key], data[key], data);
104
108
  if (errMsg !== '') {
105
109
  errKey = key;
@@ -138,33 +142,31 @@ class Validator {
138
142
  let errMsg = '';
139
143
  for (let rule of rules) {
140
144
  // 如果数据为空,则判断是否是必填
141
- if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
142
- if (rule.rule === 'required') {
145
+ if (rule.rule === 'required') {
146
+ if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
143
147
  errMsg = rule.message;
144
148
  }
145
149
  }
146
- else {
147
- if (typeof rule.rule === 'function') {
148
- if (!rule.rule(value)) {
149
- errMsg = rule.message;
150
- }
151
- }
152
- else if (rule.sameKey != null) {
153
- if (data != null) {
154
- if (!ruleFns.same(value, data[rule.sameKey])) {
155
- errMsg = rule.message;
156
- }
157
- }
150
+ if (typeof rule.rule === 'function') {
151
+ if (!rule.rule(value)) {
152
+ errMsg = rule.message;
158
153
  }
159
- else if (rule.rule === 'required') {
160
- if (!ruleFns.pattern(ruleRegexs.required, String(value))) {
154
+ }
155
+ else if (rule.sameKey != null) {
156
+ if (data != null) {
157
+ if (!ruleFns.same(value, data[rule.sameKey])) {
161
158
  errMsg = rule.message;
162
159
  }
163
160
  }
164
- else {
165
- if (!ruleFns.pattern(rule.rule, String(value))) {
166
- errMsg = rule.message;
167
- }
161
+ }
162
+ else if (rule.rule === 'required') {
163
+ if (!ruleFns.pattern(ruleRegexs.required, String(value))) {
164
+ errMsg = rule.message;
165
+ }
166
+ }
167
+ else {
168
+ if (!ruleFns.pattern(rule.rule, String(value))) {
169
+ errMsg = rule.message;
168
170
  }
169
171
  }
170
172
  if (errMsg !== '') {
@@ -177,30 +179,32 @@ class Validator {
177
179
  let rules = [];
178
180
  let trule = rule.split('|');
179
181
  for (let r of trule) {
180
- let message = defaultMsg;
181
- let rrule;
182
+ let message = ruleErrMsg;
183
+ let rrule = null;
182
184
  let sameKey;
183
185
  if (ruleRegexs.same.test(r)) {
184
186
  let m = r.match(ruleRegexs.same);
185
187
  if (m != null) {
186
188
  rrule = ruleRegexs.same;
187
- sameKey = r.match(ruleRegexs.same)[1];
188
- message = defaultMsgs['same'];
189
+ let m = r.match(ruleRegexs.same);
190
+ if (m != null) {
191
+ sameKey = m[1];
192
+ }
193
+ message = message || defaultMsgs['same'];
189
194
  }
190
195
  }
191
196
  else if (rule === 'required') {
192
197
  rrule = 'required';
193
- message = ruleErrMsg || defaultMsgs.required;
198
+ message = message || ruleErrMsg || defaultMsgs.required;
199
+ // eslint-disable-next-line no-prototype-builtins
194
200
  }
195
- else if (Object.prototype.hasOwnProperty.call(ruleRegexs, r)) {
201
+ else if (ruleRegexs.hasOwnProperty(r)) {
196
202
  rrule = ruleRegexs[r];
197
- message = defaultMsgs[r] || defaultMsg;
203
+ message = message || defaultMsgs[r];
198
204
  }
199
- message = ruleErrMsg || message;
200
205
  rules.push({ rule: rrule, message: message, sameKey });
201
206
  }
202
207
  return rules;
203
208
  }
204
209
  }
205
- // @ts-ignore: Unreachable code error
206
210
  export default Validator;
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.19",
8
+ "version": "0.2.21",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https//gitee.com/towardly/ph.git",
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Tenny
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.