@nuuf/chink2

0.3.0 • Public • Published

chink2

Chain string objects together parent>child style

import {
  create, // Creates a new entity
  add, // Adds an entity to a list and a parent if specified, or to root if using root
  remove, // Deletes an entity from the list, removing all descendants
  swap, // Swaps two entities
  update, // Updates the data of an entity
  find, // Returns a parsed entity, finding it by ID
  inject, // Injects an entity into the list binding it to a sibling
  switchParent, // Connects an entity to a different parent
  addToParent, // Connects an entity to a parent
  ejectFromParent, // Removes the connection between parent and child, or switches the parent to root if using root
  getChildren, // Returns a list of all the children of an entity
  getChildrenParsed, // Returns a list of all the children (parsed) of an entity
  getRootEntities, // Returns a list of all the children of the root entity (if using root)
  getRootEntitiesParsed, // Returns a list of all the children (parsed) of the root entity (if using root)
  hasParent, // Checks if the entity has a parent without parsing it
  crunchList, // Removes all null entities and updates all indices
  bindList, // Binds a list so that you do not have to pass it every time
  getPath, // Returns an object with arrays containing the path of a value to the first parent
  generateListStructure // Renders a string of the list structure
} from '@nuuf/chink2';

  const list = [];

  bindList( list );
  // setting root enables automatic add to root if no parent is specified
  // the reason for using a root entity is simply for performance when using inject
  setRoot();
  add( create( 'myData', 'myEntity' ) );
  
  add( create() );

  add( create( null, 'myParent' ) );
  add( create( null, 'myChild' ), find( 'myParent' ) );
  add( create( null, 'myNewParent' ) );

  // add() uses addToParent() if entity has a parent or if it is already in the list
  // and addToParent() uses switchParent() if the entity has a parent

  // in this case the same as addToParent() and switchParent()
  add( find( 'myChild' ), find( 'myNewParent' ) ); 

  remove( find( 'myParent' ) );

  remove( JSON.parse( list[ 2 ] ) );

  swap( find( 'myEntity' ), find( 'myChild' ) );

  // throws error
  // remove( find( 'root' ) );

  inject( create( null, 'injected before' ), find( 'myChild' ) );
  inject( create( null, 'injected after' ), find( 'myChild' ), true );

  ejectFromParent( find( 'myEntity' ) );

  // logs a rendered string for visualisation
  console.log( generateListStructure() );
  // Can be used to collect data like getPath(entity, attributes[])
  // Where attributes is something like ['key1', 'key2'] in entity.data[key1/key2]
  console.log( getPath( find( 'myEntity' ) ) );

Readme

Keywords

none

Package Sidebar

Install

npm i @nuuf/chink2

Weekly Downloads

1

Version

0.3.0

License

MIT

Unpacked Size

22.8 kB

Total Files

5

Last publish

Collaborators

  • nuuf