Skip to content

Repository files navigation

logical-compiler

build version downloads license

Compile MongoDB-style boolean expressions ($and, $or, $not, $nor) with callbacks and custom functions

Installation

Use one of the two based on your project's dependency manager.

$ npm install logical-compiler --save

$ yarn add logical-compiler

Getting started

import compile from 'logical-compiler'; // ESM
// or: const compile = require('logical-compiler'); // CommonJS

compile(expression, options);

Arguments:

  • expression - the boolean expression to be executed.
  • options - an optional object specifying options.
    • fns - an optional attribute specifying any function(s) used on the expression.

Operators

AND operator

let expression = { $and: [true, true] };
compile(expression); // true

expression = { $and: [true, false] };
compile(expression); // false

expression = { $and: [false, true] };
compile(expression); // false

expression = { $and: [false, false] };
compile(expression); // false

OR operator

let expression = { $or: [true, true] };
compile(expression); // true

expression = { $or: [true, false] };
compile(expression); // true

expression = { $or: [false, true] };
compile(expression); // true

expression = { $or: [false, false] };
compile(expression); // false

NOT operator

$not takes a single expression and returns its negation.

let expression = { $not: true };
compile(expression); // false

expression = { $not: { $and: [true, true] } };
compile(expression); // false

NOR operator

$nor takes an array and returns the negation of $or over it, i.e. true only when every operand is false.

let expression = { $nor: [false, false] };
compile(expression); // true

expression = { $nor: [false, true] };
compile(expression); // false

Unrecognized operator

const expression = { $someOp: ['x', 'y'] };

compile(expression); // Error: Unrecognized operator: '$someOp'

Primitives

undefined

compile(); // Error: Expected an expression

boolean

compile(true); // true

compile(false); // false

string

const expression = 'test';

compile(expression); // Error: Unexpected token 'test'

number

const expression = 201;

compile(expression); // Error: Unexpected token '201'

Callbacks

A callback is a function that returns a boolean. If it returns a promise, compile resolves to a promise you await.

let cb = () => true;
compile(cb); // true

cb = () => Promise.resolve(false);
await compile(cb); // false

Nested promise callback

const expression = {
  $and: [true, () => Promise.resolve(true)],
};

await compile(expression); // true

Functions

A function is defined on the fns attribute of the options argument. It may return a boolean or a promise.

const options = {
  fns: {
    isEven: (number) => number % 2 === 0,
    isEqual: (num1, num2) => Promise.resolve(num1 === num2),
  },
};

compile({ isEven: 6 }, options); // true

await compile({ isEqual: [3, 5] }, options); // false

Nested promise function

const options = {
  fns: {
    authenticated: () => Promise.resolve(true),
  },
};

const expression = {
  $or: [false, { authenticated: null }],
};

await compile(expression, options); // true

Undefined function

const expression = { someFn: ['x', 'y'] };

compile(expression, options); // Error: Undefined function: 'someFn'

Compound expressions

let expression = {
  $or: [{ $and: [true, true] }, false],
};

compile(expression); // true
expression = {
  $or: [() => false, () => false],
};

compile(expression); // false
expression = {
  $and: [() => true, true],
};

compile(expression); // true
expression = {
  $or: [
    () => false,
    false,
    {
      $and: [true, { $or: [() => true, false] }],
    },
  ],
};

compile(expression); // true
const options = {
  fns: {
    any: (target, values) => values.includes(target),
    all: (targets, values) => targets.every((target) => values.includes(target)),
  },
};

expression = {
  $and: [
    () => true, 
    { $or: [true, false] },
    { any: [5, [1, 3, 4, 6, 7]] }
  ],
};

compile(expression, options); // false

expression = {
  $or: [
    () => false,
    false,
    { $and: [true, false] },
    {
      all: [
        [3, 4, 7],
        [2, 3, 4, 5, 6, 7],
      ],
    },
  ],
};

compile(expression, options); // true

Operators can be nested in any fashion to achieve the desired logical check.

Multiple keys (implicit AND)

When an object expression has more than one key, the keys are AND-ed together, at any nesting depth. This applies to operators and functions alike.

const options = {
  fns: {
    isEven: (number) => number % 2 === 0,
  },
};

let expression = { $or: [false, true], isEven: 6 };
compile(expression, options); // true  ($or AND isEven)

expression = { $or: [false, true], isEven: 7 };
compile(expression, options); // false ($or is true, but isEven is false)

An empty object is not a valid expression and throws:

compile({}); // Error: Expected an expression

Error handling

All errors thrown by compile are instances of LogicalCompilerError, a subclass of Error that carries a .code field for programmatic handling. The human-readable .message is unchanged.

import compile, { LogicalCompilerError } from 'logical-compiler';

try {
  compile({ $unknown: [] });
} catch (error) {
  error instanceof LogicalCompilerError; // true
  error.code; // 'UNRECOGNIZED_OPERATOR'
}

In CommonJS the class is also available as compile.LogicalCompilerError.

Codes: UNRECOGNIZED_OPERATOR, UNDEFINED_FUNCTION, UNEXPECTED_TOKEN, UNEXPECTED_RETURN_TYPE, EXPECTED_EXPRESSION.

TypeScript

Type declarations ship with the package (both CommonJS and ESM), so no separate @types install is needed. LogicalCompilerError and the Expression, Options, Fn, Callback, and ErrorCode types are exported for use in your own code.

IMPORTANT NOTES
  1. compile returns boolean for fully synchronous expressions and Promise<boolean> when any callback or function returns a promise. $and stops at the first false; $or stops at the first true. Later operands (including async ones) are not evaluated.

  2. Callbacks and functions must explicitly return boolean values to avoid the ambiguity of relying on truthiness. Relying on truthiness would pose a serious loophole because the executable might accidentally resolve to true on a non-boolean value. If the library encounters a callback that resolves to a non-boolean value, it throws an exception. See MDN documentation on truthy values.

Licence

MIT © Mutai Mwiti | GitHub | GitLab

DISCLAIMER: All opinions expressed in this repository are mine and do not reflect any company or organisation I'm involved with.

About

Compile MongoDB-like boolean expressions based on boolean operators

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages