Coder Social home page Coder Social logo

jeradg / ceibo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from san650/ceibo

0.0 2.0 0.0 13 KB

Ceibo is a JavaScript micro library to model trees that evaluate arbitrary code when accessing its nodes.

License: MIT License

JavaScript 95.50% HTML 4.50%

ceibo's Introduction

Ceibo

JavaScript micro library to model trees that evaluate arbitrary code when accessing its nodes.

The tree is modeled as a plain JavaScript object where each node has an arbitrary getter function. This allows to have a representation of a tree where a subtree is generated on the fly when a node is accessed.

Examples

Let's start by doing the most simple case, the identity case:

var root = Ceibo.create({
  foo: {
    bar: 'baz';
  }
});

console.log(root.foo.bar); // "baz"

You can create special node types called descriptors that allow you to respond to node access:

var root = Ceibo.create({
  foo: {
    isDescriptor: true,

    get() {
      return 'bar';
    }
  }
});

console.log(root.foo); // "bar"

As you can see, a descriptor is a JavaScript object that has a isDescriptor attribute.

You can define a get method or you can declare a value attribute, then the value attribute is going to be used as is:

var root = Ceibo.create({
  foo: {
    isDescriptor: true,

    value(answer) {
      return `The answer to life, the universe and everything is ${answer}`;
    }
  }
});

console.log(root.foo('42')); // "The answer to life, the universe and everything is 42"

descriptors can inspect and mutate the target object by defining a setup method:

var tree = Ceibo.create({
  foo: {
    isDescriptor: true,

    get() {
      return 'bar';
    },

    setup(target, keyName) {
      Ceibo.defineProperty(target, keyName.toUpperCase(), 'generated property');
    }
  }
});

console.log(tree.foo); // "bar"
console.log(tree.FOO); // "generated property"

Note that Ceibo trees are read-only, so you cannot reassign attributes:

var root = Ceibo.create({
  foo: 'bar';
});

root.foo = 'baz'; // => throws an error!

You can redefine how each value type is processed when the Ceibo tree is created:

function buildString(treeBuilder, target, keyName, value) {
  Ceibo.defineProperty(target, keyName, `Cuack ${value}`);
}

var root = Ceibo.create(
  {
    foo: 'first value'
  },
  {
    builder: {
      string: buildString
    }
  }
);

console.log(root.foo); // "Cuack first value"

Redefine how plain objects are processed to generate custom attributes:

function buildObject(treeBuilder, target, keyName, value) {
  var childNode = {
    generatedProperty: 'generated property'
  };

  // define current keyName and assign the new object
  Ceibo.defineProperty(target, keyName, childNode);

  // continue to build the tree recursively
  treeBuilder.processNode(value, childNode, target);
}

var root = Ceibo.create(
  {
    foo: {
      bar: 'baz'
    }
  },
  {
    builder: {
      object: buildObject
    }
  }
);

console.log(tree.generatedProperty); // "generated property"
console.log(tree.foo.generatedProperty); // "generated property"
console.log(tree.foo.bar); // "baz"

You can navigate to parent nodes

var tree = Ceibo.create({
  foo: {
    bar: {
      baz: 'a value'
    }
  }
});

console.log(Ceibo.parent(tree.foo.bar).bar.baz); // "a value"

You can assign custom parents to trees

var parentTree = Ceibo.create({ foo: 'value' });
var childTree = Ceibo.create({ bar: 'another value' }, { parent: parentTree });

console.log(Ceibo.parent(childTree).foo); // "value"

License

Ceibo is licensed under the MIT license.

See LICENSE for the full license text.

ceibo's People

Contributors

san650 avatar

Watchers

 avatar  avatar

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.