json-joy
Version:
Collection of libraries for building collaborative editing apps.
24 lines (23 loc) • 695 B
JavaScript
import { clone as deepClone } from '@jsonjoy.com/util/lib/json-clone/clone';
import { decode } from '../codec/json';
export function applyOp(doc, op, mutate) {
if (!mutate)
doc = deepClone(doc);
return op.apply(doc);
}
export function applyOps(doc, ops, mutate) {
if (!mutate)
doc = deepClone(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 };
}
export function applyPatch(doc, patch, options) {
const result = applyOps(doc, decode(patch, options), options.mutate);
return result;
}