UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

31 lines (30 loc) 1.09 kB
import { xforms } from './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 */ export const transform = (accepted, proposed) => { const length = accepted.length; for (let i = 0; i < length; i++) { const against = accepted[i]; const transformFunction = 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; };