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

formula-functionizer

v1.0.4

Published

A tool for parsing formulas and create reusable functions

Downloads

442

Readme

Formula Functionizer formula-functionizer

A library for parsing formulas and create reusable functions in javascript and typescript.

Formula functionizer allows to interpret formulas (and in particular excel formulas) in a javascript way, in particular by using variables instead of cells references.

In addition, it creates reusable functions. It only parses the formula once when the function is created, allowing to apply the same formula for several variables values. That can be really useful when dealing with large arrays or generating function plots.

Getting started

npm install --save formula-functionizer
import { Parser } from 'formula-functionizer';

// Create a parser
const parser = new Parser();

// Parse a formula to create a function
const fn = parser.parse('x^2 + 10 - y');

// Use your function !
console.log(fn({ x: 4, y: 5 })); // 21

Parser features

  • Operators (can behave like js or excel operators, see below)
    • Arithmetic operators +, -, *, / and ^
    • Comparison operators >, >=, <, <=, = and <>
  • Custom functions MYFUNCTION(4,5)
    • Can support all excel functions using formula.js (see below)
  • Arrays [1, 3, MYFUNCTION(4,5)]
  • Arbitrary variables like myVariable, you need to pass their value when executing the function

Options

Options can be passed when creating a parser using the Parser constructor.

import { Parser } from 'formula-functionizer';

const parser = new Parser({
  operators: 'excel',
  functions: {
    DOUBLE: (x) => x * 2,
  },
});

The provided options are merged with the default options, you can specify only the options that matter to you.

operators option

  • Possible values : excel, javascript.
  • Default value: excel
  • Describes how the operators (arithmetic and comparison) should be handled.
    • excel : Operators give the same result as in excel. For example, the + operator tries to coerce values as numbers. 1 + '1' returns 2, 1 + '1A' returns the #!VALUE error and 1/0 returns the #!DIV0 error.
    • javascript : Operators give the same result as when used in javascript. For example, 1 + '1' returns '11', 1/0 returns Infinity and 0^0 returns NaN.

functions option

  • An object with keys as function names and functions as values.
  • Default value: {}
  • These functions can be used in your formulas. You can use this option to define your own functions or import excel formulas from formulajs. (see below)
  • If you try calling a function that is not defined in your formula, the #?NAME will be returned.

Use excel formulas

You can use excel formulas using the formulajs library. To do this, start by installing the formulajs package.

npm install --save @formulajs/formulajs

import { Parser } from 'formula-functionizer';
import * as formulajs from '@formulajs/formulajs';

const parser = new Parser({
  functions: formulajs,
});

const fn = parser.parse('IF(SUM(x, y) > 10, "big", "small")');

console.log(fn({ x: 4, y: 5 })); // small

You can add your own functions to the formulajs functions this way :

import { Parser } from 'formula-functionizer';
import * as formulajs from '@formulajs/formulajs';

const parser = new Parser({
  functions: {
    ...formulajs,
    DOUBLE: (x) => x * 2,
  },
});

const fn = parser.parse('DOUBLE(SUM(x, y))');

console.log(fn({ x: 4, y: 5 })); // 18

References

This package is heavily inspired by handsontable/formula-parser