/* eslint-disable @typescript-eslint/ban-types */ /* eslint-disable spaced-comment */ /* eslint-disable @typescript-eslint/no-unsafe-argument */ /* eslint-disable @typescript-eslint/no-this-alias */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-return */ /* eslint-disable @typescript-eslint/ban-ts-comment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ import { Obj } from 'src/type'; export function isFunction(val: any) { return val instanceof Function; } export function isPlainObject(o: any) { return Object.prototype.toString.call(o) === '[object Object]'; } export function isString(o: any) { return Object.prototype.toString.call(o) === '[object String]'; } export function getValueByPath(source: Obj, path: string[], defaultValue: any) { return path.reduce((ret, nextKey) => ret?.[nextKey] ?? defaultValue, source); } export const isDef = (val: any) => { return val !== undefined && val !== null; }; export const noop = (val: any) => val; /** * debounce * @param {*} fn * @param {*} ms */ export function debounce(func: Function, wait: number, immediate: boolean) { let timeout: any; let result: any; const debounced = function () { //@ts-ignore const context = this; const args = arguments; if (timeout) clearTimeout(timeout); if (immediate) { // 如果已经执行过,不再执行 const callNow = !timeout; timeout = setTimeout(() => { timeout = null; }, wait); if (callNow) result = func.apply(context, args); } else { timeout = setTimeout(() => { func.apply(context, args); }, wait); } return result; }; debounced.cancel = function () { //@ts-ignore clearTimeout(timeout); timeout = null; }; return debounced; }