Updated Sparky to add ECOMP functionality Browse, Specialized Search, BYOQ, and the...
[aai/sparky-fe.git] / src / utils / DiffUtil.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21
22 var deepDiffMapper = function() {
23     return {
24         VALUE_CREATED: 'created',
25         VALUE_UPDATED: 'updated',
26         VALUE_DELETED: 'deleted',
27         VALUE_UNCHANGED: 'unchanged',
28         map: function(obj1, obj2) {
29             if (this.isFunction(obj1) || this.isFunction(obj2)) {
30                 throw 'Invalid argument. Function given, object expected.';
31             }
32             if (this.isValue(obj1) || this.isValue(obj2)) {
33                 return {
34                     type: this.compareValues(obj1, obj2),
35                     data: (obj1 === undefined) ? obj2 : obj1
36                 };
37             }
38
39             var diff = {};
40             for (var key in obj1) {
41                 if (this.isFunction(obj1[key])) {
42                     continue;
43                 }
44
45                 var value2 = undefined;
46                 if ('undefined' != typeof(obj2[key])) {
47                     value2 = obj2[key];
48                 }
49
50                 diff[key] = this.map(obj1[key], value2);
51             }
52             for (var key in obj2) {
53                 if (this.isFunction(obj2[key]) || ('undefined' != typeof(diff[key]))) {
54                     continue;
55                 }
56
57                 diff[key] = this.map(undefined, obj2[key]);
58             }
59
60             return diff;
61
62         },
63         compareValues: function(value1, value2) {
64             if (value1 === value2) {
65                 return this.VALUE_UNCHANGED;
66             }
67             if (this.isDate(value1) && this.isDate(value2) && value1.getTime() === value2.getTime()) {
68                         return this.VALUE_UNCHANGED;
69             }
70             if ('undefined' == typeof(value1)) {
71                 return this.VALUE_CREATED;
72             }
73             if ('undefined' == typeof(value2)) {
74                 return this.VALUE_DELETED;
75             }
76
77             return this.VALUE_UPDATED;
78         },
79         isFunction: function(obj) {
80             return {}.toString.apply(obj) === '[object Function]';
81         },
82         isArray: function(obj) {
83             return {}.toString.apply(obj) === '[object Array]';
84         },
85         isObject: function(obj) {
86             return {}.toString.apply(obj) === '[object Object]';
87         },
88         isDate: function(obj) {
89             return {}.toString.apply(obj) === '[object Date]';
90         },
91         isValue: function(obj) {
92             return !this.isObject(obj) && !this.isArray(obj);
93         }
94     }
95 }();
96
97
98 export default deepDiffMapper;