notes + found several bugs...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-07-17 02:08:26 +03:00
parent 167a127f51
commit 89098cb298

47
diff.js
View File

@ -33,8 +33,6 @@
// NOTE: for Array items this does not shift positions of other item // NOTE: for Array items this does not shift positions of other item
// positions nor does it affect the the array lengths. // positions nor does it affect the the array lengths.
var EMPTY = {type: 'EMPTY'} var EMPTY = {type: 'EMPTY'}
var NONE = {type: 'NONE'} var NONE = {type: 'NONE'}
@ -86,9 +84,10 @@ var zip = function(func, ...arrays){
: [] } : [] }
// get common chuncs (LCS)... // get common chunks (LCS)...
// XXX handle sparse arrays correctly... // XXX handle sparse arrays correctly...
// ...now empty slots get filled with undefined... // ...now empty slots get filled with undefined...
// XXX this does not distinguish empty and null/undefined...
var getCommonSections = function(A, B, cmp, min_chunk){ var getCommonSections = function(A, B, cmp, min_chunk){
cmp = cmp || function(a, b){ cmp = cmp || function(a, b){
return a === b || a == b } return a === b || a == b }
@ -309,11 +308,8 @@ var partHandlers = {
// //
// // holds both index and attribute keys (mode-dependant)... // // holds both index and attribute keys (mode-dependant)...
// items: [ // items: [
// // Simple item diff... // // NOTE: if an item does not exist in either A or B its
// // XXX unused.... // // key will be null...
// [<key>, <diff>],
//
// // [S]plice section starting at key...
// [<key-a>, <key-b>, <diff>], // [<key-a>, <key-b>, <diff>],
// //
// ... // ...
@ -394,6 +390,17 @@ Types.handle = function(type, obj, ...args){
// XXX do we need to differentiate things like: new Number(123) vs. 123??? // XXX do we need to differentiate things like: new Number(123) vs. 123???
// XXX check seen -- avoid recursion... // XXX check seen -- avoid recursion...
// XXX support Map(..) and other new-style types... // XXX support Map(..) and other new-style types...
// XXX TEST: the format should survive JSON.parse(JSON.stringify(..))...
// XXX BUGS:
// _diff(new Array(1), [null | undefined])
// -> null, should be a change
// NOTE: passing a NaN will yield correct results...
// ...the problem is in getCommonSections(..) not
// distinguishing between null and empty...
// _diff(new Array(5), [])
// -> will only catch the length change...
// ...the problem is in getCommonSections(..) not
// distinguishing between null and empty...
var _diff = var _diff =
function(A, B, options, cache){ function(A, B, options, cache){
// XXX might be a god idea to mix in default options (different // XXX might be a god idea to mix in default options (different
@ -438,9 +445,9 @@ function(A, B, options, cache){
// is not last it will match any set of items... // is not last it will match any set of items...
var type = Object var type = Object
for(var t of Types.keys()){ for(var t of Types.keys()){
// skip non-conctructor stuff... // leave pure objects for last...
if(t === Object if(t === Object
// leave pure objects for last... // skip non-conctructor stuff...
|| !(t instanceof Function)){ || !(t instanceof Function)){
continue continue
} }
@ -480,6 +487,7 @@ function(A, B, options, cache){
} }
// Flatten the diff format...
// //
// Format: // Format:
// [ // [
@ -512,8 +520,13 @@ function(A, B, options, cache){
// ... // ...
// ] // ]
// //
// NOTE: all indexes (arrays) are given within the actual object, not // NOTE: all indexes (for arrays) are given relative to the actual input
// accounting for the patch process. // objects respectively as they were given. This does not account for
// the patch process.
// NOTE: this will lose some meta-information the diff format contains
// like the type information which is not needed for patching but
// may be useful for a more thorough compatibility check.
//
// //
// XXX does change order matter here??? // XXX does change order matter here???
// ...some changes can affect changes after them (like splicing // ...some changes can affect changes after them (like splicing
@ -524,6 +537,7 @@ function(A, B, options, cache){
// ...i.e. type handlers etc. // ...i.e. type handlers etc.
// ......or this could be more generic... // ......or this could be more generic...
// XXX we should be able to provide "fuzz" (context) to the changes... // XXX we should be able to provide "fuzz" (context) to the changes...
// XXX TEST: the format should survive JSON.parse(JSON.stringify(..))...
var flatten = var flatten =
function(diff, res, path, options){ function(diff, res, path, options){
res = res || [] res = res || []
@ -544,14 +558,15 @@ function(diff, res, path, options){
// Array... // Array...
} else if(diff.type == 'Array'){ } else if(diff.type == 'Array'){
// length changed... // length...
if(diff.length != null){ // XXX should this be given after all the element changes???
res.push({ // ...but it should be before all the nested changes...
;(diff.length != null)
&& res.push({
path: path.concat('length'), path: path.concat('length'),
A: diff.length[0], A: diff.length[0],
B: diff.length[1], B: diff.length[1],
}) })
}
// items... // items...
;(diff.items || []) ;(diff.items || [])
.forEach(function(e){ .forEach(function(e){