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

json-tree-wrap

v0.0.2

Published

Wrappers for hierarchical JSON objects that support manipulation, events, traversal and dynamic flattening to an array

Downloads

5

Readme

json-tree-wrap

Wrappers for hierarchical JSON objects that support manipulation, events, traversal and dynamic flattening to an array - useful for UI binding.

Install

Todo

TreeWrapper

Core type to wrap a JSON object:

var json = {
  name: 'Root item',
  items: [{
    name: 'Child item 1',
    items: [{
      name: 'Child item 2'
    }]
  }]
};

var treeWrapper = new TreeWrapper({
    onAdd: function(parent, indexInParent, newItem, stateObj) { }
    // ... other events - see below
});

var rootItemWrapper = treeWrapper.wrap(json);
var child1Wrapper = rootItemWrapper.getChild(0);

child1Wrapper.addChild(...)

The TreeWrapper assumes there is always a root item with all children below that.

TreeFlattener

Projects a hierarchical JSON object to a flat list of items and updates that list dynamically as the underlying object is manipulated via the TreeItemWrapper type. Very useful for UI binding.

var json = {
  name: 'Root item',
  items: [{
    name: 'Child item 1',
    items: [{
      name: 'Child item 2'
    }]
  }]
};

var treeObserver = new TreeObserver();
var treeWrapper = new TreeWrapper({ observer: treeObserver });
var treeFlattener = new TreeFlattener(treeWrapper, treeObserver);

treeWrapper.wrap(json);

var flattenedItems = treeFlattener.getItems();

Each item in flattenedItems has the following properties:

Property | Description

depth | 0-based integer of how nested the item is. Root items have depth 1, first level children have depth 1 etc. item | reference to the item in the underlying JSON object parent | reference to the parent item (null for root items) in the underlying JSON object

So for example flattenedItems from above example would contain:

[{
    depth: 0,
    item: (ref to 'Root item')
    parent: null
}, {
    depth: 1,
    item: (ref to 'Child item 1')
    parent: (ref to 'Root item')
}, {
    depth: 2,
    item: (ref to 'Child item 2')
    parent: (ref to 'Child item 1')
}]

TreeWrapper API

new TreeWrapper(options)

Constructor with following options:

  • childrenProp: Name of the child items property. Defaults to 'items'
  • observer: An optional TreeObserver instance that will be notified when the tree is manipulated via the TreeItemWrapper API.

wrap(jsonObject)

Returns a TreeItemWrapper that can be used to traverse / manipulate the root item. From the root item you can navigate to all child items via getChildItem function.

If a TreeObserver instance is passed to the TreeWrapper constructor, then wrap will also traverse the tree and call TreeObserver.onInit for each item.

traverse(jsonObject, callback)

Traverses the hierarchical jsonObject and calls callback for each item encountered. If a custom childrenProp was configured in the constructor this is used to navigate the tree.

TreeItemWrapper API

getChild(index)

Returns a TreeItemWrapper for the child at the given index.

addChild(index, newObj, stateObj)

Inserts newObj at given index under this tree item. Will call TreeObserver.onAdd if an observer was configured in the TreeWrapper constructor.

stateObj is an optional object that is passed through to TreeObserver.onAdd. It's useful for initializing properties on the flattened item wrappers created by TreeFlattener. See TreeFlattener section below for an example.

Example:

var json = {
    name: 'root item',
    items: [{
        name: 'child item 1'
    }]
};

var treeWrapper = new TreeWrapper();
var rootWrapper = treeWrapper.wrap(json);
rootWrapper.addChild(0, {
    name: 'new item'
});

assert.deepEqual(json, {
    name: 'root item',
    items: [{
        name: 'new item'    
    }, {
        name: 'child item 1'
    }]
});

addChildAtEnd(newObj, stateObj)

Convenience function that calls addChild to insert newObj as the last child under this item.

addAbove(newObj, stateObj)

Convenience function that calls addChild to insert newObj directly above this item (under the same parent).

Example:

var json = {
    name: 'root item',
    items: [{
        name: 'child item 1'
    }]
};

var treeWrapper = new TreeWrapper();
var rootWrapper = treeWrapper.wrap(json);
var child1Wrapper = rootWrapper.getChild(0);

child1Wrapper.addAbove({
    name: 'new item'
});

assert.deepEqual(json, {
    name: 'root item',
    items: [{
        name: 'new item'    
    }, {
        name: 'child item 1'
    }]
});

addBelow(newObj, stateObj)

Convenience function that calls addChild to insert newObj directly below this item (under the same parent).

Example:

var json = {
    name: 'root item',
    items: [{
        name: 'child item 1'
    }]
};

var treeWrapper = new TreeWrapper();
var rootWrapper = treeWrapper.wrap(json);
var child1Wrapper = rootWrapper.getChild(0);

child1Wrapper.addBelow({
    name: 'new item'
});

assert.deepEqual(json, {
    name: 'root item',
    items: [{
        name: 'child item 1'
    }, {
       name: 'new item'    
   }]
});

remove()

Removes this item from it's parent. Will call TreeObserver.onRemove if an observer was configured in the TreeWrapper constructor.

removeChild(index)

Removes the child item at the given index.
Will call TreeObserver.onRemove if an observer was configured in the TreeWrapper constructor.

moveChild(removeIndex, insertIndex)

This form moves a child item from removeIndex to insertIndex. Will call TreeObserver.onMove if an observer was configured in the TreeWrapper constructor.

moveChild(removeIndex, newParent, newParentInsertIndex)

This form moves a child item from removeIndex under this instance and inserts it at newParentInsertIndex under the newParent TreeItemWrapper instance.

Example:

var json = {
    items: [{
        name: 'item 1'
    },{
        name: 'item 2'
    }]
};

var rootWrapper = treeWrapper.wrap(json);
var item1Wrapper = rootWrapper.getChild(0);

// Move item 2 under item 1:
rootWrapper.moveChild(1, item1Wrapper, 0);

assert.deepEqual(rootWrapper.unwrap(), {
    items: [{
        name: 'item 1',
        items: [{
            name: 'item 2'
        }]
    }]
});

unwrap()

Returns the underlying JSON object.

traverse(callback)

Traverses the underlying hierarchical jsonObject and calls callback for each item encountered.

TreeFlattener Usage

TODO (for now, take a look at the tests)

Licence

MIT