# Business rules as text with NEWMACRO()

> Compile small rules written in a simple language (IF, ELSE, AND, OR, RETURN, helper calls) into a function that you run with a model, so the rules can live in a file or a database.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: en
Content-SHA256: d802bfef67ede8fdf42be2807b173b3732349293d75dcc93f6bd3a325d48c417
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

`NEWMACRO(text, [nocompile], [isasync])` turns a few lines of a small language into a function `function(model, helpers)`. It is meant for rules that change more often than the code: a discount, a price, a permission check, a validation that someone who does not write JavaScript maintains in a file or in a database.

The language, as it was checked:

- `IF condition`, `ELSE IF condition`, `ELSE` and `FI` (the end of the condition) work as in a text-based rule. Conditions use `>`, `<`, `>=`, `<=`, `=` and `AND` / `OR`.
- `RETURN value` ends the macro with a value. `YES` and `NO` (also `TRUE`, `FALSE`, `OK`) are booleans, text between quotes is text and numbers are numbers.
- A word is a **property of the model**, written in lower case: `age` reads `model.age`, and `total = price * qty` writes `model.total`.
- `NAME(arguments)` calls a **helper**, the function with that name (lower case) in the `helpers` object you pass; inside it `this` is the model. With `isasync` set to `true` the macro is an async function and awaits the helpers.
- `#name` is a temporary variable of the run: `#temp = num + 1` and `RETURN #temp`.
- Use names of **several letters**: a property or a variable of one letter was not recognised in the checks.

`NEWMACRO()` compiles the text with `new Function`, so a macro is code: only compile text that you wrote or that a trusted person maintains, never text that a visitor sent. With `nocompile` set to `true` it returns the JavaScript that it generates instead of the function, which is useful to see what a rule does.

```js controllers/rules.js
const helpers = {
	percent: function(value, rate) {
		return value * rate / 100;
	}
};

const age = NEWMACRO("IF age >= 18\n\tRETURN 'adult'\nELSE\n\tRETURN 'minor'\nFI");

const level = NEWMACRO("IF points > 100\n\tRETURN 'gold'\nELSE IF points > 50\n\tRETURN 'silver'\nELSE\n\tRETURN 'bronze'\nFI");

const access = NEWMACRO("IF active AND score >= 5\n\tRETURN yes\nFI\nRETURN no");

const total = NEWMACRO("total = price * qty\nIF qty >= 10\n\ttotal = total - PERCENT(total, 10)\nFI\nRETURN total");

const temporary = NEWMACRO("#next = count + 1\nRETURN #next");

const slow = NEWMACRO("RETURN LOOKUP(code)", false, true);

exports.install = function() {
	ROUTE('POST /rules/', rules);
	ROUTE('GET /source/', source);
};

async function rules($) {
	const model = $.body;
	$.json({
		age: age(model, helpers),
		level: level(model, helpers),
		access: access(model, helpers),
		total: total(model, helpers),
		model_total: model.total,
		temporary: temporary(model, helpers),
		slow: await slow(model, { lookup: async code => 'found ' + code })
	});
}

function source($) {
	$.plain(NEWMACRO("IF points > 100\n\tRETURN 'gold'\nFI", true));
}
```
