add function to show diff between two objects
This commit is contained in:
parent
8a992aa440
commit
3e1b124bd9
@ -1,6 +1,7 @@
|
|||||||
import { ITrackable } from '../listing/models/trackable';
|
import { ITrackable } from '../listing/models/trackable';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import { FormGroup } from '@angular/forms';
|
import { FormGroup } from '@angular/forms';
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
export function capitalize(value: string): string {
|
export function capitalize(value: string): string {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
@ -82,3 +83,33 @@ export function getLeftDateTime(ISOString: string) {
|
|||||||
|
|
||||||
return { daysLeft, hoursLeft, minutesLeft };
|
return { daysLeft, hoursLeft, minutesLeft };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function deepDiffObj(base: Record<string, unknown>, object: Record<string, unknown>) {
|
||||||
|
if (!object) {
|
||||||
|
throw new Error(`The object compared should be an object: ${object}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!base) {
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = _.transform(object, (result: Record<string, unknown>, value, key) => {
|
||||||
|
if (!_.has(base, key)) {
|
||||||
|
result[key] = value;
|
||||||
|
} // fix edge case: not defined to explicitly defined as undefined
|
||||||
|
if (!_.isEqual(value, base[key])) {
|
||||||
|
result[key] =
|
||||||
|
_.isPlainObject(value) && _.isPlainObject(base[key])
|
||||||
|
? deepDiffObj(base[key] as Record<string, unknown>, value as Record<string, unknown>)
|
||||||
|
: value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// map removed fields to undefined
|
||||||
|
_.forOwn(base, (value, key) => {
|
||||||
|
if (!_.has(object, key)) {
|
||||||
|
res[key] = undefined;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user