Commonly used auxiliary methods in front-end js
- introduction
- Conversion of middle digits in numbers
- Large digital conversions
- Verify stitched image path
- Randomly generate file name
- Calculate time (including days) based on the difference between timestamps
- ID number verification
- Mobile phone number verification
- Greater than 0 and with at most two decimal places
introduction
When working on a project, everyone will more or less need to encapsulate an auxiliary method to help their project progress stably. It can also make it easier for them to change the whole situation when they change a method, so they will encapsulate it.
Conversion of middle digits in numbers
export const replaceStart = (str, frontLen, endLen) => {
// str:字符串,frontLen:前面保留位数,endLen:后面保留位数。
var len = str.length - frontLen - endLen;
var start = '';
for (var i = 0; i < len; i++) {
start += '*';
}
return str.substring(0, frontLen) + start + str.substring(str.length - endLen);
}
Large digital conversions
export const numFormat = (num) => {
if (num >= 10000) {
num = Math.round(num / 1000) / 10 + 'w+';
}
return num;
};
Verify stitched image path
export const isHttpOrHttps = (url) => {
const reg = /http[s]{0,1}:\/\/([\w.]+\/?)\S*/;
if (url) {
if (reg.test(url)) {
return url
} else {
return process.uniEnv.UNI_BASE_OSS_IMAGES + url
}
} else {
return;
}
};
// process.uniEnv.UNI_BASE_OSS_IMAGES 字段根据项目改变
Randomly generate file name
export const randomString = (len, suffix) => {
const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz12345678';
const maxPos = chars.length;
let fileName = '';
for (let i = 0; i < len; i++) {
fileName += chars.charAt(Math.floor(Math.random() * maxPos));
}
return fileName + '_' + new Date().getTime() + '.' + suffix;
};
Calculate time (including days) based on the difference between timestamps
export const getTimeContainDay = (aftertimestamp) => {
const nowtimestamp = new Date().getTime()
const difValue = aftertimestamp - nowtimestamp;
// 计算出相差天数
let day = Math.floor(difValue / (24 * 3600 * 1000));
// 计算天数后剩余的毫秒数
let leave1 = difValue % (24 * 3600 * 1000);
let hour = Math.floor(leave1 / (3600 * 1000));
let min = Math.floor(difValue / 1000 / 60 % 60);
let second = Math.floor(difValue / 1000 % 60);
if (parseInt(hour) < 10) {
hour = "0" + hour;
}
if (parseInt(min) < 10) {
min = "0" + min;
}
if (parseInt(second) < 10) {
second = "0" + second;
}
return day + " 天 " + hour + " 时 " + min + " 分 " + second + " 秒"
}
ID number verification
export const isvalidIdCard = (str) => {
const reg18 =
/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
const reg15 =
/^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$/;
return reg18.test(str) || reg15.test(str);
};
Mobile phone number verification
export const isvalidPhone = (str) => {
const reg = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/;
return reg.test(str);
};
Greater than 0 and with at most two decimal places
export const maxTwoDecimal = (num) => {
const reg = /^([1-9]\d*(\.\d{1,2})?|([0](\.([0][1-9]|[1-9]\d{0,1}))))$/;
return reg.test(num);
}
The above are some auxiliary methods I have encapsulated, which will be updated later.