@jhqn/utils-core 0.0.1-beta.24 → 0.0.1-beta.25

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
@@ -111,12 +111,12 @@ const isEmail = (val) => {
111
111
  const reg = /^[a-zA-Z\d](?:\w|-)+@[a-zA-Z\d]+\.[a-zA-Z]{2,5}$/;
112
112
  return reg.test(val);
113
113
  };
114
- const isIdCard = (val) => {
114
+ const isIdCard = (val, compatible = true) => {
115
115
  if (!isDef(val))
116
116
  return false;
117
117
  const card15 = /^[1-9]\d{7}(?:0[1-9]|10|11|12)(?:[0-2][1-9]|10|20|30|31)\d{2}[\dXx]$/;
118
118
  const card18 = /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|1[0-2])(?:[0-2][1-9]|10|20|30|31)\d{3}[\dXx]$/;
119
- return card15.test(val) || card18.test(val);
119
+ return card18.test(val) || (compatible ? card15.test(val) : false);
120
120
  };
121
121
  const nonASCII = (text) => /[^\x20-\x7E]/.test(text);
122
122
  const isValidResCode = (res, code = 200) => res.code === code;
@@ -168,21 +168,23 @@ const hideThird = (str) => {
168
168
  return "**";
169
169
  return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
170
170
  };
