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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@marianmeres/condition-builder

v1.9.2

Published

A tool for creating hierarchical logical _conditions_ and _expressions_, mainly to be used in - but not limited to - an sql _where_ statement.

Readme

@marianmeres/condition-builder

A tool for creating hierarchical logical conditions and expressions, mainly to be used in - but not limited to - an sql where statement.

Terminology

Expression

Expression is the base building block. It consists of key, operator and value.

`a=b`;

Condition

Condition is a hierarchical collection of one or more expressions or conditions joined by a logical join operator, which is either and or or.

// condition with one expression
`a=b`;

// condition with 2 expressions joined by `or`
`a=b or c=d`;

// condition of multiple hierarchically structured expressions and conditions
`a=b or (c>d and (e<f or g!=h))`;

Installation

deno

deno add jsr:@marianmeres/condition-builder

nodejs

npm i @marianmeres/condition-builder

Usage

import { Condition } from "@marianmeres/condition-builder";

Example

The core api consists of 2 methods and(...) and or(...). For the first call you can use any one of them.

const c = new Condition();

c.and("a", OPERATOR.eq, "b");
// c.or("a", OPERATOR.eq, "b"); // same effect as above for the first call
assertEquals(c.toString(), "a=b");

c.or("c", OPERATOR.neq, "d");
assertEquals(c.toString(), "a=b or c!=d");

c.or(
	new Condition()
		.and("e", OPERATOR.lt, "f")
		.and("g", OPERATOR.eq, "h")
		.or(
			new Condition()
				.and("i", OPERATOR.match, "j")
				.and("k", OPERATOR.nmatch, "l"),
		),
);

assertEquals(c.toString(), "a=b or c!=d or (e<f and g=h or (i~j and k!~l))");

// dump & restore
const c2 = Condition.restore(c.dump());
assertEquals(c2.toString(), "a=b or c!=d or (e<f and g=h or (i~j and k!~l))");

// or export the condition as POJO structure for manual processing (eg evaluation)
const structure = c.toJSON();

Expression validation and rendering

Point of this package is to create a textual representation of the logical conditions blocks to be used in an sql where statement. By default, the package is content and dialect agnostic. Just renders the input as is, which may not be always desired.

Validation

To validate the condition, you must provide the validate function which will validate every expression before being added to the condition.

const c = new Condition({
	// this example will allow only a known keys to be set
	validate: (ctx: ExpressionContext) => {
		const { key } = ctx;
		const keyWhitelist = ["foo"];
		if (!keyWhitelist.includes(key)) {
			throw new TypeError(`Key '${key}' not allowed`);
		}
	},
});

// `foo` key is allowed
c.and("foo", OPERATOR.eq, "1");

// `bar` is not
assertThrows(() => c.and("bar", OPERATOR.neq, "2"));

Rendering

To match the textual representation for any specific format you must provide any of the renderKey, renderValue, or renderOperator functions.

For example for postgresql dialect you may use something like this:

const c = new Condition({
	// escape identifiers in postgresql dialect
	renderKey: (ctx: ExpressionContext) => `"${ctx.key.replaceAll('"', '""')}"`,
	// escape values in postgresql dialect
	renderValue: (ctx: ExpressionContext) =>
		`'${ctx.value.toString().replaceAll("'", "''")}'`,
	// read below
	// renderOperator(ctx: ExpressionContext): string
});
c.and('fo"o', OPERATOR.eq, "ba'r");
assertEquals(c.toString(), `"fo""o"='ba''r'`);

Built-in operators rendering

There is a default built-in operator-to-symbol replacement logic (targeting postgresql dialect), loosely inspired by postgrest.

Any found operator in the map below will be replaced with its symbol. If the operator is not found in the map, no replacement will happen. You can customize this logic by providing your own custom renderOperator function.

// default opinionated conversion map of operators to operator symbols.
{
    eq: "=", 
    neq: "!=", 
    gt: ">", 
    gte: ">=", 
    lt: "<", 
    lte: "<=",
    like: " ilike ", 
    nlike: " not ilike ",
    match: "~*", 
    nmatch: "!~*", 
    is: " is ",
    nis: " is not ",
    in: " in ", 
    nin: " not in ",
};

// but you can safely use any operator you see fit...
const e = new Expression("foo", "==", "bar");
assertEquals(e.toString(), "foo==bar");

Related

@marianmeres/condition-parser

Package Identity

  • Name: @marianmeres/condition-builder
  • Author: Marian Meres
  • Repository: https://github.com/marianmeres/condition-builder
  • License: MIT