Skip to content
Myr

patches.js

This file

Path

src/patches.js

146 lines · 4.5 KiB · JavaScript

Last changed by

Merge pull request #200 from stefanwille/upgrade-prettierMichel Weststratebefore adoption1139f57

History of this file

Take it

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1–85why, one step back
1import {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 back
86 : key in baseValue
87 ? "replace"88 : "add"

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 89–95why, one step back
89 if (origValue === baseValue && op === "replace") return
90 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 back
96 ? {op: "add", path, value: origValue}
97 : {op: "replace", path, value: origValue}

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 98–146why, one step back
98 )
99 })100}101102export function applyPatches(draft, patches) {103 for (let i = 0; i < patches.length; i++) {104 const patch = patches[i]105 if (patch.path.length === 0 && patch.op === "replace") {106 draft = patch.value107 } else {108 const path = patch.path.slice()109 const key = path.pop()110 const base = path.reduce((current, part) => {111 if (!current)112 throw new Error(113 "Cannot apply patch, path doesn't resolve: " +114 patch.path.join("/")115 )116 return current[part]117 }, draft)118 if (!base)119 throw new Error(120 "Cannot apply patch, path doesn't resolve: " +121 patch.path.join("/")122 )123 switch (patch.op) {124 case "replace":125 case "add":126 // TODO: add support is not extensive, it does not support insertion or `-` atm!127 base[key] = patch.value128 break129 case "remove":130 if (Array.isArray(base)) {131 if (key === base.length - 1) base.length -= 1132 else133 throw new Error(134 `Remove can only remove the last key of an array, index: ${key}, length: ${135 base.length136 }`137 )138 } else delete base[key]139 break140 default:141 throw new Error("Unsupported patch operation: " + patch.op)142 }143 }144 }145 return draft146}