patches.js
This file
Path
src/patches.js
142 lines · 4.3 KiB · JavaScript
Last changed by
improve applyPatches perfaleclarsonbefore adoptionfe82ed9
History of this fileTake it
Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1–85why, 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"Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57
lines 86–88why, one step back86 : key in baseValue87 ? "replace"88 : "add"Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 89–95why, one step back89 if (origValue === baseValue && op === "replace") return90 const path = basepath.concat(key)91 patches.push(op === "remove" ? {op, path} : {op, path, value})92 inversePatches.push(93 op === "add"94 ? {op: "remove", path}95 : op === "remove"Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57
lines 96–97why, one step back96 ? {op: "add", path, value: origValue}97 : {op: "replace", path, value: origValue}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 98–104why, one step back98 )99 })100}101102export function applyPatches(draft, patches) {103 for (let i = 0; i < patches.length; i++) {104 const patch = patches[i]improve applyPatches perfaleclarsonbefore adoptionfe82ed9
lines 105–106why, one step back105 const {path} = patch106 if (path.length === 0 && patch.op === "replace") {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 107–108why, one step back107 draft = patch.value108 } else {improve applyPatches perfaleclarsonbefore adoptionfe82ed9
lines 109–112why, one step back109 let base = draft110 for (let i = 0; i < path.length - 1; i++) {111 base = base[path[i]]112 if (!base || typeof base !== "object")Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 113–114why, one step back113 throw new Error(114 "Cannot apply patch, path doesn't resolve: " +improve applyPatches perfaleclarsonbefore adoptionfe82ed9
line 115why, one step back115 path.join("/")Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 116why, one step back116 )improve applyPatches perfaleclarsonbefore adoptionfe82ed9
lines 117–118why, one step back117 }118 const key = path[path.length - 1]Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 119–142why, one step back119 switch (patch.op) {120 case "replace":121 case "add":122 // TODO: add support is not extensive, it does not support insertion or `-` atm!123 base[key] = patch.value124 break125 case "remove":126 if (Array.isArray(base)) {127 if (key === base.length - 1) base.length -= 1128 else129 throw new Error(130 `Remove can only remove the last key of an array, index: ${key}, length: ${131 base.length132 }`133 )134 } else delete base[key]135 break136 default:137 throw new Error("Unsupported patch operation: " + patch.op)138 }139 }140 }141 return draft142}