1.毫秒转时间
(如:3600000=>{days:0, hours:1, minutes:0, seconds:0});
例子:109380000=>{days: 1, hours: 6, minutes: 23, seconds: 0}。
const formatDuring = (time) => {
const days = parseInt(time / (1000 * 60 * 60 * 24))
const hours = parseInt((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
const minutes = parseInt((time % (1000 * 60 * 60)) / (1000 * 60))
const seconds = Math.floor((time % (1000 * 60)) / 1000)
return {days, hours, minutes, seconds}
}
2.数组对象过滤重复的key数据
例子:const arr=[{key:1,value:11},{key:2,value:22},{key:1,value:33}]
=>
[{"key": 1,"value": 11},{"key": 2,"value": 22}]
export const nonredundantArr = (arr,key) => {
const m = new Map();
return arr.filter((ele) => !m.has(ele[key]) && m.set(ele[key], ""));
}
3.防抖
例子:const formatShops = (val=””) => {
shopRun({pageNum: 1, pageSize: 300, shopNAme: val}).then((res) => {
setShopList(res.data.list)
}).catch((err) => {});
};
let debounceAjaxShop = debounce(formatShops, 500);
function debounce(fun, delay) {
return function (args) {
let that = this;
let _args = args;
clearTimeout(fun.id);
fun.id = setTimeout(function () {
fun.call(that, _args);
}, delay);
};
}
4.URL构建
const build = (url, params) => {
// URL构建方法
const ps = [];
if (params) {
for (let p in params) {
if (p) {
ps.push(p + "=" + encodeURIComponent(params[p]));
}
}
}
return url + "?" + ps.join("&");
}
5.表格单元格的Tooltip
const tipShowLine = (text,width=”110px”,line=”2”) => (
<div
style={{
width,
display: '-webkit-box',
overflow: 'hidden',
whiteSpace: 'normal !important',
textOverflow: 'ellipsis',
wordWrap: 'break-word',
webkitLineClamp: line,
webkitBoxOrient: 'vertical',
}}
>
<Tooltip placement="topLeft" title={text}>
{text ? <span>{text}</span> : '-'}
</Tooltip>
</div>
);
6.页面跳转带参history(目前在saas用)
// A code block
var foo = 'bar';
import { history } from 'umi';
export const navTo = (path: string, query: any) => {
history.push({
pathname: path,
query,
});
};
还会持续添加ing。。。
觉得还可以就点个赞吧👍