Code Monkey home page Code Monkey logo

key-tree-store's Introduction

key-tree-store

Simple tool for storing/retrieving objects events based hierarchical keypaths.

It lets you store and retrive objects that are at an equal or deeper key path than what you give it.

install

npm install key-tree-store

example

Assume you've got a structure like this:

{
    'first': [ {obj: 1}, {obj: 2} ],
    'first.stuff': [ {obj: 3} ],
    'first.something.other': [ {obj: 4}, {obj: 5} ]
}

Then you can retrive it by key. Where it returns anything at or deeper than level supplied.

var KeyTree = require('key-tree-store');

var tree = new KeyTree();

tree.add('first', {id: 'one'});
tree.add('first.second', {id: 'two'});
tree.add('first.second', {id: 'three'});
tree.add('first.second.third', {id: 'four'});

// now we can retrieve them by key
tree.get('first'); // returns all of them
tree.get('first.second'); // returns array of objects two, three and four
tree.get('first.second.third'); // returns array of object four;

// the `get` method returns them all in an array
// if we still need them grouped by key we can use
// `getGrouped`
tree.getGrouped('first.second'); // returns {'first.second': [...], 'first.second.third': [...]}

// calling `.get()` or `.getGrouped` without arguments
// returns all in appropriate format

// that's all there is to it

removing items:

var KeyTree = require('key-tree-store');

var tree = new KeyTree();
var obj1 = {obj: '1'};

tree.add('key.path', obj1);

// removes it no matter what key
tree.remove(obj1);

running items:

// as a shortcut, there's also the `run` method
// to help you run functions that match they keypath
// this assumes you're storing functions, of course.
var KeyTree = require('key-tree-store');

var tree = new KeyTree();

tree.add('key.path', function () {
    console.log('function ran!');
});

tree.run('key'); //=> function ran!

// you can also optionally pass a context or arguments to run them with
tree.run('key', {some: 'object'});

// with arguments
tree.run('key', {some: 'object'}, 'arg1', 'arg2');

credits

If you like this follow @HenrikJoreteg on twitter.

license

MIT

key-tree-store's People

Contributors

bernharduw avatar henrikjoreteg avatar pgilad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

key-tree-store's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.