Giter Club home page Giter Club logo

depresso's Introduction

depresso API Documentation

You have a deeply nested object with some missing values. You also have some rules how to calculate a value from other values. Depresso takes an object and the calculation rules, and does a dependency resolution. Let's see an example.

We have an object with a placeholder for missing values:

    MISSING = -1

    inventory =
      general:
        discount: 0.1
        price: MISSING
      products: [
        name: 'Bag'
        netPrice: 20
        tax: 0.1
        price: MISSING
      ,
        name: 'Beer'
        netPrice: 10
        tax: 0.2
        price: 12
      ] 

There are two rules

  • A product price can be calculated by adding tax to the net price.
  • The total price is the sum of all product prices.

Here is the code describing these rules:

    rules = [
      target: '/products//price'
      depends: 
        netPrice: '../netPrice'
        tax: '../tax'
      calculate: ->
        @netPrice * (1 + @tax)
    ,
      target: '/general/price'
      depends:
        prices: '/products//price'
      calculate: ->
        _.reduce(
          @prices
          (x,y) -> x+y
          0)
    ]

A rule consists of

  • A target pointing to one or more nodes in the object.
  • Dependencies, mapping a name to an xpath-like expression.
  • A calculation which has access to the values defined in the dependencies, and returns a value that will be written into the underlying object.

Circular rules should be avoided.

Finally, we specify which node(s) should be calculated:

    {resolve} = require 'depresso'
    resolve inventory, rules, '/general/price'
    console.log inventory.general.price # prints 34

The xpath-like expressions used throughout the examples are provided by SpahQL, the order of dependency resolution is calculated with DepGraph.

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.