fast-json-patch
Advanced tools
Comparing version 0.5.2 to 0.5.4
/*! | ||
* https://github.com/Starcounter-Jack/JSON-Patch | ||
* json-patch-duplex.js version: 0.5.2 | ||
* json-patch-duplex.js version: 0.5.4 | ||
* (c) 2013 Joachim Wester | ||
@@ -5,0 +5,0 @@ * MIT license |
/*! | ||
* https://github.com/Starcounter-Jack/JSON-Patch | ||
* json-patch-duplex.js version: 0.5.2 | ||
* json-patch-duplex.js version: 0.5.4 | ||
* (c) 2013 Joachim Wester | ||
@@ -5,0 +5,0 @@ * MIT license |
{ | ||
"name": "fast-json-patch", | ||
"version": "0.5.2", | ||
"version": "0.5.4", | ||
"description": "Fast implementation of JSON-Patch (RFC-6902) with duplex (observe changes) capabilities", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/Starcounter-Jack/JSON-Patch", |
@@ -73,2 +73,3 @@ JSON-Patch | ||
Applying patches: | ||
```js | ||
@@ -85,2 +86,3 @@ var myobj = { firstName:"Albert", contactDetails: { phoneNumbers: [ ] } }; | ||
Generating patches: | ||
```js | ||
@@ -99,2 +101,3 @@ var myobj = { firstName:"Joachim", lastName:"Wester", contactDetails: { phoneNumbers: [ { number:"555-123" }] } }; | ||
Comparing two object trees: | ||
```js | ||
@@ -108,2 +111,3 @@ var objA = {user: {firstName: "Albert", lastName: "Einstein"}}; | ||
Validating a sequence of patches: | ||
```js | ||
@@ -117,3 +121,3 @@ var obj = {user: {firstName: "Albert"}}; | ||
else { | ||
for (var i=0; i<errors.length; i++) { | ||
for (var i=0; i < errors.length; i++) { | ||
if (!errors[i]) { | ||
@@ -226,3 +230,4 @@ console.log("Valid patch at index", i, patches[i]); | ||
OPERATION_FROM_REQUIRED | Operation `from` property is not present (applicable in `move` and `copy` operations) | ||
OPERATION_VALUE_REQUIRED | Operation `value` property is not present (applicable in `add`, `replace` and `test` operations) | ||
OPERATION_VALUE_REQUIRED | Operation `value` property is not present, or `undefined` (applicable in `add`, `replace` and `test` operations) | ||
OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED | Operation `value` property object has at least one `undefined` value (applicable in `add`, `replace` and `test` operations) | ||
OPERATION_PATH_CANNOT_ADD | Cannot perform an `add` operation at the desired path | ||
@@ -234,4 +239,18 @@ OPERATION_PATH_UNRESOLVABLE | Cannot perform the operation at a path that does not exist | ||
## `undefined`s (JSON to JS extension) | ||
As `undefined` is not a valid value for any JSON node, it's also not valid value o JSON Patch operation object value property. Therefore, for valid JSON document, `jsonpatch` will not generate JSON Patches that sets anything to `undefined`. | ||
However, to play nicer with natural JavaScipt objects `jsonpatch` can be applied to an object that contains `undefined`, in such case we will treat it as JS does. `.apply` will handle JSON Patches with `value: undefined` as any other falsy value. `.generate`, `.compare`, `.observe` methods will also produce JSON Patches with `undefined`s, but only for (non valid) JSON documents that contains it. | ||
## :no_entry_sign: `undefined`s (JS to JSON projection) | ||
~~As `undefined` is not a valid value for any JSON node, it's also not valid value o JSON Patch operation object value property. Therefore `jsonpatch` will not generate JSON Patches that sets anything to `undefined`.~~ | ||
~~However, to play nicer with natural JavaScipt objects `jsonpatch` can be applied to an object that contains `undefined`, in such case we will use it as native `JSON.stringify` - we will treat them as non-existing nodes, and map to `null` for array elements.~~ | ||
## Changelog | ||
To see the list of recent changes, see [Releases](https://github.com/Starcounter-Jack/JSON-Patch/releases). |
/*! | ||
* https://github.com/Starcounter-Jack/JSON-Patch | ||
* json-patch-duplex.js version: 0.5.2 | ||
* json-patch-duplex.js version: 0.5.4 | ||
* (c) 2013 Joachim Wester | ||
@@ -335,19 +335,3 @@ * MIT license | ||
if (!(arr[a].name === 'length' && _isArray(arr[a].object)) && !(arr[a].name === '__Jasmine_been_here_before__')) { | ||
var type = arr[a].type; | ||
switch (type) { | ||
case 'new': | ||
type = 'add'; | ||
break; | ||
case 'deleted': | ||
type = 'delete'; | ||
break; | ||
case 'updated': | ||
type = 'update'; | ||
break; | ||
} | ||
observeOps[type].call(arr[a], patches, getPath(root, arr[a].object)); | ||
observeOps[arr[a].type].call(arr[a], patches, getPath(root, arr[a].object)); | ||
} | ||
@@ -549,3 +533,4 @@ a++; | ||
// Find the object | ||
var keys = patch.path.split('/'); | ||
var path = patch.path || ""; | ||
var keys = path.split('/'); | ||
var obj = tree; | ||
@@ -634,2 +619,21 @@ var t = 1; | ||
/** | ||
* Recursively checks whether an object has any undefined values inside. | ||
*/ | ||
function hasUndefined(obj) { | ||
if (obj === undefined) { | ||
return true; | ||
} | ||
if (typeof obj == "array" || typeof obj == "object") { | ||
for (var i in obj) { | ||
if (hasUndefined(obj[i])) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error. | ||
@@ -652,2 +656,4 @@ * @param {object} operation - operation object (patch) | ||
throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_REQUIRED', index, operation, tree); | ||
} else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && hasUndefined(operation.value)) { | ||
throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED', index, operation, tree); | ||
} else if (tree) { | ||
@@ -654,0 +660,0 @@ if (operation.op == "add") { |
/*! | ||
* https://github.com/Starcounter-Jack/JSON-Patch | ||
* json-patch-duplex.js version: 0.5.2 | ||
* json-patch-duplex.js version: 0.5.4 | ||
* (c) 2013 Joachim Wester | ||
@@ -345,21 +345,3 @@ * MIT license | ||
) { | ||
var type = arr[a].type; | ||
//old record type names before 10/29/2013 (http://wiki.ecmascript.org/doku.php?id=harmony:observe) | ||
//this block can be removed when Chrome 33 stable is released | ||
switch(type) { | ||
case 'new': | ||
type = 'add'; | ||
break; | ||
case 'deleted': | ||
type = 'delete'; | ||
break; | ||
case 'updated': | ||
type = 'update'; | ||
break; | ||
} | ||
observeOps[type].call(arr[a], patches, getPath(root, arr[a].object)); | ||
observeOps[arr[a].type].call(arr[a], patches, getPath(root, arr[a].object)); | ||
} | ||
@@ -571,3 +553,4 @@ a++; | ||
// Find the object | ||
var keys = patch.path.split('/'); | ||
var path = patch.path || ""; | ||
var keys = path.split('/'); | ||
var obj = tree; | ||
@@ -656,2 +639,21 @@ var t = 1; //skip empty element - http://jsperf.com/to-shift-or-not-to-shift | ||
/** | ||
* Recursively checks whether an object has any undefined values inside. | ||
*/ | ||
export function hasUndefined(obj:any): boolean { | ||
if (obj === undefined) { | ||
return true; | ||
} | ||
if (typeof obj == "array" || typeof obj == "object") { | ||
for (var i in obj) { | ||
if (hasUndefined(obj[i])) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
@@ -685,2 +687,6 @@ * Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error. | ||
else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && hasUndefined(operation.value)) { | ||
throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED', index, operation, tree); | ||
} | ||
else if (tree) { | ||
@@ -687,0 +693,0 @@ if (operation.op == "add") { |
/*! | ||
* https://github.com/Starcounter-Jack/JSON-Patch | ||
* json-patch-duplex.js version: 0.5.2 | ||
* json-patch-duplex.js version: 0.5.4 | ||
* (c) 2013 Joachim Wester | ||
@@ -216,3 +216,4 @@ * MIT license | ||
// Find the object | ||
var keys = patch.path.split('/'); | ||
var path = patch.path || ""; | ||
var keys = path.split('/'); | ||
var obj = tree; | ||
@@ -294,2 +295,21 @@ var t = 1; | ||
/** | ||
* Recursively checks whether an object has any undefined values inside. | ||
*/ | ||
function hasUndefined(obj) { | ||
if (obj === undefined) { | ||
return true; | ||
} | ||
if (typeof obj == "array" || typeof obj == "object") { | ||
for (var i in obj) { | ||
if (hasUndefined(obj[i])) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error. | ||
@@ -312,2 +332,4 @@ * @param {object} operation - operation object (patch) | ||
throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_REQUIRED', index, operation, tree); | ||
} else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && hasUndefined(operation.value)) { | ||
throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED', index, operation, tree); | ||
} else if (tree) { | ||
@@ -314,0 +336,0 @@ if (operation.op == "add") { |
/*! | ||
* https://github.com/Starcounter-Jack/JSON-Patch | ||
* json-patch-duplex.js version: 0.5.2 | ||
* json-patch-duplex.js version: 0.5.4 | ||
* (c) 2013 Joachim Wester | ||
@@ -215,3 +215,4 @@ * MIT license | ||
// Find the object | ||
var keys = patch.path.split('/'); | ||
var path = patch.path || ""; | ||
var keys = path.split('/'); | ||
var obj = tree; | ||
@@ -294,2 +295,21 @@ var t = 1; //skip empty element - http://jsperf.com/to-shift-or-not-to-shift | ||
/** | ||
* Recursively checks whether an object has any undefined values inside. | ||
*/ | ||
export function hasUndefined(obj:any): boolean { | ||
if (obj === undefined) { | ||
return true; | ||
} | ||
if (typeof obj == "array" || typeof obj == "object") { | ||
for (var i in obj) { | ||
if (hasUndefined(obj[i])) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
@@ -323,2 +343,6 @@ * Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error. | ||
else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && hasUndefined(operation.value)) { | ||
throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED', index, operation, tree); | ||
} | ||
else if (tree) { | ||
@@ -325,0 +349,0 @@ if (operation.op == "add") { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
103316
2076
250
0