jcommon
Version:
JavaScript 常用纯函数工具库
141 lines (140 loc) • 4.05 kB
TypeScript
/**
* @description: 获取嵌套数据,处理空值异常
* @author: wuxh
* @Date: 2020-05-06 12:13:59
* @param defaultResult 默认值
* @param args 属性访问路径
* @returns 目标值或默认值
* @example:
getV('', {name: {children: 123}}, 'name', 'children')
=> 123
*/
export declare const getV: <T>(defaultResult: T, ...args: any[]) => any;
/**
* @description: 深拷贝,克隆(只包含可遍历属性<常用>)
* @author: wuxh
* @Date: 2020-05-06 12:14:45
* @param {obj}
* @return: Object
* @example:
clone({name: 123})
=> {name: 123}
*/
export declare const cloneObj: (obj: any) => any;
/**
* @description: 简单的深拷贝
* @author: wuxh
* @Date: 2021-09-02 22:33:47
* @param {any} obj
* @return {any} obj
* @example:
const person={name:'xiaoming',child:{name:'Jack'}}
cloneJson(person) => {name:'xiaoming',child:{name:'Jack'}}
*/
export declare const cloneJson: (obj: any) => any;
/**
* @description: 深度合并对象(当前用于合并系统配置文件 app-data.json) 已存在的属性默认不覆盖
* @author: wuxh
* @Date: 2020-05-06 12:15:30
* @param {oldObj}
* @param {newObj}
* @param {keys} 强制覆盖属性的key组成的数组
* @return: Object
* @example:
mergeObj({name: 111}, {name:333, value: 222}, []) => {name: 111, value: 222}
mergeObj({name: 111}, {name:333, value: 222}, ['name']) => {name: 333, value: 222}
*/
export declare const mergeObj: (oldObj: {
[x: string]: any;
}, newObj: {
[x: string]: any;
}, keys: string | string[]) => {
[x: string]: any;
};
/**
* @description: 判断对象是否为空
* @author: wuxh
* @Date: 2021-08-21 23:08:42
* @param {string} obj
* @return {*} boolean
* @example: isEmptyObject({}) => true
*/
export declare const isEmptyObject: (obj: any) => boolean;
/**
* @description: cleanObject 去除对象中value为空(null,undefined,'')的属性
* @author: wuxh
* @Date: 2021-09-02 22:07:34
* @param {*} { [k: string]: any }
* @return {*} { [k: string]: any }
* @example:
cleanObject({
name: '',
pageSize: 10,
page: 1
}) => {
pageSize: 10,
page: 1
}
*/
export declare const cleanObject: (object: {
[k: string]: any;
}) => {
[k: string]: any;
};
/**
* @description: 深克隆 deepClone
* @author: wxingheng
* @Date: 2022-04-10 22:19:43
* @param {any} value
* @param {*} stack
* @return {*}
* @example: deepClone(obj) => new obj
*/
export declare const deepClone: (target: any) => any;
/**
* @description: 判断两个对象是否相等
* @author: wxingheng
* @Date: 2022-05-13 16:35:33
* @param {any} a
* @param {any} b
* @return {*}
* @example: isEqual({a: 1}, {a: 1}) => true; isEqual({a: 1}, {a: 2}) => false; isEqual({a: 1}, {b: 1}) => false
*/
export declare const isEqual: (a: any, b: any) => boolean;
/**
* @description: 将list转换为树结构
* @author: wxingheng
* @Date: 2022-09-30 11:37:32
* @return {*}
* @example: convertDataToTree(data) => treeData
*/
export declare const convertDataToTree: (data: any[], id?: string, pid?: string, children?: string) => any[];
/**
* @description: 将树结构转换为list
* @author: wxingheng
* @Date: 2022-09-30 11:40:43
* @param {any} tree 树结构
* @param {string} children 子节点字段
* @return {*}
* @example: convertTreeToList (treeData) => listData
*/
export declare const convertTreeToList: (tree: any[], children?: string) => any[];
/**
* @description: 数组的分类,根据某个字段分类,返回一个对象,key为字段值,value为数组
* @author: wxingheng
* @Date: 2022-09-30 11:53:38
* @param {any} arr
* @param {string} key
* @return {*}
* @example:
* const arr = [
* {type: 1, name: 'a'},
* {type: 2, name: 'b'},
* {type: 1, name: 'c'},
* {type: 2, name: 'd'},
* {type: 1, name: 'e'},
* {type: 2, name: 'f'},
* ]
* groupBy(arr, 'type') => {1: [{type: 1, name: 'a'}, {type: 1, name: 'c'}, {type: 1, name: 'e'}], 2: [{type: 2, name: 'b'}, {type: 2, name: 'd'}, {type: 2, name: 'f'}]}
*/
export declare const groupBy: (arr: any[], key: string) => any;