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

logical-compiler

v0.1.0

Published

Compile MongoDB-like boolean expressions based on boolean operators AND and OR

Downloads

2

Readme

logical-compiler

build version downloads license

Compile MongoDB-like boolean expressions based on boolean operators AND and OR

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';

compile(expression, options); // false

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

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

Simple callback

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

cb = () => false;
compile(cb); // false

Promise callback

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

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

Nested promise callback

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

compile(expression); // Error: Unexpected nested promise callback

Functions

Simple function

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

let expression = { isEven: 7 };
compile(expression, options); // false

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

Note that the function is defined on the fns attribute of the options argument.

Promise function

const options = {
  fns: {
    isEqual: (num1, num2) => Promise.resolve(num1 === num2),
  },
};

expression = { isEqual: [3, 3] };
await compile(expression, options); // true

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

Nested promise function

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

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

compile(expression, options); // Error: Unexpected nested promise function

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.

IMPORTANT NOTES
  1. An asynchronous call can be made inside a callback or function. Currently, the library does not support promise returning callbacks or functions on nested expressions. If one is found, an exception is thrown. Promise returning executables are only allowed ALONE. The clean workaround is to resolve values resulting from asynchronous calls before invoking the compiler.

  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.