json-joy
Version:
Collection of libraries for building collaborative editing apps.
35 lines (34 loc) • 1.24 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.transform = void 0;
const transforms_1 = require("./transforms");
/**
* Takes array of proposed patches and transforms them against an array of
* already accepted patches.
*
* @param accepted Array of already accepted operations.
* @param proposed Array of proposed operations. Proposed operations are mutated inline.
* @param acceptedWins Whether accepted operation should win on when paths match exactly.
*
* @returns Array of transformed changes
*/
const transform = (accepted, proposed) => {
const length = accepted.length;
for (let i = 0; i < length; i++) {
const against = accepted[i];
const transformFunction = transforms_1.xforms[against.op()];
if (transformFunction) {
const transformed = [];
for (const op of proposed) {
const newOps = transformFunction(against, op);
if (Array.isArray(newOps))
transformed.push(...newOps);
else if (newOps)
transformed.push(newOps);
}
proposed = transformed;
}
}
return proposed;
};
exports.transform = transform;
;