Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

treeize

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

treeize

Converts tabular row data (as from SQL joins, flat JSON, etc) to deep tree graphs based on simple column naming conventions.

  • 0.0.5
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

treeize

Converts row data (in JSON/associative array format) to object/tree structure based on column naming conventions.

Installation

npm install --save treeize

Usage

  • treeize.grow(flatData, options) - takes your results/rows of flat associative data and returns a full object graph.
  • treeize.set(options) - sets global options for the lib. For example, to use a path delimiter of '>' instead of '+', call treeize.set({ delimiter: '>' })

Notes

  • The column/attribute order is not important. All attributes are sorted by depth and then alphabetically before mapping.
  • Each attribute name of the flat data must consist of the full path to its node & attribute, seperated by the delimiter. id suggests an id attribute on a root element, whereas name+first implies a first attribute on a name object within a root element.
  • To imply a collection in the path/attribute-name, use a plural name (e.g. "subjects" instead of "subject"). Otherwise, use a singular name for a singular object.
  • Use a + delimiter (default) to seperate path nodes. To change this, use the treeize.set([options]) function.

Assumptions

This library has several assumptions that make it possible.

  1. That each row represents a singular child item, that may contain many repeated ancestor columns.
  2. That each element in a collection node (including the root) will have a unique identifying signature (necessary to prevent duplication). This can be any one attribute, or the combination of any/all attributes.

Example

var treeize = require('treeize');

var flatData = [
  {
    "name":             "Mittens",
    "age":              12,
    "toys+name":        "mouse",
    "toys+owner+name":  "Mittens"
  },
  {
    "name":             "Mittens",
    "age":              12,
    "toys+name":        "yarn",
    "toys+owner+name":  "Ms. Threadz"
  },
  {
    "name":             "Tiger",
    "age":              7,
    "toys+name":        "a stick",
    "toys+owner+name":  "Mother Nature"
  }
];

var converted = treeize.grow(flatData);

Output

[
  {
    "name": "Mittens",
    "age": 12,
    "toys": [
      {
        "name": "mouse",
        "owner": {
          "name": "Mittens"
        }
      },
      {
        "name": "yarn",
        "owner": {
          "name": "Ms. Threadz"
        }
      }
    ]
  },
  {
    "name": "Tiger",
    "age": 7,
    "toys": [
      {
        "name": "a stick",
        "owner": {
          "name": "Mother Nature"
        }
      }
    ]
  }
]

Keywords

FAQs

Package last updated on 02 Sep 2013

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc