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

@jexop/core

v1.0.11

Published

Evaluate expressions with operators that are represented purely JSON objects

Downloads

372

Readme

JSON Expression Operators

The JSON Expression Operators library allows you to evaluate expressions with operators that are represented purely as JSON objects. It is completely extensible, giving you the ability to add or remove operators as required.

Basic Usage

To use the library, first import the evaluate and registry functions from the @jexop/core package, then add the operators that you wish to use to the registry:

import { evaluate, registry } from '@jexop/core';

// register all default operators
registry.addDefaults();

Now you can construct an expression as a JSON object. Here's an example:

const expression = {
  op: 'if',
  condition: {
    op: '>',
    left: {
      op: 'context',
      path: 'x',
    },
    right: {
      op: 'context',
      path: 'y',
    },
  },
  then: {
    op: 'context',
    path: 'foo',
  },
  else: 'something else',
};

In this example, the expression is checking if the value of x is greater than the value of y. If the condition is true, it returns the value of foo that is in the given context, otherwise it returns the string 'something else'.

Finally, you can evaluate the expression by calling the evaluate function and providing the context required:

const context = {
  foo: 'bar',
  x: 19,
  y: 7,
};

const result = evaluate(expression, context);

The result variable will contain the result of the expression evaluation.

Adding Custom Operators

In addition to the default operators provided by @jexop/core, you can also add custom operators. To add a custom operator, use the set or add functions on the registry.

Here's an example of adding a custom operator called 'is' that checks if two values are equal:

registry.set('is', ({ a, b }, ctx) => a === b);

You can also add multiple custom operators at once using the add function:

registry.add({
  'is': ({ a, b }, ctx) => a === b,
  "isn't": ({ a, b }, ctx) => a !== b,
});

Now you can use the custom operators in expressions. For example:

const expression = {
  op: 'is',
  a: 45,
  b: 17,
};
const result = evaluate(expression); // result will be false

Operators

The @jexop/core package provides many basic operators by default. These operators cover common use cases and provide a good starting point for most applications.

Additionally, there are more operators available in the following packages:

You can install and use these packages to access a wider range of operators for your expressions.

Literal properties and operator

Any property value that is not an object will be considered a literal value. Arrays will have each item evaluated.

Objects that have no op property will also be considered literal. Further to that there is a literal operator to ensure that the value is passed as-is. This may be useful for returning objects that have an op property that are not intended to be evaluated.

literal

Returns value as-it, without evaluation.

| Property | Description | | ------------------ | ---------------------------- | | value (optional) | Literal value to be returned |

Examples

{
  "op": "literal",
  "value": {
    "op": "anything",
    "foo": "bar"
  }
}
{
  "op": "literal",
  "value": 17
}
{
  "op": "literal",
  "value": "could be anything"
}

Array Operators

array:map

| Property | Description | | -------- | -------------------------------------------------------------------- | | items | Array that will be run over | | to | Object to evaluate with additional item and index in the context |

Examples

{
  "op": "array:map",
  "items": [2, 4, 6],
  "to": {
    "op": "<",
    "left": {
      "op": "context",
      "path": "item"
    },
    "right": 5
  }
}

Boolean Operators

not, !

Returns the logical not of the value.

| Property | Description | | -------- | ----------- | | value | Input |

Examples

// returns true
{
  "op": "not",
  "value": false
}
// returns false
{
  "op": "!",
  "values": [
    {
      "op": "=",
      "a": 100,
      "b": {
        "op": "plus",
        "a": 1,
        "b": 99
      }
    }
  ]
}

and, &&

Returns the result of all values combined with the logical and operation.

| Property | Description | | -------- | ----------- | | values | Inputs |

Examples

// returns false
{
  "op": "and",
  "values": [true, false]
}
// returns true
{
  "op": "and",
  "values": [
    true,
    {
      "op": "=",
      "a": 100,
      "b": {
        "op": "plus",
        "a": 1,
        "b": 99
      }
    }
  ]
}

nand

Returns the result of all values combined with the logical nand operation.

| Property | Description | | -------- | ----------- | | values | Inputs |

Examples

// returns true
{
  "op": "nand",
  "values": [true, false]
}
// returns false
{
  "op": "nand",
  "values": [
    true,
    {
      "op": "=",
      "a": 100,
      "b": {
        "op": "plus",
        "a": 1,
        "b": 99
      }
    }
  ]
}

or, ||

Returns the result of all values combined with the logical or operation.

| Property | Description | | -------- | ----------- | | values | Inputs |

Examples

// returns true
{
  "op": "or",
  "values": [true, false]
}
// returns false
{
  "op": "or",
  "values": [
    false,
    {
      "op": "!=",
      "a": 100,
      "b": {
        "op": "plus",
        "a": 1,
        "b": 99
      }
    }
  ]
}

