deep-tls
Version:
deep-tls 提供了一些对数据进行深度操作的工具,如:深度相等测试、深度遍历、深拷贝等;其中,深拷贝deepCopy可对任意数据进行深度拷贝,包括 函数 function、正则 RegExp、Map、Set、Date、Array、URL 等等;支持含循环引用对象的拷贝,并且不会丢失成员的引用关系 和 类型信息,支持扩展,可根据数据类型定制拷贝逻辑,也可指定拷贝深度;所以,通过它可实现对任意类型的数据进行任意想要的拷贝
69 lines (68 loc) • 6.37 kB
TypeScript
import type { OptionalBoolean } from "type-tls";
/**
* 循环遍历的回调函数
* (key,value,obj,currDepth))=> stopInfo : any
* @param key : string 当前被遍历的属性名;
* @param value : any 当前被遍历的属性值;
* @param obj : any 当前被遍历的属性所属的对象;
* @param currDepth : number 当前遍历的深度值,从 startDepth 所表示的值开始计数;
* @returns stopInfo : any 表示是否中止循环,并且该值会被 deepLoopOwnProperty 函数返回,如果返回的值是真值,则终止循环;
*/
export declare type DepthLoopPropertyCallback<ThisVal> = (this: ThisVal, key: string, value: any, obj: any, currDepth: number) => any;
/**
* deepLoop的公共选项
* @property maxDepth?:number|null|undefined; // 可选;默认值为:Infinity,即无限深度;要循环遍历的最大深度;当值为 undefined 或 null 时,会使用默认值,表示无限深度;被循环遍历的值本身的深度为 0 ,被循环遍历值的成员的深度为 1 ,依次类推;
* @property thisValue?: ThisVal; // 可选; callback 回调函数的this值 ;默认值:当前被遍历的属性所属的对象;
*/
export interface DeepLoopOptions<ThisVal = any> {
maxDepth?: number | null | undefined;
thisValue?: ThisVal;
}
/**
* deepLoopOwnPropertyOptions的选项
* @property allOwnProps?: OptionalBoolean; // 可选;默认值:undefined; 是否要遍历自身所有的属性,包不可枚举的,但不包括原型链上的属性;true:遍历对象自身(不包括原型上的)的所有属性(包括不可枚举的); false : 只遍历对象自身中(不包括原型上的)可枚举的属性
*/
export interface DeepLoopOwnPropertyOptions<ThisVal = any> extends DeepLoopOptions<ThisVal> {
allOwnProps?: OptionalBoolean;
}
/**
* 递归遍历自身属性链中的所有属性(不包含原型上的属性)
* @param target : object 必选; 被遍历的目标对象
* @param callback : (key,value,obj,currDepth))=> stopInfo : any 必选; 循环遍历的回调函数; key : 当前被遍历的属性名;value : 当前被遍历的属性值;obj : 当前被遍历的属性所属的对象;currDepth : 当前遍历的深度值,从 startDepth 所表示的值开始计数;返回值 stopInfo : 表示是否中止循环,并且该值会被 deepLoopOwnProperty 函数返回,如果返回的值是真值,则终止循环;
* @param options ?:DeepLoopOwnPropertyOptions<ThisVal>|null|undefined 可选;选项对象;可配置的属性如下:
* @property maxDepth?:number|null|undefined; // 可选;默认值为:Infinity,即无限深度;要循环遍历的最大深度;当值为 undefined 或 null 时,会使用默认值,表示无限深度;被循环遍历的值本身的深度为 0 ,被循环遍历值的成员的深度为 1 ,依次类推;
* @property thisValue?: ThisVal; // 可选; callback 回调函数的this值 ;默认值:当前被遍历的属性所属的对象;
* @property allOwnProps?: OptionalBoolean; // 可选;默认值:undefined; 是否要遍历自身所有的属性,包不可枚举的,但不包括原型链上的属性;true:遍历对象自身(不包括原型上的)的所有属性(包括不可枚举的); false : 只遍历对象自身中(不包括原型上的)可枚举的属性
*
* @returns stopInfo : any 终止循环时返回的信息;
*/
export declare function deepLoopOwnProperty<Target extends object>(target: Target, callback: DepthLoopPropertyCallback<Target>, options?: {
maxDepth?: number | null | undefined;
allOwnProps?: OptionalBoolean;
thisValue?: undefined;
} | null | undefined): any;
export declare function deepLoopOwnProperty<Target extends object, ThisVal extends Exclude<any, undefined>>(target: Target, callback: DepthLoopPropertyCallback<ThisVal>, options: {
maxDepth?: number | null | undefined;
allOwnProps?: OptionalBoolean;
thisValue: ThisVal;
} | null | undefined): any;
export declare function deepLoopOwnProperty<Target extends object, ThisVal>(target: Target, callback: DepthLoopPropertyCallback<ThisVal extends undefined ? Target : ThisVal>, options?: DeepLoopOwnPropertyOptions<ThisVal> | null | undefined): any;
/**
* 递归遍历自身包括原型的属性链中的所有可枚举的属性
* @param target : object 必选; 被遍历的目标对象
* @param callback : (key,value,obj,currDepth))=> stopInfo : any 必选; 循环遍历的回调函数; key : 当前被遍历的属性名;value : 当前被遍历的属性值;obj : 当前被遍历的属性所属的对象;currDepth : 当前遍历的深度值,从 startDepth 所表示的值开始计数;返回值 stopInfo : 表示是否中止循环,并且该值会被 deepLoopOwnProperty 函数返回,如果返回的值是真值,则终止循环;
* @param options ?:DeepLoopOptions<ThisVal>|null|undefined 可选;选项对象;可配置的属性如下:
* @property maxDepth?:number|null|undefined; // 可选;默认值为:Infinity,即无限深度;要循环遍历的最大深度;当值为 undefined 或 null 时,会使用默认值,表示无限深度;被循环遍历的值本身的深度为 0 ,被循环遍历值的成员的深度为 1 ,依次类推;
* @property thisValue?: ThisVal; // 可选; callback 回调函数的this值 ;默认值:当前被遍历的属性所属的对象;
*
* @returns stopInfo : any 终止循环时返回的信息;
*/
export declare function deepLoopPropertyWithPrototype<Target extends object>(target: Target, callback: DepthLoopPropertyCallback<Target>, options?: {
maxDepth?: number | null | undefined;
thisValue?: undefined;
} | null | undefined): any;
export declare function deepLoopPropertyWithPrototype<Target extends object, ThisVal extends Exclude<any, undefined>>(target: Target, callback: DepthLoopPropertyCallback<ThisVal>, options: {
maxDepth?: number | null | undefined;
thisValue: ThisVal;
} | null | undefined): any;
export declare function deepLoopPropertyWithPrototype<Target extends object, ThisVal>(target: Target, callback: DepthLoopPropertyCallback<ThisVal extends undefined ? Target : ThisVal>, options?: DeepLoopOptions<ThisVal> | null | undefined): any;