just-diff-apply
Advanced tools
Comparing version 5.1.1 to 5.2.0
# just-diff-apply | ||
## 5.2.0 | ||
### Minor Changes | ||
- Add support for 'move' op (https://datatracker.ietf.org/doc/html/rfc6902#section-4.4) | ||
## 5.0.0 | ||
### Major Changes | ||
- Disallow prototype updates |
69
index.js
@@ -50,2 +50,3 @@ module.exports = { | ||
var ADD = 'add'; | ||
var MOVE = 'move'; | ||
@@ -66,13 +67,29 @@ function diffApply(obj, diff, pathConverter) { | ||
var thisOp = thisDiff.op; | ||
var thisPath = thisDiff.path; | ||
if (pathConverter) { | ||
thisPath = pathConverter(thisPath); | ||
if (!Array.isArray(thisPath)) { | ||
throw new Error('pathConverter must return an array'); | ||
var thisPath = transformPath(pathConverter, thisDiff.path); | ||
var thisFromPath = thisDiff.from && transformPath(pathConverter, thisDiff.from); | ||
var toPath, toPathCopy, lastToProp, subToObject, valueToMove; | ||
if (thisFromPath) { | ||
// MOVE only, "fromPath" is effectively path and "path" is toPath | ||
toPath = thisPath; | ||
thisPath = thisFromPath; | ||
toPathCopy = toPath.slice(); | ||
lastToProp = toPathCopy.pop(); | ||
prototypeCheck(lastToProp); | ||
if (lastToProp == null) { | ||
return false; | ||
} | ||
} else { | ||
if (!Array.isArray(thisPath)) { | ||
throw new Error('diff path must be an array, consider supplying a path converter'); | ||
var thisToProp; | ||
while (((thisToProp = toPathCopy.shift())) != null) { | ||
prototypeCheck(thisToProp); | ||
if (!(thisToProp in subToObject)) { | ||
subToObject[thisToProp] = {}; | ||
} | ||
subToObject = subToObject[thisToProp]; | ||
} | ||
} | ||
var pathCopy = thisPath.slice(); | ||
@@ -84,2 +101,3 @@ var lastProp = pathCopy.pop(); | ||
} | ||
var thisProp; | ||
@@ -93,8 +111,12 @@ while (((thisProp = pathCopy.shift())) != null) { | ||
} | ||
if (thisOp === REMOVE || thisOp === REPLACE) { | ||
if (thisOp === REMOVE || thisOp === REPLACE || thisOp === MOVE) { | ||
var path = thisOp === MOVE ? thisDiff.from : thisDiff.path; | ||
if (!subObject.hasOwnProperty(lastProp)) { | ||
throw new Error(['expected to find property', thisDiff.path, 'in object', obj].join(' ')); | ||
throw new Error(['expected to find property', path, 'in object', obj].join(' ')); | ||
} | ||
} | ||
if (thisOp === REMOVE) { | ||
if (thisOp === REMOVE || thisOp === MOVE) { | ||
if (thisOp === MOVE) { | ||
valueToMove = subObject[lastProp]; | ||
} | ||
Array.isArray(subObject) ? subObject.splice(lastProp, 1) : delete subObject[lastProp]; | ||
@@ -105,2 +127,6 @@ } | ||
} | ||
if (thisOp === MOVE) { | ||
subObject[lastToProp] = valueToMove; | ||
} | ||
} | ||
@@ -110,2 +136,23 @@ return subObject; | ||
function transformPath(pathConverter, thisPath) { | ||
if(pathConverter) { | ||
thisPath = pathConverter(thisPath); | ||
if(!Array.isArray(thisPath)) { | ||
throw new Error([ | ||
'pathConverter must return an array, returned:', | ||
thisPath, | ||
].join(' ')); | ||
} | ||
} else { | ||
if(!Array.isArray(thisPath)) { | ||
throw new Error([ | ||
'diff path', | ||
thisPath, | ||
'must be an array, consider supplying a path converter'] | ||
.join(' ')); | ||
} | ||
} | ||
return thisPath; | ||
} | ||
function jsonPatchPathConverter(stringPath) { | ||
@@ -112,0 +159,0 @@ return stringPath.split('/').slice(1); |
{ | ||
"name": "just-diff-apply", | ||
"version": "5.1.1", | ||
"version": "5.2.0", | ||
"description": "Apply a diff to an object. Optionally supports jsonPatch protocol", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -33,7 +33,15 @@ <!-- DO NOT EDIT THIS FILE! THIS FILE WAS AUTOGENERATED BY TEMPLATE-MATE --> | ||
const obj2 = {a: 3, b: 5}; | ||
diffApply(obj2, | ||
[ | ||
{ "op": "move", "from": ['a'], "path": ['c']}, | ||
] | ||
); | ||
obj2; // {b: 5, c: 3} | ||
// using converter to apply jsPatch standard paths | ||
// see http://jsonpatch.com | ||
import {diffApply, jsonPatchPathConverter} from 'just-diff-apply' | ||
const obj2 = {a: 3, b: 5}; | ||
diffApply(obj2, [ | ||
const obj3 = {a: 3, b: 5}; | ||
diffApply(obj3, [ | ||
{ "op": "remove", "path": '/b' }, | ||
@@ -43,7 +51,7 @@ { "op": "replace", "path": '/a', "value": 4 } | ||
], jsonPatchPathConverter); | ||
obj2; // {a: 4, c: 5} | ||
obj3; // {a: 4, c: 5} | ||
// arrays (array key can be string or numeric) | ||
const obj3 = {a: 4, b: [1, 2, 3]}; | ||
diffApply(obj3, [ | ||
const obj4 = {a: 4, b: [1, 2, 3]}; | ||
diffApply(obj4, [ | ||
{ "op": "replace", "path": ['a'], "value": 3 } | ||
@@ -53,7 +61,7 @@ { "op": "replace", "path": ['b', 2], "value": 4 } | ||
]); | ||
obj3; // {a: 3, b: [1, 2, 4, 9]} | ||
obj4; // {a: 3, b: [1, 2, 4, 9]} | ||
// nested paths | ||
const obj4 = {a: 4, b: {c: 3}}; | ||
diffApply(obj4, [ | ||
const obj5 = {a: 4, b: {c: 3}}; | ||
diffApply(obj5, [ | ||
{ "op": "replace", "path": ['a'], "value": 5 } | ||
@@ -63,3 +71,3 @@ { "op": "remove", "path": ['b', 'c']} | ||
]); | ||
obj4; // {a: 5, b: {d: 4}} | ||
obj5; // {a: 5, b: {d: 4}} | ||
``` |
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
14087
358
70