Skip to content

Instantly share code, notes, and snippets.

@moaaz-bhnas
Last active June 11, 2023 17:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moaaz-bhnas/2c538e33282372e428be552858d7b136 to your computer and use it in GitHub Desktop.
Save moaaz-bhnas/2c538e33282372e428be552858d7b136 to your computer and use it in GitHub Desktop.
A function that removes edges and nodes from Shopify Storefront data
function isArray(data: any): Boolean {
return data && data.constructor === Array;
}
function isObject(data: any): Boolean {
return data && data.constructor === Object;
}
function withoutEdgesAndNodes(data: any): any {
let result = Array.isArray(data) ? [] : {};
if (!isObject(data) && !isArray(data)) return data;
for (const key in data) {
if (typeof key === "string" && key === "edges") {
result = withoutEdgesAndNodes(data.edges.map((edge: any) => edge.node));
} else {
result = Object.assign(result, {
[key]: withoutEdgesAndNodes(data[key]),
});
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment