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

@maslox303/polyglot_js

v1.3.1

Published

Cross language interpreter of simple logical expressions

Downloads

12

Readme

Polyglot

An cross-platform, cross-language interpreter of simple, parametrizable logical expression in Reversed Polish Notation.

The motivation

Currently, there really isn't a way to share a simple piece of parametrized logic between frontend, and backend code. Let's say, for instance, that you wanted to display data in a list, with actions that the users can perform on a row. The user may, or may not have permissions to perform an action and we want to grey-out the unavailable actions. One way to solve that problem, would be to contaminate the list data, with names of actions that the user can take, for each row, which were evaluated on the backend. But what if generating the definition of that list, is expensive? How do you validate that the user trying to perform an action on a row, has permission to do so?

One way to solve that problem, would be to attach a logical expression, to the action definition, that would determine whether the user had permissions to execute it. If that expression would be executable by both frontend and backend, frontend could use it to grey-out unavailable actions and backend could use it to validate permissions (given that the expression was cryptographically signed). Thats where polyglot comes in.

The package

This package provides a compiled interpreter of a simple language to express logical expressions, that can accept parameters. It is compiled to a native binary, as well as to webassemby, with bindings for several programming languages (currently: Rust, C# and javascript).

Javascript instructions

To use this package in your javascript project, install it with your favorite package manager npm install @maslox303/polyglot_js. You then have to initialize the module, before using the evaluate function. Example:

import init from '@maslox303/polyglot_js'
import {evaulate} from '@maslox303/polyglot_js'

await init();
console.log(evaluate('(= @a 1)', {a: 1}));

Language

The language interpreted by polyglot is essentially constructed from boolean operators and comparison operators written in a Lisp-like Reversed Polish Notation For example: (= @a 1) means 'parameter with name a is equal to 1'. Comparison expressions can be also combined with boolean operators. For example (& (= @a 1) (= @b 'abc')) means 'parameter a is equal to 1 and parameter b is equal to string "abc"'. Boolean operators accepts any number of subexpressions as argumetnts. For example (| (= @a 1) (= @b 'abc') (! @c g)).

Boolean operators:

  • & and
  • | or

Comparison operators

  • = equal
  • ! not equal
  • > first operand greater than second (only numeric values)
  • ] first operand greater or equal than second (only numeric values)
  • < first operand less than second (only numeric values)
  • [ first operand less or equal than second (only numeric values)

Numeric values can be specified in ' or as bare strings: 123 equivalent to '123'.

Parameters can be accessed using @

Interpreter implementation file, contains unit tests with additional expression examples.