immutable-typescript
Version:
Immutable objects for TypeScript
86 lines (85 loc) • 2.93 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Proxy = /** @class */ (function () {
function Proxy(proxied, parentProxy, prop) {
this.proxied = proxied;
this.parentProxy = parentProxy;
this.prop = prop;
}
Proxy.prototype.at = function (prop) {
if (Array.isArray(this.proxied[prop])) {
return new ArrayProxy(this.proxied[prop], this, prop);
}
return new Proxy(this.proxied[prop], this, prop);
};
Proxy.prototype.set = function (prop, val) {
var copy = new this.proxied.constructor();
Object.assign(copy, this.proxied);
copy[prop] = val;
return this.plugInObject(copy);
};
Proxy.prototype.plugInObject = function (copy) {
if (this.parentProxy == null) {
return copy;
}
return this.parentProxy.set(this.prop, copy);
};
return Proxy;
}());
exports.Proxy = Proxy;
var ArrayProxy = /** @class */ (function (_super) {
__extends(ArrayProxy, _super);
function ArrayProxy() {
return _super !== null && _super.apply(this, arguments) || this;
}
ArrayProxy.prototype.unshift = function () {
var elements = [];
for (var _i = 0; _i < arguments.length; _i++) {
elements[_i] = arguments[_i];
}
var copy = this.proxied.slice();
copy.unshift.apply(copy, elements);
return this.plugInObject(copy);
};
ArrayProxy.prototype.remove = function (index, count) {
var copy = this.proxied.slice();
copy.splice(index, count);
return this.plugInObject(copy);
};
return ArrayProxy;
}(Proxy));
exports.ArrayProxy = ArrayProxy;
var DictProxy = /** @class */ (function (_super) {
__extends(DictProxy, _super);
function DictProxy() {
return _super !== null && _super.apply(this, arguments) || this;
}
DictProxy.prototype.del = function () {
};
return DictProxy;
}(Proxy));
exports.DictProxy = DictProxy;
var ImmutableUtils = /** @class */ (function () {
function ImmutableUtils() {
}
ImmutableUtils.asImmutable = function (obj) {
var copy = new obj.constructor();
Object.assign(copy, obj);
return copy;
};
ImmutableUtils.update = function (obj) {
return new Proxy(obj, null, null);
};
return ImmutableUtils;
}());
exports.ImmutableUtils = ImmutableUtils;