Skip to content

Instantly share code, notes, and snippets.

@visualjeff
Created January 27, 2021 04:20
Show Gist options
  • Save visualjeff/412f89aecce959fd71513dbc809be3e4 to your computer and use it in GitHub Desktop.
Save visualjeff/412f89aecce959fd71513dbc809be3e4 to your computer and use it in GitHub Desktop.
Script for processing megaMenu. Save to root of forge project as index.js. Run as "node index.js"
// Note you may need to update the path to our sample data.
let data = require('./src/stories/menus/megaMenu/megamenu.json');
const sortByName = (a, b) => {
return a.name > b.name ? 1 : b.name > a.name ? -1 : 0;
};
// This function should be memoized for performance reasons.
const sortParent = (data) => {
let sortedData = [...data];
sortedData.sort(sortByName);
return sortedData.map((value) => {
return { children: sortChildren(value), name: value.name, url: value.url };
});
};
// This function should be memoized for performance reasons.
const sortChildren = (data) => {
const { children } = data;
let sortedChildren = [...children];
sortedChildren.sort(sortByName);
return sortedChildren.map((child) => {
return {
parent: child.parent,
children: sortChildren(child),
name: child.name,
url: child.url,
};
});
};
const parseData = (data) => {
if (Array.isArray(data)) {
return data.map((child) => {
return {
//parent: child.parent ? child.parent : null,
//children: child.children,
name: child.name,
url: child.url,
};
});
} else {
return data;
}
};
// Sort everything first
const sortedMegaMenu = sortParent(data.megaMenu);
// Get first menu in megaMenu. The easy one.
console.log('Mega Menu');
console.log(JSON.stringify(parseData(sortedMegaMenu), null, 2));
console.log();
// From the megaMenu select appliances. #0 out of 22 possible choices.
let departmentItem = 0;
// Get data for secondary menu. Departments for appliances.
console.log('Secondary Menu');
console.log(
JSON.stringify(parseData(sortedMegaMenu[departmentItem].children), null, 2),
);
console.log();
// From the secondary menu (which department for appliances). Choice the first option Cooking appliances.
let categoryItem = 0;
// Get data for Cooking appliances. Which is a category of Appliances department.
console.log('Third and final Menu');
console.log(
JSON.stringify(
parseData(sortedMegaMenu[departmentItem].children[categoryItem].children),
null,
2,
),
);
// This will display 4 items. The next and forth menu to slide open is for espots?
// NOTE: What you'll notices is there is enough data to build out another menu but Costco.com doesn't use that data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment