(document, operation, validateOperation, mutateDocument, banPrototypeModifications, index)
| 106 | * @return `{newDocument, result}` after the operation |
| 107 | */ |
| 108 | export function applyOperation(document, operation, validateOperation, mutateDocument, banPrototypeModifications, index) { |
| 109 | if (validateOperation === void 0) { validateOperation = false; } |
| 110 | if (mutateDocument === void 0) { mutateDocument = true; } |
| 111 | if (banPrototypeModifications === void 0) { banPrototypeModifications = true; } |
| 112 | if (index === void 0) { index = 0; } |
| 113 | if (validateOperation) { |
| 114 | if (typeof validateOperation == 'function') { |
| 115 | validateOperation(operation, 0, document, operation.path); |
| 116 | } |
| 117 | else { |
| 118 | validator(operation, 0); |
| 119 | } |
| 120 | } |
| 121 | /* ROOT OPERATIONS */ |
| 122 | if (operation.path === "") { |
| 123 | var returnValue = { newDocument: document }; |
| 124 | if (operation.op === 'add') { |
| 125 | returnValue.newDocument = operation.value; |
| 126 | return returnValue; |
| 127 | } |
| 128 | else if (operation.op === 'replace') { |
| 129 | returnValue.newDocument = operation.value; |
| 130 | returnValue.removed = document; //document we removed |
| 131 | return returnValue; |
| 132 | } |
| 133 | else if (operation.op === 'move' || operation.op === 'copy') { // it's a move or copy to root |
| 134 | returnValue.newDocument = getValueByPointer(document, operation.from); // get the value by json-pointer in `from` field |
| 135 | if (operation.op === 'move') { // report removed item |
| 136 | returnValue.removed = document; |
| 137 | } |
| 138 | return returnValue; |
| 139 | } |
| 140 | else if (operation.op === 'test') { |
| 141 | returnValue.test = _areEquals(document, operation.value); |
| 142 | if (returnValue.test === false) { |
| 143 | throw new JsonPatchError("Test operation failed", 'TEST_OPERATION_FAILED', index, operation, document); |
| 144 | } |
| 145 | returnValue.newDocument = document; |
| 146 | return returnValue; |
| 147 | } |
| 148 | else if (operation.op === 'remove') { // a remove on root |
| 149 | returnValue.removed = document; |
| 150 | returnValue.newDocument = null; |
| 151 | return returnValue; |
| 152 | } |
| 153 | else if (operation.op === '_get') { |
| 154 | operation.value = document; |
| 155 | return returnValue; |
| 156 | } |
| 157 | else { /* bad operation */ |
| 158 | if (validateOperation) { |
| 159 | throw new JsonPatchError('Operation `op` property is not one of operations defined in RFC-6902', 'OPERATION_OP_INVALID', index, operation, document); |
| 160 | } |
| 161 | else { |
| 162 | return returnValue; |
| 163 | } |
| 164 | } |
| 165 | } /* END ROOT OPERATIONS */ |
no test coverage detected
searching dependent graphs…