publicodes
API / publicodes
Getting started
npm install publicodes
The library exports a default class Engine
which allows you to instantiate an publicodes interpreter with a set of publicodes rules.
import Engine from 'publicodes'
import { parse as parseYaml } from 'yaml'
// We define a list of publicodes rules
const rules = `
dépenses primeur:
somme:
- 1.5 kg * 2€/kg # carottes
- 500g * 6€/kg # champignons
- 3 avocats * 1.5€/avocats
`
const parsedRules = parseYaml(rules)
const engine = new Engine(parsedRules)
const result = engine.evaluate('dépenses primeur')
👉 Continue to the « Getting started » section
Classes
Class | Description |
---|
Engine | The engine is used to parse rules and evaluate expressions. It is the main entry point to the publicodes library. |
Functions
Function | Description |
---|
parseExpression | Parse a publicodes expression into an JSON object representing the AST. |
parsePublicodes | Parse a set of publicodes rules |
parseUnit | Parse a unit string into a Unit object |
reduceAST | This function allows to construct a specific value while exploring the AST with a simple reducing function as argument. |
serializeEvaluation | Serialize the evaluation of a node into a publicodes expression |
serializeUnit | Serialize a unit to a readable string |
simplifyNodeUnit | Simplify if possible the unit of a node |
transformAST | This function creates a transormation of the AST from on a simpler callback function fn |
traverseASTNode | Apply a transform function on children. Not recursive. |
Namespaces
Namespace | Description |
---|
utils | Contains utilities to manipulate publicodes rules by name. |
Type Aliases