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

json-based-conditions-and-rules-logic-evaluator

v1.0.2

Published

script that parses some customizable conditions and rules, then outputs a predefined value of that matching rule, or a default value

Downloads

8

Readme

JSON based conditions and rules logic evaluator

What

A script that parses some customizable conditions and rules, then outputs a predefined value of that matching rule, or a default value.

Why

My z12 micropad uses 4 layers: vim, browser, figma and default. Encoders do different things on each layer. More often than not, when working in Figma I wanted to zoom using the right encoder, but if I had the default layer active, the encoder would scroll instead. I created a node script (active-app-qmk-layer-updater) that checks the active app every half a second, and uses this script as a dependency to match the app name and title to a set of conditions and rules. This way I can get an output (in my case, a layer index) to change the kaymap layer in the QMK keyboard.

Setup

To determine which output to return, we need to match a set of conditions and rules, which are completely configurable using a simple JSON file, against a literals object.

Literals

Literals hold values to be compared against, by reference. These are not part of the main JSON file, because they are the dymamic values that the main script will be using to match against the conditions and rules. This object will be sent to the parser as a second argument.

Be sure to .trim().toLowerCase() each value while building the literal object if you want to remove spaces from both ends of the string and make it case insensitive.

Conditions

CONDITONS is an object that includes one object per condition to be parsed by the rules.

Each condition has an id, and requires two values and an operator that will calculate if the condition is fulfilled.

The left hand side value is a reference to literals explained above.

The right hand value is the string we want to compare against. It can also be an array of strings. In that case, for the condition to be fulfilled, at least one of the strings in the array need to satisfy the operation.

The operator is any of the conditions defined in the conditions folder: contains, ends, equals or starts.

A condition returns a boolean, being true when it fulfills.

Rules

RULES is an array that includes one object per set of conditions.

Each set of conditions can have one or several conditions. In case of having more than one condition, the operator will define their logic. or operator will define that at least one of the conditions needs to be met in order to satisfy the rule. and operator will define that all the conditions must be met in order to satisfy the rule.

Rules are evaluated top down.

The first rule that fulfills will break the loop and return the output value.

Default value

If no rule is satisfied, default value will be returned.

How to run it

Import the parser in the script running this dependency:

const { parser } = require('json-based-conditions-and-rules-logic-evaluator');

Then load the config JSON file (or create a static JSON file):

const configfile = fs.readFileSync(path);
const configObject = JSON.parse(configfile);

Create a literals object that will include the key/value pairs to be used as references in the conditions lhs (left hand side):

const literals = {
  'app': 'Terminal',
  'title': 'VIM',
};

In my case, that literals object is updated every half a second. It's up to your app's purpose to update this.

Then call the parser, passing the config and literals objects as arguments:

const output = parser(configObject, literals);

This script will evaluate the output according to the predefined conditons and rules. Your app can then consume the output and do whatever action with it.

To Do

  • [x] Documentation
    • [x] Basic use
    • [x] Configuration file
  • [x] Examples
  • [x] Tests

Users