|
| 1 | +import _, { isEqual, map } from 'lodash'; |
| 2 | + |
| 3 | +export const compare = (a, b) => { |
| 4 | + const result = { |
| 5 | + different: [], |
| 6 | + missing_from_first: [], |
| 7 | + missing_from_second: [], |
| 8 | + }; |
| 9 | + |
| 10 | + _.reduce( |
| 11 | + a, |
| 12 | + function (result, value, key) { |
| 13 | + if (Object.prototype.hasOwnProperty.call(b, key)) { |
| 14 | + if (isEqual(value, b[key])) { |
| 15 | + return result; |
| 16 | + } else { |
| 17 | + if (typeof a[key] != typeof {} || typeof b[key] != typeof {}) { |
| 18 | + //dead end. |
| 19 | + result.different.push(key); |
| 20 | + return result; |
| 21 | + } else { |
| 22 | + var deeper = compare(a[key], b[key]); |
| 23 | + result.different = result.different.concat( |
| 24 | + map(deeper.different, (sub_path) => { |
| 25 | + return key + '.' + sub_path; |
| 26 | + }), |
| 27 | + ); |
| 28 | + |
| 29 | + result.missing_from_second = result.missing_from_second.concat( |
| 30 | + map(deeper.missing_from_second, (sub_path) => { |
| 31 | + return key + '.' + sub_path; |
| 32 | + }), |
| 33 | + ); |
| 34 | + |
| 35 | + result.missing_from_first = result.missing_from_first.concat( |
| 36 | + map(deeper.missing_from_first, (sub_path) => { |
| 37 | + return key + '.' + sub_path; |
| 38 | + }), |
| 39 | + ); |
| 40 | + return result; |
| 41 | + } |
| 42 | + } |
| 43 | + } else { |
| 44 | + result.missing_from_second.push(key); |
| 45 | + return result; |
| 46 | + } |
| 47 | + }, |
| 48 | + result, |
| 49 | + ); |
| 50 | + |
| 51 | + _.reduce( |
| 52 | + b, |
| 53 | + function (result, value, key) { |
| 54 | + if (Object.prototype.hasOwnProperty.call(a, key)) { |
| 55 | + return result; |
| 56 | + } else { |
| 57 | + result.missing_from_first.push(key); |
| 58 | + return result; |
| 59 | + } |
| 60 | + }, |
| 61 | + result, |
| 62 | + ); |
| 63 | + |
| 64 | + return result; |
| 65 | +}; |
0 commit comments