Skip to content

Commit

Permalink
fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieudutour authored Feb 23, 2023
1 parent 4f0cc1c commit ea8d5e6
Showing 1 changed file with 1 addition and 29 deletions.
30 changes: 1 addition & 29 deletions Sources/JSONPatch/JSONPatchGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,35 +145,7 @@ struct JSONPatchGenerator {
}

private mutating func add(path: JSONPointer, value: JSONElement) {
if let removalIndex = findPreviouslyRemoved(value: value) {
guard case let .remove(removedPath, _) = operations[removalIndex] else {
return
}
operations.remove(at: removalIndex)
operations.append(.move(from: removedPath, old: value, path: path, value: value))
return
}
if let oldPath = findUnchangedValue(value: value) {
operations.append(.copy(from: oldPath, path: path, value: value))
} else {
operations.append(.add(path: path, value: value))
}
}

private func findUnchangedValue(value: JSONElement) -> JSONPointer? {
for (pointer, old) in unchanged where value == old {
return pointer
}
return nil
}

private func findPreviouslyRemoved(value: JSONElement) -> Int? {
return operations.firstIndex { (op) -> Bool in
guard case let .remove(_, old) = op else {
return false
}
return value == old
}
operations.append(.add(path: path, value: value))
}
}

Expand Down

0 comments on commit ea8d5e6

Please sign in to comment.