typescript-immutable-helper
Version:
Helpers for handling immutable objects with typescript
110 lines (109 loc) • 3.88 kB
TypeScript
/**
* Class that helps to replaceValueOf a new object by encapsulating a deep copy of the source object
* If input object is frozen by (@link Object.freeze()} or {@link deepFreeze} then the replica will be produced frozen
* freeze in --> deep freeze out
* Warns if source object is just frozen, not deep frozen
**/
export declare class ReplicationBuilder<T> {
private replica;
private readonly freeze;
/**
* default constructor
* @param {RT} sourceObject traversing object
*/
private constructor();
static forObject<T>(sourceObject: T): ReplicationBuilder<T>;
/**
* @deprecated since 0.4.1
* use property instead
*/
getChild<K extends keyof T>(childNode: K): ReplicaChildOperator<T, T[K]>;
/** switch to child node
* @param {K} childNode of the root node
* @returns {ReplicaChildOperator<T, T[K]>} operator of child node
**/
property<K extends keyof T>(childNode: K): ReplicaChildOperator<T, T[K]>;
/**
* @deprecated since 0.4.1
* use replaceValueOf instead
*/
modify<K extends keyof T>(childNode: K): PropertyModifier<ReplicationBuilder<T>, T[K]>;
replaceValueOf<K extends keyof T>(childNode: K): PropertyModifier<ReplicationBuilder<T>, T[K]>;
/**
* @deprecated since 0.4.1
* use removeProperty instead
*/
delete<K extends keyof T>(childNode: K): ReplicationBuilder<T>;
removeProperty<K extends keyof T>(childNode: K): ReplicationBuilder<T>;
/**
* produces the replica
* @returns {T} replica
*/
build(): T;
}
/**
* Operator for nodes of the replica
*/
export declare class ReplicaChildOperator<RT, T> {
private readonly buildFunction;
private readonly node;
private readonly replica;
private readonly relativePath;
constructor(buildFunction: () => RT, replica: RT, node: T, relativePath: string | number | symbol);
/**
* @deprecated since 0.4.1
* use property instead
*/
getChild<K extends keyof T>(childNode: K): ReplicaChildOperator<RT, T[K]>;
/** switch to child node
* @param {K} childNode of this node
* @returns {ReplicaChildOperator<RT, N[K]>} traversable child node
**/
property<K extends keyof T>(childNode: K): ReplicaChildOperator<RT, T[K]>;
/**
* @deprecated since 0.4.1
* use replaceValueOf instead
*/
modify<K extends keyof T>(childNode: K): PropertyModifier<ReplicaChildOperator<RT, T>, T[K]>;
replaceValueOf<K extends keyof T>(childNode: K): PropertyModifier<ReplicaChildOperator<RT, T>, T[K]>;
/**
* @deprecated since 0.4.1
* use removeProperty instead
*/
delete<K extends keyof T>(childNode: K): ReplicaChildOperator<RT, T>;
removeProperty<K extends keyof T>(childNode: K): ReplicaChildOperator<RT, T>;
/**
* produces the replica
* @returns {RT} replica
*/
build(): RT;
}
export declare class PropertyModifier<PT, VT> {
private readonly replica;
private readonly parent;
private readonly relativePathToRoot;
constructor(parent: PT, relativePathToRoot: string | number | symbol, rootObject: any);
/**
* @deprecated since 0.4.1
* use with instead
*/
to(value: VT): PT;
/**
* set the value of the property
* @param {VT} value
* @returns {PT}
*/
with(value: VT): PT;
/**
* sets the value of the property by a function
* @param setFunction:(VT) => T injected function that creates the new value by knowing the old value (e.g. (oldValueArray) => [...oldValueArray, appendedValue]
* @returns PT this
*/
by(setFunction: (VT: any) => VT): PT;
/**
* applies a function on this property
* @param {(VT) => void} executeOnCloneFunction function that is executed
* @returns {PT}
*/
withCloneAndDo(executeOnCloneFunction: (VT: any) => void): PT;
}