“This is the 23rd day of my participation in the First Challenge 2022. For details: First Challenge 2022”
Hello, everyone, I touch fish small public, the real strong, will not complain, if you do not want to be looked down upon by others, you have to pay more than others ten times a hundred times more effort, to stand higher than others. The previous article was about uni-App to implement forms use of wechat applets; Today I’m going to share a few common JS wrappers used in projects.
Date format conversion (timestamp conversion)
Dates and times are often displayed on the page, which is the longest we use when working on a project.
const formatTime = stamps => {
let _date = new Date(stamps * 1000);
let Y = _date.getFullYear();
let M = (_date.getMonth() + 1) < 10 ? '0' + (_date.getMonth() + 1) : (_date.getMonth() + 1);
let D = _date.getDate() < 10 ? '0' + _date.getDate() : _date.getDate();
let h = _date.getHours() < 10 ? '0' + _date.getHours() : _date.getHours();
let m = _date.getMinutes() < 10 ? '0' + _date.getMinutes() : _date.getMinutes();
let s = _date.getSeconds();
return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s;
}
Copy the code
Deep copy
Like this deep copy, I usually submit a lot of form data. When submitting data, the value of the form object in data may be changed, which may affect the display of page data or the submission of the next form. The source parameter is to pass the data that needs to be deep-copied.
export function deepCopy(source) { if (typeof source === 'object') { const sourceCopy = source instanceof Array ? [] : {} for (const item in source) { if (! source[item]) { sourceCopy[item] = source[item] } else { sourceCopy[item] = typeof source[item] === 'object' ? deepCopy(source[item]) : source[item] } } return sourceCopy } return source }Copy the code
The countdown
Countdown is also a function we often use, here directly encapsulated, it is not too convenient, here the time parameter pass time stamp seconds.
export function formatTimeFunc(time) { let h = parseInt((time / 60 / 60) % 24) let m = parseInt((time / 60) % 60) let s = parseInt(time % 60) h = h > 9 ? h : '0' + h m = m > 9 ? m : '0' + m s = s > 9 ? s : If '0' + s (time < = 0) {return} '0 seconds' if (h = = 0 && m = = 0) {return ` `} ${s} seconds else if (h = = 0) {return ` $${s} {m} points seconds `} Else {return '${h}' ${m} minutes ${s} seconds'}}Copy the code
Method of getting URL parameters
Like this method, I usually use it on H5 pages. Remember that this URL is only used when an Android or IOS app carries parameters to jump to H5 pages.
function getParams(url) { const res = {}; if (url.includes("?" )) { const str = url.split("?" ) [1]; const arr = str.split("&"); arr.forEach((item) => { const key = item.split("=")[0]; const val = item.split("=")[1]; res[key] = decodeURIComponent(val); / / decoding}); } return res; } // apply const user= getParams("http://www.baidu.com?user=%E9%98%BF%E9%A3%9E&age=16"); console.log(user, "params"); // {user: 'a ', age: '16'} 'params'Copy the code
A method to determine whether a variable is empty
This is also common in projects, because it’s safe to check if it’s empty. The source argument is passed in the variable that needs to be judged null.
export function isEmpty(source) {
if (typeof source === "undefined") return true
if (typeof source === "string") return source.trim(" ").length === 0
if (source === null) return true
if (isArray(source) && source.length === 0) return true
if (isObject(source) && Object.keys(source).length === 0) return true
return false
}
Copy the code
Conclusion:
These methods are extracted from my project to share, well, this is the end of the article, welcome everyone (like + comment + attention) have a question can communicate with each other. I hope this article is helpful to you and I hope you can support me more. Today is the 23rd day of my participation in the first Wen Challenge 2022. Come on! Persistence is victory.