zhengyiming
1 天以前 2e18c1477942ea6df0592807051e78bd192a75b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import dayjs from 'dayjs';
import { BoleRegExp } from '@bole-core/core';
import { round, floor, omitBy } from 'lodash';
 
export function format(date: string | Date, fmt = 'YYYY-MM-DD') {
  return date ? dayjs(date).format(fmt) : '';
}
 
export const canPreviewFun = (url: string) => {
  const _can = BoleRegExp.RegCanPreview.test(url);
  BoleRegExp.RegCanPreview.lastIndex = 0;
  return _can;
};
 
export function toRound(val: number, num = 2) {
  if (val >= 0) {
    return round(val, num);
  } else {
    const abs = Math.abs(val);
    return 0 - round(abs, num);
  }
}
 
export function toThousand(input) {
  const num = toRound(Number(input)).toFixed(2);
  if (Number(num) >= 0) {
    return num.toString().replace(/(^|\s)\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','));
  } else {
    const posNum = (0 - Number(num)).toFixed(2);
    return (
      '-' + posNum.toString().replace(/(^|\s)\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
    );
  }
}
 
export function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    const r = (Math.random() * 16) | 0,
      v = c === 'x' ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}
 
export function setTemplateId() {
  return guid().replace(/-/g, '').toUpperCase().slice(0, 16);
}
 
// 时间 0填充
const timePad = (num: number) => {
  return num < 10 ? '0' + num : num;
};
 
export const secondsToMinutes = (seconds: number) => {
  const minutes = floor(seconds / 60);
  const resetSeconds = seconds % 60;
  return `${timePad(minutes)}:${timePad(resetSeconds)}`;
};
 
export const hiddenIDNumber = (realIDNumber: string) =>
  realIDNumber.replace(/^(.{6})(?:\d+)(.{4})$/, '$1********$2');
 
export const hiddenPhoneNumber = (realPhoneNumbe: string) =>
  realPhoneNumbe.replace(/^(.{3})(?:\d+)(.{4})$/, '$1****$2');
 
export function filterJoin(list: any[], separator = '-') {
  return list.filter(Boolean).join(separator);
}
 
export class StringUtils {
  static insertSpaces(str: string, space = 4) {
    if (!str) return '';
    const regex = new RegExp(`(.{${space}})`, 'g');
    return str.replace(regex, '$1 ');
  }
 
  static societyCreditCodeInsertSpaces(str: string) {
    if (!str) return '';
    return str.replace(/(.{4})(.{4})(.{4})(.{6})/g, '$1 $2 $3 $4');
  }
 
  static idNumberInsertSpaces(str: string) {
    if (!str) return '';
    return str.replace(/(.{3})(.{3})(.{4})(.{4})(.{4})/g, '$1 $2 $3 $4 $5');
  }
 
  static phoneNumberAddSpace(realPhoneNumber: string) {
    if (!realPhoneNumber) return '';
    return realPhoneNumber.replace(/^(\d{3})(\d*)(\d{4})$/, '$1 $2 $3');
  }
}
 
export function paginateList<T>(list: T[], pageIndex: number, pageSize: number) {
  const startIndex = (pageIndex - 1) * pageSize;
  const endIndex = startIndex + pageSize;
  return list.slice(startIndex, endIndex);
}
 
export function omitByFalse<T>(object: { [key: string]: any }) {
  return omitBy(object, (v) => !v) as any as T;
}
 
export function filterCN(str: string) {
  return str.replace(/[\u4e00-\u9fa5]/gi, '');
}
 
export function filterNumbersFromString(str: string) {
  return str.replace(/\D/g, '');
}
 
export function calculateAge(idCardNumber) {
  if (BoleRegExp.RegIDCard.test(idCardNumber)) {
    const birthDateStr = idCardNumber.match(/(\d{6})(\d{8})/)[2];
    const birthDate = new Date(
      birthDateStr.substring(0, 4),
      birthDateStr.substring(4, 6) - 1,
      birthDateStr.substring(6, 8)
    );
 
    // Calculate age
    const today = new Date();
    const age =
      today.getFullYear() -
      birthDate.getFullYear() -
      (today.getMonth() < birthDate.getMonth() ||
      (today.getMonth() === birthDate.getMonth() && today.getDate() < birthDate.getDate())
        ? 1
        : 0);
 
    return age;
  }
  return null;
}