json-joy
Version:
Collection of libraries for building collaborative editing apps.
38 lines (37 loc) • 1.1 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyOp = applyOp;
exports.applyOps = applyOps;
exports.applyPatch = applyPatch;
const clone_1 = require("@jsonjoy.com/util/lib/json-clone/clone");
const json_1 = require("../codec/json");
function applyOp(doc, op, mutate) {
if (!mutate)
doc = (0, clone_1.clone)(doc);
return op.apply(doc);
}
function applyOps(doc, ops, mutate) {
if (!mutate)
doc = (0, clone_1.clone)(doc);
const res = [];
const length = ops.length;
for (let i = 0; i < length; i++) {
const opResult = ops[i].apply(doc);
doc = opResult.doc;
res.push(opResult);
}
return { doc, res };
}
function applyPatch(doc, patch, options) {
if (!options.mutate)
doc = (0, clone_1.clone)(doc);
const res = [];
const length = patch.length;
for (let i = 0; i < length; i++) {
const op = (0, json_1.operationToOp)(patch[i], options);
const opResult = op.apply(doc);
doc = opResult.doc;
res.push(opResult);
}
return { doc, res };
}
;