🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

flatToTrees

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flatToTrees

Utility to convert flat data into hierarchical data

0.1.2
latest
Source
npm
Version published
Weekly downloads
7
-22.22%
Maintainers
1
Weekly downloads
 
Created
Source

flatToTrees

Utility to convert flat data into hierarchical data. In particular, data returned from a SQL query.

This library was inspired by treeize which removes duplicate leaves. flatToTrees can do this but is NOT the default behavior.

treeize also doesn't work well with heterogenous records, i.e. records that have different columns and names. (This is less aggregious than the removal of duplicate leaves)

Travis

Installation

npm install flatToTrees

Usage

flatToTrees(obj, options)

Arguments

  • obj - An object.
  • options:
    • delimiter - (default: .) Path delimiter that defines the hierarchy.
    • removeDuplicateLeaves - (default: false) Flag to remove duplicate leaves in the trees.

Example

const flatToTrees = require('flatToTrees');

const o = [
	{a: 1, 'xs.b': 2, 'xs.x': 10, 'xs.cs.d': 3, 'ys.g': 12, 'ys.h': 20},
	{a: 1, 'xs.b': 2, 'xs.x': 10, 'xs.cs.d': 3, 'ys.g': 12, 'ys.h': 21},
];
const trees = flatToTrees(o);
/*
	trees === [ { a: 1,
		xs: [ { b: 2, cs: [ { d: 3 }, { d: 3 } ], x: 10 } ],
		ys: [ { g: 12, h: 20 }, { g: 12, h: 21 } ] } ]
*/

If removeDuplicateLeaves was set to true in the options parameter, then:

/*
trees === [ { a: 1,
    xs: [ { b: 2, cs: [ { d: 3 } ], x: 10 } ],
    ys: [ { g: 12, h: 20 }, { g: 12, h: 21 } ] } ]
*/

Notice that trees.xs.cs has only one copy of {d: 3}.

For more examples, see the test code.

Details

Flat data like data from a SQL query can have duplicate information especially when JOINs are used. flatToTrees will take an array of objects and combine any keys of the form:

<key1>.<key2>...<keyN>.<key>

Here the delimiter is the default of .. <key1> through <keyN> will be arrays in the form:

<key1>
    <key2>
        ...
            <keyN>

Any key's that are of the form:

<key>

will be used to combine like records. In the above example, the flat data has a = 1 for both records. This is why they are combined into a single hierarchy.

The combination logic is repeated at every level of arrays until the <key> is reached where it's simply added to the end of the final array.

In the above example, xs.cs implies the following hierarchy:

xs: [
    cs: [
        // objects go here
    ]
]

And xs.cs.d, is placed in the hierarchy like:

xs: [
    cs: [
        {d: 3}
    ]
]

If you're coming to this library from treeize, it's IMPORTANT to note that there is NO naming convention on keys as is true with treeize.

Prefixes to keys, i.e. <key1> through <keyN> will be considered arrays.

Although this library was originally developed with SQL data in mind, which will guarantee that every column has the same name for each record and each record has every column, i.e. homogenous data, the library works as expected when neither is true. The usefulness of such a scenario is debatable, nonetheless, it still works well.

FAQs

Package last updated on 09 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts