Skip to content

Instantly share code, notes, and snippets.

@matinrco
Created December 5, 2022 16:21
Show Gist options
  • Save matinrco/903125ddb9aef91c05c41931e83aba50 to your computer and use it in GitHub Desktop.
Save matinrco/903125ddb9aef91c05c41931e83aba50 to your computer and use it in GitHub Desktop.
Find path to a node in tree with typescript/javascript (WIP)
const options = [
{
label: "Mobile Phones",
value: "d0a4aca6-2217-11ed-861d-0242ac120002",
code: "Mobile",
subCategories: [
{
label: "Samsung",
value: "1d4939c9-7613-43f2-b60c-cf25157b9aa2",
code: "Mobile-Samsung",
subCategories: [
{
label: "S Series",
value: "d0a4aca6-2217-11ed-861d-0242ac120003",
code: "Mobile-Samsung-S-Series",
},
],
},
{
label: "Apple",
value: "f3f08983-0141-4c35-b960-678f016ea9c9",
code: "Mobile-Apple",
},
],
},
{
label: "Tablet",
value: "b1c92d93-65ed-4ba0-8fd0-cbc81b0bacad",
code: "Tablet",
subCategories: [
{
label: "Samsung Tablet",
value: "1e9444ee-5b76-4f70-936f-b368e608ea56",
code: "IMC-95822032",
},
],
},
{
label: "Smart Watch",
value: "785b1f4b-3626-49f2-97f1-630438934038",
code: "Smart-Watch",
subCategories: [
{
label: "Samsung Smart Watch",
value: "266a1e60-2727-4db6-a88b-405bed722d18",
code: "Smart-Watch-Samsung",
},
],
},
{
label: "Speakers",
value: "198c5be9-3d14-46aa-a648-5d99caf6cb23",
code: "Speaker",
subCategories: [
{
label: "Samsung Speakers",
value: "fa1a0186-c35b-40e6-a1bd-fbce69921465",
code: "IMC-89942570",
},
{
label: "Sony Speakers",
value: "7ad0d447-0188-43bf-9381-26271734267a",
code: "IMC-10303056",
},
],
},
{
label: "Handsfree",
value: "ea73647f-0105-4287-b67f-1d776e5a1db0",
code: "Handsfree",
subCategories: [
{
label: "Samsung Handsfree",
value: "f8b2207e-e320-4da2-b150-857462ffdd49",
code: "IMC-41734787",
},
],
},
];
// we are going to find this item path in tree
const targetValue = "d0a4aca6-2217-11ed-861d-0242ac120003";
interface Category {
label: string;
value: string;
code: string;
subCategories?: Category[];
}
const getFlatCategoryPath = (
categories: Category[],
targetValue: string
): Omit<Category, "subCategories">[] => {
const buildPath = (
subCategories: Category[],
targetValue: string,
currentPath: string
): string | undefined => {
for (const category of subCategories) {
if (category.value === targetValue) {
return currentPath + "/" + category.value;
} else if (category.subCategories) {
const path = buildPath(
category.subCategories,
targetValue,
currentPath + "/" + category.value
);
if (path) return path;
}
}
};
const foundPath = buildPath(categories, targetValue, "");
const flatCategoryPath: ReturnType<typeof getFlatCategoryPath> = [];
const buildFlatCategoryPath = (subCategories: Category[], path: string) => {
for (const category of subCategories) {
if (path?.split("/").includes(category.value)) {
flatCategoryPath.push({
code: category.code,
label: category.label,
value: category.value,
});
}
if (category.subCategories) {
buildFlatCategoryPath(category.subCategories, path);
}
}
};
if (foundPath) {
buildFlatCategoryPath(categories, foundPath);
}
return flatCategoryPath;
};
console.log(getFlatCategoryPath(options, targetValue));
// expected output:
// [
// {
// code: "Mobile",
// label: "Mobile Phones",
// value: "d0a4aca6-2217-11ed-861d-0242ac120002",
// },
// {
// code: "Mobile-Samsung",
// label: "Samsung",
// value: "1d4939c9-7613-43f2-b60c-cf25157b9aa2",
// },
// {
// code: "Mobile-Samsung-S-Series",
// label: "S Series",
// value: "d0a4aca6-2217-11ed-861d-0242ac120003",
// },
// ];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment