@negotiate-gpt/formulas
v1.0.3
Published
Formula Logic handler for Negotiate-GPT
Downloads
81
Readme
@negotiate-gpt/formulas
This package handles formulas for the Negotiate-GPT platform.
Installation
npm install @negotiate-gpt/formulas
or
yarn add @negotiate-gpt/formulas
Usage
Basic Usage
ES6
import { parseFormula } from '@negotiate-gpt/formulas'
const formula = parseFormula('2 + 2')
const result = formula.execute()
console.log(result.valid) // true
console.log(result.value) // 4
CommonJS
const { parseFormula } = require('@negotiate-gpt/formulas')
const formula = parseFormula('2 + 2')
const result = formula.execute()
console.log(result.valid) // true
console.log(result.value) // 4
Use Variables
ES6
import { parseFormula } from '@negotiate-gpt/formulas'
const formula = parseFormula('$x + 2')
const result = formula.execute({ x: 2 })
console.log(result.valid) // true
console.log(result.value) // 4
TypeChecking
ES6
import { parseFormula } from '@negotiate-gpt/formulas'
const formula = parseFormula('$x + $y')
const typeCheckResult = formula.typeCheck(
{ x: {type: 'number'}, y: {type: 'number'} }
)
console.log(typeCheckResult.valid) // true
console.log(typeCheckResult.type) // {type: 'number'}
const invalidTypeCheckResult = formula.typeCheck(
{ x: {type: 'number'}, y: {type: 'boolean'} }
)
console.log(invalidTypeCheckResult.valid) // false
console.log(invalidTypeCheckResult.message) // 'Both operands must be numbers'
Tokenization
You can access the tokenization of the formula for highlighting and error communication.
ES6
import { parseFormula } from '@negotiate-gpt/formulas'
const formula = parseFormula('5 + $myVar * (if ($myVar > 5, 0, 1))')
console.log(formula.tokenized)
/*
[
{ type: 'number', value: '5', startIndex: 0, endIndex: 1 },
{ type: 'whitespace', value: ' ', startIndex: 1, endIndex: 2 },
{ type: 'operator', value: '+', startIndex: 2, endIndex: 3 },
{ type: 'whitespace', value: ' ', startIndex: 3, endIndex: 4 },
{ type: 'variable', value: '$myVar', startIndex: 4, endIndex: 10 },
{ type: 'whitespace', value: ' ', startIndex: 10, endIndex: 11 },
{ type: 'operator', value: '*', startIndex: 11, endIndex: 12 },
{ type: 'whitespace', value: ' ', startIndex: 12, endIndex: 13 },
{ type: 'openParen', value: '(', startIndex: 13, endIndex: 14 },
{ type: 'function', value: 'if', startIndex: 14, endIndex: 16 },
{ type: 'whitespace', value: ' ', startIndex: 16, endIndex: 17 },
{ type: 'openParen', value: '(', startIndex: 17, endIndex: 18 },
{ type: 'variable', value: '$myVar', startIndex: 18, endIndex: 24 },
{ type: 'whitespace', value: ' ', startIndex: 24, endIndex: 25 },
{ type: 'operator', value: '>', startIndex: 25, endIndex: 26 },
{ type: 'whitespace', value: ' ', startIndex: 26, endIndex: 27 },
{ type: 'number', value: '5', startIndex: 27, endIndex: 28 },
{ type: 'comma', value: ',', startIndex: 28, endIndex: 29 },
{ type: 'whitespace', value: ' ', startIndex: 29, endIndex: 30 },
{ type: 'number', value: '0', startIndex: 30, endIndex: 31 },
{ type: 'comma', value: ',', startIndex: 31, endIndex: 32 },
{ type: 'whitespace', value: ' ', startIndex: 32, endIndex: 33 },
{ type: 'number', value: '1', startIndex: 33, endIndex: 34 },
{ type: 'closeParen', value: ')', startIndex: 34, endIndex: 35 },
{ type: 'closeParen', value: ')', startIndex: 35, endIndex: 36 }
]
*/
Error handling
There are three types of errors:
FormulaParsingError
: This is an error that occurs when the formula could not be parsed into an AST.FormulaExecutionError
: This is an error that occurs when the formula could not be executed.InvalidTypeCheckReturn
: This is an error that occurs when the type check fails.
These errors are returned objects not subclasses of Error
.
FormulaParsingError
ES6
import { parseFormula } from '@negotiate-gpt/formulas'
const formula = parseFormula('2 +')
console.log(formula.valid) // false
console.log(formula.message) // 'Not enough nodes for operator'
console.log(formula.sourceToken) // '{ type: 'operator', value: '+', startIndex: 2, endIndex: 3 }'
console.log(formula.type) // 'NotEnoughNodesForOperator'
console.log(formula.tokenization)
/*
[
{ type: 'number', value: '2', startIndex: 0, endIndex: 1 },
{ type: 'whitespace', value: ' ', startIndex: 1, endIndex: 2 },
{ type: 'operator', value: '+', startIndex: 2, endIndex: 3 }
]
*/
FormulaExecutionError
ES6
import { parseFormula } from '@negotiate-gpt/formulas'
const formula = parseFormula('2 + get($emptyArray, 0)')
const result = formula.execute({emptyArray: []})
console.log(result.valid) // false
console.log(result.message) // 'Array index 0 out of bounds for array of length 0'
console.log(result.sourceNode.sourceToken) // '{ type: 'function', value: 'get', startIndex: 4, endIndex: 7, numberOfArgs: 2 }'
console.log(result.type) // 'ArrayIndexOutOfBounds'
InvalidTypeCheckReturn
ES6
import { parseFormula } from '@negotiate-gpt/formulas'
const formula = parseFormula('if($numVar>5, 0, $boolVar)')
const typeCheckResult = formula.typeCheck(
{ numVar: {type: 'number'}, boolVar: {type: 'boolean'} }
)
console.log(typeCheckResult.valid) // false
console.log(typeCheckResult.message) // 'if must have arguments of the same type'
console.log(typeCheckResult.sourceNode.sourceToken) // '{ type: 'function', value: 'if', startIndex: 0, endIndex: 2, numberOfArgs: 3 }'