JavaScript工具函数,封装的一些常用的js函数
软件架构说明
- pnpm i
- pnpm build
- pnpm build(构建)
- pnpm debug(调试源码)
- Fork 本仓库
- 新建 Feat_xxx 分支
- 提交代码
- 新建 Pull Request
接口:
export type IKey = string | symbol | number
export interface IObject { [key: IKey]: T | IObject }
export interface IPromise extends IObject { promise: Promise resolve: (res: any) => unknown reject: (err: any) => unknown }
export type IInstance = { _instance: Function } & T
export type IDemoteArray = Array<IDemoteArray | T>
/**产生区间随机数
- @param {number} min 最小区间
- @param {number} max 最大区间
- @param {boolean} bool 包含最大值
- @return {number} 随机数 **/
export type IRandomNum = (min: number, max: number, bool?: boolean) => number
/**获取url的参数
- @param {string} url 待截取的地址
- @return {object} 参数对象 **/
export type IUrlSplit = (url: string) => IObject
/**添加url的参数
- @param {string} url 待添加参数的地址
- @param {object} query 待添加的参数
- @return {string} 添加参数后的url **/
export type IUrlJoin = (url: string, query: object) => string
/**获取数据类型
- @param {any} data 待检测数据
- @return {string} 数据类型 **/
export type IGetType = (data: any) => typeof data | T[keyof T] | "null";
/**批量判断数据类型
- @param {any} data 待检测数据
- @param {any} whiteList 数据类型名单
- @return {boolean} 是否在白名单中 **/
export type IGetTypeByList = (data: any, whiteList: string[]) => boolean;
/**lodash中的 _.get(),获取对象某级属性
- @param {IObject} object 目标对象
- @param {string} key 对象层级
- @param {any} defaultValue 未取得时默认值
- @return {IObject[IKey]} 对象某个属性 **/
export type IGetValue = <T, U = IObject | IObject[IKey]>(object: U, key: string, defaultValue?: any) => U
/**lodash中的 _.set(),赋值对象某级属性
- @param {IObject} object 目标对象
- @param {string} key 对象层级
- @param {any} value 需要赋的值
- @return {IObject} 目标对象 **/
export type ISetValue = (object: IObject, key: string, value?: any) => IObject
/**对象混入
- @param {IObject} target 目标对象
- @param {string} source 需要混入的对象集合
- @param {boolean} overwrite 是否重写覆盖原有属性
- @return {IObject} 目标对象 **/
export type IMixIn = <U extends IObject>(target?: U, source?: IObject, overwrite?: boolean) => U
/**枚举值反向映射
- @param {IObject} target 目标对象
- @return {IObject} 目标对象 **/
export type IEnumInversion = (target: IObject) => IObject
/**对象复制
- @param {IObject} target 目标对象
- @return {IObject} 目标对象 **/
export type ICloneDeep = (target?: any) => any
/**生成 对象 类型的初始值
- @param {string} type 数据类型
- @param {any} __init 初始值
- @return {any} 目标对象 **/
export type ICreateObjectVariable = (type: string, source?: any) => any
/**Object.create 根据源对象产出新对象
- @param {Function|Object} source 源对象
- @return {Function|Object} 对象产物 **/
export type ICreateObject = <T, U extends T>(source: T) => U
/**类的继承
- @param {Function} source 源对象
- @return {Function} 继承产物 **/
export type IInherit = (source: T, target?: Function) => Function
/**生成类的实例单例
- @param {Function} classProto 类
- @param {Boolean} overwrite 是否覆盖已有单例
- @param {any[]} params 构造函数的参数
- @return {IObject} 实例化的单例 **/
export type IGetInstance = (classProto: IInstance, overwrite?: boolean, ...params: any[]) => Function
/**通过装饰器将属性混入类中
- @param {IObject} params 混入的属性
- @return {ClassDecorator} 装饰器钩子函数 **/
export type IClassDecorator = (params: IObject) => (target: TFunction) => void
/**JSON.parse封装
- @param {string} target 字符串
- @return {IObject} 对象 **/
export type IStringToJson = (target: string) => IObject
/**JSON.stringify封装
- @param {IObject} target 对象
- @return {string} 字符串 **/
export type IJsonToString = (target: IObject) => string
/**节流(throttle):高频事件触发,但在 n 秒内只会执行一次
- @param {Function} fn 节流处理的函数
- @param {number} time 执行间隔/毫秒
- @return {Function} 处理后的函数 **/
export type IThrottle = (fn: Function, time: number) => (...args: any[]) => void
/**防抖(debounce):触发高频事件后 n 秒内函数只会执行一次
- @param {Function} fn 防抖处理的函数
- @param {number} time 允许运行函数间隔/毫秒
- @return {Function} 处理后的函数 **/
export type IDebounce = (fn: Function, time: number) => (...args: any[]) => void
/**
- Promise扁平化,避免Promise嵌套
- @returns {Promise,resolve,reject} */
export type IDefer = () => IPromise
/**await与try catch 捕获异常处理方法
- @param {Promise} defer 延迟函数
- @returns {Promise} [error, result] */
export type ICatchAwait<T extends Promise> = (defer: T) => T
/**数组乱序
- @param {Array} arr 目标数组
- @returns {Array} 乱序后的数组 */
export type IArrayRandom<T extends any[]> = (arr: T) => T
/**数组数组去重
- @param {Array} arr 目标数组
- @returns {Array} 去重后的数组 */
export type IArrayUniq<T extends any[]> = (arr: T) => T
/**数组扁平化
- @param {Array} arr 目标数组
- @returns {Array} 扁平化的数组 */
export type IArrayDemote<T extends IDemoteArray> = (arr: T, result?: T) => T
/**IElementParams
- @param {string} ele 标签类型
- @param {CSSStyleDeclaration} style 样式
- @param {Attr} attr 属性
- @param {object} parent 父元素 */
interface IElementParams { ele: T | string style: CSSStyleDeclaration attr: Attr parent: T }
/**新增标签,设置属性及样式
- @param {IElementParams} params 配置
- @return {ElementObject} 生成的标签 */
export type ICreateElement<T = HTMLElement> = (params: IElementParams) => T
/**浏览器事件
- @param {Document} ele 标签
- @param {string} type 事件类型
- @param {(e: Event) => void} handler 事件回调
- @return {void} */
export type IAddHandler = (ele: T, type: string, handler: (e: Event) => void) => void
/**取消事件冒泡
- @param {Event} e 浏览器事件对象
- @return {void} */
export type IStopBubble = (e: Event) => void
/**取消默认事件
- @param {Event} e 浏览器事件对象
- @return {void} */
export type IStopDefault = (e: Event) => void
/**取消浏览器事件
- @param {Document} ele 标签
- @param {string} type 事件类型
- @param {(e: Event) => void} handler 事件回调
- @return {void} */
export type IRemoveHandler = (ele: T, type: string, handler: (e: Event) => void) => void
/**取消默认事件
- @param {Event} e 浏览器事件对象
- @return {void} */
export type IDispatchEvent = (ele: T, data: any) => void
export type IRequestParams = T | IObject | null
// 请求路径
export type IUrl = string
// 环境判断
export type IEnv = 'Window' | 'Node'
// fetch返回取值方式
export type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer"
// 请求方式
export type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION"
// body结构
export type IRequestBody = IRequestParams
// heads结构
export type IRequestHeaders = IRequestParams
// 请求基础函数
export type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise
// 请求函数体
export type IRequestFn = (url?: IUrl, query?: IObject, body?: IRequestBody, opts?: IRequestOptions) => Promise
// 请求参数
export type IRequestOptions = { method?: IRequestMethods query?: IRequestParams<IObject> body?: IRequestBody headers?: IRequestHeaders controller?: AbortController timeout?: number timer?: number | unknown | null [key: string]: any }
// 拦截器
export type IInterceptors = { use(type: "request" | "response" | "error", fn: Function): void get reqFn(): Function get resFn(): Function get errFn(): Function }
// 公共函数
export type IRequestBase = { readonly origin: string chackUrl: (url: IUrl) => boolean envDesc: () => IEnv errorFn: <Err = any, R = Function>(reject: R) => (err: Err) => R clearTimer: (opts: IRequestOptions) => void initAbort: <T = IRequestOptions>(opts: T) => T requestType: () => IRequestBaseFn fixOrigin: (fixStr: string) => string fetch: IRequestBaseFn http: IRequestBaseFn getDataByType: (type: IDataType, response: Response) => Promise }
// 初始化参数
export type IRequestInit = { initDefaultParams: (url: IUrl, opts: IRequestOptions) => any initFetchParams: (url: IUrl, opts: IRequestOptions) => any initHttpParams: (url: IUrl, opts: IRequestOptions) => any }
// 请求主体类
export type IRequest = { GET: IRequestFn POST: IRequestFn DELETE: IRequestFn PUT: IRequestFn OPTIONS: IRequestFn HEAD: IRequestFn PATCH: IRequestFn } & IRequestBase