?包含数据处理、格式转换、正则校验、日期等工具。
import dayjs from 'dayjs';
// 随机生成6位数字符串
export const randomStr = () => Math.random().toString(20).slice(2, 8);
// 反转字符串
export const reverseStr = (str: string) => str.split('').reverse().join('');
// 英文首字母大写
export const capitalizeEveryWordStr = (str: string) =>
str.replace(/\b[a-z]/g, (char) => char.toUpperCase());
// 打乱数组元素顺序
export const shuffle = (array: any[]) => array.sort(() => 0.5 - Math.random());
// 手机号格式化 188***8888
export const formatPhoneNumber = (phone: number): string => {
let phoneNumber = phone.toString();
return phoneNumber.substring(0, 3) + '****' + phoneNumber.substring(7);
}
// 名称隐私化
export const formatName = (name: string): string => {
return name.substring(0, 1) + ''.padStart(name.length - 1, '*')
}
// 复制
export const copy = (params: string) => {
navigator.clipboard.writeText(params ? params : "").then(res => {
console.log('复制成功!');
}).catch(err => {
console.log('复制失败,请手动复制!');
})
}
// RGB到十六进制转换机制
export const rgbToHex = (r: number, g: number, b: number) =>
'#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
// 对象转换成url query参数
export const toQueryString = (obj: any) => new URLSearchParams(obj).toString();
//url query参数转对象
export const getUrlQuery = (): any => {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const params = Object.fromEntries(urlParams.entries());
return params;
}
// url UTF-8字符串二次编码转汉字
export const toUTFString = (url:string) => decodeURI(decodeURI(url));
// 解码
// @ts-ignore
export const escapeHtml = (str: string) => {
let s = str;
if (!str?.length) return '';
s = str?.replace(/&/g, '&');
s = s?.replace(/</g, '<');
s = s?.replace(/>/g, '>');
s = s?.replace(/ /g, ' ');
s = s?.replace(/'/g, "'");
s = s?.replace(/"/g, '"');
if (
s.indexOf('&') === -1 &&
s.indexOf('<') === -1 &&
s.indexOf('>') === -1 &&
s.indexOf(' ') === -1 &&
s.indexOf(''') === -1 &&
s.indexOf('"') === -1
)
return s;
else return escapeHtml(s);
};
// 转码
export const unEscapeHtml = (str: string) => {
let s = '';
if (str.length == 0) return '';
s = str.replace(/&/g, '&');
s = s.replace(/</g, '<');
s = s.replace(/>/g, '>');
s = s.replace(/ /g, ' ');
s = s.replace(/'/g, ''');
s = s.replace(/"/g, '"');
return s;
};
// 常用正则表达式
export const regexs = {
txt: /\.(txt)$/i, // txt文件
pdf: /\.(pdf)$/i, // pdf文件
ppt: /\.(ppt)$/i, // ppt文件
word: /\.(doc|docx)$/i, // word文件
excel: /\.(xls|xlsx|xlsm|csv)$/i, // 表格文件
radio: /\.(mp3|wav|wmv)$/i, // 音频文件
video: /\.(mp4|m2v|mkv|avi)$/i, // 视频文件
image: /\.(jpg|jpeg|png|bmp|gif|webp)$/i, // 图片文件
postal: /^[1-9]\d{5}$/, // 邮政编码
mobile: /^1[3456789]\d{9}$/, // 手机号
symbol: /[^\w\s,.!?]/, // 是否包含特殊字符
email: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, // 邮箱
phone: /^(\+?\d{1,4}[-\s]?)?\(?\d{1,3}\)?[-\s]?\d{1,4}[-\s]?\d{1,9}$/, // 电话号
url: /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i, // 链接
id: /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[xX\d]$/, // 身份证
ip: /^(?:(?:[0-9]{1,3}\.){3}[0-9]{1,3})|(?:(?:(?:(?:[A-Fa-f0-9]{1,4}:){1,7}[A-Fa-f0-9]{1,4})|(?:[A-Fa-f0-9]{1,4}:){7}:|:(?::[A-Fa-f0-9]{1,4}){1,7}|::))(?:\/\d{1,2})?$/, // IP地址 IPv4和IPv6地址
password: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/, // 包含大小写字母、数字和特殊符号的至少8位数密码
};
export const regexFun = (
value: string,
type:
| 'txt'
| 'pdf'
| 'ppt'
| 'word'
| 'excel'
| 'radio'
| 'video'
| 'image'
| 'postal'
| 'mobile'
| 'symbol'
| 'email'
| 'phone'
| 'url'
| 'id'
| 'ip'
| 'password',
) => regexs[type].test(value);
// 获取当天日期
export const currentDate = () => dayjs().format('YYYY-MM-DD HH:mm:ss');
// 获取今天的起止时间
export const currentDayDate = () => [
dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
];
// 获取本周的起止时间
export const currentWeekDate = () => [
dayjs().startOf('week').format('YYYY-MM-DD HH:mm:ss'),
dayjs().endOf('week').format('YYYY-MM-DD HH:mm:ss'),
];
// 获取本月的起止时间
export const currentMonthDate = () => [
dayjs().startOf('month').format('YYYY-MM-DD HH:mm:ss'),
dayjs().endOf('month').format('YYYY-MM-DD HH:mm:ss'),
];
// 获取本年的起止时间
export const currentYearDate = () => [
dayjs().startOf('year').format('YYYY-MM-DD HH:mm:ss'),
dayjs().endOf('year').format('YYYY-MM-DD HH:mm:ss'),
];
// 获取最近三天的起止时间
export const lastThreeDayDate = () => [
dayjs().subtract(2, 'days').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
];
// 获取最近三周的起止时间
export const lastThreeWeekDate = () => [
dayjs().subtract(2, 'week').startOf('week').format('YYYY-MM-DD HH:mm:ss'),
dayjs().endOf('week').format('YYYY-MM-DD HH:mm:ss'),
];
// 获取最近三月的起止时间
export const lastThreeMonthDate = () => [
dayjs().subtract(2, 'month').startOf('month').format('YYYY-MM-DD HH:mm:ss'),
dayjs().endOf('month').format('YYYY-MM-DD HH:mm:ss'),
];
// 获取最近三年的起止时间
export const lastThreeYaerDate = () => [
dayjs().subtract(2, 'year').startOf('year').format('YYYY-MM-DD HH:mm:ss'),
dayjs().endOf('year').format('YYYY-MM-DD HH:mm:ss'),
];
// 获取当天属于本周的第几天
export const currentDayOfWeekStr = () =>
dayjs().diff(dayjs().startOf('week'), 'days') + 1;
// 获取当天属于本月的第几天
export const currentDayOfMonthStr = () =>
dayjs().diff(dayjs().startOf('month'), 'days') + 1;
// 获取当天属于今年的第几天
export const currentDayOfYearStr = () =>
dayjs().diff(dayjs().startOf('year'), 'days') + 1;
// 获取本周属于本月的第几周
export const currentWeekOfMonthStr = () =>
dayjs().startOf('week').diff(dayjs().startOf('month'), 'week') + 1;
// 获取本周属于本年的第几周
export const currentWeekOfYearStr = () =>
dayjs().startOf('week').diff(dayjs().startOf('year'), 'week') + 1;
// 获取本月属于本年的第几月
export const currentMonthOfYearStr = () =>
dayjs().startOf('month').diff(dayjs().startOf('year'), 'month') + 1;
// 获取本月总天数
export const currentMonthDays = () => dayjs().daysInMonth();
// 获取本年总天数
export const currentYearDays = () =>
dayjs().endOf('year').diff(dayjs().startOf('year'), 'days') + 1;