JavaScript函数工具整理

日常函数总结

修改当前历史记录 静默修改浏览器 URL 不刷新页面

1
window.history.replaceState({ path: 'url' }, '', url);

解析 URL 参数

1
2
3
4
5
6
7
8
9
10
11
function analysisURL(string) {
return JSON.parse(
`{ ${string
.slice(string.indexOf('?') + 1)
.split('&')
.map(
o => `"${o.slice(0, o.indexOf('='))}":"${o.slice(o.indexOf('=') + 1)}"`
)
.join(',')}}`
);
}

格式化 Date

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
function processingTime(date) {
if ((!date && typeof date !== 'number') || date === '') {
return new Date();
} else if (date instanceof Date) {
return date;
} else {
return new Date(date);
}
}
/**
* @description Formatted time string
* @example
* // returns 2021
* format(YYYY)
* // returns 21
* format(YY)
* // returns 2021-02-01 09:00:00
* format(YYYY-MM-DD hh:mm:ss)
* // returns 21-2-1 9:0:0
* format(YY-M-D h:m:s)
* @param {String} [format='YYYY-MM-DD hh:mm:ss'] A format string defaults to YYYY-MM-DD hh:mm:ss
* @param {(Date|String|Number)} [date=''] Can be a time object or a parameter of new Date
* @returns {String} A formatted time string
*
*/
function format(format = 'YYYY-MM-DD hh:mm:ss', date = '') {
date = processingTime(date);
let Y = date.getFullYear(),
M = date.getMonth() + 1,
D = date.getDate(),
h = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds(),
replaces = [
{ reg1: /MM/, reg2: /M/, str1: 'MM', str2: 'M', self: M },
{ reg1: /DD/, reg2: /D/, str1: 'DD', str2: 'D', self: D },
{ reg1: /hh/, reg2: /h/, str1: 'hh', str2: 'h', self: h },
{ reg1: /mm/, reg2: /m/, str1: 'mm', str2: 'm', self: m },
{ reg1: /ss/, reg2: /s/, str1: 'ss', str2: 's', self: s },
];
if (/Y{4}/.test(format)) {
format = format.replace(/Y{4}/, Y);
} else if (/Y{2}/.test(format)) {
format = format.replace(/Y{2}/, `${Y}`.substr(2));
}
replaces.forEach(({ reg1, reg2, str1, str2, self }) => {
if (format.includes(str1)) {
format = format.replace(reg1, self < 10 ? `0${self}` : self);
} else if (format.includes(str2)) {
format = format.replace(reg2, self);
}
});
return format;
}

随机数

1
2
3
4
5
6
7
8
9
10
/**
* @description 随机从区间中获取一个整数 例:[1,10]
*
* @param {Number} min
* @param {Number} max
* @returns {Number} randomInteger — A numeric expression.
*/
function closedInterval(min, max) {
return Math.floor(Math.random() * (max + 1 - min)) + min;
}
1
2
3
4
5
6
7
8
9
10
/**
* @description 随机从区间中获取一个整数 例:(1,10)
*
* @param {Number} min
* @param {Number} max
* @returns {Number} randomInteger — A numeric expression.
*/
function openInterval(min, max) {
return Math.floor(Math.random() * (max - (min + 1)) + min + 1);
}
1
2
3
4
5
6
7
8
9
10
/**
* @description 随机从区间中获取一个浮点数 例:(1,10)
*
* @param {Number} min
* @param {Number} max
* @returns randomFloating — A numeric expression.
*/
function openIntervalF(min, max) {
return min + Math.random() * (max - min);
}
1
2
3
4
5
6
7
8
9
10
/**
* @description 随机从区间中获取一个整数 例:[1,10)
*
* @param {Number} min
* @param {Number} max
* @returns {Number} randomInteger — A numeric expression.
*/
function closeLeftOpenRight(min, max) {
return Math.ceil(Math.random() * (max - min)) + min;
}
1
2
3
4
5
6
7
8
9
10
/**
* @description 随机从区间中获取一个整数 例:(1,10]
*
* @param {Number} min
* @param {Number} max
* @returns {Number} randomInteger — A numeric expression.
*/
function openLeftCloseRight(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}

数字数组排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const quickSort = arr => {
const a = [...arr];
if (a.length < 2) return a;
const pivotIndex = Math.floor(arr.length / 2);
const pivot = a[pivotIndex];
const [lo, hi] = a.reduce(
(acc, val, i) => {
if (val < pivot || (val === pivot && i != pivotIndex)) {
acc[0].push(val);
} else if (val > pivot) {
acc[1].push(val);
}
return acc;
},
[[], []]
);
return [...quickSort(lo), pivot, ...quickSort(hi)];
};

移除 emoji

1
2
3
4
5
6
7
8
9
10
11
12
13
function removeEmoji(content) {
let conByte = new TextEncoder('utf-8').encode(content);
for (let i = 0; i < conByte.length; i++) {
if ((conByte[i] & 0xf8) == 0xf0) {
for (let j = 0; j < 4; j++) {
conByte[i + j] = 0x30;
}
i += 3;
}
}
content = new TextDecoder('utf-8').decode(conByte);
return content.replaceAll('0000', '');
}
-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!
0%