mirror of
https://github.com/flynx/diff.js.git
synced 2025-10-29 02:50:10 +00:00
added deep cmp(..)...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
f4f174de81
commit
28128ca3a1
21
diff.js
21
diff.js
@ -542,6 +542,8 @@ var Types = {
|
||||
// NOTE: this will include direct links to items.
|
||||
// NOTE: for format info see doc for Types...
|
||||
//
|
||||
// XXX might be a good idea to make a .walk(..) version of this...
|
||||
// ...i.e. pass a function a nd call it with each change...
|
||||
// XXX special case: empty sections do not need to be inserted...
|
||||
// ...splice in a sparse array and store an Array diff with only
|
||||
// length changed...
|
||||
@ -952,6 +954,19 @@ Types.set('Text', {
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// Deep-compare objects...
|
||||
//
|
||||
// XXX would be nice to do a fast fail version of this, i.e. fail on
|
||||
// first mismatch and do not waste time compiling a full diff we
|
||||
// are going to throw away anyway...
|
||||
// ...this would be possible with a live .walk(..) that would
|
||||
// report changes as it finds them...
|
||||
var cmp =
|
||||
module.cmp =
|
||||
function(A, B){
|
||||
return Types.diff(A, B) == null ? true : false }
|
||||
|
||||
|
||||
// Diff interface function...
|
||||
//
|
||||
// This is a front-end to Types.diff(..), adding a metadata wrapper to
|
||||
@ -1091,7 +1106,7 @@ var _patch = function(diff, obj){
|
||||
if(type == 'item'){
|
||||
// object attr...
|
||||
if(typeof(key) == typeof('str')){
|
||||
if(change.B.type == EMPTY.type){
|
||||
if(cmp(change.B, EMPTY)){
|
||||
delete target[key]
|
||||
|
||||
} else {
|
||||
@ -1109,10 +1124,10 @@ var _patch = function(diff, obj){
|
||||
|
||||
} else if(j == null){
|
||||
// XXX better EMPTY check -- use diff
|
||||
if(!('B' in change) || change.B.type == EMPTY.type){
|
||||
if(!('B' in change) || cmp(change.B, EMPTY)){
|
||||
delete target[i]
|
||||
|
||||
} else if(!('B' in change) || change.B.type == NONE.type){
|
||||
} else if(!('B' in change) || cmp(change.B, NONE)){
|
||||
target.splice(i, 1)
|
||||
|
||||
} else {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user