MCPcopy Create free account
hub / github.com/Starcounter-Jack/JSON-Patch / applyOperation

Function applyOperation

commonjs/core.js:110–250  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

108 * @return `{newDocument, result}` after the operation
109 */
110function 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 */

Callers 4

core.jsFile · 0.70
getValueByPointerFunction · 0.70
applyPatchFunction · 0.70
applyReducerFunction · 0.70

Calls 3

validatorFunction · 0.70
getValueByPointerFunction · 0.70
_areEqualsFunction · 0.70

Tested by

no test coverage detected