MCPcopy Index your code
hub / github.com/Starcounter-Jack/JSON-Patch / applyOperation

Function applyOperation

module/core.mjs:108–248  ·  view source on GitHub ↗
(document, operation, validateOperation, mutateDocument, banPrototypeModifications, index)

Source from the content-addressed store, hash-verified

106 * @return `{newDocument, result}` after the operation
107 */
108export 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 */

Callers 4

core.mjsFile · 0.70
getValueByPointerFunction · 0.70
applyPatchFunction · 0.70
applyReducerFunction · 0.70

Calls 6

_deepCloneFunction · 0.90
unescapePathComponentFunction · 0.90
isIntegerFunction · 0.90
validatorFunction · 0.70
getValueByPointerFunction · 0.70
_areEqualsFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…