added .reverse(..), still experimental...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-07-26 01:14:46 +03:00
parent a4d26d9407
commit 5a0ddfdd68

69
diff.js
View File

@ -335,6 +335,7 @@ var proxy = function(path, func){
// like the type information which is not needed for patching but // like the type information which is not needed for patching but
// may be useful for a more thorough compatibility check. // may be useful for a more thorough compatibility check.
// //
// XXX Q: do we need to support both the flat and tree diff formats???
var Types = { var Types = {
__cache: null, __cache: null,
@ -404,7 +405,8 @@ var Types = {
// sorted list of types... // sorted list of types...
// XXX do we need to cache this??? // XXX do we need to cache this???
get types(){ get typeKeys(){
var that = this
var h = this.handlers var h = this.handlers
var order = new Map() var order = new Map()
var i = 0 var i = 0
@ -429,8 +431,14 @@ var Types = {
: order.get(a) - order.get(b) : order.get(a) - order.get(b)
}) })
}, },
get types(){
var that = this
return this.typeKeys
.map(function(e){
return that.get(e) })
},
get typeNames(){ get typeNames(){
return this.types.map(function(e){ return e.name || e }) }, return this.typeKeys.map(function(e){ return e.name || e }) },
// Detect handler type... // Detect handler type...
@ -447,7 +455,7 @@ var Types = {
// NOTE: if A and B types mismatch we treat them as Object... // NOTE: if A and B types mismatch we treat them as Object...
detect: function(A, B, options){ detect: function(A, B, options){
var type var type
var types = this.types var types = this.typeKeys
// explicit checkers have priority over instance tests... // explicit checkers have priority over instance tests...
for(var t of types){ for(var t of types){
@ -565,6 +573,28 @@ var Types = {
}, },
reverse: function(diff){
var that = this
var res = []
this.walk(diff, function(change){
var c = Object.assign({}, change)
// path...
c.path = c.path.slice().map(function(e){
return e instanceof Array ?
e.slice().reverse()
: e })
that.types.forEach(function(type){
type.reverse
&& (c = type.reverse.call(that, c)) })
res.push(c)
})
return res
},
// User API... // User API...
// Build a diff between A and B... // Build a diff between A and B...
@ -680,16 +710,6 @@ var Types = {
return obj return obj
}, },
// Reverse diff...
//
// XXX should we do this or reverse patch / undo-patch???
reverse: function(diff){
// XXX
//this.walk(diff, function(change){
// // XXX
//})
},
// Check if diff is applicable to obj... // Check if diff is applicable to obj...
// //
check: function(diff, obj, options){ check: function(diff, obj, options){
@ -760,6 +780,12 @@ var Types = {
// walk: function(diff, func, path){ // walk: function(diff, func, path){
// .. // ..
// }, // },
//
// // Reverse the change...
// //
// reverse: function(change){
// ..
// },
// } // }
// //
// //
@ -804,6 +830,15 @@ Types.set('Basic', {
delete change.type delete change.type
return func(change) return func(change)
}, },
reverse: function(change){
var t = change.B
var b = 'B' in change
'A' in change
&& (change.B = change.A)
b
&& (change.A = t)
return change
},
}) })
@ -997,6 +1032,12 @@ Types.set(Array, {
return obj return obj
}, },
reverse: function(change){
if('length' in change){
change.length = change.length.slice().reverse()
}
return change
},
// part handlers... // part handlers...
items: function(diff, A, B, options){ items: function(diff, A, B, options){
@ -1270,6 +1311,7 @@ function(diff, obj, options, types){
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// XXX
var DiffClassPrototype = { var DiffClassPrototype = {
// system meta information... // system meta information...
format: 'object-diff', format: 'object-diff',
@ -1289,6 +1331,7 @@ var DiffClassPrototype = {
}, },
} }
// XXX
var DiffPrototype = { var DiffPrototype = {
// system meta information... // system meta information...
get format(){ get format(){