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

silva

v0.0.2

Published

A generic purpose immutable tree data structure, but created primarily to create a personal custom CMS.

Downloads

3

Readme

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: Array[Tree]. 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: Array[Tree]. 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.