Skip to content

Instantly share code, notes, and snippets.

@Robokishan
Created March 11, 2022 05:20
Show Gist options
  • Save Robokishan/bf4a11c563d3aa79eedf932875c58e2d to your computer and use it in GitHub Desktop.
Save Robokishan/bf4a11c563d3aa79eedf932875c58e2d to your computer and use it in GitHub Desktop.
diff json
export const getDeletedItems = (
primarykey: string,
oldValues: Array<any>,
newValues: Array<any>
): Array<any> => {
return oldValues.filter((old) => {
return !newValues.some((newValue) => {
return newValue[primarykey] === old[primarykey];
});
});
};
export const getAddedItems = (
primarykey: string,
oldValues: Array<any>,
newValues: Array<any>
): Array<any> => {
return newValues.filter((newValue) => {
return !newValue[primarykey];
});
};
export const getModifiedItems = (
primarykey: string,
oldValues: Array<any>,
newValues: Array<any>
): Array<any> => {
return oldValues.filter((old) => {
newValues.map((newValue) => {
if (newValue[primarykey] && newValue[primarykey] === old[primarykey]) {
_isEqual(newValue, old);
}
});
});
};
import _isEqual from 'lodash/isEqual';
import _differenceBy from 'lodash/differenceBy';
import _keyBy from 'lodash/keyBy';
import _reduce from 'lodash/reduce';
import _isNil from 'lodash/isNil';
import _pick from 'lodash/pick';
import _map from 'lodash/map';
import _property from 'lodash/property';
const EMPTY_ARRAY = [];
const EMPTY_OBJECT = {};
const getEntriesToBeCreated = (newValues, oldValues, { uniqueIdKeyGetter }) =>
_differenceBy(newValues, oldValues, uniqueIdKeyGetter);
const getEntriesToBeDeleted = (newValues, oldValues, { uniqueIdKeyGetter }) =>
_differenceBy(oldValues, newValues, uniqueIdKeyGetter);
const getEntriesToBeEdited = (
newValues,
oldValues,
{ keysToBeChecked, uniqueId }
) => {
const newValuesMap = _keyBy(newValues, uniqueId);
const oldValuesMap = _keyBy(oldValues, uniqueId);
return _reduce(
oldValuesMap,
(acc, oldValue, key) => {
const newValue = newValuesMap[key];
if (_isNil(newValue)) return acc;
const newValueToBeConsidered = _pick(newValue, keysToBeChecked);
const oldValueToBeConsidered = _pick(oldValue, keysToBeChecked);
if (!_isEqual(oldValueToBeConsidered, newValueToBeConsidered)) {
acc.push(newValue);
}
return acc;
},
[]
);
};
export interface CRUDType {
entriesToBeCreated: any;
entriesToBeUpdated: any;
entriesToBeDeleted: any;
}
export const getCRUDPayload = (
newValues: any,
oldValues: any,
{
keysToBeChecked = EMPTY_ARRAY,
uniqueId = 'id',
uniqueIdKeyGetter = _property(uniqueId),
} = EMPTY_OBJECT
): CRUDType => {
const entriesToBeCreated = getEntriesToBeCreated(newValues, oldValues, {
uniqueIdKeyGetter,
});
const entriesToBeUpdated = getEntriesToBeEdited(newValues, oldValues, {
keysToBeChecked,
uniqueId,
});
const entriesToBeDeleted = _map(
getEntriesToBeDeleted(newValues, oldValues, { uniqueIdKeyGetter }),
uniqueId
);
return {
entriesToBeCreated,
entriesToBeUpdated,
entriesToBeDeleted,
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment