All files / switchman-core/src/json-core core.js

94.44% Statements 17/18
75% Branches 6/8
100% Functions 1/1
94.11% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 462x 2x   2x   51x 2x     49x       49x 49x                           49x   392x   392x 66x 66x     326x     49x       2x  
const jsonPatch = require('fast-json-patch');
const jsonFormat = require('json-format');
 
const core = {
    patch(patchStr, targetStr, format) {
        if (typeof patchStr != 'string') {
            throw new Error('$ failed! patchStr is not string');
        }
 
        Iif (typeof targetStr != 'string') {
            throw new Error('$ failed! targetStr is not string');
        }
 
        let target = JSON.parse(targetStr);
        const patches = JSON.parse(patchStr);
 
        // let errors = jsonPatch.validate(patches, target);
 
        // for (let i = 0; i < errors.length; i++) {
        //     if (!errors[i]) {
        //         console.log("Valid patch at index", i, patches[i]);
        //     }
        //     else {
        //         console.error("Invalid patch at index", i, errors[i], patches[i]);
        //     }
        // }
 
 
        for (let i = 0; i < patches.length; i++) {
 
            let error = jsonPatch.validate([patches[i]], target);
 
            if (error) {
                Eif (error.operation.op === 'replace') {
                    continue;
                }
            }
            target = jsonPatch.applyOperation(target, patches[i]).newDocument;
        }
 
        return jsonFormat(target, format);
    }
};
 
module.exports = core;