started work on patch(..)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-07-20 02:02:01 +03:00
parent bd61727b12
commit f4f174de81

80
diff.js
View File

@ -1074,61 +1074,61 @@ var _patch = function(diff, obj){
var NONE = diff.options.placeholders.NONE var NONE = diff.options.placeholders.NONE
var EMPTY = diff.options.placeholders.EMPTY var EMPTY = diff.options.placeholders.EMPTY
diff.diff Types.walk(diff, function(change){
.forEach(function(change){ // replace the object itself...
// replace the object itself... if(path.length == 0){
if(path.length == 0){ return change.B
return change.B }
}
var type = change.type || 'item' var type = change.type || 'item'
var target = change.path var target = change.path
.slice(0, -1) .slice(0, -1)
.reduce(function(res, e){ .reduce(function(res, e){
return res[e]}, obj) return res[e]}, obj)
var key = change.path[change.path.length-1] var key = change.path[change.path.length-1]
if(type == 'item'){ if(type == 'item'){
// object attr... // object attr...
if(typeof(key) == typeof('str')){ if(typeof(key) == typeof('str')){
if(change.B.type == EMPTY.type){ if(change.B.type == EMPTY.type){
delete target[key] delete target[key]
} else {
target[key] = change.B
}
// array item...
} else { } else {
var i = key instanceof Array ? key[0] : key // XXX also check what is overwritten...
var j = key instanceof Array ? key[1] : key target[key] = change.B
}
if(i == null){ // array item...
target.splice(j, 0, change.B) } else {
var i = key instanceof Array ? key[0] : key
var j = key instanceof Array ? key[1] : key
} else if(j == null){ if(i == null){
// XXX better EMPTY check -- use diff target.splice(j, 0, change.B)
if(!('B' in change) || change.B.type == EMPTY.type){
delete target[i]
} else if(!('B' in change) || change.B.type == NONE.type){ } else if(j == null){
target.splice(i, 1) // XXX better EMPTY check -- use diff
if(!('B' in change) || change.B.type == EMPTY.type){
delete target[i]
} else { } else if(!('B' in change) || change.B.type == NONE.type){
// XXX target.splice(i, 1)
}
} else if(i == j){
target[j] = change.B
} else { } else {
target[j] = change.B // XXX
} }
} else if(i == j){
target[j] = change.B
} else {
target[j] = change.B
} }
} }
}
}) })
return obj return obj
} }