171
- const hideHead = (str) => {
171
+ const hideHead = (str, count) => {
172
172
  if (!isDef(str))
173
173
  return "";
174
174
  const len = str.length;
175
+ const cnt = count !== void 0 ? Math.max(Math.min(len, count), 0) : len - 1;
175
176
  if (len === 1)
176
177
  return "*";
177
- return "*".repeat(len - 1) + str[len - 1];
178
+ return "*".repeat(cnt) + str.substring(cnt);
178
179
  };
179
- const hideTail = (str) => {
180
+ const hideTail = (str, count) => {
180
181
  if (!isDef(str))
181
182
  return "";
182
183
  const len = str.length;
184
+ const cnt = count !== void 0 ? Math.max(Math.min(len, count), 0) : len - 1;
183
185
  if (len === 1)
184
186
  return "*";
185
- return str[0] + "*".repeat(len - 1);
187
+ return str.substring(0, len - cnt) + "*".repeat(cnt);
186
188
  };
187
189
  const hidePhone = (phone) => {
188
190
  if (phone) {
@@ -196,9 +198,12 @@ const hidePhone = (phone) => {
196
198
  }
197
199
  return "";
198
200
  };
199
- const hideCardNo = (cardNo) => {
201
+ const hideCardNo = (cardNo, strong = true) => {
200
202
  if (cardNo) {
201
- return cardNo.replace(/^(.{4})\d+(.{2})$/, "$1*******$2");
203
+ if (isIdCard(cardNo, false)) {
204
+ return strong ? hideMiddle(cardNo) : cardNo.replace(/^(.)\d{3}(.{8})\d{5}(.)$/, "$1***$2*****$3");
205
+ }
206
+ return cardNo;
202
207
  }
203
208
  return "";
204
209
  };
@@ -217,7 +222,7 @@ const hideName = (name) => {
217
222
  return "";
218
223
  };
219
224
  const hideSurname = (name) => {
220
- return hideHead(name);
225
+ return hideHead(name, 1);
221
226
  };
222
227
  const hideFirstName = (name) => {
223
228
  return hideTail(name);
@@ -225,14 +230,29 @@ const hideFirstName = (name) => {
225
230
  const hideEmail = (email) => {
226
231
  if (email) {
227
232
  if (!email.includes("@"))
228
- return "";
233
+ return email;
229
234
  const arr = email.split("@");
230
235
  const name = arr[0];
231
236
  const domain = arr[1];
232
- const len = name?.length;
233
- if (!len)
237
+ if (!name || !domain)
234
238
  return email;
235
- return `${hideTail(name)}@${domain}`;
239
+ const len = name.length;
240
+ let newName = "";
241
+ if (len <= 3) {
242
+ newName = `${name}***`;
243
+ } else {
244
+ newName = `${name.substring(0, len - 3)}***`;
245
+ }
246
+ return `${newName}@${domain}`;
247
+ }
248
+ return "";
249
+ };
250
+ const hideBankCard = (bankCard) => {
251
+ if (bankCard) {
252
+ const len = bankCard.length;
253
+ if (len <= 10)
254
+ return bankCard;
255
+ return bankCard.substring(0, 6) + "*".repeat(len - 10) + bankCard.substring(len - 4, len);
236
256
  }
237
257
  return "";
238
258
  };
@@ -432,6 +452,7 @@ exports.filterObj = filterObj;
432
452
  exports.fixiOSInputAutoZoomIn = fixiOSInputAutoZoomIn;
433
453
  exports.getBaseAttachUrl = getBaseAttachUrl;
434
454
  exports.getHostEnv = getHostEnv;
455
+ exports.hideBankCard = hideBankCard;
435
456
  exports.hideCardNo = hideCardNo;
436
457
  exports.hideEmail = hideEmail;
437
458
  exports.hideFirstName = hideFirstName;
package/dist/index.d.cts CHANGED
@@ -221,6 +221,10 @@ declare type Undefinable<T> = T | undefined;
221
221
  * 可 Null 或 Undefined
222
222
  */
223
223
  declare type NullOrUndefinable<T> = Nullable<Undefinable<T>>;
224
+ /**
225
+ * alias for NullOrUndefinable
226
+ */
227
+ declare type UnDef<T> = NullOrUndefinable<T>;
224
228
  /**
225
229
  * 请求结果
226
230
  */
@@ -242,53 +246,61 @@ declare type MaybeArray<T = any> = T | Array<T>;
242
246
  * 隐藏中间部分
243
247
  * @param {string} str 需要隐藏的字符串
244
248
  */
245
- declare const hideMiddle: (str: NullOrUndefinable<string>) => string;
249
+ declare const hideMiddle: (str: UnDef<string>) => string;
246
250
  /**
247
251
  * 隐藏首尾仅保留中间3分之1
248
252
  * @param {string} str 需要隐藏的字符串
249
253
  */
250
- declare const hideThird: (str: NullOrUndefinable<string>) => string;
254
+ declare const hideThird: (str: UnDef<string>) => string;
251
255
  /**
252
256
  * 隐藏首部
253
257
  * @param {string} str 需要隐藏的字符串
258
+ * @param {number} count 隐藏的字符数
254
259
  */
255
- declare const hideHead: (str: NullOrUndefinable<string>) => string;
260
+ declare const hideHead: (str: UnDef<string>, count?: number) => string;
256
261
  /**
257
262
  * 隐藏尾部
258
263
  * @param {string} str 需要隐藏的字符串
264
+ * @param {number} count 隐藏的字符数
259
265
  */
260
- declare const hideTail: (str: NullOrUndefinable<string>) => string;
266
+ declare const hideTail: (str: UnDef<string>, count?: number) => string;
261
267
  /**
262
268
  * 隐藏手机号
263
269
  * @param {string} phone 手机号
264
270
  */
265
- declare const hidePhone: (phone: NullOrUndefinable<string>) => string;
271
+ declare const hidePhone: (phone: UnDef<string>) => string;
266
272
  /**
267
273
  * 隐藏身份证号
268
274
  * @param {string} cardNo 身份证号
275
+ * @param {boolean} strong 是否强化隐藏
269
276
  */
270
- declare const hideCardNo: (cardNo: NullOrUndefinable<string>) => string;
277
+ declare const hideCardNo: (cardNo: UnDef<string>, strong?: boolean) => string;
271
278
  /**
272
279
  * 隐藏姓名第一个字
273
280
  * @param {string} name 姓名
274
281
  * @deprecated 请使用 hideSurname
275
282
  */
276
- declare const hideName: (name: NullOrUndefinable<string>) => string;
283
+ declare const hideName: (name: UnDef<string>) => string;
277
284
  /**
278
285
  * 隐藏姓氏
279
286
  * @param {string} name 姓名
280
287
  */
281
- declare const hideSurname: (name: NullOrUndefinable<string>) => string;
288
+ declare const hideSurname: (name: UnDef<string>) => string;
282
289
  /**
283
290
  * 隐藏名字
284
291
  * @param {string} name 姓名
285
292
  */
286
- declare const hideFirstName: (name: NullOrUndefinable<string>) => string;
293
+ declare const hideFirstName: (name: UnDef<string>) => string;
287
294
  /**
288
295
  * 隐藏邮箱
289
296
  * @param {string} email 邮箱
290
297
  */
291
- declare const hideEmail: (email: NullOrUndefinable<string>) => string;
298
+ declare const hideEmail: (email: UnDef<string>) => string;
299
+ /**
300
+ * 隐藏银行卡号
301
+ * @param {string} bankCard 银行卡号
302
+ */
303
+ declare const hideBankCard: (bankCard: UnDef<string>) => string;
292
304
 
293
305
  /**
294
306
  * 判断是否是浏览器环境
@@ -549,7 +561,7 @@ declare const toThousands: (num: number) => string | undefined;
549
561
  * @param currency
550
562
  * @param fraction
551
563
  */
552
- declare const currencyFmt: (currency: NullOrUndefinable<number | bigint>, fraction?: number) => string;
564
+ declare const currencyFmt: (currency: UnDef<number | bigint>, fraction?: number) => string;
553
565
 
554
566
  type VantUploadFileStatus = '' | 'uploading' | 'done' | 'failed'
555
567
  type AntdUploadFileStatus = 'removed' | 'uploading' | 'done' | 'error'
@@ -607,14 +619,17 @@ declare const recoverFile: (resource: Resource) => {
607
619
  deletable?: boolean | undefined;
608
620
  reupload?: boolean | undefined;
609
621
  fileName?: string | undefined;
610
- thumbUrl?: string | undefined;
622
+ thumbUrl?: string | undefined; /**
623
+ * 统一文件字段内容
624
+ * @param resource
625
+ */
611
626
  percentage?: number | undefined;
612
627
  };
613
628
  /**
614
629
  * 附件格式
615
630
  * @param data
616
631
  */
617
- declare const attachFmt: (data: NullOrUndefinable<MaybeArray<Resource>> | string) => {
632
+ declare const attachFmt: (data: UnDef<MaybeArray<Resource>> | string) => {
618
633
  url: string | undefined;
619
634
  content: string | undefined;
620
635
  uri: string;
@@ -631,7 +646,10 @@ declare const attachFmt: (data: NullOrUndefinable<MaybeArray<Resource>> | string
631
646
  deletable?: boolean | undefined;
632
647
  reupload?: boolean | undefined;
633
648
  fileName?: string | undefined;
634
- thumbUrl?: string | undefined;
649
+ thumbUrl?: string | undefined; /**
650
+ * 统一文件字段内容
651
+ * @param resource
652
+ */
635
653
  percentage?: number | undefined;
636
654
  }[];
637
655
 
@@ -654,7 +672,7 @@ declare const checkImg: (ext?: string) => boolean;
654
672
  * 是否是空值/空数组/空对象 null undefined '' [] {} Set Map
655
673
  * @param val
656
674
  */
657
- declare const isEmpty: (val: NullOrUndefinable<Numeric | boolean | Record<any, any> | Array<any> | Set<any> | Map<any, any>>) => boolean;
675
+ declare const isEmpty: (val: UnDef<Numeric | boolean | Record<any, any> | Array<any> | Set<any> | Map<any, any>>) => boolean;
658
676
  /**
659
677
  * 判断是否合法JSON字符串
660
678
  * @param str
@@ -682,22 +700,23 @@ declare const isValidKey: (key: string | number | symbol, object: object) => key
682
700
  * 是否是中国手机号
683
701
  * @param val
684
702
  */
685
- declare const isPhone: (val: NullOrUndefinable<Numeric>) => boolean;
703
+ declare const isPhone: (val: UnDef<Numeric>) => boolean;
686
704
  /**
687
705
  * 是否是中国座机号
688
706
  * @param val
689
707
  */
690
- declare const isTel: (val: NullOrUndefinable<Numeric>) => boolean;
708
+ declare const isTel: (val: UnDef<Numeric>) => boolean;
691
709
  /**
692
710
  * 是否是邮箱
693
711
  * @param val
694
712
  */
695
- declare const isEmail: (val: NullOrUndefinable<string>) => boolean;
713
+ declare const isEmail: (val: UnDef<string>) => boolean;
696
714
  /**
697
715
  * 是否是身份证
698
- * @param val 身份证号
716
+ * @param {string|null|undefined} val 身份证号
717
+ * @param {boolean} compatible 兼容15位
699
718
  */
700
- declare const isIdCard: (val: NullOrUndefinable<string>) => boolean;
719
+ declare const isIdCard: (val: UnDef<string>, compatible?: boolean) => boolean;
701
720
  /**
702
721
  * 包含非ASCII字符
703
722
  * @param text
@@ -735,4 +754,4 @@ declare const isValidPageRes: (res: PageRes, code?: number) => boolean;
735
754
  */
736
755
  declare const isValidFileType: (file: File, type: string) => boolean;
737
756
 
738
- export { type NullOrUndefinable, type Nullable, type Numeric, type PageRes, type PageResData, type Res, type Undefinable, attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, currencyFmt, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getHostEnv, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isDef, isEmail, isEmpty, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, isValidResCode, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, recoverFile, replacer, resUrl, reviver, setBaseAttachUrl, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, toThousands, userAgent, waitTime };
757
+ export { type MaybeArray, type NullOrUndefinable, type Nullable, type Numeric, type PageRes, type PageResData, type Res, type UnDef, type Undefinable, attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, currencyFmt, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getHostEnv, hideBankCard, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isDef, isEmail, isEmpty, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, isValidResCode, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, recoverFile, replacer, resUrl, reviver, setBaseAttachUrl, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, toThousands, userAgent, waitTime };
package/dist/index.d.mts CHANGED
@@ -221,6 +221,10 @@ declare type Undefinable<T> = T | undefined;
221
221
  * 可 Null 或 Undefined
222
222
  */
223
223
  declare type NullOrUndefinable<T> = Nullable<Undefinable<T>>;
224
+ /**
225
+ * alias for NullOrUndefinable
226
+ */
227
+ declare type UnDef<T> = NullOrUndefinable<T>;
224
228
  /**
225
229
  * 请求结果
226
230
  */
@@ -242,53 +246,61 @@ declare type MaybeArray<T = any> = T | Array<T>;
242
246
  * 隐藏中间部分
243
247
  * @param {string} str 需要隐藏的字符串
244
248
  */
245
- declare const hideMiddle: (str: NullOrUndefinable<string>) => string;
249
+ declare const hideMiddle: (str: UnDef<string>) => string;
246
250
  /**
247
251
  * 隐藏首尾仅保留中间3分之1
248
252
  * @param {string} str 需要隐藏的字符串
249
253
  */
250
- declare const hideThird: (str: NullOrUndefinable<string>) => string;
254
+ declare const hideThird: (str: UnDef<string>) => string;
251
255
  /**
252
256
  * 隐藏首部
253
257
  * @param {string} str 需要隐藏的字符串
258
+ * @param {number} count 隐藏的字符数
254
259
  */
255
- declare const hideHead: (str: NullOrUndefinable<string>) => string;
260
+ declare const hideHead: (str: UnDef<string>, count?: number) => string;
256
261
  /**
257
262
  * 隐藏尾部
258
263
  * @param {string} str 需要隐藏的字符串
264
+ * @param {number} count 隐藏的字符数
259
265
  */
260
- declare const hideTail: (str: NullOrUndefinable<string>) => string;
266
+ declare const hideTail: (str: UnDef<string>, count?: number) => string;
261
267
  /**
262
268
  * 隐藏手机号
263
269
  * @param {string} phone 手机号
264
270
  */
265
- declare const hidePhone: (phone: NullOrUndefinable<string>) => string;
271
+ declare const hidePhone: (phone: UnDef<string>) => string;
266
272
  /**
267
273
  * 隐藏身份证号
268
274
  * @param {string} cardNo 身份证号
275
+ * @param {boolean} strong 是否强化隐藏
269
276
  */
270
- declare const hideCardNo: (cardNo: NullOrUndefinable<string>) => string;
277
+ declare const hideCardNo: (cardNo: UnDef<string>, strong?: boolean) => string;
271
278
  /**
272
279
  * 隐藏姓名第一个字
273
280
  * @param {string} name 姓名
274
281
  * @deprecated 请使用 hideSurname
275
282
  */
276
- declare const hideName: (name: NullOrUndefinable<string>) => string;
283
+ declare const hideName: (name: UnDef<string>) => string;
277
284
  /**
278
285
  * 隐藏姓氏
279
286
  * @param {string} name 姓名
280
287
  */
281
- declare const hideSurname: (name: NullOrUndefinable<string>) => string;
288
+ declare const hideSurname: (name: UnDef<string>) => string;
282
289
  /**
283
290
  * 隐藏名字
284
291
  * @param {string} name 姓名
285
292
  */
286
- declare const hideFirstName: (name: NullOrUndefinable<string>) => string;
293
+ declare const hideFirstName: (name: UnDef<string>) => string;
287
294
  /**
288
295
  * 隐藏邮箱
289
296
  * @param {string} email 邮箱
290
297
  */
291
- declare const hideEmail: (email: NullOrUndefinable<string>) => string;
298
+ declare const hideEmail: (email: UnDef<string>) => string;
299
+ /**
300
+ * 隐藏银行卡号
301
+ * @param {string} bankCard 银行卡号
302
+ */
303
+ declare const hideBankCard: (bankCard: UnDef<string>) => string;
292
304
 
293
305
  /**
294
306
  * 判断是否是浏览器环境
@@ -549,7 +561,7 @@ declare const toThousands: (num: number) => string | undefined;
549
561
  * @param currency
550
562
  * @param fraction
551
563
  */
552
- declare const currencyFmt: (currency: NullOrUndefinable<number | bigint>, fraction?: number) => string;
564
+ declare const currencyFmt: (currency: UnDef<number | bigint>, fraction?: number) => string;
553
565
 
554
566
  type VantUploadFileStatus = '' | 'uploading' | 'done' | 'failed'
555
567
  type AntdUploadFileStatus = 'removed' | 'uploading' | 'done' | 'error'
@@ -607,14 +619,17 @@ declare const recoverFile: (resource: Resource) => {
607
619
  deletable?: boolean | undefined;
608
620
  reupload?: boolean | undefined;
609
621
  fileName?: string | undefined;
610
- thumbUrl?: string | undefined;
622
+ thumbUrl?: string | undefined; /**
623
+ * 统一文件字段内容
624
+ * @param resource
625
+ */
611
626
  percentage?: number | undefined;
612
627
  };
613
628
  /**
614
629
  * 附件格式
615
630
  * @param data
616
631
  */
617
- declare const attachFmt: (data: NullOrUndefinable<MaybeArray<Resource>> | string) => {
632
+ declare const attachFmt: (data: UnDef<MaybeArray<Resource>> | string) => {
618
633
  url: string | undefined;
619
634
  content: string | undefined;
620
635
  uri: string;
@@ -631,7 +646,10 @@ declare const attachFmt: (data: NullOrUndefinable<MaybeArray<Resource>> | string
631
646
  deletable?: boolean | undefined;
632
647
  reupload?: boolean | undefined;
633
648
  fileName?: string | undefined;
634
- thumbUrl?: string | undefined;
649
+ thumbUrl?: string | undefined; /**
650
+ * 统一文件字段内容
651
+ * @param resource
652
+ */
635
653
  percentage?: number | undefined;
636
654
  }[];
637
655
 
@@ -654,7 +672,7 @@ declare const checkImg: (ext?: string) => boolean;
654
672
  * 是否是空值/空数组/空对象 null undefined '' [] {} Set Map
655
673
  * @param val
656
674
  */
657
- declare const isEmpty: (val: NullOrUndefinable<Numeric | boolean | Record<any, any> | Array<any> | Set<any> | Map<any, any>>) => boolean;
675
+ declare const isEmpty: (val: UnDef<Numeric | boolean | Record<any, any> | Array<any> | Set<any> | Map<any, any>>) => boolean;
658
676
  /**
659
677
  * 判断是否合法JSON字符串
660
678
  * @param str
@@ -682,22 +700,23 @@ declare const isValidKey: (key: string | number | symbol, object: object) => key
682
700
  * 是否是中国手机号
683
701
  * @param val
684
702
  */
685
- declare const isPhone: (val: NullOrUndefinable<Numeric>) => boolean;
703
+ declare const isPhone: (val: UnDef<Numeric>) => boolean;
686
704
  /**
687
705
  * 是否是中国座机号
688
706
  * @param val
689
707
  */
690
- declare const isTel: (val: NullOrUndefinable<Numeric>) => boolean;
708
+ declare const isTel: (val: UnDef<Numeric>) => boolean;
691
709
  /**
692
710
  * 是否是邮箱
693
711
  * @param val
694
712
  */
695
- declare const isEmail: (val: NullOrUndefinable<string>) => boolean;
713
+ declare const isEmail: (val: UnDef<string>) => boolean;
696
714
  /**
697
715
  * 是否是身份证
698
- * @param val 身份证号
716
+ * @param {string|null|undefined} val 身份证号
717
+ * @param {boolean} compatible 兼容15位
699
718
  */
700
- declare const isIdCard: (val: NullOrUndefinable<string>) => boolean;
719
+ declare const isIdCard: (val: UnDef<string>, compatible?: boolean) => boolean;
701
720
  /**
702
721
  * 包含非ASCII字符
703
722
  * @param text
@@ -735,4 +754,4 @@ declare const isValidPageRes: (res: PageRes, code?: number) => boolean;
735
754
  */
736
755
  declare const isValidFileType: (file: File, type: string) => boolean;
737
756
 
738
- export { type NullOrUndefinable, type Nullable, type Numeric, type PageRes, type PageResData, type Res, type Undefinable, attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, currencyFmt, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getHostEnv, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isDef, isEmail, isEmpty, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, isValidResCode, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, recoverFile, replacer, resUrl, reviver, setBaseAttachUrl, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, toThousands, userAgent, waitTime };
757
+ export { type MaybeArray, type NullOrUndefinable, type Nullable, type Numeric, type PageRes, type PageResData, type Res, type UnDef, type Undefinable, attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, currencyFmt, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getHostEnv, hideBankCard, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isDef, isEmail, isEmpty, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, isValidResCode, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, recoverFile, replacer, resUrl, reviver, setBaseAttachUrl, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, toThousands, userAgent, waitTime };
package/dist/index.d.ts CHANGED
@@ -221,6 +221,10 @@ declare type Undefinable<T> = T | undefined;
221
221
  * 可 Null 或 Undefined
222
222
  */
223
223
  declare type NullOrUndefinable<T> = Nullable<Undefinable<T>>;
224
+ /**
225
+ * alias for NullOrUndefinable
226
+ */
227
+ declare type UnDef<T> = NullOrUndefinable<T>;
224
228
  /**
225
229
  * 请求结果
226
230
  */
@@ -242,53 +246,61 @@ declare type MaybeArray<T = any> = T | Array<T>;
242
246
  * 隐藏中间部分
243
247
  * @param {string} str 需要隐藏的字符串
244
248
  */
245
- declare const hideMiddle: (str: NullOrUndefinable<string>) => string;
249
+ declare const hideMiddle: (str: UnDef<string>) => string;
246
250
  /**
247
251
  * 隐藏首尾仅保留中间3分之1
248
252
  * @param {string} str 需要隐藏的字符串
249
253
  */
250
- declare const hideThird: (str: NullOrUndefinable<string>) => string;
254
+ declare const hideThird: (str: UnDef<string>) => string;
251
255
  /**
252
256
  * 隐藏首部
253
257
  * @param {string} str 需要隐藏的字符串
258
+ * @param {number} count 隐藏的字符数
254
259
  */
255
- declare const hideHead: (str: NullOrUndefinable<string>) => string;
260
+ declare const hideHead: (str: UnDef<string>, count?: number) => string;
256
261
  /**
257
262
  * 隐藏尾部
258
263
  * @param {string} str 需要隐藏的字符串
264
+ * @param {number} count 隐藏的字符数
259
265
  */
260
- declare const hideTail: (str: NullOrUndefinable<string>) => string;
266
+ declare const hideTail: (str: UnDef<string>, count?: number) => string;
261
267
  /**
262
268
  * 隐藏手机号
263
269
  * @param {string} phone 手机号
264
270
  */
265
- declare const hidePhone: (phone: NullOrUndefinable<string>) => string;
271
+ declare const hidePhone: (phone: UnDef<string>) => string;
266
272
  /**
267
273
  * 隐藏身份证号
268
274
  * @param {string} cardNo 身份证号
275
+ * @param {boolean} strong 是否强化隐藏
269
276
  */
270
- declare const hideCardNo: (cardNo: NullOrUndefinable<string>) => string;
277
+ declare const hideCardNo: (cardNo: UnDef<string>, strong?: boolean) => string;
271
278
  /**
272
279
  * 隐藏姓名第一个字
273
280
  * @param {string} name 姓名
274
281
  * @deprecated 请使用 hideSurname
275
282
  */
276
- declare const hideName: (name: NullOrUndefinable<string>) => string;
283
+ declare const hideName: (name: UnDef<string>) => string;
277
284
  /**
278
285
  * 隐藏姓氏
279
286
  * @param {string} name 姓名
280
287
  */
281
- declare const hideSurname: (name: NullOrUndefinable<string>) => string;
288
+ declare const hideSurname: (name: UnDef<string>) => string;
282
289
  /**
283
290
  * 隐藏名字
284
291
  * @param {string} name 姓名
285
292
  */
286
- declare const hideFirstName: (name: NullOrUndefinable<string>) => string;
293
+ declare const hideFirstName: (name: UnDef<string>) => string;
287
294
  /**
288
295
  * 隐藏邮箱
289
296
  * @param {string} email 邮箱
290
297
  */
291
- declare const hideEmail: (email: NullOrUndefinable<string>) => string;
298
+ declare const hideEmail: (email: UnDef<string>) => string;
299
+ /**
300
+ * 隐藏银行卡号
301
+ * @param {string} bankCard 银行卡号
302
+ */
303
+ declare const hideBankCard: (bankCard: UnDef<string>) => string;
292
304
 
293
305
  /**
294
306
  * 判断是否是浏览器环境
@@ -549,7 +561,7 @@ declare const toThousands: (num: number) => string | undefined;
549
561
  * @param currency
550
562
  * @param fraction
551
563
  */
552
- declare const currencyFmt: (currency: NullOrUndefinable<number | bigint>, fraction?: number) => string;
564
+ declare const currencyFmt: (currency: UnDef<number | bigint>, fraction?: number) => string;
553
565
 
554
566
  type VantUploadFileStatus = '' | 'uploading' | 'done' | 'failed'
555
567
  type AntdUploadFileStatus = 'removed' | 'uploading' | 'done' | 'error'
@@ -607,14 +619,17 @@ declare const recoverFile: (resource: Resource) => {
607
619
  deletable?: boolean | undefined;
608
620
  reupload?: boolean | undefined;
609
621
  fileName?: string | undefined;
610
- thumbUrl?: string | undefined;
622
+ thumbUrl?: string | undefined; /**
623
+ * 统一文件字段内容
624
+ * @param resource
625
+ */
611
626
  percentage?: number | undefined;
612
627
  };
613
628
  /**
614
629
  * 附件格式
615
630
  * @param data
616
631
  */
617
- declare const attachFmt: (data: NullOrUndefinable<MaybeArray<Resource>> | string) => {
632
+ declare const attachFmt: (data: UnDef<MaybeArray<Resource>> | string) => {
618
633
  url: string | undefined;
619
634
  content: string | undefined;
620
635
  uri: string;
@@ -631,7 +646,10 @@ declare const attachFmt: (data: NullOrUndefinable<MaybeArray<Resource>> | string
631
646
  deletable?: boolean | undefined;
632
647
  reupload?: boolean | undefined;
633
648
  fileName?: string | undefined;
634
- thumbUrl?: string | undefined;
649
+ thumbUrl?: string | undefined; /**
650
+ * 统一文件字段内容
651
+ * @param resource
652
+ */
635
653
  percentage?: number | undefined;
636
654
  }[];
637
655
 
@@ -654,7 +672,7 @@ declare const checkImg: (ext?: string) => boolean;
654
672
  * 是否是空值/空数组/空对象 null undefined '' [] {} Set Map
655
673
  * @param val
656
674
  */
657
- declare const isEmpty: (val: NullOrUndefinable<Numeric | boolean | Record<any, any> | Array<any> | Set<any> | Map<any, any>>) => boolean;
675
+ declare const isEmpty: (val: UnDef<Numeric | boolean | Record<any, any> | Array<any> | Set<any> | Map<any, any>>) => boolean;
658
676
  /**
659
677
  * 判断是否合法JSON字符串
660
678
  * @param str
@@ -682,22 +700,23 @@ declare const isValidKey: (key: string | number | symbol, object: object) => key
682
700
  * 是否是中国手机号
683
701
  * @param val
684
702
  */
685
- declare const isPhone: (val: NullOrUndefinable<Numeric>) => boolean;
703
+ declare const isPhone: (val: UnDef<Numeric>) => boolean;
686
704
  /**
687
705
  * 是否是中国座机号
688
706
  * @param val
689
707
  */
690
- declare const isTel: (val: NullOrUndefinable<Numeric>) => boolean;
708
+ declare const isTel: (val: UnDef<Numeric>) => boolean;
691
709
  /**
692
710
  * 是否是邮箱
693
711
  * @param val
694
712
  */
695
- declare const isEmail: (val: NullOrUndefinable<string>) => boolean;
713
+ declare const isEmail: (val: UnDef<string>) => boolean;
696
714
  /**
697
715
  * 是否是身份证
698
- * @param val 身份证号
716
+ * @param {string|null|undefined} val 身份证号
717
+ * @param {boolean} compatible 兼容15位
699
718
  */
700
- declare const isIdCard: (val: NullOrUndefinable<string>) => boolean;
719
+ declare const isIdCard: (val: UnDef<string>, compatible?: boolean) => boolean;
701
720
  /**
702
721
  * 包含非ASCII字符
703
722
  * @param text
@@ -735,4 +754,4 @@ declare const isValidPageRes: (res: PageRes, code?: number) => boolean;
735
754
  */
736
755
  declare const isValidFileType: (file: File, type: string) => boolean;
737
756
 
738
- export { type NullOrUndefinable, type Nullable, type Numeric, type PageRes, type PageResData, type Res, type Undefinable, attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, currencyFmt, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getHostEnv, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isDef, isEmail, isEmpty, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, isValidResCode, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, recoverFile, replacer, resUrl, reviver, setBaseAttachUrl, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, toThousands, userAgent, waitTime };
757
+ export { type MaybeArray, type NullOrUndefinable, type Nullable, type Numeric, type PageRes, type PageResData, type Res, type UnDef, type Undefinable, attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, currencyFmt, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getHostEnv, hideBankCard, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isDef, isEmail, isEmpty, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, isValidResCode, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, recoverFile, replacer, resUrl, reviver, setBaseAttachUrl, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, toThousands, userAgent, waitTime };
package/dist/index.mjs CHANGED
@@ -100,12 +100,12 @@ const isEmail = (val) => {
100
100
  const reg = /^[a-zA-Z\d](?:\w|-)+@[a-zA-Z\d]+\.[a-zA-Z]{2,5}$/;
101
101
  return reg.test(val);
102
102
  };
103
- const isIdCard = (val) => {
103
+ const isIdCard = (val, compatible = true) => {
104
104
  if (!isDef(val))
105
105
  return false;
106
106
  const card15 = /^[1-9]\d{7}(?:0[1-9]|10|11|12)(?:[0-2][1-9]|10|20|30|31)\d{2}[\dXx]$/;
107
107
  const card18 = /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|1[0-2])(?:[0-2][1-9]|10|20|30|31)\d{3}[\dXx]$/;
108
- return card15.test(val) || card18.test(val);
108
+ return card18.test(val) || (compatible ? card15.test(val) : false);
109
109
  };
110
110
  const nonASCII = (text) => /[^\x20-\x7E]/.test(text);
111
111
  const isValidResCode = (res, code = 200) => res.code === code;
@@ -157,21 +157,23 @@ const hideThird = (str) => {
157
157
  return "**";
158
158
  return str.slice(0, partLen) + "*".repeat(len - 2 * partLen) + str.slice(len - partLen);
159
159
  };
160
- const hideHead = (str) => {
160
+ const hideHead = (str, count) => {
161
161
  if (!isDef(str))
162
162
  return "";
163
163
  const len = str.length;
164
+ const cnt = count !== void 0 ? Math.max(Math.min(len, count), 0) : len - 1;
164
165
  if (len === 1)
165
166
  return "*";
166
- return "*".repeat(len - 1) + str[len - 1];
167
+ return "*".repeat(cnt) + str.substring(cnt);
167
168
  };
168
- const hideTail = (str) => {
169
+ const hideTail = (str, count) => {
169
170
  if (!isDef(str))
170
171
  return "";
171
172
  const len = str.length;
173
+ const cnt = count !== void 0 ? Math.max(Math.min(len, count), 0) : len - 1;
172
174
  if (len === 1)
173
175
  return "*";
174
- return str[0] + "*".repeat(len - 1);
176
+ return str.substring(0, len - cnt) + "*".repeat(cnt);
175
177
  };
176
178
  const hidePhone = (phone) => {
177
179
  if (phone) {
@@ -185,9 +187,12 @@ const hidePhone = (phone) => {
185
187
  }
186
188
  return "";
187
189
  };
188
- const hideCardNo = (cardNo) => {
190
+ const hideCardNo = (cardNo, strong = true) => {
189
191
  if (cardNo) {
190
- return cardNo.replace(/^(.{4})\d+(.{2})$/, "$1*******$2");
192
+ if (isIdCard(cardNo, false)) {
193
+ return strong ? hideMiddle(cardNo) : cardNo.replace(/^(.)\d{3}(.{8})\d{5}(.)$/, "$1***$2*****$3");
194
+ }
195
+ return cardNo;
191
196
  }
192
197
  return "";
193
198
  };
@@ -206,7 +211,7 @@ const hideName = (name) => {
206
211
  return "";
207
212
  };
208
213
  const hideSurname = (name) => {
209
- return hideHead(name);
214
+ return hideHead(name, 1);
210
215
  };
211
216
  const hideFirstName = (name) => {
212
217
  return hideTail(name);
@@ -214,14 +219,29 @@ const hideFirstName = (name) => {
214
219
  const hideEmail = (email) => {
215
220
  if (email) {
216
221
  if (!email.includes("@"))
217
- return "";
222
+ return email;
218
223
  const arr = email.split("@");
219
224
  const name = arr[0];
220
225
  const domain = arr[1];
221
- const len = name?.length;
222
- if (!len)
226
+ if (!name || !domain)
223
227
  return email;
224
- return `${hideTail(name)}@${domain}`;
228
+ const len = name.length;
229
+ let newName = "";
230
+ if (len <= 3) {
231
+ newName = `${name}***`;
232
+ } else {
233
+ newName = `${name.substring(0, len - 3)}***`;
234
+ }
235
+ return `${newName}@${domain}`;
236
+ }
237
+ return "";
238
+ };
239
+ const hideBankCard = (bankCard) => {
240
+ if (bankCard) {
241
+ const len = bankCard.length;
242
+ if (len <= 10)
243
+ return bankCard;
244
+ return bankCard.substring(0, 6) + "*".repeat(len - 10) + bankCard.substring(len - 4, len);
225
245
  }
226
246
  return "";
227
247
  };
@@ -407,4 +427,4 @@ const attachFmt = (data) => {
407
427
  return attachList.map(recoverFile);
408
428
  };
409
429
 
410
- export { attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, currencyFmt, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getHostEnv, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isDef, isEmail, isEmpty, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, isValidResCode, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, recoverFile, replacer, resUrl, reviver, setBaseAttachUrl, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, toThousands, userAgent, waitTime };
430
+ export { attachFmt, camel2Kebab, camel2Pascal, camel2Snake, camel2Title, checkImg, currencyFmt, dateDuration, dateFmt, filterObj, fixiOSInputAutoZoomIn, getBaseAttachUrl, getHostEnv, hideBankCard, hideCardNo, hideEmail, hideFirstName, hideHead, hideMiddle, hideName, hidePhone, hideSurname, hideTail, hideThird, inBrowser, isArrStr, isDef, isEmail, isEmpty, isIdCard, isJSONStr, isNumeric, isPhone, isTel, isValidArrRes, isValidFileType, isValidKey, isValidPageRes, isValidRes, isValidResCode, jClear, jDebug, jError, jGroup, jGroupC, jGroupE, jInfo, jLog, jTime, jTimeEnd, jWarn, kebab2Camel, kebab2Pascal, kebab2Snake, kebab2Title, nonASCII, parseToJSON, pascal2Camel, pascal2Kebab, pascal2Snake, pascal2Title, recoverFile, replacer, resUrl, reviver, setBaseAttachUrl, snake2Camel, snake2Kebab, snake2Pascal, snake2Title, title2Camel, title2Kebab, title2Pascal, title2Snake, toThousands, 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.24",
4
+ "version": "0.0.1-beta.25",
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",