nor

Returns the result of all values combined with the logical nor operation.

| Property | Description | | -------- | ----------- | | values | Inputs |

Examples

// returns false
{
  "op": "nor",
  "values": [true, false]
}
// returns true
{
  "op": "nor",
  "values": [
    false,
    {
      "op": "!=",
      "a": 100,
      "b": {
        "op": "plus",
        "a": 1,
        "b": 99
      }
    }
  ]
}

Branch Operators

if

| Property | Description | | ----------- | ------------------------------- | | condition | Condition to check | | then | Returned if condition is truthy | | else | Returned if condition is falsey |

NOTE: both then and else are evaluated no matter what the condition is.

switch

| Property | Description | | ---------- | ------------------------------------------------------- | | expr | Input value (string) to match against keys in options | | options | Key-value dictionary/object | | fallback | Returned if no options match expr |

Examples

// given context = { "foo": "bar" }, returns { "one": "thing" }
// given context = { "foo": "another" }, returns 7
// given context = { "foo": "other" }, returns "default case"
{
  "op": "switch",
  "expr": {
    "op": "context",
    "path": "foo"
  },
  "options": {
    "bar": { "one": "thing" },
    "another": 7
  },
  "fallback": "default case"
}
// given context = { "foo": "bar" }, returns { "one": "thing" }
// given context = { "foo": "another", "value": 9 }, returns 9
// given context = { "foo": "other" }, returns "default case"
{
  "op": "switch",
  "expr": {
    "op": "context",
    "path": "foo"
  },
  "options": {
    "bar": { "one": "thing" },
    "another": {
      "op": "context",
      "path": "value"
    }
  },
  "fallback": "default case"
}

Equality Operators

eq, =, ==, ===

Returns true if inputs are exactly equal. Otherwise, returns false.

| Property | Description | | -------- | ----------- | | a | Input | | b | Input |

Examples

// returns false
{
  "op": "eq",
  "a": "foo",
  "b": "bar"
}
// returns true
{
  "op": "=",
  "a": 100,
  "b": {
    "op": "plus",
    "a": 1,
    "b": 99
  }
}

ne, !=, !==

Returns false if inputs are exactly equal. Otherwise, returns true.

| Property | Description | | -------- | ----------- | | left | Input | | right | Input |

Examples

// returns false
{
  "op": "ne",
  "a": "foo",
  "b": "bar"
}
// returns true
{
  "op": "!=",
  "a": 100,
  "b": {
    "op": "plus",
    "a": 1,
    "b": 99
  }
}

Numeric Operators

add, plus, +

Adds two numbers.

| Property | Description | | -------- | ----------- | | a | Augend | | b | Addend |

Computes a + b.

If a or b are not numeric they are defaulted to 0.

Examples

// returns 12
{
  "op": "add",
  "a": 10,
  "b": 2
}
// returns 110
{
  "op": "+",
  "a": 10,
  "b": {
    "op": "plus",
    "a": 1,
    "b": 99
  }
}

clamp

Ensures that a value is between a given min and max (inclusive).

| Property | Description | | -------- | -------------------------- | | min | Lower bound | | max | Upper bound | | value | Input number to be clamped |

If value is less that min, then min is returned. If value is greater than max then max is returned. Otherwise, value is returned.

Examples

// returns 25
{
  "op": "clamp",
  "min": 10,
  "max": 50,
  "value": 25
}
// returns 50
{
  "op": "clamp",
  "min": 10,
  "max": 50,
  "value": {
    "op": "plus",
    "a": 1,
    "b": 99
  }
}

comparison

Compares two numbers

| Property | Description | | -------- | ----------- | | left | Input | | right | Input |

| Operator | Comparison | | ---------- | ------------------------------------------ | | lt, < | left is less than right | | le, <= | left is less than or equal to right | | gt, > | left is greater than right | | ge, >= | left is greater than or equal to right |

Examples

// returns true
{
  "op": "<",
  "left": 10,
  "right": 50
}
// returns false
{
  "op": "ge",
  "left": 10,
  "right": 50
}

divide, /

Divides one number by another.

| Property | Description | | -------- | ----------- | | left | Dividend | | right | Divisor |

Computes left ÷ right

Examples

// returns 5
{
  "op": "divide",
  "left": 10,
  "right": 2
}
// returns 5
{
  "op": "/",
  "left": {
    "op": "divide",
    "left": 100,
    "right": 2
  },
  "right": 10
}

is-number

Returns true if the value is a number. Otherwise, returns false.

| Property | Description | | -------- | -------------- | | value | Value to check |

multiply, *

Multiplies two numbers.

