@jhqn/utils-core 0.0.1-beta.22 → 0.0.1-beta.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/dist/index.cjs CHANGED
@@ -119,16 +119,76 @@ const nonASCII = (text) => /[^\x20-\x7E]/.test(text);
119
119
  const isValidRes = (res) => res.code === 200 && isDef(res.data);
120
120
  const isValidArrRes = (res) => res.code === 200 && Array.isArray(res.data);
121
121
  const isValidPageRes = (res) => res.code === 200 && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
122
+ const isValidFileType = (file, type) => {
123
+ if (type === "*")
124
+ return true;
125
+ const fileType = file.type;
126
+ const fileName = file.name;
127
+ function validate(item) {
128
+ if (item.startsWith(".")) {
129
+ return fileName.endsWith(item);
130
+ }
131
+ if (item.endsWith("/*")) {
132
+ return fileType.startsWith(item.replace("/*", ""));
133
+ }
134
+ if (item.includes("/")) {
135
+ return fileType === item;
136
+ }
137
+ return false;
138
+ }
139
+ const typeArrStr = type.split(",").map((item) => item.trim());
140
+ if (typeArrStr.length > 1) {
141
+ return typeArrStr.some(validate);
142
+ }
143
+ return validate(type);
144
+ };
122
145
 
123
- const hidePhone = (str) => {
124
- if (str) {
125
- if (isPhone(str)) {
126
- return str.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
146
+ const hideMiddle = (str) => {
147
+ if (!isDef(str))
148
+ return "";
149
+ const len = str.length;
150
+ if (len === 1)
151
+ return "*";
152
+ if (len === 2)
153
+ return "**";
154
+ return str[0] + "*".repeat(len - 2) + str[len - 1];
155
+ };
156
+ const hideThird = (str) => {
157
+ if (!isDef(str))
158
+ return "";
159
+ const len = str.length;
160
+ const partLen = Math.round(len / 3);
161
+ if (len === 1)
162
+ return "*";
163
+ if (len === 2)
164
+ return "**";
165
+ return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
166
+ };
167
+ const hideHead = (str) => {
168
+ if (!isDef(str))
169
+ return "";
170
+ const len = str.length;
171
+ if (len === 1)
172
+ return "*";
173
+ return "*".repeat(len - 1) + str[len - 1];
174
+ };
175
+ const hideTail = (str) => {
176
+ if (!isDef(str))
177
+ return "";
178
+ const len = str.length;
179
+ if (len === 1)
180
+ return "*";
181
+ return str[0] + "*".repeat(len - 1);
182
+ };
183
+ const hidePhone = (phone) => {
184
+ if (phone) {
185
+ if (isPhone(phone)) {
186
+ return phone.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
127
187
  }
128
- if (isTel(str)) {
129
- return str.replace(/^(\d{3,4})-?\d{3,4}(\d{4})$/, "$1****$2");
188
+ if (isTel(phone)) {
189
+ return phone.replace(/^(\d{3,4})-?\d{3,4}(\d{4})$/, "$1****$2");
130
190
  }
131
- return str;
191
+ return phone;
132
192
  }
133
193
  return "";
134
194
  };
@@ -152,26 +212,25 @@ const hideName = (name) => {
152
212
  }
153
213
  return "";
154
214
  };
155
- const hideMiddle = (str) => {
156
- if (!isDef(str))
157
- return "";
158
- const len = str.length;
159
- if (len === 1)
160
- return "*";
161
- if (len === 2)
162
- return "**";
163
- return str[0] + "*".repeat(len - 2) + str[len - 1];
215
+ const hideSurname = (name) => {
216
+ return hideHead(name);
164
217
  };
165
- const hideThird = (str) => {
166
- if (!isDef(str))
167
- return "";
168
- const len = str.length;
169
- const partLen = Math.round(len / 3);
170
- if (len === 1)
171
- return "*";
172
- if (len === 2)
173
- return "**";
174
- return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
218
+ const hideFirstName = (name) => {
219
+ return hideTail(name);
220
+ };
221
+ const hideEmail = (email) => {
222
+ if (email) {
223
+ if (!email.includes("@"))
224
+ return "";
225
+ const arr = email.split("@");
226
+ const name = arr[0];
227
+ const domain = arr[1];
228
+ const len = name?.length;
229
+ if (!len)
230
+ return email;
231
+ return `${hideTail(name)}@${domain}`;
232
+ }
233
+ return "";
175
234
  };
176
235
 
177
236
  const inBrowser = typeof window !== "undefined";
@@ -294,9 +353,14 @@ exports.filterObj = filterObj;
294
353
  exports.fixiOSInputAutoZoomIn = fixiOSInputAutoZoomIn;
295
354
  exports.getHostEnv = getHostEnv;
296
355
  exports.hideCardNo = hideCardNo;
356
+ exports.hideEmail = hideEmail;
357
+ exports.hideFirstName = hideFirstName;
358
+ exports.hideHead = hideHead;
297
359
  exports.hideMiddle = hideMiddle;
298
360
  exports.hideName = hideName;
299
361
  exports.hidePhone = hidePhone;
362
+ exports.hideSurname = hideSurname;
363
+ exports.hideTail = hideTail;
300
364
  exports.hideThird = hideThird;
301
365
  exports.inBrowser = inBrowser;
302
366
  exports.isArrStr = isArrStr;
@@ -308,6 +372,7 @@ exports.isNumeric = isNumeric;
308
372
  exports.isPhone = isPhone;
309
373
  exports.isTel = isTel;
310
374
  exports.isValidArrRes = isValidArrRes;
375
+ exports.isValidFileType = isValidFileType;
311
376
  exports.isValidKey = isValidKey;
312
377
  exports.isValidPageRes = isValidPageRes;
313
378
  exports.isValidRes = isValidRes;
package/dist/index.d.ts CHANGED
@@ -234,31 +234,57 @@ declare type PageResData<T = any> = Service.PageResponseData<T>;
234
234
  */
235
235
  declare type PageRes<T = any> = Service.PageResponse<T>;
236
236
 
237
+ /**
238
+ * 隐藏中间部分
239
+ * @param {string} str 需要隐藏的字符串
240
+ */
241
+ declare const hideMiddle: (str: NullOrUndefinable<string>) => string;
242
+ /**
243
+ * 隐藏首尾仅保留中间3分之1
244
+ * @param {string} str 需要隐藏的字符串
245
+ */
246
+ declare const hideThird: (str: NullOrUndefinable<string>) => string;
247
+ /**
248
+ * 隐藏首部
249
+ * @param {string} str 需要隐藏的字符串
250
+ */
251
+ declare const hideHead: (str: NullOrUndefinable<string>) => string;
252
+ /**
253
+ * 隐藏尾部
254
+ * @param {string} str 需要隐藏的字符串
255
+ */
256
+ declare const hideTail: (str: NullOrUndefinable<string>) => string;
237
257
  /**
238
258
  * 隐藏手机号
239
- * @param str
259
+ * @param {string} phone 手机号
240
260
  */
241
- declare const hidePhone: (str: NullOrUndefinable<string>) => string;
261
+ declare const hidePhone: (phone: NullOrUndefinable<string>) => string;
242
262
  /**
243
263
  * 隐藏身份证号
244
- * @param cardNo
264
+ * @param {string} cardNo 身份证号
245
265
  */
246
266
  declare const hideCardNo: (cardNo: NullOrUndefinable<string>) => string;
247
267
  /**
248
- * 隐藏姓名
249
- * @param name
268
+ * 隐藏姓名第一个字
269
+ * @param {string} name 姓名
270
+ * @deprecated 请使用 hideSurname
250
271
  */
251
272
  declare const hideName: (name: NullOrUndefinable<string>) => string;
252
273
  /**
253
- * 隐藏中间部分
254
- * @param str
274
+ * 隐藏姓氏
275
+ * @param {string} name 姓名
255
276
  */
256
- declare const hideMiddle: (str: NullOrUndefinable<string>) => string;
277
+ declare const hideSurname: (name: NullOrUndefinable<string>) => string;
257
278
  /**
258
- * 隐藏首尾仅保留中间3分之1
259
- * @param str
279
+ * 隐藏名字
280
+ * @param {string} name 姓名
260
281
  */
261
- declare const hideThird: (str: NullOrUndefinable<string>) => string;
282
+ declare const hideFirstName: (name: NullOrUndefinable<string>) => string;
283
+ /**
284
+ * 隐藏邮箱
285
+ * @param {string} email 邮箱
286
+ */
287
+ declare const hideEmail: (email: NullOrUndefinable<string>) => string;
262
288
 
263
289
  /**
264
290
  * 判断是否是浏览器环境
@@ -576,5 +602,12 @@ declare const isValidArrRes: (res: Res<any[]>) => boolean;
576
602
  * @param res
577
603
  */
578
604
  declare const isValidPageRes: (res: PageRes) => boolean;
605
+ /**
606
+ * 校验文件类型
607
+ * 1. XXX/YYY 2. .xxx 3. XXX/* 4.*
608
+ * @param {File} file 需要校验的文件
609
+ * @param {string} type 文件类型, 与input.accept一致
610
+ */
611
+ declare const isValidFileType: (file: File, type: string) => boolean;
579
612
 
580
- export { NullOrUndefinable, Nullable, Numeric, PageRes, PageResData, Res, Undefinable, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getHostEnv, hideCardNo, hideMiddle, hideName, hidePhone, hideThird, inBrowser, isArrStr, isDef, isEmail, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidKey, isValidPageRes, isValidRes, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, replacer, reviver, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, userAgent, waitTime };
613
+ export { NullOrUndefinable, Nullable, Numeric, PageRes, PageResData, Res, Undefinable, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getHostEnv, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isDef, isEmail, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, replacer, reviver, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, userAgent, waitTime };
package/dist/index.mjs CHANGED
@@ -108,16 +108,76 @@ const nonASCII = (text) => /[^\x20-\x7E]/.test(text);
108
108
  const isValidRes = (res) => res.code === 200 && isDef(res.data);
109
109
  const isValidArrRes = (res) => res.code === 200 && Array.isArray(res.data);
110
110
  const isValidPageRes = (res) => res.code === 200 && isDef(res.data) && Array.isArray(res.data.list || res.data.records);
111
+ const isValidFileType = (file, type) => {
112
+ if (type === "*")
113
+ return true;
114
+ const fileType = file.type;
115
+ const fileName = file.name;
116
+ function validate(item) {
117
+ if (item.startsWith(".")) {
118
+ return fileName.endsWith(item);
119
+ }
120
+ if (item.endsWith("/*")) {
121
+ return fileType.startsWith(item.replace("/*", ""));
122
+ }
123
+ if (item.includes("/")) {
124
+ return fileType === item;
125
+ }
126
+ return false;
127
+ }
128
+ const typeArrStr = type.split(",").map((item) => item.trim());
129
+ if (typeArrStr.length > 1) {
130
+ return typeArrStr.some(validate);
131
+ }
132
+ return validate(type);
133
+ };
111
134
 
112
- const hidePhone = (str) => {
113
- if (str) {
114
- if (isPhone(str)) {
115
- return str.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
135
+ const hideMiddle = (str) => {
136
+ if (!isDef(str))
137
+ return "";
138
+ const len = str.length;
139
+ if (len === 1)
140
+ return "*";
141
+ if (len === 2)
142
+ return "**";
143
+ return str[0] + "*".repeat(len - 2) + str[len - 1];
144
+ };
145
+ const hideThird = (str) => {
146
+ if (!isDef(str))
147
+ return "";
148
+ const len = str.length;
149
+ const partLen = Math.round(len / 3);
150
+ if (len === 1)
151
+ return "*";
152
+ if (len === 2)
153
+ return "**";
154
+ return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
155
+ };
156
+ const hideHead = (str) => {
157
+ if (!isDef(str))
158
+ return "";
159
+ const len = str.length;
160
+ if (len === 1)
161
+ return "*";
162
+ return "*".repeat(len - 1) + str[len - 1];
163
+ };
164
+ const hideTail = (str) => {
165
+ if (!isDef(str))
166
+ return "";
167
+ const len = str.length;
168
+ if (len === 1)
169
+ return "*";
170
+ return str[0] + "*".repeat(len - 1);
171
+ };
172
+ const hidePhone = (phone) => {
173
+ if (phone) {
174
+ if (isPhone(phone)) {
175
+ return phone.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
116
176
  }
117
- if (isTel(str)) {
118
- return str.replace(/^(\d{3,4})-?\d{3,4}(\d{4})$/, "$1****$2");
177
+ if (isTel(phone)) {
178
+ return phone.replace(/^(\d{3,4})-?\d{3,4}(\d{4})$/, "$1****$2");
119
179
  }
120
- return str;
180
+ return phone;
121
181
  }
122
182
  return "";
123
183
  };
@@ -141,26 +201,25 @@ const hideName = (name) => {
141
201
  }
142
202
  return "";
143
203
  };
144
- const hideMiddle = (str) => {
145
- if (!isDef(str))
146
- return "";
147
- const len = str.length;
148
- if (len === 1)
149
- return "*";
150
- if (len === 2)
151
- return "**";
152
- return str[0] + "*".repeat(len - 2) + str[len - 1];
204
+ const hideSurname = (name) => {
205
+ return hideHead(name);
153
206
  };
154
- const hideThird = (str) => {
155
- if (!isDef(str))
156
- return "";
157
- const len = str.length;
158
- const partLen = Math.round(len / 3);
159
- if (len === 1)
160
- return "*";
161
- if (len === 2)
162
- return "**";
163
- return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
207
+ const hideFirstName = (name) => {
208
+ return hideTail(name);
209
+ };
210
+ const hideEmail = (email) => {
211
+ if (email) {
212
+ if (!email.includes("@"))
213
+ return "";
214
+ const arr = email.split("@");
215
+ const name = arr[0];
216
+ const domain = arr[1];
217
+ const len = name?.length;
218
+ if (!len)
219
+ return email;
220
+ return `${hideTail(name)}@${domain}`;
221
+ }
222
+ return "";
164
223
  };
165
224
 
166
225
  const inBrowser = typeof window !== "undefined";
@@ -271,4 +330,4 @@ const dateDuration = (start, end) => {
271
330
  return dd.format(format);
272
331
  };
273
332
 
274
- export { camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getHostEnv, hideCardNo, hideMiddle, hideName, hidePhone, hideThird, inBrowser, isArrStr, isDef, isEmail, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidKey, isValidPageRes, isValidRes, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, replacer, reviver, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, userAgent, waitTime };
333
+ export { camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getHostEnv, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isDef, isEmail, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, replacer, reviver, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, userAgent, waitTime };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jhqn/utils-core",
3
3
  "type": "module",
4
- "version": "0.0.1-beta.22",
4
+ "version": "0.0.1-beta.23",
5
5
  "author": "jade-gjz <jiangang.zhang@jaderd.com>",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/jaderd-jh/utils/tree/main/packages/core#readme",