How to find the keyword, search the first occurance , take decision and then move to substring for further analysis in JAVA SCRIPT [closed]

Java Script is not my first languages, need this to write firebase cloud function, please help and here is my problem:

Trying to covert few json diffs into a logical message about the operation occurred, which included multiple operations as find the keyword, take a decision and then move forward with next remaining string.

Here is my json diff in different operations :

  1. When added a new key

{ parameters: { [32m+ newer_value: { [39m [32m+ defaultValue: { [39m [32m+ value: “gaa_new” [39m [32m+ } [39m [32m+ } [39m } version: { [31m- versionNumber: “498” [39m [32m+ versionNumber: “499” [39m [31m- updateTime: “2021-03-09T17:57:08.755697Z” [39m [32m+ updateTime: “2021-03-09T18:01:05.485532Z” [39m } }

Expected Output Message :

A new key is added,

key name : newer_value

value : gaa_new

{ parameters: { [32m+ add_config: { [39m [32m+ defaultValue: { [39m [32m+ value: “test” [39m [32m+ } [39m [32m+ } [39m } version: { [31m- versionNumber: “500” [39m [32m+ versionNumber: “501” [39m [31m- updateTime: “2021-03-09T18:14:14.954691Z” [39m [32m+ updateTime: “2021-03-09T18:19:15.276567Z” [39m } }

Expected Output Message :

A new key is added,

key name : add_config

value : test

  1. When remove multiple keys :

{ parameters: { [31m- dummy_value: { [39m [31m- defaultValue: { [39m [31m- value: “10” [39m [31m- } [39m [31m- } [39m [31m- newer_value: { [39m [31m- defaultValue: { [39m [31m- value: “gaa_new” [39m [31m- } [39m [31m- } [39m } version: { [31m- versionNumber: “499” [39m [32m+ versionNumber: “500” [39m [31m- updateTime: “2021-03-09T18:01:05.485532Z” [39m [32m+ updateTime: “2021-03-09T18:14:14.954691Z” [39m } }

Expected Output Message :

few existing keys are removed,

a. key name : dummy_value

value : 10

b. key name : newer_value

value : gaa_new

  1. When same key value is updated :

{ parameters: { dummy_value: { defaultValue: { [31m- value: “100000” [39m [32m+ value: “10” [39m } } } version: { [31m- versionNumber: “497” [39m [32m+ versionNumber: “498” [39m [31m- updateTime: “2021-03-09T17:45:47.053923Z” [39m [32m+ updateTime: “2021-03-09T17:57:08.755697Z” [39m } }

Expected Output Message :

key name : dummy_value

old value : 100000

new value : 10

31m+  ->   addition of something
31m-  ->   deletion of something

Please help me how to proceed with that, if there is any library to achieve that then also please let me know and if not then how to traverse a string and evaluate the same.

UPDATE 1 : Here I’m mentioning my theoretical approach : When encountered “parameters” string, will look for { or {31m+ {31m- based on that would identifiy the operation as Updated, Added or deleted respectively , and the key would be identified as next string before : and after { or {32m+ or {31m- and so on.

UPDATE 2 : I used json diff without color codes as suggested by “Nina” in the comments but somehow that is not giving correct result when I am trying to send that into my email utility.

Answer

You could separate the lines with escape colors and check the signs.

const
    process = string => {
        const lines = string.match(/[3dm.*?[39m/g).map(s => s.slice(4, -4));
        let colon = lines[0].indexOf(':'),
            i = 0,
            key = '';

        if (lines[0][0] === '-' && lines[1][0] === '+' && lines[0].slice(1, colon) === lines[1].slice(1, colon)) {
            return [
                'UPDATE A KEY',
                `key name : ${lines[0].slice(1, colon).trim()}`,
                `old value: ${JSON.parse(lines[0].slice(colon + 1))}`,
                `new value: ${JSON.parse(lines[1].slice(colon + 1))}`
            ];
        }

        while (lines[i].slice(colon + 1).trim() === '{') {
            key += (key && '.') + lines[i].slice(1, colon).trim();
            colon = lines[++i].indexOf(':');
        }

        key += (key && '.') + lines[i].slice(1, colon).trim();

        return [
            lines[0][0] === '-' ? 'DELETE A KEY' : 'ADD A KEY',
            `key name: ${key}`,
            `value   : ${JSON.parse(lines[i].slice(colon + 1))}`,
        ];
    },
    data = [
        '{ parameters: { [32m+ newer_value: { [39m [32m+ defaultValue: { [39m [32m+ value: "gaa_new" [39m [32m+ } [39m [32m+ } [39m } version: { [31m- versionNumber: "498" [39m [32m+ versionNumber: "499" [39m [31m- updateTime: "2021-03-09T17:57:08.755697Z" [39m [32m+ updateTime: "2021-03-09T18:01:05.485532Z" [39m } }',
        '{ parameters: { [32m+ add_config: { [39m [32m+ defaultValue: { [39m [32m+ value: "test" [39m [32m+ } [39m [32m+ } [39m } version: { [31m- versionNumber: "500" [39m [32m+ versionNumber: "501" [39m [31m- updateTime: "2021-03-09T18:14:14.954691Z" [39m [32m+ updateTime: "2021-03-09T18:19:15.276567Z" [39m } }',
        '{ parameters: { [31m- dummy_value: { [39m [31m- defaultValue: { [39m [31m- value: "10" [39m [31m- } [39m [31m- } [39m [31m- newer_value: { [39m [31m- defaultValue: { [39m [31m- value: "gaa_new" [39m [31m- } [39m [31m- } [39m } version: { [31m- versionNumber: "499" [39m [32m+ versionNumber: "500" [39m [31m- updateTime: "2021-03-09T18:01:05.485532Z" [39m [32m+ updateTime: "2021-03-09T18:14:14.954691Z" [39m } }',
        '{ parameters: { dummy_value: { defaultValue: { [31m- value: "100000" [39m [32m+ value: "10" [39m } } } version: { [31m- versionNumber: "497" [39m [32m+ versionNumber: "498" [39m [31m- updateTime: "2021-03-09T17:45:47.053923Z" [39m [32m+ updateTime: "2021-03-09T17:57:08.755697Z" [39m } }'
    ];

console.log(data.map(process));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Leave a Reply

Your email address will not be published. Required fields are marked *