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 | - |
| 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
| Type alias | Description |
|---|---|
| ASTNode | Represents a node in the Abstract Syntax Tree of a publicodes expression. |
| BinaryOp | - |
| EngineOptions | Options for the engine constructor |
| EvaluatedNode | An ASTNode decorated with evaluation information |
| Evaluation | - |
| ExprAST | AST of a publicodes expression. |
| FlagOptions | - |
| Logger | A logger object that can be passed to the engine to log messages. |
| ParsedRules | A set of rules that have been parsed from a RawPublicodes object. |
| Possibility | Represents a single possibility value in a “une possibilité” mechanism. It can be a constant value (string or number), or a reference to an existing rule. |
| PublicodesExpression | A publicodes expression that can be evaluated by the Engine. |
| RawPublicodes | Rules as they are output from the compilation step : a javascript object transformed from the yaml files |
| Rule | - |
| RuleNode | - |
| Situation | A situation object that can be passed to the engine |
| StrictOptions | - |
| UnaryOp | - |
| Unit | Represents the unit of a number. |
| WarnOptions | - |