tallycat-parser

0.0.3 • Public • Published

This the parser for Tallycat. It uses a parser grammar using OhmJS

The parser can do arithmetic (+ - / *) on numbers with units. For example the string 2ft * 3 will return 6 feet as an object. It understand common imperial and metric units for length, weight, time, area, volume, and more.

Here's a few more simple examples.

2ft * 2ft * 2ft as gal => 59.8442 gallons
2 TB as GB => 2000 gigabytes
2 TiB as GiB => 2048 gibibytes
5 years as seconds => 157680000 seconds

Use the library from node like this:

import {Parser} from "tallycat-parser";

let result = Parser.parseString('4ft * 6');
console.log(result.toString())
// prints Literal 24 1*foot^1 / 

Parser.parseString returns a LiteralNumber object. To get the numeric value call getValue(). To print the rest of the structure call:

console.log(result.getValue())
result._numers.forEach(n => {
    console.log(n.getName(),n.getDimension(),n.getFactor())
})
result._denoms.forEach(n => {
    console.log(n.getName(),n.getDimension(),n.getFactor())
})

Readme

Keywords

none

Package Sidebar

Install

npm i tallycat-parser

Weekly Downloads

1

Version

0.0.3

License

ISC

Unpacked Size

74.9 kB

Total Files

27

Last publish

Collaborators

  • joshmarinacci