Julienng/object-visitor

function utility to go through a object tree representation

★ 1Forks 0TypeScriptGitHub ↗Compare

README

object-visitor

Object visitor is an utility function to traverse an object or a list that have a tree shape

Usage

We are going to test the visitor function with the object bellow

const tree = [
  {
    label: 'first',
    open: false,
    children: [
      { label: 'children-first-first-element', open: false },
      { label: 'children-first-second-element', open: false },
      { label: 'children-first-third-element', open: false }
    ]
  },
  {
    label: 'second',
    open: false
  },
  {
    label: 'first',
    open: false,
    children: [
      { label: 'children-third-first-element', open: false },
      { label: 'children-third-second-element', open: false },
      { label: 'children-third-third-element', open: false }
    ]
  }
]

To use the visitor on the previous example to change all open keys to true, we can do :

visitor(treeList, curr => {
  curr.open = true
})

// or
visitor(
  treeList,
  curr => {
    curr.open = true
  },
  curr => curr.children
)

This project is created using typescript-library-starter

Contributors

Julienng

Issues