Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

foliage

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

foliage

A cursor like tree data structure on top of sprout.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.7K
increased by9.06%
Maintainers
1
Weekly downloads
 
Created
Source

NPM


Travis CI Coverage Status


Foliage

Foliage is lightweight tree data structure modeled loosely after Om's Cursor and OmniscientJS's immstruct.

In more practical terms, Foliage is a thin layer on top of sprout.

What problems does it attempt to solve?

  1. Decouple React components from rest of app. Our Flux-like framework, Microcosm, keeps all state in a single app instance. It can be troublesome to pass down this context to child components that need to modify state. Foliage makes it easier to "branch" off a subset of data while still having the ability to reference the root.
  2. Data traversal. It is simpler to run queries for specific records on objects, with a query like data.users[params.id]. However JavaScript objects aren't good at enumeration. Foliage provides some helpers out of the box for this.
  3. It is small. Foliage isn't trying to do too much or be too smart. Like Microcosm, it will be embedded in other tools and should be as small as possible.

Opinions

  1. Keep a naming convention similar to ES6 maps

Working with Foliage

Foliage accepts a seed:

let plant = new Folage({ berries: true })

Querying records

get pulls data out of a "plant."

let plant = new Folage({ berries: true })

plant.get('berries').valueOf() // => true

Take out that valueOf must be called to retrieve the value out of a plant. This is because get returns a branch. Now let's dig into that.

Branches

Calling get returns a branch. Technically, this is called a cursor, but let's keep with the dendrology theme.

let oak = new Folage({
  squirrels: {
    squeakem: { weight: 2, height: 12 }
    chatters: { weight: 5, height: 8 }
  }
})

let squirrels = oak.get('squirrels')

In this example, squirrels is a subset of oak focused on the squirrels key. Under the hood, they point to the same underlying data. This means if you set in squirrel, oak will be modified as well:

squirrels.set(['squeakem', 'weight'], 5)
oak.get(['squirrels', 'squeakem', 'weight']).valueOf() // => 5

A couple of things are going on here. First, set is used to modify data. It maps directly to sprout's assoc method. Second, both get and set accept an array of keys. When given an array, they will traverse the tree for the leaf value instead of just returning the key from the most immediate level.

Phoning home

All branches have a reference to their parent. No matter how branched, the trunk can be found:

let plant = new Foliage({ fizz: 'buzz' })
let fiz   = plant.get('fiz')

assert(fiz.trunk() === plant)

Prior art

There is nothing novel about Foliage, and probably slightly flawed in it's mimicking of:

FAQs

Package last updated on 06 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc