UNPKG

typescript-immutable-helper

Version:

Helpers for handling immutable objects with typescript

184 lines 6.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _cloneDeep = require("lodash.clonedeep"); var _set = require("lodash.set"); var _get = require("lodash.get"); var deepFreeze_1 = require("./deepFreeze"); /** * 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 **/ var ReplicationBuilder = /** @class */ (function () { /** * default constructor * @param {RT} sourceObject traversing object */ function ReplicationBuilder(sourceObject) { this.replica = null; this.replica = _cloneDeep(sourceObject); this.freeze = Object.isFrozen(sourceObject); if (this.freeze && !deepFreeze_1.isDeepFrozen(sourceObject)) { console.warn('Source object is frozen but not deep frozen. Please care that always deepFreeze() is used to recursively freeze the object'); } } ReplicationBuilder.forObject = function (sourceObject) { return new ReplicationBuilder(sourceObject); }; /** * @deprecated since 0.4.1 * use property instead */ ReplicationBuilder.prototype.getChild = function (childNode) { return this.property(childNode); }; /** switch to child node * @param {K} childNode of the root node * @returns {ReplicaChildOperator<T, T[K]>} operator of child node **/ ReplicationBuilder.prototype.property = function (childNode) { var _this = this; var node = this.replica[childNode]; return new ReplicaChildOperator((function () { return _this.build(); }), this.replica, node, childNode); }; /** * @deprecated since 0.4.1 * use replaceValueOf instead */ ReplicationBuilder.prototype.modify = function (childNode) { return this.replaceValueOf(childNode); }; ReplicationBuilder.prototype.replaceValueOf = function (childNode) { return new PropertyModifier(this, childNode, this.replica); }; /** * @deprecated since 0.4.1 * use removeProperty instead */ ReplicationBuilder.prototype.delete = function (childNode) { return this.removeProperty(childNode); }; ReplicationBuilder.prototype.removeProperty = function (childNode) { if (this.replica[childNode]) { delete this.replica[childNode]; } return this; }; /** * produces the replica * @returns {T} replica */ ReplicationBuilder.prototype.build = function () { if (this.freeze) { this.replica = deepFreeze_1.deepFreeze(this.replica); } return this.replica; }; return ReplicationBuilder; }()); exports.ReplicationBuilder = ReplicationBuilder; /** * Operator for nodes of the replica */ var ReplicaChildOperator = /** @class */ (function () { function ReplicaChildOperator(buildFunction, replica, node, relativePath) { this.buildFunction = buildFunction; this.node = node; this.replica = replica; this.relativePath = relativePath; } /** * @deprecated since 0.4.1 * use property instead */ ReplicaChildOperator.prototype.getChild = function (childNode) { return this.property(childNode); }; /** switch to child node * @param {K} childNode of this node * @returns {ReplicaChildOperator<RT, N[K]>} traversable child node **/ ReplicaChildOperator.prototype.property = function (childNode) { var branch = this.node[childNode]; return new ReplicaChildOperator(this.buildFunction, this.replica, branch, this.relativePath + '.' + childNode); }; /** * @deprecated since 0.4.1 * use replaceValueOf instead */ ReplicaChildOperator.prototype.modify = function (childNode) { return this.replaceValueOf(childNode); }; ReplicaChildOperator.prototype.replaceValueOf = function (childNode) { return new PropertyModifier(this, this.relativePath + '.' + childNode, this.replica); }; /** * @deprecated since 0.4.1 * use removeProperty instead */ ReplicaChildOperator.prototype.delete = function (childNode) { return this.removeProperty(childNode); }; ReplicaChildOperator.prototype.removeProperty = function (childNode) { if (this.node[childNode]) { delete this.node[childNode]; } return this; }; /** * produces the replica * @returns {RT} replica */ ReplicaChildOperator.prototype.build = function () { return this.buildFunction(); }; return ReplicaChildOperator; }()); exports.ReplicaChildOperator = ReplicaChildOperator; var PropertyModifier = /** @class */ (function () { function PropertyModifier(parent, relativePathToRoot, rootObject) { this.replica = rootObject; this.parent = parent; this.relativePathToRoot = relativePathToRoot; } /** * @deprecated since 0.4.1 * use with instead */ PropertyModifier.prototype.to = function (value) { return this.with(value); }; /** * set the value of the property * @param {VT} value * @returns {PT} */ PropertyModifier.prototype.with = function (value) { _set(this.replica, this.relativePathToRoot, value); return this.parent; }; /** * 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 */ PropertyModifier.prototype.by = function (setFunction) { var currentvalue = _get(this.replica, this.relativePathToRoot); var value = setFunction(currentvalue); return this.with(value); }; /** * applies a function on this property * @param {(VT) => void} executeOnCloneFunction function that is executed * @returns {PT} */ PropertyModifier.prototype.withCloneAndDo = function (executeOnCloneFunction) { var currentvalue = _get(this.replica, this.relativePathToRoot); executeOnCloneFunction(currentvalue); return this.parent; }; return PropertyModifier; }()); exports.PropertyModifier = PropertyModifier; //# sourceMappingURL=replicator.js.map