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

self-referential

v1.1.3

Published

Convert self referential collection to nested or flat hierarchy

Downloads

174

Readme

self-referential

Allows a self referential collection (array of objects) to be converted into a nested or flat hierarchy

###Install & Require

npm install self-referential

var selfref = require('self-referential');

###Examples Imagine a table named categories like below where parentId foreign key refers to a record in the same categories table.

id | title | parentId --- | --- | --- 1 | Colors | null 2 | Fruits | null 3 | Meats | null 4 | Warm Colors | 1 5 | Cold Colors | 1 6 | Red | 4 7 | Orange | 4 8 | Apple | 2

We can represent this table as an array of objects as shown below:

var categories = [
       {
           id: 1,
           title: 'Colors',
           parentId: null
       },
       {
           id: 2,
           title: 'Fruits',
           parentId: null
       },
       {
           id: 3,
           title: 'Meats',
           parentId: null
       },
       {
           id: 4,
           title: 'Warm Colors',
           parentId: 1
       },
       {
           id: 5,
           title: 'Cold Colors',
           parentId: 1
       },
       {
           id: 6,
           title: 'Red',
           parentId: 4
       },
       {
           id: 7,
           title: 'Orange',
           parentId: 4
       },
       {
           id: 8,
           title: 'Apple',
           parentId: 2
       }
   ];

####To Nested Hierarchy To convert this to a nested hierarchy, we do:

var cfg = {
    selfKey: 'id',
    parentKey: 'parentId',
    childrenKey: 'children'

    // Optional properties
    // rootParentValues: see way below for explanation
    // rootSelfValues : see way below for explanation
};
var cats_hierarchy = selfref.toHier(cfg, categories);

cats_hierarchy has the following structure

[
    {
        "id": 1,
        "title": "Colors",
        "parentId": null,
        "children": [
            {
                "id": 4,
                "title": "Warm Colors",
                "parentId": 1,
                "children": [
                    {
                        "id": 6,
                        "title": "Red",
                        "parentId": 4,
                        "children": []
                    },
                    {
                        "id": 7,
                        "title": "Orange",
                        "parentId": 4,
                        "children": []
                    }
                ]
            },
            {
                "id": 5,
                "title": "Cold Colors",
                "parentId": 1,
                "children": []
            }
        ]
    },
    {
        "id": 2,
        "title": "Fruits",
        "parentId": null,
        "children": [
            {
                "id": 8,
                "title": "Apple",
                "parentId": 2,
                "children": []
            }
        ]
    },
    {
        "id": 3,
        "title": "Meats",
        "parentId": null,
        "children": []
    }
]

####To Flat Hierarchy To convert the above hierarchy into a flat collection, but in the same order as it would be in the hierarchy, we do:

var flat_but_in_hierarchical_order_cats = selfref.toFlatHier({childrenKey: 'children'}, cats_hierarchy);

flat_but_in_hierarchical_order_cats now has the following structure:

[
       {
              item: object: <your original item>
              hierPos: string: <position of item in a hierarchy such as 1.1 or 1.2.1 as in a table of contents in a book>
              depth: int: <depth of item in hierarchy, root has the value of zero>
              childCount: int: <number of children>
       }
       ...
]
[
    {
        "item": {
            "id": 1,
            "title": "Colors",
            "parentId": null
        },
        "hierPos": "1",
        "depth": 0,
        "childCount": 2
    },
    {
        "item": {
            "id": 4,
            "title": "Warm Colors",
            "parentId": 1
        },
        "hierPos": "1.1",
        "depth": 1,
        "childCount": 2
    },
    {
        "item": {
            "id": 6,
            "title": "Red",
            "parentId": 4
        },
        "hierPos": "1.1.1",
        "depth": 2,
        "childCount": 0
    },
    {
        "item": {
            "id": 7,
            "title": "Orange",
            "parentId": 4
        },
        "hierPos": "1.1.2",
        "depth": 2,
        "childCount": 0
    },
    {
        "item": {
            "id": 5,
            "title": "Cold Colors",
            "parentId": 1
        },
        "hierPos": "1.2",
        "depth": 1,
        "childCount": 0
    },
    {
        "item": {
            "id": 2,
            "title": "Fruits",
            "parentId": null
        },
        "hierPos": "2",
        "depth": 0,
        "childCount": 1
    },
    {
        "item": {
            "id": 8,
            "title": "Apple",
            "parentId": 2
        },
        "hierPos": "2.1",
        "depth": 1,
        "childCount": 0
    },
    {
        "item": {
            "id": 3,
            "title": "Meats",
            "parentId": null
        },
        "hierPos": "3",
        "depth": 0,
        "childCount": 0
    }
]

####Root Elements Finally, to get only the root elements of our collection, we do:

var root_cats = selfref.rootItems({selfKey: 'id', parentKey:'parentId'}, categories);

root_cats now has the following data:

[
    {
        "id": 1,
        "title": "Colors",
        "parentId": null
    },
    {
        "id": 2,
        "title": "Fruits",
        "parentId": null
    },
    {
        "id": 3,
        "title": "Meats",
        "parentId": null
    }
]

###Config Parameters & Usage Configuration parameter cfg can have the following properties:

selfKey: name of property that is uniquely identifies this record

parentKey: name of property that whose value refers to its parent's unique id

childrenKey: name of the property that the children of this model should be set to

rootParentValues: null|string|number|array

Sets nodes whose parentKey values you are specifying as the root nodes in hierarchy and goes from there

rootSelfValues: null|string|number|array

Sets nodes whose selfKey values you are specifying as the root nodes in hierarchy and goes from there

if any of those rootXValues are omitted, all of items with no matching parents will be picked as root items and then hierarchy will be generated with them and their children.

You cannot specify both rootParentValues and rootSelfValues at the same time

selfref.toHier(cfg, collectionData); cfg must have all of the above properties (except rootParentValues)

selfref.toFlatHier(cfg, hierarchyCollection); cfg must have childrenKey property only

selfref.rootItems(cfg, collectionData); cfg must have selfKey and parentKey properties

selfref.getAncestors(cfg, collectionData, selfKeyValue) returns ancestors of the item having the selfKey property value of selfKeyValue.
cfg must have selfKey and parentKey properties

selfref.getChildren(cfg, collectionData, selfKeyValue) returns children of the item having the selfKey property value of selfKeyValue.
cfg must have selfKey and parentKey properties