npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@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 }'