2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 var deepDiffMapper = function() {
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.';
32 if (this.isValue(obj1) || this.isValue(obj2)) {
34 type: this.compareValues(obj1, obj2),
35 data: (obj1 === undefined) ? obj2 : obj1
40 for (var key in obj1) {
41 if (this.isFunction(obj1[key])) {
45 var value2 = undefined;
46 if ('undefined' != typeof(obj2[key])) {
50 diff[key] = this.map(obj1[key], value2);
52 for (var key in obj2) {
53 if (this.isFunction(obj2[key]) || ('undefined' != typeof(diff[key]))) {
57 diff[key] = this.map(undefined, obj2[key]);
63 compareValues: function(value1, value2) {
64 if (value1 === value2) {
65 return this.VALUE_UNCHANGED;
67 if (this.isDate(value1) && this.isDate(value2) && value1.getTime() === value2.getTime()) {
68 return this.VALUE_UNCHANGED;
70 if ('undefined' == typeof(value1)) {
71 return this.VALUE_CREATED;
73 if ('undefined' == typeof(value2)) {
74 return this.VALUE_DELETED;
77 return this.VALUE_UPDATED;
79 isFunction: function(obj) {
80 return {}.toString.apply(obj) === '[object Function]';
82 isArray: function(obj) {
83 return {}.toString.apply(obj) === '[object Array]';
85 isObject: function(obj) {
86 return {}.toString.apply(obj) === '[object Object]';
88 isDate: function(obj) {
89 return {}.toString.apply(obj) === '[object Date]';
91 isValue: function(obj) {
92 return !this.isObject(obj) && !this.isArray(obj);
98 export default deepDiffMapper;