| Property | Description | | -------- | ------------ | | left | Multiplier | | right | Multiplicand |

Computes left × right

Examples

// returns 20
{
  "op": "multiply",
  "left": 10,
  "right": 2
}
// returns 2000
{
  "op": "*",
  "left": {
    "op": "multiply",
    "left": 100,
    "right": 2
  },
  "right": 10
}

normalize

Converts the input value to a number in the range 0-1.

| Property | Description | | -------- | ------------ | | min | Lower bound | | max | Upper bound | | value | Input number |

If value is less that min, then min is returned. If value is greater than max then max is returned. Otherwise, value is returned.

Examples

// returns 0.2
{
  "op": "normalize",
  "min": 0,
  "max": 5,
  "value": 1
}
// returns 0.5
{
  "op": "normalize",
  "min": 40,
  "max": 50,
  "value": 45
}
// returns 1
{
  "op": "normalize",
  "min": -100,
  "max": 100,
  "value": {
    "op": "plus",
    "a": 1,
    "b": 99
  }
}
// returns 1
{
  "op": "normalize",
  "min": 0,
  "max": 10,
  "value": 99
}

number

Convert to number if possible.

| Property | Description | | -------------------- | ------------------------------------------ | | value | Input | | default (optional) | Value to use if conversion is not possible |

If value is an array, the length of the array is returned.

The default property will also be converted to number if necessary.

Examples

// returns 123
{
  "op": "number",
  "value": "123"
}
// returns 3
{
  "op": "number",
  "value": [9, 8, 7]
}
// returns null
{
  "op": "number",
  "value": "foo"
}
// returns 0
{
  "op": "number",
  "value": "foo",
  "default": 0
}
// returns 7
{
  "op": "number",
  "value": "foo",
  "default": "7"
}

subtract, minus, -

Subtracts one number from another.

| Property | Description | | -------- | ----------- | | a | Minuend | | b | Subtrahend |

Computes a - b.

If a or b are not numeric they are defaulted to 0.

Examples

// returns 8
{
  "op": "subtract",
  "a": 10,
  "b": 2
}
// returns -88
{
  "op": "minus",
  "a": 10,
  "b": {
    "op": "-",
    "a": 1,
    "b": 99
  }
}

Object Operators

context

Returns the context or part of it.

| Property | Description | | -------------------- | ------------------------------------------------------------------------------------ | | path (optional) | Property path to check | | default (optional) | Value to be returned if the property doesn't exist or the value is null or undefined |

Examples

// given context = { "foo": "bar" }, returns { "foo": "bar" }
{
  "op": "context"
}
// given context = { "foo": "bar" }, returns "bar"
{
  "op": "context",
  "path": "foo"
}
// given context = { "foo": "bar" }, returns "this is the fallback"
{
  "op": "context",
  "path": "other",
  "default": "this is the fallback"
}

get

Gets the value of the given property at the given path from the input object.

| Property | Description | | -------------------- | ------------------------------------------------------------------------------------ | | path | Property path to check | | object | Input object | | default (optional) | Value to be returned if the property doesn't exist or the value is null or undefined |

Examples

// returns { "bar": 17 }
{
  "op": "get",
  "path": "foo",
  "object": {
    "foo": {
      "bar": 17
    }
  }
}
// returns 17
{
  "op": "get",
  "path": "foo.bar",
  "object": {
    "foo": {
      "bar": 17
    }
  }
}
// returns 17
{
  "op": "get",
  "path": ["foo", "bar"],
  "object": {
    "foo": {
      "bar": 17
    }
  }
}
// returns "not-found'
{
  "op": "get",
  "path": ["prop"],
  "object": {
    "foo": {
      "bar": 17
    }
  },
  "default": "not-found"
}

object:has

True if the input object has a property at the given path.

| Property | Description | | -------- | ---------------------- | | object | Input object | | path | Property path to check |

Examples

// returns true
{
  "op": "object:has",
  "path": "foo.bar",
  "object": {
    "foo": {
      "bar": 17
    }
  }
}
// returns true
{
  "op": "object:has",
  "path": ["foo", "bar"],
  "object": {
    "foo": {
      "bar": 17
    }
  }
}
// returns false
{
  "op": "object:has",
  "path": "other",
  "object": {
    "foo": {
      "bar": 17
    }
  }
}

object:keys

Returns the object's keys as an array.

| Property | Description | | ----------------- | ------------------------------------ | | object | Input object | | path (optional) | Evaluate child property at this path |

Examples

// returns ["foo", "bar"]
{
  "op": "object:keys",
  "object": {
    "foo": 12,
    "bar": 17
  }
}
// returns ["bar"]
{
  "op": "object:keys",
  "path": "foo",
  "object": {
    "foo": {
      "bar": 17
    }
  }
}