/** * #### Cloning Options * Cloning options of the [clone]{@link clone} function */ export interface ICloneOptions { /** * #### Instance constructors which cloning unwanted * Give the instance constructors to prevent them being cloned * * * * * * Example: * ```typescript * import { clone } from "@thalesrc/js-utils/object"; * * const object = { * width: 250, * element: document.querySelector("div") * }; * * const clonedObject = clone(object, {instancesToRefer: [ HTMLElement ]}); * // clonedObject.element === object.element * ``` * * * * * @default [] */ instancesToRefer?: any[]; /** * #### Value filterer to pass same references * Being called for each value while cloning. Return true to prevent cloning for the incoming value and pass the same reference instead. * * * * * * Example: * ```typescript * import { clone } from "@thalesrc/js-utils/object"; * * const john = { age: 20, willBeReferenced: true }; * const jane = { age: 25, willBeReferenced: false }; * * const array = [john, jane]; * * const clonedArray = clone(array, {valueFiltererToRefer: person => person.willBeReferenced }); * * // clonedArray[0] === array[0] * // clonedArray[1] !== array[1] * ``` * * * * * @param value incoming value to be cloned * @return Return true to pass same reference, false to clone * @default () => false */ valueFiltererToRefer?(value: any): boolean; /** * #### Properties which cloning unwanted * Give the unwanted properties to prevent them being cloned * * * * * * Example: * ```typescript * import { clone } from "@thalesrc/js-utils/object"; * * const object = { * width: 250, * element: document.querySelector("div") * }; * * const clonedObject = clone(object, {propsToRefer: [ "element" ]}); * // clonedObject.element === object.element * ``` * * * * * @default [] */ propsToRefer?: Array; /** * #### Custom Cloner Map * * * * * * Example: * ```typescript * import { clone } from "@thalesrc/js-utils/object"; * * const object = { * width: 250, * element: document.querySelector("div") * }; * * function divCloner(objectToClone, options, internalData) { * const newElement = document.createElement("div"); * newElement.classList.add("cloned"); * return newElement; * } * * const customCloners = new Map(); * customCloners.set(HTMLDivElement, divCloner); * * const clonedObject = clone(object); * ``` * * * * */ customCloners?: Map; } /** * #### Internal Data For Cloning Process */ export interface IClonerInternalData { clonedObjects: Map; parent?: Record; key?: number | string | symbol; finalizers: Array<() => void>; } export type PolyfillRequired = { [P in TNames]: (T & { [name: string]: never; })[P]; }; /** * Cloning options with all properties required * @see ICloneOptions */ export type TNonOptionalCloneOptions = PolyfillRequired; /** * Custom Cloner Function */ export type TInstanceCloner = (objectToClone: T, options: TNonOptionalCloneOptions, internalData: IClonerInternalData) => T; /** * #### Deep Clone * * A function to recursively clone objects, arrays etc. with [cloning options]{@link ICloneOptions} * * *References Functions & Symbols by default* * * * * * * Example: * ```typescript * import { clone } from "@thalesrc/js-utils/object"; * * const object = {a: 1, b: {c: true, d: ["x", "y"]}}; * * // Clone all * const clonedObject = clone(object); * // {a: 1, b: {c: true, d: ["x", "y"]}} * // object.b.d === clonedObject.b.d // false * * // Clone all but reference "d" * const clonedObject = clone(object, {propsToRefer: ["d"]}); * // {a: 1, b: {c: true, d: ["x", "y"]}} * // object.b.d === clonedObject.b.d // true * ``` * * Static usage example: * ```typescript * import "@thalesrc/js-utils/object/static/clone"; * * const object = {a: 1, b: 2}; * const clonedObject = Object.clone(object); // {a: 1, b: 2} * ``` * * * * * @param objectToClone Object to clone * @param options {ICloneOptions} Cloning Options * @see {@link ICloneOptions} for more information. */ export declare function clone(objectToClone: T, { instancesToRefer, propsToRefer, valueFiltererToRefer, customCloners }?: ICloneOptions): T;