Skip to content

Instantly share code, notes, and snippets.

@christian-schulze
Created August 11, 2019 04:55
Show Gist options
  • Save christian-schulze/1c116cb4ee1eedaef21eba8061e6aa55 to your computer and use it in GitHub Desktop.
Save christian-schulze/1c116cb4ee1eedaef21eba8061e6aa55 to your computer and use it in GitHub Desktop.
const exception = {
data: {
error: {
type: "Runtime Error",
description: "asdf asdf asdf asdf",
handled: true
},
extension: {
response: {
statusCode: 500,
StatusText: "Service Unavailable"
},
exception: {},
correlationId: "13287421389746123897467812364",
stacktrace: []
}
}
};
const whitelist = ["data.error", "data.extension.correlationId"];
export const filterObject = (originalObject, propertiesWhiteList) => {
const newObject = {};
propertiesWhiteList.forEach(properties => {
const propertyStack = properties.split(".");
const maxDepth = propertyStack.length - 1;
let currentDepthNew = newObject;
let currentDepthOriginal = originalObject;
propertyStack.forEach((property, depth) => {
if (property in currentDepthOriginal) {
if (depth === maxDepth) {
currentDepthNew[property] = currentDepthOriginal[property];
} else {
if (!(property in currentDepthNew)) {
currentDepthNew[property] = {};
}
currentDepthNew = currentDepthNew[property];
currentDepthOriginal = currentDepthOriginal[property];
}
}
});
});
return newObject;
};
const filteredObject = filterObject(exception, whitelist);
console.log(filteredObject);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment