npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

build-hierarchy

v1.0.2

Published

Convert flat structures to deeply nested hierarchical data in node.js. Transform flat objects array representations to array of nested objects.

Downloads

15

Readme

code style: prettier

build-hierarchy

Convert flat structures to deeply nested hierarchical data in node.js. Transform flat objects array representations to array of nested objects.

Basic usage example:

> npm install build-hierarchy -S
const HierarchyBuilder = require('build-hierarchy');

//Optionally required to view result. 
const fs = require('fs');
const util = require('util');

/*
Taking input an array of flat structure 
and returning hierarichal data as an output.

example : 

input : [continent, country, state, city]
output : coninent -> country -> state -> city
 
*/

//Input flat structure.
const input = [
    {
        "Continent_ID": 1,
        "Continent": "Asia",
        "Country_ID": 101,
        "Country": "India",
        "State_ID": 1001,
        "State": "Gujrat",
        "City_ID": 10001,
        "City": "Surat"
    },
    {
        "Continent_ID": 1,
        "Continent": "Asia",
        "Country_ID": 101,
        "Country": "India",
        "State_ID": 1001,
        "State": "Gujrat",
        "City_ID": 10002,
        "City": "Rajkot"
    },
    {
        "Continent_ID": 1,
        "Continent": "Asia",
        "Country_ID": 101,
        "Country": "India",
        "State_ID": 1002,
        "State": "Haryana",
        "City_ID": 10003,
        "City": "Gurugram"
    },
    {
        "Continent_ID": 1,
        "Continent": "Asia",
        "Country_ID": 101,
        "Country": "India",
        "State_ID": 1002,
        "State": "Haryana",
        "City_ID": 10004,
        "City": "Panipat"
    },
    {
        "Continent_ID": 1,
        "Continent": "Asia",
        "Country_ID": 102,
        "Country": "Thailand",
        "State_ID": 1003,
        "State": "Chiang Mai",
        "City_ID": 10005,
        "City": "Doi Saket"
    },
    {
        "Continent_ID": 1,
        "Continent": "Asia",
        "Country_ID": 102,
        "Country": "Thailand",
        "State_ID": 1003,
        "State": "Chiang Mai",
        "City_ID": 10006,
        "City": "Chom Thong"
    },
    {
        "Continent_ID": 1,
        "Continent": "Asia",
        "Country_ID": 102,
        "Country": "Thailand",
        "State_ID": 1004,
        "State": "Chiang Rai",
        "City_ID": 10007,
        "City": "Mae Suai"
    },
    {
        "Continent_ID": 1,
        "Continent": "Asia",
        "Country_ID": 102,
        "Country": "Thailand",
        "State_ID": 1004,
        "State": "Chiang Rai",
        "City_ID": 10008,
        "City": "Phan"
    },
    {
        "Continent_ID": 2,
        "Continent": "Africa",
        "Country_ID": 107,
        "Country": "South Africa",
        "State_ID": 1005,
        "State": "Northern Cape",
        "City_ID": 10009,
        "City": "Kimberley"
    },
    {
        "Continent_ID": 2,
        "Continent": "Africa",
        "Country_ID": 107,
        "Country": "South Africa",
        "State_ID": 1005,
        "State": "Northern Cape",
        "City_ID": 10010,
        "City": "Sutherland"
    },
    {
        "Continent_ID": 2,
        "Continent": "Africa",
        "Country_ID": 107,
        "Country": "South Africa",
        "State_ID": 1006,
        "State": "Eastern Cape",
        "City_ID": 10011,
        "City": "Stormsrivier"
    },
    {
        "Continent_ID": 2,
        "Continent": "Africa",
        "Country_ID": 107,
        "Country": "South Africa",
        "State_ID": 1006,
        "State": "Eastern Cape",
        "City_ID": 10012,
        "City": "Hogsback"
    },
    {
        "Continent_ID": 3,
        "Continent": "North America",
        "Country_ID": 112,
        "Country": "Canada",
        "State_ID": 1007,
        "State": "Ontario",
        "City_ID": 10013,
        "City": "Toronto"
    },
    {
        "Continent_ID": 3,
        "Continent": "North America",
        "Country_ID": 112,
        "Country": "Canada",
        "State_ID": 1007,
        "State": "Ontario",
        "City_ID": 10014,
        "City": "Ottawa"
    },
    {
        "Continent_ID": 3,
        "Continent": "North America",
        "Country_ID": 112,
        "Country": "Canada",
        "State_ID": 1008,
        "State": "Quebec",
        "City_ID": 10015,
        "City": "Montreal"
    },
    {
        "Continent_ID": 3,
        "Continent": "North America",
        "Country_ID": 112,
        "Country": "Canada",
        "State_ID": 1008,
        "State": "Quebec",
        "City_ID": 10016,
        "City": "Sherbrooke"
    }
]

