收集一些前端工具函数,整理成集,以便查询使用。随时更新:2021-03-03

正则匹配相关

手机号正则验证

将手机号中间四位隐藏为****

1
2
3
4
const phone = 17600000000;
const data = phone.toString().replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
console.log(data);

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





export const setCookie = (name, val) => {
const date = new Date();
const value = val;


date.setTime(date.getTime() + 7 * 24 * 60 * 60 * 1000);


document.cookie =
name + "=" + value + "; expires=" + date.toUTCString() + "; path=/";
};





export const getCookie = (name) => {
const value = "; " + document.cookie;
const parts = value.split("; " + name + "=");

if (parts.length === 2) {
const ppop = parts.pop();
if (ppop) {
return ppop.split(";").shift();
}
}
};





export const deleteCookie = (name) => {
const date = new Date();


date.setTime(date.getTime() + -1 * 24 * 60 * 60 * 1000);


document.cookie = name + "=; expires=" + date.toUTCString() + "; path=/";
};

格式化时间

将时间戳变为个位数带 0 前缀

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19





export const formatDate = (date) => {
const da = new Date(date);
const year = da.getFullYear();
const month = da.getMonth() + 1;
const day = da.getDate();
const hours = da.getHours();
const mins = da.getMinutes();
const secs = da.getSeconds();
return `${year}-${month > 9 ? month : "0" + month}-${
day > 9 ? day : "0" + day
} ${hours > 9 ? hours : "0" + hours}:${mins > 9 ? mins : "0" + mins}:${
secs > 9 ? secs : "0" + secs
}`;
};

生成 UUID

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






export const UUID = (len, radix) => {
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(
""
);
const uuid = [];
let i;
radix = radix || chars.length;

if (len) {

for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
} else {

let r;


uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
uuid[14] = "4";



for (i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | (Math.random() * 16);
uuid[i] = chars[i === 19 ? (r & 0x3) | 0x8 : r];
}
}
}

return uuid.join("");
};

type 类型判断

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
export const isString = (e) => { 
return Object.prototype.toString.call(e).slice(8, -1) === 'String'
}

export const isNumber = (e) => {
return Object.prototype.toString.call(e).slice(8, -1) === 'Number'
}

export const isBoolean = (e) => {
return Object.prototype.toString.call(e).slice(8, -1) === 'Boolean'
}

export const isFunction = (e) => {
return Object.prototype.toString.call(e).slice(8, -1) === 'Function'
}

export const isNull = (e) => {
return Object.prototype.toString.call(e).slice(8, -1) === 'Null'
}

export const isUndefined = (e) => {
return Object.prototype.toString.call(e).slice(8, -1) === 'Undefined'
}

export const isObj = (e) => {
return Object.prototype.toString.call(e).slice(8, -1) === 'Object'
}

export const isArray = (e) => {
return Object.prototype.toString.call(e).slice(8, -1) === 'Array'
}

export const isDate = (e) => {
return Object.prototype.toString.call(e).slice(8, -1) === 'Date'
}

export const isRegExp = (e) => {
return Object.prototype.toString.call(e).slice(8, -1) === 'RegExp'
}

export const isError = (e) => {
return Object.prototype.toString.call(e).slice(8, -1) === 'Error'
}

export const isSymbol = (e) => {
return Object.prototype.toString.call(e).slice(8, -1) === 'Symbol'
}

export const isPromise = (e) => {
return Object.prototype.toString.call(e).slice(8, -1) === 'Promise'
}

export const isSet = (e) => {
return Object.prototype.toString.call(e).slice(8, -1) === 'Set'
}

random随机数

返回一定范围的整数随机数

1
2

export const randoms = (min, max) => Math.floor(min + Math.random() * ((max + 1) - min))

去除空格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20






export const trim = (str, type = 1) => {
switch (type) {
case 1:
return str.replace(/\s+/g, '')
case 2:
return str.replace(/(^\s*)|(\s*$)/g, '')
case 3:
return str.replace(/(^\s*)/g, '')
case 4:
return str.replace(/(\s*$)/g, '')
default:
return str
}
}

大小写处理

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





export const changeCase = (str, type = 4) => {
switch (type) {
case 1:
return str.replace(/\b\w+\b/g, (word) => return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase())
case 2:
return str.replace(/\b\w+\b/g, (word) => word.substring(0, 1).toLowerCase() + word.substring(1).toUpperCase())
case 3:
return str.split('').map((word) => {
if (/[a-z]/.test(word)) {
return word.toUpperCase()
} else {
return word.toLowerCase()
}
}).join('')
case 4:
return str.toUpperCase()
case 5:
return str.toLowerCase()
default:
return str
}
}