0.0.2 • Published 7 years ago

silva v0.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

Silva

A generic purpose immutable tree data structure, but created primarily for a personal project (a CMS).

At present module.exports exposes a createTree helper function that creates a tree node and the Tree data type class itself.

Installation

Using NPM or Yarn in your terminal:

yarn install silva
yarn add silva

package on NPM

API Reference

createTree()

createTree :: Object -> Tree

createTree is a helper function wrapping the constructor for the Tree class. It takes an object as it's only parameter and returns a Tree. If no object is provided, then a Tree will be returned with all properties empty, except id, which will be randomly generated. Properties that that be passed using the configuration object are:

  • id: String. A unique identifier for the Tree node. If not provided the id is randomly generated
  • children: ArrayTree. Child nodes (must be other Tree node instances). If not provided is an empty Array.
  • content: Object. Contains any information about the node. If not provided is an empty Object.
  • type: String. Signifies the node type. If not provided is an empty String.

Example:

const tree = createTree({
  type: 'document',
  children: [
    createTree({
      type: 'section'
    }),
    createTree({
      type: 'section'
    })
  ]
})

returns:

Tree {
  _id: '_jouqwjbhyiswyk9d$1492368673948',
  _children:
   [ Tree {
       _id: '_7f77ru8na9mbmrzs$1492368673948',
       _children: [],
       _content: {},
       _type: 'section' },
     Tree {
       _id: '_irzaigefeldrm2f6$1492368673948',
       _children: [],
       _content: {},
       _type: 'section' } ],
  _content: {},
  _type: 'document' }

Tree

The Tree class.

Properties

Each node instance of the Tree data type contains four properties:

  • id: String. A unique identifier for the Tree node.
  • children: ArrayTree. Child nodes (other Tree node instances).
  • content: Object. Contains any information about the node.
  • type: String. Signifies the node type.

And the following methods for manipulation of the data structure:

clone :: -> Tree

Returns a clone of the Tree object (and all children). Used in the other class methods to maintain immutability.

addChild :: Object -> Tree

Returns a new Tree object with the provided child appended to its _children array. The configuration object provided has the same options as the createTree() configuration object.

map :: (Tree -> Tree) -> Tree

Maps the current Tree with the provided function and returns a new Tree. The map function is recursive and is also applied to all the children of the current Tree node, if any exist.

containsDescendant :: String -> Boolean

Takes an id String and returns a Boolean if a Tree node with that id exists.

getDescendant :: String -> Tree

Returns a descendant Tree node based on the provided id.

addDesChild :: String, Object -> Tree

Adds a new child to the descendant identified by the provided id of the current Tree and returns a new Tree. The configuration object provided has the same options as the createTree() configuration object.

deleteDescendant :: String, Boolean -> Tree

Deletes the descendant identified by the provided id of the current Tree and returns a new Tree.

updateContent :: String/Null, (Object -> Object) -> Tree

Updates the content of any descendant in the current Tree structure. Takes a string containing the id of the Tree node to alter and a function which receives the current _content object of this Tree node and should return a new "_content". The function returns a new Tree.