//Defining levels and properties to be used.
const levels = [
    {
        "level": 1,
        "groupByKeys": ["Continent_ID"], //can be grouped by multiple properties.
        "entities": [
            {
                "id": 1,
                "name": "Continent",
                "properties": {
                    "Continent_ID": "id", //property name alias.
                    "Continent": "name"
                }
            }
        ]
    },
    {
        "level": 2,
        "groupByKeys": [
            "Country_ID"
        ],
        "entities": [
            {
                "id": 1,
                "name": "Country",
                "properties": {
                    "Country_ID": "id",
                    "Country": "name"
                }
            }
        ]
    },
    {
        "level": 3,
        "groupByKeys": [
            "State_ID"
        ],
        "entities": [
            {
                "id": 1,
                "name": "State",
                "properties": {
                    "State_ID": "id",
                    "State": "state"
                }
            }
        ]
    },
    {
        "level": 4,
        "groupByKeys": ["City_ID"],
        "entities": [
            {
                "id": 1,
                "name": "City",
                "properties": {
                    "City_ID": "id",
                    "City": "name"
                }
            }
        ]
    }
];

const builder = new HierarchyBuilder();

const resultset = builder.buildHierarchy(input, levels);

//Using util.inspect method to see depply nested data.
console.log(util.inspect(resultset, { showHidden: false, depth: null, colors: true }))

//To view better logging resultset to a JSON file.
fs.writeFile("resultset.json", JSON.stringify(resultset), err => {
    if (err) throw err;
    console.log("Done writing resultset.json");
});

//Output :
/*
[
    {
        "id": 1,
        "name": "Asia",
        "children": [
            {
                "id": 101,
                "name": "India",
                "children": [
                    {
                        "id": 1001,
                        "state": "Gujrat",
                        "children": [
                            {
                                "id": 10001,
                                "name": "Surat",
                                "children": []
                            },
                            {
                                "id": 10002,
                                "name": "Rajkot",
                                "children": []
                            }
                        ]
                    },
                    {
                        "id": 1002,
                        "state": "Haryana",
                        "children": [
                            {
                                "id": 10003,
                                "name": "Gurugram",
                                "children": []
                            },
                            {
                                "id": 10004,
                                "name": "Panipat",
                                "children": []
                            }
                        ]
                    }
                ]
            },
            {
                "id": 102,
                "name": "Thailand",
                "children": [
                    {
                        "id": 1003,
                        "state": "Chiang Mai",
                        "children": [
                            {
                                "id": 10005,
                                "name": "Doi Saket",
                                "children": []
                            },
                            {
                                "id": 10006,
                                "name": "Chom Thong",
                                "children": []
                            }
                        ]
                    },
                    {
                        "id": 1004,
                        "state": "Chiang Rai",
                        "children": [
                            {
                                "id": 10007,
                                "name": "Mae Suai",
                                "children": []
                            },
                            {
                                "id": 10008,
                                "name": "Phan",
                                "children": []
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "id": 2,
        "name": "Africa",
        "children": [
            {
                "id": 107,
                "name": "South Africa",
                "children": [
                    {
                        "id": 1005,
                        "state": "Northern Cape",
                        "children": [
                            {
                                "id": 10009,
                                "name": "Kimberley",
                                "children": []
                            },
                            {
                                "id": 10010,
                                "name": "Sutherland",
                                "children": []
                            }
                        ]
                    },
                    {
                        "id": 1006,
                        "state": "Eastern Cape",
                        "children": [
                            {
                                "id": 10011,
                                "name": "Stormsrivier",
                                "children": []
                            },
                            {
                                "id": 10012,
                                "name": "Hogsback",
                                "children": []
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "id": 3,
        "name": "North America",
        "children": [
            {
                "id": 112,
                "name": "Canada",
                "children": [
                    {
                        "id": 1007,
                        "state": "Ontario",
                        "children": [
                            {
                                "id": 10013,
                                "name": "Toronto",
                                "children": []
                            },
                            {
                                "id": 10014,
                                "name": "Ottawa",
                                "children": []
                            }
                        ]
                    },
                    {
                        "id": 1008,
                        "state": "Quebec",
                        "children": [
                            {
                                "id": 10015,
                                "name": "Montreal",
                                "children": []
                            },
                            {
                                "id": 10016,
                                "name": "Sherbrooke",
                                "children": []
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
*/

Built using

  • lodash & node.js module.

Authors

License

[MIT License] © Ayush Pratap