patches.js
This file
Path
src/patches.js
144 lines · 4.4 KiB · JavaScript
Last changed by
Merge branch 'patches'Michel Weststratebefore adoption2513fb8
History of this fileTake it
Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1–144why, one step back1import {each} from "./common"23export function generatePatches(4 state,5 basepath,6 patches,7 inversePatches,8 baseValue,9 resultValue10) {11 if (patches)12 if (Array.isArray(baseValue))13 generateArrayPatches(14 state,15 basepath,16 patches,17 inversePatches,18 baseValue,19 resultValue20 )21 else22 generateObjectPatches(23 state,24 basepath,25 patches,26 inversePatches,27 baseValue,28 resultValue29 )30}3132export function generateArrayPatches(33 state,34 basepath,35 patches,36 inversePatches,37 baseValue,38 resultValue39) {40 const shared = Math.min(baseValue.length, resultValue.length)41 for (let i = 0; i < shared; i++) {42 if (state.assigned[i] && baseValue[i] !== resultValue[i]) {43 const path = basepath.concat(i)44 patches.push({op: "replace", path, value: resultValue[i]})45 inversePatches.push({op: "replace", path, value: baseValue[i]})46 }47 }48 if (shared < resultValue.length) {49 // stuff was added50 for (let i = shared; i < resultValue.length; i++) {51 const path = basepath.concat(i)52 patches.push({op: "add", path, value: resultValue[i]})53 }54 inversePatches.push({55 op: "replace",56 path: basepath.concat("length"),57 value: baseValue.length58 })59 } else if (shared < baseValue.length) {60 // stuff was removed61 patches.push({62 op: "replace",63 path: basepath.concat("length"),64 value: resultValue.length65 })66 for (let i = shared; i < baseValue.length; i++) {67 const path = basepath.concat(i)68 inversePatches.push({op: "add", path, value: baseValue[i]})69 }70 }71}7273function generateObjectPatches(74 state,75 basepath,76 patches,77 inversePatches,78 baseValue,79 resultValue80) {81 each(state.assigned, (key, assignedValue) => {82 const origValue = baseValue[key]83 const value = resultValue[key]84 const op = !assignedValue85 ? "remove"86 : key in baseValue ? "replace" : "add"87 if (origValue === baseValue && op === "replace") return88 const path = basepath.concat(key)89 patches.push(op === "remove" ? {op, path} : {op, path, value})90 inversePatches.push(91 op === "add"92 ? {op: "remove", path}93 : op === "remove"94 ? {op: "add", path, value: origValue}95 : {op: "replace", path, value: origValue}96 )97 })98}99100export function applyPatches(draft, patches) {101 for (let i = 0; i < patches.length; i++) {102 const patch = patches[i]103 if (patch.path.length === 0 && patch.op === "replace") {104 draft = patch.value105 } else {106 const path = patch.path.slice()107 const key = path.pop()108 const base = path.reduce((current, part) => {109 if (!current)110 throw new Error(111 "Cannot apply patch, path doesn't resolve: " +112 patch.path.join("/")113 )114 return current[part]115 }, draft)116 if (!base)117 throw new Error(118 "Cannot apply patch, path doesn't resolve: " +119 patch.path.join("/")120 )121 switch (patch.op) {122 case "replace":123 case "add":124 // TODO: add support is not extensive, it does not support insertion or `-` atm!125 base[key] = patch.value126 break127 case "remove":128 if (Array.isArray(base)) {129 if (key === base.length - 1) base.length -= 1130 else131 throw new Error(132 `Remove can only remove the last key of an array, index: ${key}, length: ${133 base.length134 }`135 )136 } else delete base[key]137 break138 default:139 throw new Error("Unsupported patch operation: " + patch.op)140 }141 }142 }143 return draft144}