Urgent.News

What's breaking now, across thousands of outlets.

Tech

Turning "price - product cost - shipping" into a calculator: a plain-word formula parser with no eval

Most people don't think in spreadsheet syntax. Ask an online seller how they work out profit per order and they'll say something like "price minus product cost minus shipping." Not =B2-C2-D2 . I wanted a calculator builder where you can type exactly that, price - product cost - shipping , and get a working calculator with labelled inputs. Here's how the parser behind it works, in about 100 lines…

Most people don't think in spreadsheet syntax when calculating profit per online order. They might say something like "price minus product cost minus shipping" instead of writing "=B2-C2-D2" in a spreadsheet. To create a calculator builder that accepts formulas in plain English, the parser uses a series of steps without using a built-in eval function.

First, it cleans up the formula by normalizing the typography. Then, it converts multi-word names into a single variable, such as "price - product cost - shipping" becoming "price_product_cost_shipping". Next, it detects the inputs required for the formula and organizes them in the order they appear. After that, it converts abbreviations into human-readable labels based on a predefined dictionary.

Finally, the parser evaluates the formula safely, without using eval, by using a recursive-descent evaluator that supports numbers, basic arithmetic operations, parentheses, and a limited set of functions. This ensures that the calculator can be chained together for more complex calculations and can be accessed through a JSON API or remote MCP server, allowing tools like AI assistants to run them accurately.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Protocol Upgrade Compatibility Review: Bitfinex

Protocol Upgrade Compatibility Review: Bitfinex Target Protocol : Bitfinex (TVL: $20712.0M) Protocol Upgrade Compatibility Review – Bitfinex TVL: ≈ $20.7 B (Ethereum + L2) Prepared by: [Your Firm]…

Build & Release #3: I Deleted Every npm Token I Own

On October 7th I tried to publish @7onic-ui/tokens@0.3.7 and npm told me my own package didn't exist. npm error 404 Not Found - PUT https://registry.npmjs.org/@7onic-ui%2ftokens npm error 404…

  • Reporter deleted all npm tokens and NPMTOKEN secret from GitHub
  • Switched to Trusted Publishing for secure package publishing
  • Successfully published packages without authentication issues

More from Friday 9 October →