(document: T, operation: Operation, validateOperation: boolean | Validator<T> = false, mutateDocument: boolean = true, banPrototypeModifications: boolean = true, index: number = 0)
| 180 | * @return `{newDocument, result}` after the operation |
| 181 | */ |
| 182 | export function applyOperation<T>(document: T, operation: Operation, validateOperation: boolean | Validator<T> = false, mutateDocument: boolean = true, banPrototypeModifications: boolean = true, index: number = 0): OperationResult<T> { |
| 183 | if (validateOperation) { |
| 184 | if (typeof validateOperation == 'function') { |
| 185 | validateOperation(operation, 0, document, operation.path); |
| 186 | } |
| 187 | else { |
| 188 | validator(operation, 0); |
| 189 | } |
| 190 | } |
| 191 | /* ROOT OPERATIONS */ |
| 192 | if (operation.path === "") { |
| 193 | let returnValue: OperationResult<T> = { newDocument: document }; |
| 194 | if (operation.op === 'add') { |
| 195 | returnValue.newDocument = operation.value; |
| 196 | return returnValue; |
| 197 | } else if (operation.op === 'replace') { |
| 198 | returnValue.newDocument = operation.value; |
| 199 | returnValue.removed = document; //document we removed |
| 200 | return returnValue; |
| 201 | } |
| 202 | else if (operation.op === 'move' || operation.op === 'copy') { // it's a move or copy to root |
| 203 | returnValue.newDocument = getValueByPointer(document, operation.from); // get the value by json-pointer in `from` field |
| 204 | if (operation.op === 'move') { // report removed item |
| 205 | returnValue.removed = document; |
| 206 | } |
| 207 | return returnValue; |
| 208 | } else if (operation.op === 'test') { |
| 209 | returnValue.test = _areEquals(document, operation.value); |
| 210 | if (returnValue.test === false) { |
| 211 | throw new JsonPatchError("Test operation failed", 'TEST_OPERATION_FAILED', index, operation, document); |
| 212 | } |
| 213 | returnValue.newDocument = document; |
| 214 | return returnValue; |
| 215 | } else if (operation.op === 'remove') { // a remove on root |
| 216 | returnValue.removed = document; |
| 217 | returnValue.newDocument = null; |
| 218 | return returnValue; |
| 219 | } else if (operation.op === '_get') { |
| 220 | operation.value = document; |
| 221 | return returnValue; |
| 222 | } else { /* bad operation */ |
| 223 | if (validateOperation) { |
| 224 | throw new JsonPatchError('Operation `op` property is not one of operations defined in RFC-6902', 'OPERATION_OP_INVALID', index, operation, document); |
| 225 | } else { |
| 226 | return returnValue; |
| 227 | } |
| 228 | } |
| 229 | } /* END ROOT OPERATIONS */ |
| 230 | else { |
| 231 | if (!mutateDocument) { |
| 232 | document = _deepClone(document); |
| 233 | } |
| 234 | const path = operation.path || ""; |
| 235 | const keys = path.split('/'); |
| 236 | let obj = document; |
| 237 | let t = 1; //skip empty element - http://jsperf.com/to-shift-or-not-to-shift |
| 238 | let len = keys.length; |
| 239 | let existingPathFragment = undefined; |
no test coverage detected
searching dependent graphs…