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 🙏

© 2026 – Pkg Stats / Ryan Hefner

jsonlogic-explainer

v1.0.1

Published

A library to convert JSONLogic expressions into human-readable explanations

Readme

JSONLogic Explainer

A library to convert JSONLogic expressions into human-readable explanations.

Installation

npm install jsonlogic-explainer
# or
yarn add jsonlogic-explainer
# or
pnpm add jsonlogic-explainer

Usage

Basic Usage

import { JsonLogicExplainer } from "jsonlogic-explainer";

const explainer = new JsonLogicExplainer();

// Simple equality check
const expression1 = { "===": [{ var: "age" }, 18] };
console.log(explainer.explain(expression1));
// Output: "age equals 18"

// Conditional logic
const expression2 = { if: [{ ">": [{ var: "age" }, 18] }, "adult", "minor"] };
console.log(explainer.explain(expression2));
// Output: "if age is greater than 18 then adult else minor"

// Complex nested operations
const expression3 = {
  and: [
    { ">": [{ var: "age" }, 18] },
    {
      or: [
        { "==": [{ var: "status" }, "active"] },
        { "==": [{ var: "status" }, "pending"] },
      ],
    },
  ],
};
console.log(explainer.explain(expression3));
// Output: "age is greater than 18 and status equals active or status equals pending"

Advanced Usage

You can customize the explainer with options:

const explainer = new JsonLogicExplainer({
  variableNames: {
    age: "customer age",
    status: "account status",
  },
  data: {
    // Optional: data context for variable evaluation
  },
});

const expression = { "===": [{ var: "age" }, 18] };
console.log(explainer.explain(expression));
// Output: "customer age equals 18"

Supported JSONLogic Operations

The library supports all major JSONLogic operations:

Arithmetic Operations

  • +: Addition (e.g., [2, 3, 4] → "2 plus 3 plus 4")
  • -: Subtraction (e.g., [10, 5, 3] → "10 minus 5 minus 3")
  • *: Multiplication (e.g., [2, 3, 4] → "2 times 3 times 4")
  • /: Division (e.g., [15, 3, 2] → "15 divided by 3 divided by 2")
  • %: Modulo (e.g., [10, 3] → "10 modulo 3")
  • abs: Absolute value (e.g., [-5] → "absolute value of -5")
  • max: Maximum value (e.g., [1, 5, 3] → "maximum of 1, 5, 3")
  • min: Minimum value (e.g., [1, 5, 3] → "minimum of 1, 5, 3")

Comparison Operations

  • ==: Loose equality (e.g., ["hello", "hello"] → "hello equals hello")
  • ===: Strict equality (e.g., [18, 18] → "18 equals 18")
  • !=: Loose inequality (e.g., ["hello", "world"] → "hello does not equal world")
  • !==: Strict inequality (e.g., [18, "18"] → "18 does not strictly equal 18")
  • >: Greater than (e.g., [10, 5] → "10 is greater than 5")
  • >=: Greater than or equal (e.g., [10, 10] → "10 is greater than or equal to 10")
  • <: Less than (e.g., [5, 10] → "5 is less than 10")
  • <=: Less than or equal (e.g., [5, 5] → "5 is less than or equal to 5")
  • in: Value in collection (e.g., ["Ringo", ["John", "Paul", "George", "Ringo"]] → "Ringo in John, Paul, George, Ringo")

Logical Operations

  • and: Logical AND (e.g., [true, false, true] → "true and false and true")
  • or: Logical OR (e.g., [false, true, false] → "false or true or false")
  • !, not: Logical NOT (e.g., true → "not true")

Conditional Operations

  • if: If-then-else logic (e.g., [condition, "then", "else"] → "if condition then then else else")

Array Operations

  • map: Apply operation to each element
  • filter: Filter elements based on condition
  • reduce: Reduce array to single value
  • all: Check if all elements satisfy condition
  • none: Check if no elements satisfy condition
  • some: Check if some elements satisfy condition
  • merge: Merge arrays
  • in: Check if value exists in array

String Operations

  • cat: Concatenate strings (e.g., ["Hello", " ", "World"] → "Hello World")
  • substr: Extract substring (e.g., ["Hello World", 0, 5] → "substring of Hello World from 0 with length 5")

Variable Operations

  • var: Access variables from context (e.g., "age" → "age", ["age", 0] → "age with default value of 0")

API Reference

JsonLogicExplainer

Main class for converting JSONLogic expressions to human-readable text.

Constructor

new JsonLogicExplainer(options?: JsonLogicExplainerOptions)

Options:

  • variableNames: Mapping of variable keys to human-readable names
  • operatorNames: Custom operator descriptions (not currently implemented)
  • includeJsonLogic: Whether to include the original JSONLogic in output (not currently implemented)
  • data: Data context for variable evaluation (not currently implemented)

explain(expression: JsonLogicExpression): string

Converts a JSONLogic expression to a human-readable string.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite: pnpm test
  6. Submit a pull request

License

MIT