Code Monkey home page Code Monkey logo

jetfun's Introduction

jetfun

Convert JSON expressions to functions

This node.js package is used to convert a JavaScript object into a function. Although this package supports a wide variety of use-cases, it was originally intended to allow a browser to send a complex operation to a web server as a single JSON expression.

jetfun(expression) → function

Below is a simple example of converting a greeting object into a function. Once converted, the object can be bound to, or called against, any Object exposing the desired methods.

var jetfun = require('jetfun');

function English() { }

// greet someone in English
English.prototype.greet = function(e) {
  if (e.sex === 'male') {
    console.log('Hello Mr. ' + e.greet);
  }
  else {
    console.log('Hello Mrs. ' + e.greet);
  }
};

function Spanish() { }

// greet someone in Spanish
Spanish.prototype.greet = function(e) {
  if (e.sex === 'male') {
    console.log('Hola Señor ' + e.greet);
  }
  else {
    console.log('Hola Señora ' + e.greet);
  }
};

// convert the JSON expression to a function
var greetChris = jetfun({
  greet: 'Chris',
  sex: 'male'
});

greetChris.call(new English());
// => Hello Mr. Chris

greetChris.call(new Spanish());
// => Hola Señor Chris

handler(expression, value, [callback])

The handler function that is invoked must take the expression as the first argument. If the length property of the handler function is greater than two, then the callback must be called; otherwise, the function resolves to the return value of the handler function. The return value is assumed to be a promise if it has a then method.

Below is an example demonstrating each form of handler function and how they operate.

var jetfun = require('jetfun'),
    when = require('when');

function Calculator() { }

Calculator.prototype.clear = function() {
  return 0; // return a value
};

Calculator.prototype.add = function(e, value, callback) {
  callback(null, value + e.add); // callback with a value
};

Calculator.prototype.substract = function(e, value) {
  return when(value - e.substract); // return a promise
};

var fun = jetfun({
  do: [
    { clear: null },
    { add: 3 },
    { subtract: 1 }
  ]
});

fun.call(new Calculator(), function(err, value) {
  console.log(value); // => 2
});

jetfun's People

Contributors

bakerface avatar

Watchers

James Cloos avatar  avatar

Forkers

svikashk

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.