mirror of
https://github.com/flynx/diff.js.git
synced 2025-10-29 11:00:12 +00:00
revising the format + some docs...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
0442525606
commit
167a127f51
38
diff.js
38
diff.js
@ -217,8 +217,8 @@ var partHandlers = {
|
|||||||
return zip(
|
return zip(
|
||||||
function(n, elems){
|
function(n, elems){
|
||||||
return [
|
return [
|
||||||
i+n,
|
0 in elems ? i+n : null,
|
||||||
j+n,
|
1 in elems ? j+n : null,
|
||||||
diff(
|
diff(
|
||||||
0 in elems ? elems[0] : NONE,
|
0 in elems ? elems[0] : NONE,
|
||||||
1 in elems ? elems[1] : NONE,
|
1 in elems ? elems[1] : NONE,
|
||||||
@ -492,12 +492,19 @@ function(A, B, options, cache){
|
|||||||
// // NOTE: this is actually not different
|
// // NOTE: this is actually not different
|
||||||
// // from a string...
|
// // from a string...
|
||||||
// // array/2 - a set of 2 keys for A and B respectively
|
// // array/2 - a set of 2 keys for A and B respectively
|
||||||
|
// // NOTE: if one of the array items in undefined
|
||||||
|
// // or null then it means that the item
|
||||||
|
// // does not exist in the corresponding
|
||||||
|
// // array...
|
||||||
// path: [<key>, ...],
|
// path: [<key>, ...],
|
||||||
//
|
//
|
||||||
// // values in A and B...
|
// // values in A and B...
|
||||||
// //
|
// //
|
||||||
// // Special values:
|
// // Special values:
|
||||||
// // NONE - the slot does not exist (splice)
|
// // NONE - the slot does not exist (splice)
|
||||||
|
// // NOTE: unless options.keep_none is true,
|
||||||
|
// // NONE elements are not included in the
|
||||||
|
// // change...
|
||||||
// // EMPTY - the slot exists but it is empty (set/delete)
|
// // EMPTY - the slot exists but it is empty (set/delete)
|
||||||
// A: <value> | EMPTY | NONE,
|
// A: <value> | EMPTY | NONE,
|
||||||
// B: <value> | EMPTY | NONE,
|
// B: <value> | EMPTY | NONE,
|
||||||
@ -505,6 +512,9 @@ function(A, B, options, cache){
|
|||||||
// ...
|
// ...
|
||||||
// ]
|
// ]
|
||||||
//
|
//
|
||||||
|
// NOTE: all indexes (arrays) are given within the actual object, not
|
||||||
|
// accounting for the patch process.
|
||||||
|
//
|
||||||
// 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
|
||||||
// with arrays), this ultimately affects how patching is done...
|
// with arrays), this ultimately affects how patching is done...
|
||||||
@ -513,10 +523,12 @@ function(A, B, options, cache){
|
|||||||
// XXX should this follow the same extensible structure as _diff???
|
// XXX should this follow the same extensible structure as _diff???
|
||||||
// ...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...
|
||||||
var flatten =
|
var flatten =
|
||||||
function(diff, res, path){
|
function(diff, res, path, options){
|
||||||
res = res || []
|
res = res || []
|
||||||
path = path || []
|
path = path || []
|
||||||
|
options = options || {}
|
||||||
|
|
||||||
// no difference...
|
// no difference...
|
||||||
if(diff == null){
|
if(diff == null){
|
||||||
@ -532,6 +544,7 @@ function(diff, res, path){
|
|||||||
|
|
||||||
// Array...
|
// Array...
|
||||||
} else if(diff.type == 'Array'){
|
} else if(diff.type == 'Array'){
|
||||||
|
// length changed...
|
||||||
if(diff.length != null){
|
if(diff.length != null){
|
||||||
res.push({
|
res.push({
|
||||||
path: path.concat('length'),
|
path: path.concat('length'),
|
||||||
@ -539,15 +552,28 @@ function(diff, res, path){
|
|||||||
B: diff.length[1],
|
B: diff.length[1],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
// items...
|
||||||
;(diff.items || [])
|
;(diff.items || [])
|
||||||
.forEach(function(e){
|
.forEach(function(e){
|
||||||
|
var v = e[2]
|
||||||
var i = e[0] == e[1] ?
|
var i = e[0] == e[1] ?
|
||||||
e[0]
|
e[0]
|
||||||
: [e[0], e[1]]
|
: [e[0], e[1]]
|
||||||
var v = e[2]
|
|
||||||
var p = path.concat([i])
|
var p = path.concat([i])
|
||||||
|
|
||||||
flatten(v, res, p)
|
if(!options.keep_none
|
||||||
|
&& (v.A === NONE || v.B === NONE)){
|
||||||
|
// NOTE: we do not need to flatten(..) this as
|
||||||
|
// it is guaranteed not to be a diff...
|
||||||
|
res.push({
|
||||||
|
path: p,
|
||||||
|
// write only the value that is not NONE...
|
||||||
|
[v.A === NONE ? 'B' : 'A']: v.A === NONE ? v.B : v.A,
|
||||||
|
})
|
||||||
|
|
||||||
|
} else {
|
||||||
|
flatten(v, res, p, options)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Object...
|
// Object...
|
||||||
@ -558,7 +584,7 @@ function(diff, res, path){
|
|||||||
var v = e[1]
|
var v = e[1]
|
||||||
var p = path.concat([i])
|
var p = path.concat([i])
|
||||||
|
|
||||||
flatten(v, res, p)
|
flatten(v, res, p, options)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Other...
|
// Other...
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user