sample-proxies/json-patch-app-services/apiproxy/resources/jsc/lib.json-patch-duplex.js
/// Apply a json-patch operation on an object tree
Apply a json-patch operation on an object tree
Apply a json - patch operation on an object tree
function apply(tree, patches, listen) {
        try  {
            patches.forEach(function (patch) {
                var keys = patch.path.split('/');
                keys.shift()
                ;
                var obj = tree;
                var t = 0;
                var len = keys.length;
                while(true) {
                    if(obj instanceof Array) {
                        var index = parseInt(keys[t], 10);
                        t++;
                        if(t >= len) {
                            arrOps[patch.op].call(patch, obj, index, tree)
                            ;
                            break;
                        }
                        obj = obj[index];
                    } else {
                        var key = keys[t];
                        if(key.indexOf('~') != -1) {
                            key = key.replace('~1', '/').replace('~0', '~');
                        }
                        t++;
                        if(t >= len) {
                            objOps[patch.op].call(patch, obj, key, tree)
                            ;
                            break;
                        }
                        obj = obj[key];
                    }
                }
            });
        } catch (e) {
            return false;
        }
        return true;
    }
function apply ( tree , patches , listen ) { try { patches . forEach ( function ( patch ) { var keys = patch . path . split ( '/' ) ; keys . shift ( ) ; var obj = tree ; var t = 0 ; var len = keys . length ; while ( true ) { if ( obj instanceof Array ) { var index = parseInt ( keys [ t ] , 10 ) ; t ++ ; if ( t >= len ) { arrOps [ patch . op ] . call ( patch , obj , index , tree ) ; break ; } obj = obj [ index ] ; } else { var key = keys [ t ] ; if ( key . indexOf ( '~' ) != - 1 ) { key = key . replace ( '~1' , '/' ) . replace ( '~0' , '~' ) ; } t ++ ; if ( t >= len ) { objOps [ patch . op ] . call ( patch , obj , key , tree ) ; break ; } obj = obj [ key ] ; } } } ) ; } catch ( e ) { return false ; } return true ; }
Apply a json-patch operation on an object tree
Apply a json - patch operation on an object tree
// Find the object // Remove empty element // Apply patch // escape chars // Apply patch