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