src/json-patch-duplex.js
// Dirty check if obj is different from mirror, generate patches and update mirror
Dirty check if obj is different from mirror, generate patches and update mirror
Dirty check if obj is different from mirror generate patches and update mirror
function _generate(mirror, obj, patches, path) {
        var newKeys = Object.keys(obj);
        var oldKeys = Object.keys(mirror);
        var changed = false;
        var deleted = false;
        var added = false;
        for(var t = 0; t < oldKeys.length; t++) {
            var key = oldKeys[t];
            var oldVal = mirror[key];
            if(obj.hasOwnProperty(key)) {
                var newVal = obj[key];
                if(oldVal instanceof Object) {
                    _generate(oldVal, newVal, patches, path + "/" + key);
                } else {
                    if(oldVal != newVal) {
                        changed = true;
                        patches.push({
                            op: "replace",
                            path: path + "/" + key,
                            value: newVal
                        });
                        mirror[key] = newVal;
                    }
                }
            } else {
                patches.push({
                    op: "remove",
                    path: path + "/" + key
                });
                deleted = true
                ;
            }
        }
        if(!deleted && newKeys.length == oldKeys.length) {
            return;
        }
        for(var t = 0; t < newKeys.length; t++) {
            var key = newKeys[t];
            if(!mirror.hasOwnProperty(key)) {
                patches.push({
                    op: "add",
                    path: path + "/" + key,
                    value: obj[key]
                });
            }
        }
    }
function _generate ( mirror , obj , patches , path ) { var newKeys = Object . keys ( obj ) ; var oldKeys = Object . keys ( mirror ) ; var changed = false ; var deleted = false ; var added = false ; for ( var t = 0 ; t < oldKeys . length ; t ++ ) { var key = oldKeys [ t ] ; var oldVal = mirror [ key ] ; if ( obj . hasOwnProperty ( key ) ) { var newVal = obj [ key ] ; if ( oldVal instanceof Object ) { _generate ( oldVal , newVal , patches , path + "/" + key ) ; } else { if ( oldVal != newVal ) { changed = true ; patches . push ( { op : "replace" , path : path + "/" + key , value : newVal } ) ; mirror [ key ] = newVal ; } } } else { patches . push ( { op : "remove" , path : path + "/" + key } ) ; deleted = true ; } } if ( ! deleted && newKeys . length == oldKeys . length ) { return ; } for ( var t = 0 ; t < newKeys . length ; t ++ ) { var key = newKeys [ t ] ; if ( ! mirror . hasOwnProperty ( key ) ) { patches . push ( { op : "add" , path : path + "/" + key , value : obj [ key ] } ) ; } } }
Dirty check if obj is different from mirror, generate patches and update mirror
Dirty check if obj is different from mirror generate patches and update mirror
// property has been deleted