Spanish Total.js
English
Documentation For AI agents
Contents

Macros

Compile a small rule language into a function with NEWMACRO(), for rules that users write.

.md GitHub

NEWMACRO(code) compiles a small rule language into a function. It is meant for rules that users write in an admin, such as prices or discounts. The function receives a model and an object with helpers.

var rule = NEWMACRO([
    '#total = price * count',
    'IF #total > 1000 AND country == "SK"',
    '    RETURN discount(#total)',
    'FI',
    'RETURN #total'
].join('\n'));

rule({ price: 600, count: 2, country: 'SK' }, { discount: function(value) { return value * 0.8; } }); // 960

A macro has IF, ELSE IF, ELSE, FI, RETURN, AND, OR, NULL and the values TRUE, FALSE, YES, NO. Words are properties of the model, #name is a temporary variable and name(...) calls a helper. Keywords can be in any case, but properties and helpers are read in lower case. NEWMACRO(code, true) returns the generated JavaScript instead of a function, and NEWMACRO(code, false, true) makes an async function that awaits the helpers.

Made in Europe