The Fate API

The primary Fate API is a single JavaScript module that exposes several functions for compiling and evaluating scripts. The most important of these functions is compile().

var fate = require('fatejs');
var compiled = fate.compile(script);
compiled(context);

The purpose of this function is to compile script strings into executable JavaScript functions. The context argument provided to the compiled script is an Object whose properties will be treated as globals. If nothing is provided, a default Globals instance will be used. Here is an example of using the fate() function:

var fate = require('fatejs');
var script = fate.compile("name | 'Hello, %!'");
script({ name: 'World' }); //-> Hello, World!

There are additional support structures and functions attached to the fate() function. By default, they are as follows:

  • compile(script) - Compiles the specified source, producing a Function that is used to invoke the compiled script.
  • evaluate(script, [context]) - Compiles and evaluates the specified source against the provided context object. By default, the context is a Globals instance provided by the globals() Function.
  • globals([extensions]) - creates a new Fate Globals instance.

Compilation

The fate.evaluate() function will parse, compile, and generate a Function for your script all in one pass. But there are cases where you may want the compiled JavaScript for a script rather than a function. In these cases, you'll want to call fate.compile().

results matching ""

    No results matching ""