Last active
June 11, 2023 17:04
-
-
Save moaaz-bhnas/2c538e33282372e428be552858d7b136 to your computer and use it in GitHub Desktop.
A function that removes edges and nodes from Shopify Storefront data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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