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-rules-engine-to-json-logic

v0.1.2

Published

A minimal Typescript library for converting a json-rules-engine condition to a JsonLogic rule specification.

Downloads

19

Readme

:warning: This package is under active development: Compatibility and APIs may change.

Build status Version Minzipped Size License

twitter github youtube linkedin

Why?

From the json-rules-engine README:

json-rules-engine is a powerful, lightweight rules engine. Rules are composed of simple json structures, making them human readable and easy to persist.

Rule engines often come in handy because they allow you to:

  • Express complex rules in JSON, and
  • Can be evaluated securely (no use of eval)

Because of this, user defined rules can be persisted and securely evaluated on both your frontend and backend, making them ideal for scenarios when you need to allow configurable rules to be securely evaluated during runtime (e.g. an automation engine).

There are many 3rd party libraries that depend on rules engines, json-logic being one of the more popular options. As such, these libraries often expect as input, or they output a json-logic rule. This can sometimes be an issue as it might force you to adopt json-logic throughout your entire application, or it might mean you're unable to use those libraries if you choose to roll a different engine.

By providing the means to convert a json-rules-engine condition to a json-logic rule, json-rules-engine-to-json-logic allows you to take advantage of libraries that integrate with json-logic without having to adopt it across your entire application should you want to use json-rules-engine.

Key Features

  • written in typescript
  • zero dependencies
  • minimal size: less than 1kb minified + zipped
  • works with plain JavaScript too - you don't need to use TypeScript.
  • small api: only two functions you need to learn
  • isomorphic: works in Node.js and all modern browsers

Installation

$ npm install json-rules-engine-to-json-logic

API

toJsonRule(condition: JsonRulesEngineCondition): JsonLogicRule

Converts a json-rules-engine condition to a json-logic rule.

import { toJsonRule } from "json-rules-engine-to-json-logic";

const jsonLogicRule = toJsonRule({
  all: [
    {
      fact: "name",
      operator: "equal",
      value: "Harry Potter",
    },
  ],
});

// => { and: [{ "===": [{ var: "name" }, "Harry Potter"] }] };

CompatibilityError(message: string)

Thrown on failed conversions when the json-rules-engine condition uses functionality that can't be converted. Contains the following properties:

  • name: "CompatibilityError"
  • message: a string, indicating what was incompatible.

Example

import { toJsonRule } from "json-rules-engine-to-json-logic";
import { Engine } from "json-rules-engine";
import jsonLogic from "json-logic-js";

const facts = {
  name: "Harry Potter",
};

const conditions = {
  all: [
    {
      fact: "name",
      operator: "equal",
      value: "Harry Potter",
    },
  ],
};

// evaluate using json-rules-engine
const engine = new Engine();
engine.addRule({
  conditions,
  event: { type: "isHarry" },
});
const result = await engine.run(facts);
const jsonRulesResult = result.results.length > 0;

// evaluate using json-logic
const jsonLogicRule = toJsonRule(conditions);
const jsonLogicResult = jsonLogic.apply(jsonLogicRule, facts);

// assert both return the same result
console.assert(jsonLogicResult === jsonRulesResult);

This is available in the examples

Compatibility

Given both libraries offer different functionality, it is impossible to 100% convert between one and the other. In the case where a conversion isn't possible, a Compatibility error will be thrown, or a feature will be ignored. Current incompatibilities include:

  • json-rules-engine uses json-path whereas json-logic uses dot notation. json-path is the more powerful of the two, and a lot of functionality doesn't port over. A Compatibility error will be thrown if a condition uses json-path functionality that can't be ported over to dot notation.
  • json-rules-engine provides a params property that allows you to create function handlers to provide dynamic facts during run-time. This isn't possible with json-logic and is therefore ignored.

See Also

License

MIT