Skip to content

Instantly share code, notes, and snippets.

@mfbx9da4
Created April 21, 2020 12:08
Show Gist options
  • Save mfbx9da4/55d2a60c267987dbdde1749653286ff7 to your computer and use it in GitHub Desktop.
Save mfbx9da4/55d2a60c267987dbdde1749653286ff7 to your computer and use it in GitHub Desktop.
edges = [
{
node: {
description: null,
namespace: "arena",
valueType: "JSON_STRING",
value:
'["https://babywipeseco.myshopify.com/collections/nappies","https://babywipeseco.myshopify.com/collections/wipes","https://babywipeseco.myshopify.com/collections/"]',
id: "gid://shopify/Metafield/12637127966851",
key: "app_home__link",
},
},
{
node: {
description: null,
namespace: "arena",
valueType: "JSON_STRING",
value:
'["https://cdn.arenacommerce.com/babywipeseco/PuraNappiesBanner.png","https://cdn.arenacommerce.com/babywipeseco/PuraWipesBanner.png","https://cdn.arenacommerce.com/babywipeseco/TestBanner.gif"]',
id: "gid://shopify/Metafield/12637127999619",
key: "app_home__image",
},
},
{
node: {
description: null,
namespace: "arena",
valueType: "INTEGER",
value: "1",
id: "gid://shopify/Metafield/12637127999619",
key: "app_home__aggregations__count",
},
},
{
node: {
description: null,
namespace: "arena",
valueType: "STRING",
value: "Summer Sale 20% Off all orders",
id: "gid://shopify/Metafield/12637281747075",
key: "app_home_usp",
},
},
];
transformMetaFields = (metaFields) => {
const ret = {};
for (let i = 0; i < metaFields.length; i++) {
const item = metaFields[i];
if (item && item.node && item.node.value) {
const { node } = item;
let { value } = node;
if (node.valueType === "JSON_STRING") {
value = JSON.parse(node.value);
} else if (node.valueType === "INTEGER") {
value = parseInt(node.value, 10);
}
const keys = node.key.split("__");
let dict = ret;
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (i === keys.length - 1) {
dict[key] = value;
} else {
if (!(key in dict)) dict[key] = {};
dict = dict[key];
}
}
}
}
return ret;
};
transformMetaFields(edges);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment