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

ClassDescription
EngineThe engine is used to parse rules and evaluate expressions. It is the main entry point to the publicodes library.

Functions

FunctionDescription
parseExpressionParse a publicodes expression into an JSON object representing the AST.
parsePublicodesParse a set of publicodes rules
parseUnitParse a unit string into a Unit object
reduceASTThis function allows to construct a specific value while exploring the AST with a simple reducing function as argument.
serializeEvaluationSerialize the evaluation of a node into a publicodes expression
serializeUnitSerialize a unit to a readable string
simplifyNodeUnitSimplify if possible the unit of a node
transformASTThis function creates a transormation of the AST from on a simpler callback function fn
traverseASTNodeApply a transform function on children. Not recursive.

Namespaces

NamespaceDescription
utilsContains utilities to manipulate publicodes rules by name.

Type Aliases

Type aliasDescription
ASTNodeRepresents a node in the Abstract Syntax Tree of a publicodes expression.
EngineOptionsOptions for the engine constructor
EvaluatedNodeAn ASTNode decorated with evaluation information
Evaluation-
ExprASTAST of a publicodes expression.
LoggerA logger object that can be passed to the engine to log messages.
ParsedRulesA set of rules that have been parsed from a RawPublicodes object.
PublicodesExpressionA publicodes expression that can be evaluated by the Engine.
RawPublicodesRules as they are output from the compilation step : a javascript object transformed from the yaml files
Rule-
RuleNode-
SituationA situation object that can be passed to the engine
StrictOptions-
UnitRepresents the unit of a number.