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

tree-controller

v1.0.3

Published

Utility for walking and controlling tree nodes and their state

Downloads

9

Readme

TreeController

Utility for walking and controlling tree nodes and their state.

  • install:
    • npm install tree-controller
    • yarn add tree-controller
  • import
    • import { TreeController } from 'tree-controller';
    • import * as TreeController from 'tree-controller';
    • const TreeController = require('tree-controller');
  • Features:
    • written in typescript, exported with declaration files but contains default export for requiring;
    • TreeController.Tree: class with configurable accessors for getting a node's parent and children, so you are not tied to a single shape of node;
    • TreeController.State: Handles tree state independently from Tree and optionally can stream via RxJS;
    • TreeController.Walker: Handles walking up and down independently from Tree;
  • Docs
  • TODO

Example use case: File Selector with status indicator

If you're building some type of file selector, you have a tree whose nodes can be expandable/collapsible and some type of selection that should be disabled for directories (branch nodes) but enabled for files (leaf nodes). Also, let's suppose you want to show some kind of status, like an icon that shows if the file is local, in the cloud or downloading, And clicking that icon would trigger some kind of action: example image

That could be described using the following example:

const tree = new TreeController.Tree(Tree1.root);
const state = new TreeController.State<any, S>(
    tree,
    {
        expanded: <TreeController.State.Constructor.Params.Keys.KeyRecord<boolean>>{
            propagation: 'none',
            selection: 'branch', // allows for multiple nodes to be expanded at once
            defaultValue: false, // starts with all nodes collapsed
        },
        checked: <TreeController.State.Constructor.Params.Keys.KeyRecord<boolean>>{
            propagation: 'none',
            selection: 'multi-leaf',
            defaultValue: false, // starts with all nodes unchecked
        },
        sync: <TreeController.State.Constructor.Params.Keys.KeyRecord<'local'|'cloud'|'downloading'>>{
            propagation: 'none',
            selection: 'multi',
            defaultValue: 'local',
            values: [
                {
                    value: 'cloud',
                    nodes: [Tree1.root, Tree1.node_2, Tree1.node_2_1, Tree1.node_2_2]
                },
                {
                    value: 'downloading',
                    nodes: [Tree1.node_1, Tree1.node_1_2]
                }
            ]
        },
    },
);

Them setting and getting states are as simple as:

state.setState(Tree1.root, 'expanded', true);
state.setState(Tree1.root, 'checked', true);
state.setState(Tree1.root, 'sync', 'downloading');
// propagation and selection mode will be inherited from provided configuration

const rootSyncState = state.getState(Tree1.root, 'sync');
const rootState = state.getState(Tree1.root);

But lets suppose you want to build a custom algorithm for determining the icon for sync. Let's make any parent that has a child downloading also show the downloading icon. You could do that using a custom tree walking callback:

const propagateSyncFactory = (state) =>
    (node, parent, tree) => {
        const syncState = state.getState(node, 'sync');
        if (syncState !== 'downloading') state.setState(node, 'sync', 'downloading')
    }

state.setState(Tree1.node_1_1, 'sync', 'downloading');
TreeController.Walker.walkUpSyncSkipSelf(Tree1.node_1_1, tree, propagateSyncFactory(state))

TODO

  • [X] tree constructor root
  • [X] tree constructor node
  • [X] tree constructor children
  • [X] tree constructor fillNodesArray
  • [X] tree constructor parent
  • [X] Tree method defaultGetNodeFactory
  • [X] Tree method defaultGetChildrenFactory
  • [X] Tree method defaultFillNodesArray
  • [X] Tree method defaultGeParentFactory
  • [X] Walker method walkDownSyncSkipSelf
  • [X] Walker method walkUpSyncSkipSelf
  • [X] state constructor tree
  • [X] state constructor keys propagation: 'none'
  • [X] state constructor keys propagation: 'down'
  • [X] state constructor keys propagation: 'up'
  • [X] state constructor keys selection: 'multi'
  • [X] state constructor keys selection: 'single'
  • [ ] state constructor keys selection: 'single-branch'
  • [ ] state constructor keys selection: 'multi-branch'
  • [ ] state constructor keys selection: 'single-leaf'
  • [ ] state constructor keys selection: 'multi-leaf'
  • [X] state constructor keys defaultValue
  • [X] state constructor keys values: value
  • [X] state constructor keys values: nodes
  • [X] state method getConfig
  • [X] state method getStateObject
  • [X] state method $getStateObject
  • [X] state method getState
  • [X] state method $getstate
  • [X] state method setState
  • [X] state method resetStateKey
  • [X] state method setStateSingleSelection
  • [X] state method setStateMultiSelection
  • [X] state method setWalkingNone
  • [X] state method setStateWalkingDown
  • [X] state method setStateWalkingUp