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

deep-object-graph

v1.1.3

Published

Converts flat JSON objects into deep object graph.

Downloads

15

Readme

Deep Object Graph

Build Status

An alternative to treeize.

Converts flat JSON objects into deep object graph.

Purpose

Results that comes back from a SQL join query usually have many duplicated data (depending on how complex the joins are).

deep-object-graph converts this flat data into a clean and readable JSON by removing these duplicated data and merging data that shares the same parent entity.

Usage

Install it.

npm install deep-object-graph --save

To use.

Note: Each object needs to have an unique field to identify them with other objects that share the same unique field for merging. By Default this field name is 'id', it is configurable with options.primaryKeyField.

import deepObjectGraph from 'deep-object-graph';
// const deepObjectGraph = require('deep-object-graph').default; // es5-syntax.

// Create new instance with default options shown.
const dogInstance = new deepObjectGraph({
	primaryKeyField: 'id', // The key name of the unique field to identify an object.
    delimiter: '.', // The delimiter char to determine nesting.
    specialKeyArray: [] // The key name of fields where values will be in an array even if there is only one object.
}); 

const data = [{
    "id": "1",
    "name": "Cat",
    "tags.id": "t2",
    "tags.tag": "animal",
    "tags.description": null,
    "tags.metadata": {
        "isStray": true
    }
}];

let results = dogInstance.convert(data);

// 'results' will be...
[{
    "id": "1",
    "name": "Cat",
    "tags": {
      "id": "t2",
      "tag": "animal",
      "description": null,
      "metadata": {
      	"isStray": true
      }
    }
}]

Examples

// Input...
[
	{
        "id": "1",
        "name": "Cat",
        "tags.id": "t2",
        "tags.tag": "animal",
        "tags.description": null,
        "tags.meta": {
            "fieldA": true
        }
    },{
        "id": "1",
        "name": "Cat",
        "tags.id": "t1",
        "tags.tag": "colour",
        "tags.description": null,
        "tags.meta": {
            "fieldB": true
        },
    },{
        "id": "2",
        "name": "Dog",
        "tags.id": "t2",
        "tags.tag": "animal",
        "tags.description": null,
        "tags.meta": {
            "fieldA": true
        }
    },{
        "id": "2",
        "name": "Dog",
        "tags.id": "t1",
        "tags.tag": "colour",
        "tags.description": null,
        "tags.meta": {
            "fieldB": true
        }
	}
]

// Output...
[
	{
        "id": "1",
        "name": "Cat",
        "tags": [{
            "id": "t2",
            "tag": "animal",
            "description": null,
            "meta": {
                "fieldA": true
            }
        },{
            "id": "t1",
            "tag": "colour",
            "description": null,
            "meta": {
                "fieldB": true
            }
        }]
    },{
        "id": "2",
        "name": "Dog",
        "tags": [{
            "id": "t2",
            "tag": "animal",
            "description": null,            
            "meta": {
                "fieldA": true   
            }
        },{
            "id": "t1",
            "tag": "colour",
            "description": null,
            "meta": {
                "fieldB": true
            }
        }]
	}
]

Convert to array if value type is non-object.

// Input...
[
	{
        "id": "1",
        "name": "Cat",
        "metaData": 'valueA'
    },{
        "id": "1",
        "name": "Cat",
        "metaData": 'valueB'
    },{
        "id": "1",
        "name": "Cat",
        "metaData": 'valueC'
    }
]

// Output...
[
	{
        "id": "1",
        "name": "Cat",
        "metaData": ['valueA', 'valueB', 'valueC']
    }
]

primaryKeyField option.

let thisDogInstance = new DeepObjectGraph({
    primaryKeyField: 'uuid'
});

// Input...
[
	{
        "uuid": "1",
        "name": "Cat",
        "tags.uuid": "t2",
        "tags.tag": "animal",
        "tags.description": null,
        "tags.metadata": {
            "isStray": true
        }
    }
]

// Output...
[
	{
        "uuid": "1",
        "name": "Cat",
        "tags": {
            "uuid": "t2",
            "tag": "animal",
            "description": null,
            "metadata": {
                "isStray": true
            }
        }
    }
]

delimiter option.

let dogInstance = new DeepObjectGraph({
    delimiter: ':'
});

// Input...
[
	{
        "id": "1",
        "name": "Cat",
        "tags:id": "t2",
        "tags:tag": "animal",
        "tags:description": null,
        "tags:meta:isHungry": true,
        "tags:meta:isCute": false,
        "tags:meta:isNotStray:hasName": true
    },{
        "id": "1",
        "name": "Cat",
        "tags:id": "t2",
        "tags:tag": "animal",
        "tags:description": null,
        "tags:meta:isHungry": true,
        "tags:meta:isCute": false,
        "tags:meta:isNotStray:isSpayed": true
	}
]

// Output...
[
	{
        "id": "1",
        "name": "Cat",
        "tags": {
            "id": "t2",
            "tag": "animal",
            "description": null,
            "meta": {
                "isHungry": true,
                "isCute": false,
                "isNotStray": {
                    "hasName": true,
                    "isSpayed": true
                }
            }
        }
    }
]

specialKeyArray option to force value into an array.

let dogInstance = new DeepObjectGraph({
    specialKeyArray: ['tags']
});

// Input...
[
	{
        "id": "1",
        "name": "Cat",
        "tags.id": "t2",
        "tags.tag": "animal",
        "tags.description": null,
        "tags.metadata": {
            "isStray": true
        }
	}
];

// Output...
[
	{
        "id": "1",
        "name": "Cat",
        "tags": [{
            "id": "t2",
            "tag": "animal",
            "description": null,
            "metadata": {
                "isStray": true
            }
        }]
	}
]

Refer to test.js for more examples.