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

calculon

v1.2.0

Published

Expressions parser and compiler

Downloads

8

Readme

Calculon

Rich expressions parser and compiler for DSL written on JavaScript. Calculon is a subset of JavaScript created for template string rendering. It's inspired by many languages such a Ruby to create user friendly syntax. It simple but expressive and contains construction destructuring, array slicing, filters, pipelining, ast inlining and other cool things.

Calculon is safe and doesn't allow to access to JS properties or overwrite them from expressions.

Calculon is fault tolerant and tries not to produce any exceptions on runtime. So undefined filters and variables are normal and produces undefined values.

1 | add 2 | mul 5 == 15 // -> true
"b" | concat "a" _ "c" | toUpperCase // -> "ABC"
`Hi there ${ n | plural "time" "times" }!` -> Hi there 1 time.
{
  background: n | gte 1 "red" "green",
  border: `${ [0, 2][n] } + px solid`
} // -> {background: red, border: '2px solid'}
[1, 2, 3, 4][0..n] // -> [0, 1]

Installation

Via npm:

npm i calculon

Usage

const Calculon = require('calculon');
const calc = Calculon.new({
  primitives: {
    number: {
      add(a, b) {
        return a + b;
      }
    }
  },
  filters: {
    add(a, b) {
      return a + b;
    }
  }
});

calc('1.add(1)'); // -> 2
calc('1 | add 1'); // -> 2

Syntax

Primitives

Calculon contains basic primitives for numbers, strings and booleans.

Numbers

Numbers could be defined like so:

10
-10
1000
1_000
-1_023.12 == -1023.12

Currently calculon supports decimal numbers only.

Strings

Strings should be surrounded with single quote, double quote or akut:

"Hello"
'Hello'
`Hello`
:Hello

Strings with akut could contain calculon expressions to evaluate:

`Hello ${ username | toUpperCase } `

Booleans

Booleans are the same as in JS:

true
false

Arrays

Calculon support arrays:

[1, 2, 3]

Array could contain values of any type. To get array value by its index use square brackets:

[1,2,3][0] // -> 1

Also array could return the last item by magic index *:

[1,2,3,4][*] // -> 4

Objects

Objects are the same as in JS:

{a: 1}
{"a": 1}
{["a" + "a"]: 1}

AST

Calculon allow to use ast as a first class citizen. So it allow to pass AST as an arugment or use in other way:

@{a.toString()
[@{a | toLowerCase}, @{1 + a}]

Example below creates single ast and array of two ast values.

Smart destructuring

Arrays

To get slice from array you can just use simple range extraction:

[1, 2, 3, 4][1..2] // -> [2, 3]

Or use another array:

[1,2,3,4][[1,2]] // -> [2, 3]

Objects

Calculon allow to get exact keys from object and put it into new object:

{a: 1, b: 2, c: 3}['a', 'c'] // -> {a: 1, c: 3}

Nested objects can be destructured too:

{a: {b: 1, c: 2}}['a':['c']] // -> {a: {c: 2}}

Filters and pipes

Calculon contains filters syntax:

"a" | toUpperCase // -> "A"

Filters could be piped:

"Hello" | toUpperCase | reverse // -> "OLLEH"

Filters could have arguments separated with whitespace:

"0" | repeat 5 // -> '00000'

To pass filtered value to exact position in filters arguments use underscore:

"b" | concat "a" _ "c" // -> "abc"
users | map :name // users=[{name:'john'}, {name: 'jack'}, {name: 'jim'}] -> ['john', 'jack', 'jim']

Define filter

Filters are defines when calculon instantiates with options.filters:

var eval = Calculon.new({
  filters: {
    add(a, b) {
      return a + b;
    },
  },
});

Primitives' methods

Calculon allows to override methods own methods for primitives. For example we can define not method for booleans to invert boolean value:

var eval = Calculon.new({
  primitivies: {
    boolean: {
      not(value) {
        return !value;
      }
    }
  }
});

eval('true.not()'); //-> false
eval('false.not()'); //-> true
eval('isOk.not()', {isOk: true}); //-> false

Security

Calculon doesn't allow to overwrite object constructors (or even get it) so it's safe to use with untrusted code. Also calculon is read only and it has no constructions to modify values or variables.

Credentials

Parser is made with PegJS powerful and simple parser generator.

License

MIT.