深拷贝
js
export const deepclone = data => {
let type = Object.prototype.toString.call(data).slice(8, -1)
let arr = []
let obj = {}
if (type === 'Object') {
Object.keys(data).forEach(item => {
obj[item] = deepclone(data[item])
})
return obj
} else if (type === 'Array') {
data.forEach(item => {
arr.push(deepclone(item))
})
return arr
} else {
return data
}
}
TS
export const deepclone = <T>(data: any): T => {
let type = Object.prototype.toString.call(data).slice(8, -1)
let arr: [] = []
let obj:any = {}
if (type === 'Object') {
Object.keys(data).forEach((item:string) => {
obj[item] = deepclone(data[item] as object)
})
return obj as T
} else if (type === 'Array') {
(data as []).forEach(item => {
arr.push(deepclone(item))
})
return arr as T
} else {
return data as T
}
}
判断数据类型
js
export const getDataType = data => Object.prototype.toString.call(data).slice(8, -1)
TS
export const getDataType = (data: any) => Object.prototype.toString.call(data).slice(8, -1)
节流 一段时间内只执行一次
js
export const throttle = (fn, time = 1000) => {
let timer = true
return function () {
if (timer) {
timer = false
fn(...arguments)
setTimeout(() => {
timer = true
}, time);
}
}
}
TS
export const throttle = (fn: Function, time: number = 1000) => {
let timer = true
return function () {
if (timer) {
timer = false
fn(...arguments)
setTimeout(() => {
timer = true
}, time);
}
}
}
防抖
js
export function debounce(fn, time) {
let timer = null;
return function () {
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, arguments);
}, time);
};
}
TS
export function debounce(fn: Function, time: number = 1000) {
let timer: number;
return function () {
clearTimeout(timer);
timer = window.setTimeout(() => {
// fn.apply(this, arguments);
fn(...arguments)
}, time);
};
}
判断对象上是否存在某属性
js
export const includeAttribute = (obj, tar) => {
if (Object.prototype.toString.call(obj).slice(8, -1) != 'Object') return obj + '不是对象'
if (!tar) return '未找到要判断的目标'
for (let i = 0; i < Object.keys(obj).length; i++) {
let ele = Object.keys(obj)[i]
if (Object.prototype.toString.call(obj[ele]).slice(8, -1) === 'Object') {
if (includeAttribute(obj[ele], tar)) {
return true
}
}
if (ele === tar) {
return true
}
}
return false
}
TS
export const includeAttribute = (obj:any, tar:string) => {
if (Object.prototype.toString.call(obj).slice(8, -1) != 'Object') return obj + '不是对象'
if (!tar) return '未找到要判断的目标'
for (let i = 0; i < Object.keys(obj).length; i++) {
let ele = Object.keys(obj)[i]
if (Object.prototype.toString.call(obj[ele]).slice(8, -1) === 'Object') {
if (includeAttribute(obj[ele], tar)) {
return true
}
}
if (ele === tar) {
return true
}
}
return false
}
时间处理
js
export const handleTime = (date, option = 'all', type = '/') => {
let time
if (!date) {
time = new Date(Date.now())
} else {
time = new Date(date)
}
let year = time.getFullYear()
let month = time.getMonth() + 1
let day = time.getDate()
let week = time.getDay()
let hour = time.getHours(); // 时
let minutes = time.getMinutes(); // 分
let seconds = time.getSeconds(); //秒
if (option.includes('year')) {
return year
} else if (option.includes('month')) {
return month
} else if (option.includes('day')) {
return day
} else if (option.includes('week')) {
return week
} else if (option == "all") {
let time_str;
time_str = year + type;
month <= 9
? (time_str += "0" + month + type)
: (time_str += month + type);
day <= 9
? (time_str += "0" + day)
: (time_str += day);
return time_str;
} else if (option == "timeToSeconds") {
let time_str;
time_str = year + type;
month <= 9 ? (time_str += "0" + month + type) : (time_str += month + type);
day <= 9 ? (time_str += "0" + day + " ") : (time_str +=day + " ");
hour <= 9 ? (time_str += "0" + hour + ":") : (time_str += hour + ":");
minutes <= 9 ? (time_str += "0" + minutes + ":") : (time_str += minutes + ":");
seconds <= 9 ? (time_str += "0" + seconds) : (time_str += seconds);
return time_str;
} else {
return "未知类型";
}
}
TS
export const handleTime = (
date = Date.now(),
option = "all",
type: string = "/"
): number | string => {
let time = new Date(date);
let year = time.getFullYear();
let month = time.getMonth() + 1;
let day = time.getDate();
let week = time.getDay();
let hour = time.getHours(); // 时
let minutes = time.getMinutes(); // 分
let seconds = time.getSeconds(); //秒
if (option.includes("year")) {
return year;
} else if (option.includes("month")) {
return month;
} else if (option.includes("day")) {
return day;
} else if (option.includes("week")) {
return week;
} else if (option == "all") {
let time_str;
time_str = year + type;
month <= 9
? (time_str += "0" + month + type)
: (time_str += month + type);
day <= 9
? (time_str += "0" + day)
: (time_str += day);
return time_str;
} else if (option == "timeToSeconds") {
let time_str;
time_str = year + type;
month <= 9 ? (time_str += "0" + month + type) : (time_str += month + type);
day <= 9 ? (time_str += "0" + day + " ") : (time_str +=day + " ");
hour <= 9 ? (time_str += "0" + hour + ":") : (time_str += hour + ":");
minutes <= 9 ? (time_str += "0" + minutes + ":") : (time_str += minutes + ":");
seconds <= 9 ? (time_str += "0" + seconds) : (time_str += seconds);
return time_str;
} else {
return "未知类型";
}
};
};```