Point JS tool function when the time to use directly copy and paste
Work will always encounter repetitive work content, write more every time to write will be very tired. So I decided to write a tool function, most of the content to write out of their own tool library. Some of it is written by myself, and most of it is borrowed from others!
1. Time formatting
const formatTime = function(timestamp, nos = 1, bindf = The '-') {
if(! timestamp) {return The '-'
}
if ((timestamp + "").includes('Invalid Date')) {
return The '-'
}
var date = new Date(timestamp)
var strLen = timestamp.toString().length
// Check whether the timestamp is less than 13 bits. If it is less than 13 bits, add 0, i.e. multiply by 10 to the power of the difference
if (strLen < 13) {
var sub = 13 - strLen
sub = Math.pow(10, sub) // Compute 10 to the n
date = new Date(timestamp * sub)
}
var y = date.getFullYear()
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
var d = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours())
var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes())
var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds())
if (nos == 1) {
return y + bindf + M + bindf + d + ' ' + h + ':' + m + ':' + s
} else if (nos == 2) {
return y + bindf + M + bindf + d
} else if (nos == 3) {
return M + bindf + d
} else if (nos == 4) {
return h + ':' + m + ':' + s
} else if (nos == 5) {
return y + bindf + M + bindf + d + ' ' + h + ':' + m
} else if (nos == 6) {
return M + bindf + d + ' ' + h + ':' + m + ':' + s
} else if (nos == 7) {
return M + bindf + d + ' ' + h + ':' + m
} else if (nos == 8) {
return d + 'day' + h + 'hour' + m + 'minutes' + s + '秒'}}Copy the code
2. Parse URL parameters
function GetUrlParam(urlStr) {
/ /? a=1&b=2&c=3 ==> {a: "1", b: "2", c: "3"}
let url = urlStr.toString();
let arrObj = url.split("?");
let params = Object.create(null)
if (arrObj.length > 1) {
arrObj = arrObj[1].split("&");
arrObj.forEach(item= > {
item = item.split("=");
params[item[0]] = item[1]})}return params;
}
Copy the code
3. Specify a range of random numbers
function RandomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Copy the code
4. A deep copy
function deepClone(obj, cache = new WeakMap(a)) {
if (typeofobj ! = ='object') return obj // Common type, return directly
if (obj === null) return obj
if (cache.get(obj)) return cache.get(obj) // Prevent loop references, the program into an infinite loop
if (obj instanceof Date) return new Date(obj)
if (obj instanceof RegExp) return new RegExp(obj)
// Find the constructor of the owning prototype, which points to the constructor of the current object
let cloneObj = new obj.constructor()
cache.set(obj, cloneObj) // Cache the copied object to handle cyclic references
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
cloneObj[key] = deepClone(obj[key], cache) // Recursive copy}}return cloneObj
}
Copy the code
5. String space processing
function trim(str, type = 1) {
/* Remove Spaces type 1- all Spaces 2- Front and rear Spaces 3- Front Spaces 4- Rear Spaces Default to 1 */
if(type && type ! = =1&& type ! = =2&& type ! = =3&& type ! = =4) return;
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:
returnstr; }}Copy the code
6. Check your bank card number
const checkBankNumber = function(bankno) {
var lastNum = bankno.substr(bankno.length - 1.1); // Get the last bit (compare with luhm)
var first15Num = bankno.substr(0, bankno.length - 1); // The first 15 or 18
var newArr = [];
for (var i = first15Num.length - 1; i > -1; i--) { // Store the first 15 or 18 bits in reverse order
newArr.push(first15Num.substr(i, 1));
}
var arrJiShu = []; // The product of odd digits *2 <9
var arrJiShu2 = []; // The product of odd digits *2 >9
var arrOuShu = []; // Even bit array
for (var j = 0; j < newArr.length; j++) {
if ((j + 1) % 2= =1) { / / an odd number
if (parseInt(newArr[j]) * 2 < 9)
arrJiShu.push(parseInt(newArr[j]) * 2);
else
arrJiShu2.push(parseInt(newArr[j]) * 2);
} else / / the coded
arrOuShu.push(newArr[j]);
}
var jishu_child1 = []; // The number of odd digits *2 >9 after the split array
var jishu_child2 = []; // The number of odd digits *2 >9 after the split array ten digits
for (var h = 0; h < arrJiShu2.length; h++) {
jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
}
var sumJiShu = 0; // The sum of the array with odd digits *2 < 9
var sumOuShu = 0; // The sum of even digit arrays
var sumJiShuChild1 = 0; // The sum of the units digits of the array divided by the odd digits *2 >9
var sumJiShuChild2 = 0; // The sum of the ten digits of the array divided by the odd digits *2 >9
var sumTotal = 0;
for (var m = 0; m < arrJiShu.length; m++) {
sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
}
for (var n = 0; n < arrOuShu.length; n++) {
sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
}
for (var p = 0; p < jishu_child1.length; p++) {
sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
}
// Calculate the sum
sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);
// Calculate Luhm
var k = parseInt(sumTotal) % 10= =0 ? 10 : parseInt(sumTotal) % 10;
var luhm = 10 - k;
if (lastNum == luhm) {
return true;
} else {
return false; }}Copy the code
7. Check YOUR ID number
const checkIdCard = function(str) {
var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
return reg.test(str);
}
Copy the code
8. Check your phone number
const checkPhone = function(val) {
return (/^1[3-9]\d{9}$/.test(val))
}
Copy the code
9. Anti-shake (last time only)
function debounce(fn, delay) {
let timer
return function(. args) {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() = > {
fn.apply(this, args)
}, delay)
}
}
Copy the code
10. Throttling (first time only)
function throttle(fn, delay) {
let last = 0 // Last trigger time
return (. args) = > {
const now = Date.now()
if (now - last > delay) {
last = now
fn.apply(this, args)
}
}
}
Copy the code