Skip to content

Instantly share code, notes, and snippets.

@artalar
Last active June 1, 2018 16:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artalar/81c1d8798c3a9ad7f986d014b815d7c0 to your computer and use it in GitHub Desktop.
Save artalar/81c1d8798c3a9ad7f986d014b815d7c0 to your computer and use it in GitHub Desktop.
import { inDevMode } from './enviroment';
const isObject = object => typeof object === 'object' && object !== null;
function Logger(location, addSnapshot) {
const logs = [];
this.setLog = (previosObject, newObject, key, description = 'objects not equal') => {
const log = {
previosObject,
newObject,
key,
description,
location,
};
if (addSnapshot) {
try {
log.previosObjectSnapshot = JSON.parse(JSON.stringify(previosObject));
// eslint-disable-next-line
} catch (e) {}
try {
log.newObjectSnapshot = JSON.parse(JSON.stringify(newObject));
// eslint-disable-next-line
} catch (e) {}
}
logs.push(log);
};
this.setLogs = newLogs => logs.push(newLogs);
this.getLogs = () => logs;
}
export const getChangedValues = ({
previosObject,
newObject,
location = '',
deep = true,
addSnapshot = false,
resultToPlainArray = false,
}) => {
// FIXME: определиться с возможной глубиной
if (location.length > 200) {
// eslint-disable-next-line
if (inDevMode()) console.error('too deep object in', location);
return [];
}
const logger = new Logger(location, addSnapshot);
const keysOfPreviosObject = Object.getOwnPropertyNames(previosObject);
const keysOfNewObject = Object.getOwnPropertyNames(newObject);
keysOfNewObject.forEach(newKey => {
if (keysOfPreviosObject.indexOf(newKey) === -1) {
logger.setLog(previosObject, newObject, newKey, 'no key in the previos object');
}
});
keysOfPreviosObject.forEach(prevKey => {
const previosValue = previosObject[prevKey];
const newValue = newObject[prevKey];
if (keysOfNewObject.indexOf(prevKey) === -1) {
logger.setLog(previosObject, newObject, prevKey, 'no key in the new object');
} else if (previosValue !== newValue) {
logger.setLog(previosObject, newObject, prevKey, 'key values is not equal');
if (deep && isObject(previosValue) && isObject(newValue) && prevKey !== 'children') {
const deepLogs = getChangedValues({
previosObject: previosValue,
newObject: newValue,
location: `${location}.${prevKey}`,
});
if (deepLogs.length) {
if (resultToPlainArray) deepLogs.forEach(log => logger.setLog(log));
else logger.setLogs(deepLogs);
}
}
}
});
return logger.getLogs();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment