UNPKG

@state-sync/js-client

Version:
182 lines (181 loc) 6.21 kB
"use strict"; 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 __()); }; })(); var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var Op = /** @class */ (function () { function Op(src) { this.path = src.path.split('/').slice(1); } return Op; }()); exports.Op = Op; var OpReplace = /** @class */ (function (_super) { __extends(OpReplace, _super); function OpReplace(src) { var _this = _super.call(this, src) || this; _this.root = src.path === '' || src.path === '/'; _this.value = src.value; return _this; } OpReplace.prototype.apply = function (json) { return this.root ? this.value : this.applySegment(json, 0); }; OpReplace.prototype.applySegment = function (json, index) { var clone = json instanceof Array ? json.slice() : __assign({}, json); if (index + 1 < this.path.length) { clone[this.path[index]] = this.applySegment(clone[this.path[index]] || {}, index + 1); } else { clone[this.path[index]] = this.value; } return clone; }; return OpReplace; }(Op)); var OpCopy = /** @class */ (function (_super) { __extends(OpCopy, _super); function OpCopy(src) { var _this = _super.call(this, src) || this; _this.root = src.path === '' || src.path === '/'; _this.from = new OpSelect({ op: 'select', path: src.from || '/' }); return _this; } OpCopy.prototype.apply = function (json) { this.value = JSON.parse(JSON.stringify(this.from.apply(json))); return this.root ? this.value : this.applySegment(json, 0); }; OpCopy.prototype.applySegment = function (json, index) { var clone = json instanceof Array ? json.slice() : __assign({}, json); if (index + 1 < this.path.length) { clone[this.path[index]] = this.applySegment(clone[this.path[index]] || {}, index + 1); } else { clone[this.path[index]] = this.value; } return clone; }; return OpCopy; }(Op)); var OpSelect = /** @class */ (function (_super) { __extends(OpSelect, _super); function OpSelect(src) { var _this = _super.call(this, src) || this; _this.root = src.path === '' || src.path === '/'; return _this; } OpSelect.prototype.apply = function (json) { var p = this.path; for (var i = 0; i < p.length; i++) { json = json ? json[p[i]] : null; } return json; }; return OpSelect; }(Op)); exports.OpSelect = OpSelect; var OpAdd = /** @class */ (function (_super) { __extends(OpAdd, _super); function OpAdd(src) { var _this = _super.call(this, src) || this; _this.value = src.value; return _this; } OpAdd.prototype.apply = function (json) { return this.applySegment(json, 0); }; OpAdd.prototype.applySegment = function (json, index) { var clone = json instanceof Array ? json.slice() : __assign({}, json); if (index + 1 < this.path.length) { clone[this.path[index]] = this.applySegment(clone[this.path[index]] || {}, index + 1); } else { if (clone instanceof Array) { if (this.path[index] === '-') { clone.push(this.value); } else { clone.splice(parseInt(this.path[index]), 0, this.value); } } else { clone[this.path[index]] = this.value; } } return clone; }; return OpAdd; }(Op)); var OpRemove = /** @class */ (function (_super) { __extends(OpRemove, _super); function OpRemove(src) { return _super.call(this, src) || this; } OpRemove.prototype.apply = function (json) { return this.applySegment(json, 0); }; OpRemove.prototype.applySegment = function (json, index) { var clone = json instanceof Array ? json.slice() : __assign({}, json); if (index + 1 < this.path.length) { clone[this.path[index]] = this.applySegment(clone[this.path[index]] || {}, index + 1); } else { if (clone instanceof Array) { clone.splice(parseInt(this.path[index]), 1); } else { delete clone[this.path[index]]; } } return clone; }; return OpRemove; }(Op)); var Patch = /** @class */ (function () { function Patch(src) { this.patches = []; for (var _i = 0, src_1 = src; _i < src_1.length; _i++) { var p = src_1[_i]; switch (p.op) { case 'replace': this.patches.push(new OpReplace(p)); break; case 'add': this.patches.push(new OpAdd(p)); break; case 'remove': this.patches.push(new OpRemove(p)); break; case 'copy': this.patches.push(new OpCopy(p)); break; default: debugger; } } } Patch.prototype.apply = function (json) { for (var _i = 0, _a = this.patches; _i < _a.length; _i++) { var patch = _a[_i]; json = patch.apply(json); } return json; }; return Patch; }()); exports.Patch = Patch;